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

88 строки
3.1KB

  1. using System.Net;
  2. using System.Text.RegularExpressions;
  3. using Microsoft.AspNetCore.Mvc.Testing;
  4. namespace CartWise.Web.Tests;
  5. public class HouseholdAccessControlTests : IClassFixture<CartWiseWebApplicationFactory>
  6. {
  7. private readonly CartWiseWebApplicationFactory _factory;
  8. public HouseholdAccessControlTests(CartWiseWebApplicationFactory factory)
  9. {
  10. _factory = factory;
  11. }
  12. [Fact]
  13. public async Task AnonymousUser_IsRedirectedToLoginWhenRequestingHousehold()
  14. {
  15. var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
  16. var response = await client.GetAsync("/household");
  17. Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
  18. Assert.Contains("/Account/Login", response.Headers.Location?.ToString());
  19. }
  20. [Fact]
  21. public async Task EachUser_OnlySeesTheirOwnHousehold()
  22. {
  23. var clientA = _factory.CreateClient();
  24. var clientB = _factory.CreateClient();
  25. await RegisterAsync(clientA, "alice2@example.com", "Alice");
  26. await RegisterAsync(clientB, "bob2@example.com", "Bob");
  27. await CreateHouseholdAsync(clientA, "Alice's Household");
  28. await CreateHouseholdAsync(clientB, "Bob's Household");
  29. var householdPageA = await clientA.GetStringAsync("/household");
  30. var householdPageB = await clientB.GetStringAsync("/household");
  31. HtmlAssert.Contains("Alice's Household", householdPageA);
  32. HtmlAssert.DoesNotContain("Bob's Household", householdPageA);
  33. HtmlAssert.Contains("Bob's Household", householdPageB);
  34. HtmlAssert.DoesNotContain("Alice's Household", householdPageB);
  35. }
  36. private static async Task RegisterAsync(HttpClient client, string email, string displayName)
  37. {
  38. var html = await client.GetStringAsync("/Account/Register");
  39. var token = ExtractAntiForgeryToken(html);
  40. var form = new Dictionary<string, string>
  41. {
  42. ["__RequestVerificationToken"] = token,
  43. ["DisplayName"] = displayName,
  44. ["Email"] = email,
  45. ["Password"] = "Sup3rSecret!23",
  46. ["ConfirmPassword"] = "Sup3rSecret!23"
  47. };
  48. var response = await client.PostAsync("/Account/Register", new FormUrlEncodedContent(form));
  49. response.EnsureSuccessStatusCode();
  50. }
  51. private static async Task CreateHouseholdAsync(HttpClient client, string name)
  52. {
  53. var html = await client.GetStringAsync("/household/create");
  54. var token = ExtractAntiForgeryToken(html);
  55. var form = new Dictionary<string, string>
  56. {
  57. ["__RequestVerificationToken"] = token,
  58. ["Name"] = name
  59. };
  60. var response = await client.PostAsync("/household/create", new FormUrlEncodedContent(form));
  61. response.EnsureSuccessStatusCode();
  62. }
  63. private static string ExtractAntiForgeryToken(string html)
  64. {
  65. var match = Regex.Match(html, "name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]+)\"");
  66. return match.Success ? match.Groups[1].Value : throw new InvalidOperationException("Anti-forgery token not found.");
  67. }
  68. }

Powered by TurnKey Linux.