Skip to content

Commit 79cb0d6

Browse files
committed
refactor: focus integration test on happy path, handle validation errors in unit tests
1 parent 6294faf commit 79cb0d6

File tree

1 file changed

+4
-53
lines changed

1 file changed

+4
-53
lines changed

Tests/IntegrationTests/Api/AuthenticationControllerTests.cs

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text.Json;
55
using System.Threading.Tasks;
66
using Ecommerce.Contracts.Authentication;
7-
using Ecommerce.IntegrationTests.Utils.Fixtures;
7+
using Ecommerce.Tests.Shared;
88
using FluentAssertions;
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.AspNetCore.Mvc;
@@ -22,64 +22,15 @@ public AuthenticationControllerTests(EcommerceWebApplicationFactoryFixture facto
2222
}
2323

2424
[Fact]
25-
public async Task RegisterShould_ReturnErrorValidation_WhenRegisterRequestIsInvalid()
25+
public async Task RegisterShould_ReturnCreated_WhenRegistrationIsSuccessful()
2626
{
2727
// Arrange
28-
var registerRequest = new RegisterRequest(
29-
string.Empty,
30-
string.Empty,
31-
string.Empty,
32-
string.Empty,
33-
string.Empty,
34-
string.Empty,
35-
0
36-
);
28+
var registerRequest = Utils.User.CreateRegisterRequest();
3729

3830
// Act
3931
var response = await _httpClient.PostAsJsonAsync("auth/register", registerRequest);
4032

4133
// Assert
42-
System.Console.WriteLine(response);
43-
44-
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
45-
46-
var content = await response.Content.ReadFromJsonAsync<ProblemDetails>();
47-
content.Should().NotBeNull();
48-
49-
// Assert specific error details
50-
content!.Type.Should().Be("ValidationException"); // Use null forgiving operator as we asserted it's not null
51-
content!.Title.Should().Be("Validation Error");
52-
content!.Status.Should().Be(400);
53-
content!.Detail.Should().Be("One or more validation errors has occured.");
54-
55-
content!.Extensions["errors"].Should().NotBeNull();
56-
57-
var errors = JsonSerializer.Deserialize<Dictionary<string, List<string>>>(
58-
JsonSerializer.Serialize(content!.Extensions["errors"]),
59-
new JsonSerializerOptions { PropertyNameCaseInsensitive = true, WriteIndented = true }
60-
);
61-
62-
if (errors is null)
63-
{
64-
throw new XunitException("Errors should not be null.");
65-
}
66-
67-
errors.Should().ContainKey("FirstName");
68-
errors["FirstName"].Should().Contain("First Name is Required");
69-
errors.Should().ContainKey("LastName");
70-
errors["LastName"].Should().Contain("Last Name is Required");
71-
errors.Should().ContainKey("Email");
72-
errors["Email"].Should().Contain("Email is required.");
73-
errors["Email"].Should().Contain("Email must be a valid format.");
74-
errors.Should().ContainKey("Password");
75-
errors["Password"].Should().Contain("Password is required.");
76-
errors.Should().ContainKey("ConfirmPassword");
77-
errors["ConfirmPassword"].Should().Contain("Confirmation password is required.");
78-
errors.Should().ContainKey("PhoneNumber");
79-
errors["PhoneNumber"].Should().Contain("Phone number is required.");
80-
81-
System.Console.WriteLine(
82-
JsonSerializer.Serialize(content, new JsonSerializerOptions { WriteIndented = true })
83-
);
34+
response.StatusCode.Should().Be(HttpStatusCode.Created);
8435
}
8536
}

0 commit comments

Comments
 (0)