From 576ae919f983dd046297fd8b9d313f8f5c9f6d95 Mon Sep 17 00:00:00 2001 From: Francis Pion Date: Mon, 29 Jan 2024 22:22:12 -0500 Subject: [PATCH] Added integration tests. --- .../OneTimePasswordRepositoryTests.cs | 31 +++++++++++++++++++ .../Repositories/SessionRepositoryTests.cs | 30 ++++++++++++++++++ .../Repositories/UserRepositoryTests.cs | 30 ++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/OneTimePasswordRepositoryTests.cs b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/OneTimePasswordRepositoryTests.cs index 71d205e..4b3bd20 100644 --- a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/OneTimePasswordRepositoryTests.cs +++ b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/OneTimePasswordRepositoryTests.cs @@ -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; @@ -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 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() { diff --git a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/SessionRepositoryTests.cs b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/SessionRepositoryTests.cs index bff02ae..4a644ff 100644 --- a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/SessionRepositoryTests.cs +++ b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/SessionRepositoryTests.cs @@ -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 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() { diff --git a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/UserRepositoryTests.cs b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/UserRepositoryTests.cs index 5a37858..1a98645 100644 --- a/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/UserRepositoryTests.cs +++ b/tests/Logitar.Identity.EFCore.SqlServer.IntegrationTests/Repositories/UserRepositoryTests.cs @@ -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 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() {