StartupControllerTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Http.Json;
  5. using System.Net.Mime;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using Jellyfin.Api.Models.StartupDtos;
  9. using Jellyfin.Extensions.Json;
  10. using Xunit;
  11. using Xunit.Priority;
  12. namespace Jellyfin.Server.Integration.Tests.Controllers
  13. {
  14. [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
  15. public sealed class StartupControllerTests : IClassFixture<JellyfinApplicationFactory>
  16. {
  17. private readonly JellyfinApplicationFactory _factory;
  18. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  19. public StartupControllerTests(JellyfinApplicationFactory factory)
  20. {
  21. _factory = factory;
  22. }
  23. [Fact]
  24. [Priority(-2)]
  25. public async Task Configuration_EditConfig_Success()
  26. {
  27. var client = _factory.CreateClient();
  28. var config = new StartupConfigurationDto()
  29. {
  30. UICulture = "NewCulture",
  31. MetadataCountryCode = "be",
  32. PreferredMetadataLanguage = "nl"
  33. };
  34. using var postResponse = await client.PostAsJsonAsync("/Startup/Configuration", config, _jsonOptions);
  35. Assert.Equal(HttpStatusCode.NoContent, postResponse.StatusCode);
  36. using var getResponse = await client.GetAsync("/Startup/Configuration");
  37. Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
  38. Assert.Equal(MediaTypeNames.Application.Json, getResponse.Content.Headers.ContentType?.MediaType);
  39. using var responseStream = await getResponse.Content.ReadAsStreamAsync();
  40. var newConfig = await JsonSerializer.DeserializeAsync<StartupConfigurationDto>(responseStream, _jsonOptions);
  41. Assert.Equal(config.UICulture, newConfig!.UICulture);
  42. Assert.Equal(config.MetadataCountryCode, newConfig.MetadataCountryCode);
  43. Assert.Equal(config.PreferredMetadataLanguage, newConfig.PreferredMetadataLanguage);
  44. }
  45. [Fact]
  46. [Priority(-2)]
  47. public async Task User_DefaultUser_NameWithoutPassword()
  48. {
  49. var client = _factory.CreateClient();
  50. using var response = await client.GetAsync("/Startup/User");
  51. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  52. Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
  53. using var contentStream = await response.Content.ReadAsStreamAsync();
  54. var user = await JsonSerializer.DeserializeAsync<StartupUserDto>(contentStream, _jsonOptions);
  55. Assert.NotNull(user);
  56. Assert.NotNull(user.Name);
  57. Assert.NotEmpty(user.Name);
  58. Assert.Null(user.Password);
  59. }
  60. [Fact]
  61. [Priority(-1)]
  62. public async Task User_EditUser_Success()
  63. {
  64. var client = _factory.CreateClient();
  65. var user = new StartupUserDto()
  66. {
  67. Name = "NewName",
  68. Password = "NewPassword"
  69. };
  70. var postResponse = await client.PostAsJsonAsync("/Startup/User", user, _jsonOptions);
  71. Assert.Equal(HttpStatusCode.NoContent, postResponse.StatusCode);
  72. var getResponse = await client.GetAsync("/Startup/User");
  73. Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
  74. Assert.Equal(MediaTypeNames.Application.Json, getResponse.Content.Headers.ContentType?.MediaType);
  75. var contentStream = await getResponse.Content.ReadAsStreamAsync();
  76. var newUser = await JsonSerializer.DeserializeAsync<StartupUserDto>(contentStream, _jsonOptions);
  77. Assert.NotNull(newUser);
  78. Assert.Equal(user.Name, newUser.Name);
  79. Assert.NotNull(newUser.Password);
  80. Assert.NotEmpty(newUser.Password);
  81. Assert.NotEqual(user.Password, newUser.Password);
  82. }
  83. [Fact]
  84. [Priority(0)]
  85. public async Task CompleteWizard_Success()
  86. {
  87. var client = _factory.CreateClient();
  88. var response = await client.PostAsync("/Startup/Complete", new ByteArrayContent(Array.Empty<byte>()));
  89. Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
  90. }
  91. [Fact]
  92. [Priority(1)]
  93. public async Task GetFirstUser_CompleteWizard_Unauthorized()
  94. {
  95. var client = _factory.CreateClient();
  96. using var response = await client.GetAsync("/Startup/User");
  97. Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
  98. }
  99. }
  100. }