DlnaControllerTests.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Http.Json;
  5. using System.Net.Mime;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Extensions.Json;
  10. using MediaBrowser.Model.Dlna;
  11. using Xunit;
  12. using Xunit.Priority;
  13. namespace Jellyfin.Server.Integration.Tests.Controllers
  14. {
  15. [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
  16. public sealed class DlnaControllerTests : IClassFixture<JellyfinApplicationFactory>
  17. {
  18. private const string NonExistentProfile = "1322f35b8f2c434dad3cc07c9b97dbd1";
  19. private readonly JellyfinApplicationFactory _factory;
  20. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  21. private static string? _accessToken;
  22. private static string? _newDeviceProfileId;
  23. public DlnaControllerTests(JellyfinApplicationFactory factory)
  24. {
  25. _factory = factory;
  26. }
  27. [Fact]
  28. [Priority(0)]
  29. public async Task GetProfile_DoesNotExist_NotFound()
  30. {
  31. var client = _factory.CreateClient();
  32. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  33. using var response = await client.GetAsync("/Dlna/Profiles/" + NonExistentProfile);
  34. Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
  35. }
  36. [Fact]
  37. [Priority(0)]
  38. public async Task DeleteProfile_DoesNotExist_NotFound()
  39. {
  40. var client = _factory.CreateClient();
  41. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  42. using var response = await client.DeleteAsync("/Dlna/Profiles/" + NonExistentProfile);
  43. Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
  44. }
  45. [Fact]
  46. [Priority(0)]
  47. public async Task UpdateProfile_DoesNotExist_NotFound()
  48. {
  49. var client = _factory.CreateClient();
  50. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  51. var deviceProfile = new DeviceProfile()
  52. {
  53. Name = "ThisProfileDoesNotExist"
  54. };
  55. using var response = await client.PostAsJsonAsync("/Dlna/Profiles/" + NonExistentProfile, deviceProfile, _jsonOptions);
  56. Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
  57. }
  58. [Fact]
  59. [Priority(1)]
  60. public async Task CreateProfile_Valid_NoContent()
  61. {
  62. var client = _factory.CreateClient();
  63. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  64. var deviceProfile = new DeviceProfile()
  65. {
  66. Name = "ThisProfileIsNew"
  67. };
  68. using var response = await client.PostAsJsonAsync("/Dlna/Profiles", deviceProfile, _jsonOptions);
  69. Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
  70. }
  71. [Fact]
  72. [Priority(2)]
  73. public async Task GetProfileInfos_Valid_ContainsThisProfileIsNew()
  74. {
  75. var client = _factory.CreateClient();
  76. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  77. using var response = await client.GetAsync("/Dlna/ProfileInfos");
  78. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  79. Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
  80. Assert.Equal(Encoding.UTF8.BodyName, response.Content.Headers.ContentType?.CharSet);
  81. var profiles = await JsonSerializer.DeserializeAsync<DeviceProfileInfo[]>(
  82. await response.Content.ReadAsStreamAsync(),
  83. _jsonOptions);
  84. var newProfile = profiles?.FirstOrDefault(x => string.Equals(x.Name, "ThisProfileIsNew", StringComparison.Ordinal));
  85. Assert.NotNull(newProfile);
  86. _newDeviceProfileId = newProfile!.Id;
  87. }
  88. [Fact]
  89. [Priority(3)]
  90. public async Task UpdateProfile_Valid_NoContent()
  91. {
  92. var client = _factory.CreateClient();
  93. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  94. var updatedProfile = new DeviceProfile()
  95. {
  96. Name = "ThisProfileIsUpdated",
  97. Id = _newDeviceProfileId
  98. };
  99. using var postResponse = await client.PostAsJsonAsync("/Dlna/Profiles/" + _newDeviceProfileId, updatedProfile, _jsonOptions);
  100. Assert.Equal(HttpStatusCode.NoContent, postResponse.StatusCode);
  101. // Verify that the profile got updated
  102. using var response = await client.GetAsync("/Dlna/ProfileInfos");
  103. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  104. Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
  105. Assert.Equal(Encoding.UTF8.BodyName, response.Content.Headers.ContentType?.CharSet);
  106. var profiles = await JsonSerializer.DeserializeAsync<DeviceProfileInfo[]>(
  107. await response.Content.ReadAsStreamAsync(),
  108. _jsonOptions);
  109. Assert.Null(profiles?.FirstOrDefault(x => string.Equals(x.Name, "ThisProfileIsNew", StringComparison.Ordinal)));
  110. var newProfile = profiles?.FirstOrDefault(x => string.Equals(x.Name, "ThisProfileIsUpdated", StringComparison.Ordinal));
  111. Assert.NotNull(newProfile);
  112. _newDeviceProfileId = newProfile!.Id;
  113. }
  114. [Fact]
  115. [Priority(5)]
  116. public async Task DeleteProfile_Valid_NoContent()
  117. {
  118. var client = _factory.CreateClient();
  119. client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
  120. using var deleteResponse = await client.DeleteAsync("/Dlna/Profiles/" + _newDeviceProfileId);
  121. Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);
  122. // Verify that the profile got deleted
  123. using var response = await client.GetAsync("/Dlna/ProfileInfos");
  124. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  125. Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
  126. Assert.Equal(Encoding.UTF8.BodyName, response.Content.Headers.ContentType?.CharSet);
  127. var profiles = await JsonSerializer.DeserializeAsync<DeviceProfileInfo[]>(
  128. await response.Content.ReadAsStreamAsync(),
  129. _jsonOptions);
  130. Assert.Null(profiles?.FirstOrDefault(x => string.Equals(x.Name, "ThisProfileIsUpdated", StringComparison.Ordinal)));
  131. }
  132. }
  133. }