Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Dec 27, 2024
1 parent 30f3077 commit 465fed6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

Nothing yet.
### Added

- Some integration tests.

### Fixed

- EventBus Mediator is now public.
- API key expiration.

## [3.0.1] - 2024-12-23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@ public async Task Given_TenantId_When_LoadAsync_Then_CorrectResults(bool found,
}
}

[Fact(DisplayName = "SaveAsync: it should remove an API key role.")]
public async Task Given_ApiKeyWithRole_When_SaveAsync_Then_RoleRemoved()
{
Role role = new(new UniqueName(new UniqueNameSettings(), "admin"));
await _roleRepository.SaveAsync(role);

ApiKey apiKey = new(new DisplayName("Test"), _secret);
apiKey.AddRole(role);

await _apiKeyRepository.SaveAsync(apiKey);

ApiKeyEntity? entity = await IdentityContext.ApiKeys.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(apiKey.Id.Value, entity.StreamId);
Assert.Equal(role.Id.Value, Assert.Single(entity.Roles).StreamId);

apiKey.RemoveRole(role);
await _apiKeyRepository.SaveAsync(apiKey);

entity = await IdentityContext.ApiKeys.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(apiKey.Id.Value, entity.StreamId);
Assert.Empty(entity.Roles);
}

[Fact(DisplayName = "SaveAsync: it should save the API key correctly.")]
public async Task Given_ApiKey_When_SaveAsync_Then_SavedCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,35 @@ public async Task Given_TenantIdUniqueName_When_LoadAsync_Then_CorrectResult(str
}
}

[Fact(DisplayName = "SaveAsync: it should remove an API key role.")]
public async Task Given_ApiKeyWithRole_When_SaveAsync_Then_RoleRemoved()
{
Role role = new(new UniqueName(new UniqueNameSettings(), "admin"));
await _roleRepository.SaveAsync(role);

User user = new(new UniqueName(new UniqueNameSettings(), Faker.Person.UserName));
user.AddRole(role);

await _userRepository.SaveAsync(user);

UserEntity? entity = await IdentityContext.Users.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(user.Id.Value, entity.StreamId);
Assert.Equal(role.Id.Value, Assert.Single(entity.Roles).StreamId);

user.RemoveRole(role);
await _userRepository.SaveAsync(user);

entity = await IdentityContext.Users.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(user.Id.Value, entity.StreamId);
Assert.Empty(entity.Roles);
}

[Fact(DisplayName = "SaveAsync: it should save the user correctly.")]
public async Task Given_User_When_SaveAsync_Then_SavedCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@ public async Task Given_TenantId_When_LoadAsync_Then_CorrectResults(bool found,
}
}

[Fact(DisplayName = "SaveAsync: it should remove an API key role.")]
public async Task Given_ApiKeyWithRole_When_SaveAsync_Then_RoleRemoved()
{
Role role = new(new UniqueName(new UniqueNameSettings(), "admin"));
await _roleRepository.SaveAsync(role);

ApiKey apiKey = new(new DisplayName("Test"), _secret);
apiKey.AddRole(role);

await _apiKeyRepository.SaveAsync(apiKey);

ApiKeyEntity? entity = await IdentityContext.ApiKeys.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(apiKey.Id.Value, entity.StreamId);
Assert.Equal(role.Id.Value, Assert.Single(entity.Roles).StreamId);

apiKey.RemoveRole(role);
await _apiKeyRepository.SaveAsync(apiKey);

entity = await IdentityContext.ApiKeys.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(apiKey.Id.Value, entity.StreamId);
Assert.Empty(entity.Roles);
}

[Fact(DisplayName = "SaveAsync: it should save the API key correctly.")]
public async Task Given_ApiKey_When_SaveAsync_Then_SavedCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,35 @@ public async Task Given_TenantIdUniqueName_When_LoadAsync_Then_CorrectResult(str
}
}

[Fact(DisplayName = "SaveAsync: it should remove an API key role.")]
public async Task Given_ApiKeyWithRole_When_SaveAsync_Then_RoleRemoved()
{
Role role = new(new UniqueName(new UniqueNameSettings(), "admin"));
await _roleRepository.SaveAsync(role);

User user = new(new UniqueName(new UniqueNameSettings(), Faker.Person.UserName));
user.AddRole(role);

await _userRepository.SaveAsync(user);

UserEntity? entity = await IdentityContext.Users.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(user.Id.Value, entity.StreamId);
Assert.Equal(role.Id.Value, Assert.Single(entity.Roles).StreamId);

user.RemoveRole(role);
await _userRepository.SaveAsync(user);

entity = await IdentityContext.Users.AsNoTracking()
.Include(x => x.Roles)
.SingleOrDefaultAsync();
Assert.NotNull(entity);
Assert.Equal(user.Id.Value, entity.StreamId);
Assert.Empty(entity.Roles);
}

[Fact(DisplayName = "SaveAsync: it should save the user correctly.")]
public async Task Given_User_When_SaveAsync_Then_SavedCorrectly()
{
Expand Down

0 comments on commit 465fed6

Please sign in to comment.