StartupControllerTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  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 postContent = new ByteArrayContent(JsonSerializer.SerializeToUtf8Bytes(config, _jsonOptions));
  35. postContent.Headers.ContentType = MediaTypeHeaderValue.Parse(MediaTypeNames.Application.Json);
  36. using var postResponse = await client.PostAsync("/Startup/Configuration", postContent).ConfigureAwait(false);
  37. Assert.Equal(HttpStatusCode.NoContent, postResponse.StatusCode);
  38. using var getResponse = await client.GetAsync("/Startup/Configuration").ConfigureAwait(false);
  39. Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
  40. Assert.Equal(MediaTypeNames.Application.Json, getResponse.Content.Headers.ContentType?.MediaType);
  41. using var responseStream = await getResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
  42. var newConfig = await JsonSerializer.DeserializeAsync<StartupConfigurationDto>(responseStream, _jsonOptions).ConfigureAwait(false);
  43. Assert.Equal(config.UICulture, newConfig!.UICulture);
  44. Assert.Equal(config.MetadataCountryCode, newConfig.MetadataCountryCode);
  45. Assert.Equal(config.PreferredMetadataLanguage, newConfig.PreferredMetadataLanguage);
  46. }
  47. [Fact]
  48. [Priority(-2)]
  49. public async Task User_DefaultUser_NameWithoutPassword()
  50. {
  51. var client = _factory.CreateClient();
  52. using var response = await client.GetAsync("/Startup/User").ConfigureAwait(false);
  53. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  54. Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
  55. using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  56. var user = await JsonSerializer.DeserializeAsync<StartupUserDto>(contentStream, _jsonOptions).ConfigureAwait(false);
  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. using var postContent = new ByteArrayContent(JsonSerializer.SerializeToUtf8Bytes(user, _jsonOptions));
  71. postContent.Headers.ContentType = MediaTypeHeaderValue.Parse(MediaTypeNames.Application.Json);
  72. var postResponse = await client.PostAsync("/Startup/User", postContent).ConfigureAwait(false);
  73. Assert.Equal(HttpStatusCode.NoContent, postResponse.StatusCode);
  74. var getResponse = await client.GetAsync("/Startup/User").ConfigureAwait(false);
  75. Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
  76. Assert.Equal(MediaTypeNames.Application.Json, getResponse.Content.Headers.ContentType?.MediaType);
  77. var contentStream = await getResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
  78. var newUser = await JsonSerializer.DeserializeAsync<StartupUserDto>(contentStream, _jsonOptions).ConfigureAwait(false);
  79. Assert.Equal(user.Name, newUser!.Name);
  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>())).ConfigureAwait(false);
  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").ConfigureAwait(false);
  97. Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
  98. }
  99. }
  100. }