Skip to content

Commit

Permalink
Fixed EventBus and API key expiration. (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 authored Dec 27, 2024
1 parent 7383990 commit 20b5589
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
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
2 changes: 1 addition & 1 deletion lib/Logitar.Identity.Core/ApiKeys/ApiKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ protected virtual void Handle(ApiKeyUpdated @event)
}
if (@event.ExpiresOn.HasValue)
{
ExpiresOn = @event.ExpiresOn.Value;
_expiresOn = @event.ExpiresOn.Value;
}

foreach (KeyValuePair<Identifier, string?> customAttribute in @event.CustomAttributes)
Expand Down
6 changes: 3 additions & 3 deletions lib/Logitar.Identity.Infrastructure/EventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Logitar.Identity.Infrastructure;

public class EventBus : IEventBus
{
private readonly IMediator _mediator;
protected IMediator Mediator { get; }

public EventBus(IMediator mediator)
{
_mediator = mediator;
Mediator = mediator;
}

public virtual async Task PublishAsync(IEvent @event, CancellationToken cancellationToken)
{
await _mediator.Publish(@event, cancellationToken);
await Mediator.Publish(@event, cancellationToken);
}
}
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 20b5589

Please sign in to comment.