|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Json; |
| 3 | +using AxisEndpoints.Example.Features.Users; |
| 4 | +using FluentAssertions; |
| 5 | + |
| 6 | +namespace AxisEndpoints.Example.Tests; |
| 7 | + |
| 8 | +public class CreateUserEndpointTests : IClassFixture<ExampleWebApplicationFactory> |
| 9 | +{ |
| 10 | + private readonly HttpClient _client; |
| 11 | + |
| 12 | + public CreateUserEndpointTests(ExampleWebApplicationFactory factory) |
| 13 | + { |
| 14 | + _client = factory.Client; |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public async Task CreateUser_ValidRequest_Returns201WithLocationHeader() |
| 19 | + { |
| 20 | + var response = await _client.PostAsJsonAsync("/api/users", new |
| 21 | + { |
| 22 | + Name = "Eve", |
| 23 | + Email = "eve@example.com", |
| 24 | + Role = "User", |
| 25 | + }); |
| 26 | + |
| 27 | + response.StatusCode.Should().Be(HttpStatusCode.Created); |
| 28 | + response.Headers.Location.Should().NotBeNull(); |
| 29 | + response.Headers.Location!.ToString().Should().Contain("/api/users/1"); |
| 30 | + |
| 31 | + var body = await response.Content.ReadFromJsonAsync<UserResponse>(); |
| 32 | + body.Should().NotBeNull(); |
| 33 | + body!.Name.Should().Be("Eve"); |
| 34 | + body.Email.Should().Be("eve@example.com"); |
| 35 | + body.Role.Should().Be("User"); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task CreateUser_DefaultRole_ReturnsUser() |
| 40 | + { |
| 41 | + var response = await _client.PostAsJsonAsync("/api/users", new |
| 42 | + { |
| 43 | + Name = "Frank", |
| 44 | + Email = "frank@example.com", |
| 45 | + }); |
| 46 | + |
| 47 | + response.StatusCode.Should().Be(HttpStatusCode.Created); |
| 48 | + var body = await response.Content.ReadFromJsonAsync<UserResponse>(); |
| 49 | + body.Should().NotBeNull(); |
| 50 | + body!.Role.Should().Be("User"); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task CreateUser_InvalidEmail_Returns400() |
| 55 | + { |
| 56 | + var response = await _client.PostAsJsonAsync("/api/users", new |
| 57 | + { |
| 58 | + Name = "Eve", |
| 59 | + Email = "not-an-email", |
| 60 | + }); |
| 61 | + |
| 62 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task CreateUser_NameTooLong_Returns400() |
| 67 | + { |
| 68 | + var response = await _client.PostAsJsonAsync("/api/users", new |
| 69 | + { |
| 70 | + Name = new string('A', 101), |
| 71 | + Email = "toolong@example.com", |
| 72 | + }); |
| 73 | + |
| 74 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task CreateUser_MissingName_Returns400() |
| 79 | + { |
| 80 | + var response = await _client.PostAsJsonAsync("/api/users", new |
| 81 | + { |
| 82 | + Email = "missing@example.com", |
| 83 | + }); |
| 84 | + |
| 85 | + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 86 | + } |
| 87 | +} |
0 commit comments