Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

118 řádky
4.5KB

  1. using System.Net;
  2. using CartWise.Domain.Entities;
  3. using CartWise.Domain.Enums;
  4. using CartWise.Infrastructure.Data;
  5. using Microsoft.AspNetCore.Mvc.Testing;
  6. using Microsoft.Extensions.DependencyInjection;
  7. namespace CartWise.Web.Tests;
  8. public class PriceAccessControlTests : IClassFixture<CartWiseWebApplicationFactory>
  9. {
  10. private readonly CartWiseWebApplicationFactory _factory;
  11. public PriceAccessControlTests(CartWiseWebApplicationFactory factory)
  12. {
  13. _factory = factory;
  14. }
  15. [Fact]
  16. public async Task AnonymousUser_IsRedirectedToLoginWhenRequestingPrices()
  17. {
  18. var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
  19. var response = await client.GetAsync("/prices");
  20. Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
  21. Assert.Contains("/Account/Login", response.Headers.Location?.ToString());
  22. }
  23. [Fact]
  24. public async Task AuthenticatedUser_WithNoObservations_SeesEmptyState()
  25. {
  26. var client = _factory.CreateClient();
  27. await WebTestHelpers.RegisterAsync(client, "pricesempty1@example.com", "Price Watcher");
  28. await WebTestHelpers.CreateHouseholdAsync(client, "Price Empty Household");
  29. var response = await client.GetAsync("/prices");
  30. response.EnsureSuccessStatusCode();
  31. var body = await response.Content.ReadAsStringAsync();
  32. HtmlAssert.Contains("No prices recorded yet", body);
  33. }
  34. [Fact]
  35. public async Task ProductPage_ReturnsNotFoundForUnknownProduct()
  36. {
  37. var client = _factory.CreateClient();
  38. await WebTestHelpers.RegisterAsync(client, "pricesunknown1@example.com", "Price Watcher 2");
  39. await WebTestHelpers.CreateHouseholdAsync(client, "Price Unknown Household");
  40. var response = await client.GetAsync($"/prices/product/{Guid.NewGuid()}");
  41. Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
  42. }
  43. [Fact]
  44. public async Task ObserveThenViewProduct_ShowsLoggedPriceInHistory()
  45. {
  46. var client = _factory.CreateClient();
  47. await WebTestHelpers.RegisterAsync(client, "pricesobserve1@example.com", "Price Observer");
  48. await WebTestHelpers.CreateHouseholdAsync(client, "Price Observe Household");
  49. var productId = SeedProduct("Observed Oats");
  50. var productHtml = await client.GetStringAsync($"/prices/product/{productId}");
  51. HtmlAssert.Contains("No price history yet", productHtml);
  52. var token = WebTestHelpers.ExtractAntiForgeryToken(productHtml);
  53. var observeResponse = await client.PostAsync($"/prices/product/{productId}/observe", new FormUrlEncodedContent(new Dictionary<string, string>
  54. {
  55. ["__RequestVerificationToken"] = token,
  56. ["price"] = "3.49"
  57. }));
  58. observeResponse.EnsureSuccessStatusCode();
  59. var afterObserveHtml = await observeResponse.Content.ReadAsStringAsync();
  60. HtmlAssert.Contains("$3.49", afterObserveHtml);
  61. var indexHtml = await client.GetStringAsync("/prices");
  62. HtmlAssert.Contains("Observed Oats", indexHtml);
  63. }
  64. [Fact]
  65. public async Task EachHousehold_OnlySeesItsOwnPriceHistory()
  66. {
  67. var clientA = _factory.CreateClient();
  68. var clientB = _factory.CreateClient();
  69. await WebTestHelpers.RegisterAsync(clientA, "pricesalice1@example.com", "Price Alice");
  70. await WebTestHelpers.RegisterAsync(clientB, "pricesbob1@example.com", "Price Bob");
  71. await WebTestHelpers.CreateHouseholdAsync(clientA, "Price Household A");
  72. await WebTestHelpers.CreateHouseholdAsync(clientB, "Price Household B");
  73. var productId = SeedProduct("Shared Catalog Item");
  74. var productHtmlA = await clientA.GetStringAsync($"/prices/product/{productId}");
  75. var tokenA = WebTestHelpers.ExtractAntiForgeryToken(productHtmlA);
  76. await clientA.PostAsync($"/prices/product/{productId}/observe", new FormUrlEncodedContent(new Dictionary<string, string>
  77. {
  78. ["__RequestVerificationToken"] = tokenA,
  79. ["price"] = "9.99"
  80. }));
  81. var indexB = await clientB.GetStringAsync("/prices");
  82. Assert.DoesNotContain("Shared Catalog Item", indexB);
  83. }
  84. private Guid SeedProduct(string name)
  85. {
  86. using var scope = _factory.Services.CreateScope();
  87. var db = scope.ServiceProvider.GetRequiredService<CartWiseDbContext>();
  88. var product = new Product(name, ProductSourceType.Manual, DateTime.UtcNow);
  89. db.Products.Add(product);
  90. db.SaveChanges();
  91. return product.ProductId;
  92. }
  93. }

Powered by TurnKey Linux.