Skip to content

Commit

Permalink
Added integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Jan 30, 2024
1 parent 3eec680 commit 576ae91
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Logitar.EventSourcing.EntityFrameworkCore.Relational;
using Logitar.Identity.Domain.Passwords;
using Logitar.Identity.Domain.Shared;
using Logitar.Identity.Domain.Users;
using Logitar.Identity.EntityFrameworkCore.Relational;
using Logitar.Identity.EntityFrameworkCore.Relational.Entities;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -108,6 +109,36 @@ public async Task LoadAsync_it_should_load_the_One_Time_Passwords_by_identifiers
Assert.Contains(oneTimePasswords, deleted.Equals);
}

[Fact(DisplayName = "SaveAsync: it should save the deleted One-Time Password.")]
public async Task SaveAsync_it_should_save_the_deleted_One_Time_Password()
{
_oneTimePassword.SetCustomAttribute("Purpose", "reset_password");
_oneTimePassword.SetCustomAttribute("UserId", UserId.NewId().Value);
_oneTimePassword.Update();
await _oneTimePasswordRepository.SaveAsync(_oneTimePassword);

OneTimePasswordEntity? entity = await IdentityContext.OneTimePasswords.AsNoTracking()
.SingleOrDefaultAsync(x => x.AggregateId == _oneTimePassword.Id.Value);
Assert.NotNull(entity);

CustomAttributeEntity[] customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.OneTimePasswords) && x.EntityId == entity.OneTimePasswordId)
.ToArrayAsync();
Assert.Equal(_oneTimePassword.CustomAttributes.Count, customAttributes.Length);
foreach (KeyValuePair<string, string> customAttribute in _oneTimePassword.CustomAttributes)
{
Assert.Contains(customAttributes, c => c.Key == customAttribute.Key && c.Value == customAttribute.Value);
}

_oneTimePassword.Delete();
await _oneTimePasswordRepository.SaveAsync(_oneTimePassword);

customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.OneTimePasswords) && x.EntityId == entity.OneTimePasswordId)
.ToArrayAsync();
Assert.Empty(customAttributes);
}

[Fact(DisplayName = "SaveAsync: it should save the specified One-Time Password.")]
public async Task SaveAsync_it_should_save_the_specified_One_Time_Password()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ public async Task LoadAsync_it_should_load_the_sessions_by_identifiers()
Assert.Contains(sessions, deleted.Equals);
}

[Fact(DisplayName = "SaveAsync: it should save the deleted session.")]
public async Task SaveAsync_it_should_save_the_deleted_session()
{
_session.SetCustomAttribute("AdditionalInformation", $@"{{""User-Agent"":""{Faker.Internet.UserAgent()}""}}");
_session.SetCustomAttribute("IpAddress", Faker.Internet.Ip());
_session.Update();
await _sessionRepository.SaveAsync(_session);

SessionEntity? entity = await IdentityContext.Sessions.AsNoTracking()
.SingleOrDefaultAsync(x => x.AggregateId == _session.Id.Value);
Assert.NotNull(entity);

CustomAttributeEntity[] customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.Sessions) && x.EntityId == entity.SessionId)
.ToArrayAsync();
Assert.Equal(_session.CustomAttributes.Count, customAttributes.Length);
foreach (KeyValuePair<string, string> customAttribute in _session.CustomAttributes)
{
Assert.Contains(customAttributes, c => c.Key == customAttribute.Key && c.Value == customAttribute.Value);
}

_session.Delete();
await _sessionRepository.SaveAsync(_session);

customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.Sessions) && x.EntityId == entity.SessionId)
.ToArrayAsync();
Assert.Empty(customAttributes);
}

[Fact(DisplayName = "SaveAsync: it should save the specified session.")]
public async Task SaveAsync_it_should_save_the_specified_session()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,36 @@ public async Task LoadAsync_it_should_load_the_users_by_identifiers()
Assert.Contains(users, deleted.Equals);
}

[Fact(DisplayName = "SaveAsync: it should save the deleted user.")]
public async Task SaveAsync_it_should_save_the_deleted_user()
{
_user.SetCustomAttribute("HealthInsuranceNumber", Faker.Person.BuildHealthInsuranceNumber());
_user.SetCustomAttribute("JobTitle", "Sales Manager");
_user.Update();
await _userRepository.SaveAsync(_user);

UserEntity? entity = await IdentityContext.Users.AsNoTracking()
.SingleOrDefaultAsync(x => x.AggregateId == _user.Id.Value);
Assert.NotNull(entity);

CustomAttributeEntity[] customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.Users) && x.EntityId == entity.UserId)
.ToArrayAsync();
Assert.Equal(_user.CustomAttributes.Count, customAttributes.Length);
foreach (KeyValuePair<string, string> customAttribute in _user.CustomAttributes)
{
Assert.Contains(customAttributes, c => c.Key == customAttribute.Key && c.Value == customAttribute.Value);
}

_user.Delete();
await _userRepository.SaveAsync(_user);

customAttributes = await IdentityContext.CustomAttributes.AsNoTracking()
.Where(x => x.EntityType == nameof(IdentityContext.Users) && x.EntityId == entity.UserId)
.ToArrayAsync();
Assert.Empty(customAttributes);
}

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

0 comments on commit 576ae91

Please sign in to comment.