Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

101 строка
3.6KB

  1. using System.Net;
  2. using System.Net.Http.Headers;
  3. using System.Net.Http.Json;
  4. namespace Campaign_Tracker.Server.Tests;
  5. public sealed class MunicipalityPriorCycleDefaultsControllerTests
  6. {
  7. [Fact]
  8. public async Task GetDefaults_WithPriorCycles_ReturnsMostRecentSelectedAndReadOnlyServices_AC1_AC2()
  9. {
  10. await using var factory = new AuthIntegrationTestFactory();
  11. using var client = CreateClient(factory);
  12. var profile = await CreateProfile(client, "LAKE02");
  13. var response = await client.GetAsync(
  14. $"/api/municipalities/{profile.ProfileId}/prior-cycle-defaults");
  15. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  16. var body = await response.Content.ReadFromJsonAsync<PriorCycleDefaultsDto>();
  17. Assert.NotNull(body);
  18. Assert.True(body.HasPriorCycles);
  19. Assert.Equal(2, body.Cycles.Length);
  20. Assert.Equal(body.Cycles[0].CycleId, body.SelectedCycleId);
  21. Assert.True(body.Cycles[0].CompletedAt.CompareTo(body.Cycles[1].CompletedAt) > 0);
  22. Assert.NotEmpty(body.Cycles[0].Services);
  23. Assert.All(body.Cycles[0].Services, service => Assert.NotEmpty(service.Values));
  24. }
  25. [Fact]
  26. public async Task GetDefaults_NoPriorCycles_ReturnsClearEmptyState_AC3()
  27. {
  28. await using var factory = new AuthIntegrationTestFactory();
  29. using var client = CreateClient(factory);
  30. var profile = await CreateProfile(client, "PINE03");
  31. var response = await client.GetAsync(
  32. $"/api/municipalities/{profile.ProfileId}/prior-cycle-defaults");
  33. Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  34. var body = await response.Content.ReadFromJsonAsync<PriorCycleDefaultsDto>();
  35. Assert.NotNull(body);
  36. Assert.False(body.HasPriorCycles);
  37. Assert.Equal("No prior cycle defaults available.", body.EmptyStateMessage);
  38. Assert.Empty(body.Cycles);
  39. Assert.Null(body.SelectedCycleId);
  40. }
  41. [Fact]
  42. public async Task GetDefaults_UnknownProfile_Returns404()
  43. {
  44. await using var factory = new AuthIntegrationTestFactory();
  45. using var client = CreateClient(factory);
  46. var response = await client.GetAsync(
  47. "/api/municipalities/does-not-exist/prior-cycle-defaults");
  48. Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
  49. }
  50. private static HttpClient CreateClient(AuthIntegrationTestFactory factory)
  51. {
  52. var client = factory.CreateClient();
  53. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
  54. "Bearer", AuthIntegrationTestFactory.CreateToken("cs@example.test", "client-services"));
  55. return client;
  56. }
  57. private static async Task<MunicipalityProfileDto> CreateProfile(HttpClient client, string jCode)
  58. {
  59. var created = await (await client.PostAsJsonAsync("/api/municipalities/profiles", new
  60. {
  61. jCode,
  62. displayName = (string?)null,
  63. })).Content.ReadFromJsonAsync<MunicipalityProfileDto>();
  64. Assert.NotNull(created);
  65. return created;
  66. }
  67. private sealed record MunicipalityProfileDto(string ProfileId);
  68. private sealed record PriorCycleDefaultsDto(
  69. string ProfileId,
  70. bool HasPriorCycles,
  71. string? SelectedCycleId,
  72. string EmptyStateMessage,
  73. PriorCycleDto[] Cycles);
  74. private sealed record PriorCycleDto(
  75. string CycleId,
  76. string CycleName,
  77. string CompletedAt,
  78. PriorCycleServiceDto[] Services);
  79. private sealed record PriorCycleServiceDto(
  80. string ServiceType,
  81. string Summary,
  82. Dictionary<string, string> Values);
  83. }

Powered by TurnKey Linux.