|
- using System.Net;
- using Microsoft.AspNetCore.Mvc.Testing;
-
- namespace CartWise.Web.Tests;
-
- public class ProductAccessControlTests : IClassFixture<CartWiseWebApplicationFactory>
- {
- private readonly CartWiseWebApplicationFactory _factory;
-
- public ProductAccessControlTests(CartWiseWebApplicationFactory factory)
- {
- _factory = factory;
- }
-
- [Fact]
- public async Task AnonymousUser_IsRedirectedToLoginWhenSearchingProducts()
- {
- var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
-
- var response = await client.GetAsync("/products/search?q=milk");
-
- Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
- Assert.Contains("/Account/Login", response.Headers.Location?.ToString());
- }
-
- [Fact]
- public async Task AuthenticatedUser_CanSearchWithNoResults()
- {
- var client = _factory.CreateClient();
- await WebTestHelpers.RegisterAsync(client, "productsearch1@example.com", "Product Searcher");
-
- var response = await client.GetAsync("/products/search?q=nonexistentitem");
-
- response.EnsureSuccessStatusCode();
- var body = await response.Content.ReadAsStringAsync();
- HtmlAssert.Contains("No products matched", body);
- }
-
- [Fact]
- public async Task Details_ReturnsNotFoundForUnknownProduct()
- {
- var client = _factory.CreateClient();
- await WebTestHelpers.RegisterAsync(client, "productdetails1@example.com", "Product Detailer");
-
- var response = await client.GetAsync($"/products/{Guid.NewGuid()}");
-
- Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
- }
- }
|