Skip to content

Commit

Permalink
feat: Extend tests to verify deleted entity query behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 committed Dec 9, 2024
1 parent 9e74b9f commit 76da825
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,37 @@ public async Task Should_Get_All_Tenants()
.FirstOrDefault(x => x.Id == _fixture.CreatedTenantId)!
.Users.Count().Should().Be(1);
}

[Test, Order(2)]
public async Task Should_Not_Get_Deleted_Tenant_By_Id()
{
var response = await _fixture.ServerClient.GetAsync($"/api/v1/Tenant/{_fixture.DeletedTenantId}");

response.StatusCode.Should().Be(HttpStatusCode.NotFound);

var message = await response.Content.ReadAsJsonAsync<TenantViewModel>();

message?.Data.Should().BeNull();
}

[Test, Order(3)]
public async Task Should_Get_All_Tenants_Including_Deleted()
{
var response = await _fixture.ServerClient.GetAsync(
"api/v1/Tenant?searchTerm=Test&pageSize=5&page=1&includeDeleted=true");

response.StatusCode.Should().Be(HttpStatusCode.OK);

var message = await response.Content.ReadAsJsonAsync<PagedResult<TenantViewModel>>();

message?.Data!.Items.Should().NotBeEmpty();
message!.Data!.Items.Should().HaveCount(2);
message.Data!.Items
.FirstOrDefault(x => x.Id == _fixture.DeletedTenantId)
.Should().NotBeNull();
}

[Test, Order(4)]
public async Task Should_Create_Tenant()
{
var request = new CreateTenantViewModel("Test Tenant 2");
Expand All @@ -80,7 +109,7 @@ public async Task Should_Create_Tenant()
tenantMessage.Data.Name.Should().Be(request.Name);
}

[Test, Order(3)]
[Test, Order(5)]
public async Task Should_Update_Tenant()
{
var request = new UpdateTenantViewModel(_fixture.CreatedTenantId, "Test Tenant 3");
Expand All @@ -107,7 +136,7 @@ public async Task Should_Update_Tenant()
tenantMessage.Data.Name.Should().Be(request.Name);
}

[Test, Order(4)]
[Test, Order(6)]
public async Task Should_Delete_Tenant()
{
var response = await _fixture.ServerClient.DeleteAsync($"/api/v1/Tenant/{_fixture.CreatedTenantId}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,37 @@ public async Task Should_Get_User_By_Id()
content.FirstName.Should().Be(TestAuthenticationOptions.FirstName);
content.LastName.Should().Be(TestAuthenticationOptions.LastName);
}

[Test, Order(2)]
public async Task Should_Get_All_User_Including_Deleted()
{
var response = await _fixture.ServerClient.GetAsync("/api/v1/user?includeDeleted=true");

response.StatusCode.Should().Be(HttpStatusCode.OK);

var message = await response.Content.ReadAsJsonAsync<PagedResult<UserViewModel>>();

message?.Data.Should().NotBeNull();

var content = message!.Data!.Items.ToList();

content.Count.Should().Be(3);

content.FirstOrDefault(x => x.Id == _fixture.DeletedUserId).Should().NotBeNull();
}

[Test, Order(3)]
public async Task Should_Not_Get_Deleted_User_By_Id()
{
var response = await _fixture.ServerClient.GetAsync("/api/v1/user/" + _fixture.DeletedUserId);

response.StatusCode.Should().Be(HttpStatusCode.NotFound);

var message = await response.Content.ReadAsJsonAsync<UserViewModel>();
message?.Data.Should().BeNull();
}

[Test, Order(4)]
public async Task Should_Create_User()
{
var user = new CreateUserViewModel(
Expand All @@ -80,7 +109,7 @@ public async Task Should_Create_User()
message?.Data.Should().NotBeEmpty();
}

[Test, Order(3)]
[Test, Order(5)]
public async Task Should_Login_User()
{
var user = new LoginUserViewModel(
Expand All @@ -95,7 +124,7 @@ public async Task Should_Login_User()
message?.Data.Should().NotBeEmpty();
}

[Test, Order(4)]
[Test, Order(6)]
public async Task Should_Get_The_Current_Active_Users()
{
var response = await _fixture.ServerClient.GetAsync("/api/v1/user/me");
Expand All @@ -114,7 +143,7 @@ public async Task Should_Get_The_Current_Active_Users()
content.LastName.Should().Be(TestAuthenticationOptions.LastName);
}

[Test, Order(5)]
[Test, Order(7)]
public async Task Should_Update_User()
{
var user = new UpdateUserViewModel(
Expand Down Expand Up @@ -155,7 +184,7 @@ public async Task Should_Update_User()
userContent.Role.Should().Be(user.Role);
}

[Test, Order(6)]
[Test, Order(8)]
public async Task Should_Change_User_Password()
{
var user = new ChangePasswordViewModel(
Expand Down Expand Up @@ -188,7 +217,7 @@ public async Task Should_Change_User_Password()
loginMessage?.Data.Should().NotBeEmpty();
}

[Test, Order(7)]
[Test, Order(9)]
public async Task Should_Delete_User()
{
var response = await _fixture.ServerClient.DeleteAsync("/api/v1/user/" + TestAuthenticationOptions.TestUserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace CleanArchitecture.IntegrationTests.Fixtures;
public sealed class TenantTestFixture : TestFixtureBase
{
public Guid CreatedTenantId { get; } = Guid.NewGuid();
public Guid DeletedTenantId { get; } = Guid.NewGuid();

public async Task SeedTestData()
{
Expand All @@ -21,9 +22,8 @@ public async Task SeedTestData()
CreatedTenantId,
"Test Tenant"));

// This tenant should not be included in any queries
var deletedTenant = new Tenant(
Guid.NewGuid(),
DeletedTenantId,
"Test Tenant2");
deletedTenant.Delete();
context.Tenants.Add(deletedTenant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace CleanArchitecture.IntegrationTests.Fixtures;

public sealed class UserTestFixture : TestFixtureBase
{
public Guid DeletedUserId { get; } = Guid.NewGuid();

public async Task SeedTestData()
{
await GlobalSetupFixture.RespawnDatabaseAsync();
Expand All @@ -30,9 +32,8 @@ public async Task SeedTestData()
"$2a$12$Blal/uiFIJdYsCLTMUik/egLbfg3XhbnxBC6Sb5IKz2ZYhiU/MzL2",
UserRole.Admin));

// This user should not be included in any queries
var deletedUsed = new User(
Guid.NewGuid(),
DeletedUserId,
Ids.Seed.TenantId,
"admin2@email.com",
"Admin2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async Task SeedTestData()
using var context = Factory.Services.GetRequiredService<ApplicationDbContext>();

var tenant = CreateTenant();
tenant.Delete();

context.Tenants.Add(tenant);
await context.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public async Task SeedTestData()
"Admin Tenant"));

var user = CreateUser();
user.Delete();

context.Users.Add(user);
await context.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public async Task Should_Get_Tenants_By_Ids()

new Guid(tenant.Id).Should().Be(createdTenant.Id);
tenant.Name.Should().Be(createdTenant.Name);
tenant.DeletedAt.Should().NotBeNullOrWhiteSpace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public async Task Should_Get_Users_By_Ids()
user.Email.Should().Be(createdUser.Email);
user.FirstName.Should().Be(createdUser.FirstName);
user.LastName.Should().Be(createdUser.LastName);
user.DeletedAt.Should().NotBeNullOrWhiteSpace();
}
}

0 comments on commit 76da825

Please sign in to comment.