From ad7a05afdf88110cfa315f5e49888e8d204fae02 Mon Sep 17 00:00:00 2001 From: Norbert Truchsess Date: Mon, 16 Sep 2024 12:30:13 +0200 Subject: [PATCH] adjust unit-tests --- .../BusinessLogic/UserUploadBusinessLogic.cs | 7 +- .../BusinessLogic/UserBusinessLogicTests.cs | 92 ++++++++++++------- .../UserUploadBusinessLogicTests.cs | 35 ++++--- 3 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/administration/Administration.Service/BusinessLogic/UserUploadBusinessLogic.cs b/src/administration/Administration.Service/BusinessLogic/UserUploadBusinessLogic.cs index 2739abbdd3..eb32fdfe2a 100644 --- a/src/administration/Administration.Service/BusinessLogic/UserUploadBusinessLogic.cs +++ b/src/administration/Administration.Service/BusinessLogic/UserUploadBusinessLogic.cs @@ -23,7 +23,6 @@ using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service; using Org.Eclipse.TractusX.Portal.Backend.Framework.IO; using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq; -using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities; @@ -41,7 +40,6 @@ public class UserUploadBusinessLogic : IUserUploadBusinessLogic private readonly UserSettings _settings; private readonly IIdentityData _identityData; private readonly IErrorMessageService _errorMessageService; - private readonly IPortalRepositories _portalRepositories; /// /// Constructor. @@ -51,21 +49,18 @@ public class UserUploadBusinessLogic : IUserUploadBusinessLogic /// Access to the identity Service /// ErrorMessage Service /// Settings - /// Portal Repositories public UserUploadBusinessLogic( IUserProvisioningService userProvisioningService, IMailingProcessCreation mailingProcessCreation, IIdentityService identityService, IErrorMessageService errorMessageService, - IOptions settings, - IPortalRepositories portalRepositories) + IOptions settings) { _userProvisioningService = userProvisioningService; _mailingProcessCreation = mailingProcessCreation; _identityData = identityService.IdentityData; _errorMessageService = errorMessageService; _settings = settings.Value; - _portalRepositories = portalRepositories; } public ValueTask UploadOwnCompanyIdpUsersAsync(Guid identityProviderId, IFormFile document, CancellationToken cancellationToken) diff --git a/tests/administration/Administration.Service.Tests/BusinessLogic/UserBusinessLogicTests.cs b/tests/administration/Administration.Service.Tests/BusinessLogic/UserBusinessLogicTests.cs index 8a73d0fe70..c8880aa92b 100644 --- a/tests/administration/Administration.Service.Tests/BusinessLogic/UserBusinessLogicTests.cs +++ b/tests/administration/Administration.Service.Tests/BusinessLogic/UserBusinessLogicTests.cs @@ -76,7 +76,7 @@ public class UserBusinessLogicTests private readonly IIdentityData _identity; private readonly ICollection _addedRoles = new HashSet(); private readonly ICollection _removedRoles = new HashSet(); - private readonly Func _processLine; + private readonly Func?, (Guid CompanyUserId, string UserName, string? Password, Exception? Error)> _processLine; private readonly Func _companyUserSelectFunction; private readonly Exception _error; private readonly UserSettings _settings; @@ -122,7 +122,7 @@ public UserBusinessLogicTests() _createdCentralCompanyId = Guid.NewGuid(); _displayName = _fixture.Create(); - _processLine = A.Fake>(); + _processLine = A.Fake?, (Guid CompanyUserId, string UserName, string? Password, Exception? Error)>>(); _companyUserSelectFunction = A.Fake>(); _identity = A.Fake(); @@ -276,12 +276,15 @@ public async Task TestUserCreationCreationError() CreateUserCreationInfo() }; - A.CallTo(() => _processLine(A.That.Matches(u => - u.FirstName == userCreationInfo.firstName && - u.LastName == userCreationInfo.lastName && - u.Email == userCreationInfo.eMail - ))).ReturnsLazily( - (UserCreationRoleDataIdpInfo creationInfo) => _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => + u.FirstName == userCreationInfo.firstName && + u.LastName == userCreationInfo.lastName && + u.Email == userCreationInfo.eMail), + A>._) + ).ReturnsLazily((CompanyNameIdpAliasData _, UserCreationRoleDataIdpInfo creationInfo, Action? _) => + _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() .With(x => x.UserName, creationInfo.UserName) .With(x => x.Error, error) .Create()); @@ -299,11 +302,14 @@ public async Task TestUserCreationCreationError() var result = await sut.CreateOwnCompanyUsersAsync(userList).ToListAsync(); - A.CallTo(() => _processLine(A.That.Matches(u => - u.FirstName == userCreationInfo.firstName && - u.LastName == userCreationInfo.lastName && - u.Email == userCreationInfo.eMail - ))).MustHaveHappenedOnceExactly(); + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => + u.FirstName == userCreationInfo.firstName && + u.LastName == userCreationInfo.lastName && + u.Email == userCreationInfo.eMail), + A>._) + ).MustHaveHappenedOnceExactly(); A.CallTo(() => _mockLogger.Log(A.That.IsEqualTo(LogLevel.Error), A._, A._)).MustHaveHappenedOnceExactly(); @@ -331,11 +337,14 @@ public async Task TestUserCreationCreationThrows() CreateUserCreationInfo() }; - A.CallTo(() => _processLine(A.That.Matches(u => - u.FirstName == userCreationInfo.firstName && - u.LastName == userCreationInfo.lastName && - u.Email == userCreationInfo.eMail - ))).Throws(() => expected); + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => + u.FirstName == userCreationInfo.firstName && + u.LastName == userCreationInfo.lastName && + u.Email == userCreationInfo.eMail), + A>._) + ).Throws(() => expected); var sut = new UserBusinessLogic( null!, @@ -352,11 +361,14 @@ public async Task TestUserCreationCreationThrows() var error = await Assert.ThrowsAsync(Act); - A.CallTo(() => _processLine(A.That.Matches(u => + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => u.FirstName == userCreationInfo.firstName && u.LastName == userCreationInfo.lastName && - u.Email == userCreationInfo.eMail - ))).MustHaveHappenedOnceExactly(); + u.Email == userCreationInfo.eMail), + A>._) + ).MustHaveHappenedOnceExactly(); A.CallTo(() => _mockLogger.Log(A.That.IsEqualTo(LogLevel.Error), A._, A._)).MustNotHaveHappened(); A.CallTo(() => _mailingProcessCreation.CreateMailProcess(A._, "NewUserTemplate", A>._)) @@ -433,8 +445,12 @@ public async Task TestCreateOwnCompanyIdpUserAsyncError() var userCreationInfoIdp = CreateUserCreationInfoIdp(); - A.CallTo(() => _processLine(A.That.Matches(u => u.FirstName == userCreationInfoIdp.FirstName))).ReturnsLazily( - (UserCreationRoleDataIdpInfo creationInfo) => _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => u.FirstName == userCreationInfoIdp.FirstName), + A>._) + ).ReturnsLazily((CompanyNameIdpAliasData _, UserCreationRoleDataIdpInfo creationInfo, Action? onSuccess) => + _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() .With(x => x.UserName, creationInfo.UserName) .With(x => x.Error, _error) .Create()); @@ -465,7 +481,11 @@ public async Task TestCreateOwnCompanyIdpUserAsyncThrows() var userCreationInfoIdp = CreateUserCreationInfoIdp(); - A.CallTo(() => _processLine(A.That.Matches(u => u.FirstName == userCreationInfoIdp.FirstName))).Throws(_error); + A.CallTo(() => _processLine( + A._, + A.That.Matches(u => u.FirstName == userCreationInfoIdp.FirstName), + A>._) + ).Throws(_error); var sut = new UserBusinessLogic( null!, @@ -1153,7 +1173,7 @@ public async Task GetOwnCompanyAppUsersAsync_ReturnsExpectedResult() A.CallTo(() => _identity.IdentityId).Returns(userId); A.CallTo(() => _userRepository.GetOwnCompanyAppUsersPaginationSourceAsync(A._, A._, A>._, A>._, A._)) - .Returns((skip, take) => Task.FromResult(new Pagination.Source(companyUsers.Count(), companyUsers.Skip(skip).Take(take)))); + .Returns((skip, take) => Task.FromResult?>(new Pagination.Source(companyUsers.Count(), companyUsers.Skip(skip).Take(take)))); var sut = new UserBusinessLogic(null!, null!, null!, _portalRepositories, _identityService, null!, null!, null!, A.Fake>()); // Act @@ -1178,7 +1198,7 @@ public async Task GetOwnCompanyAppUsersAsync_SecondPage_ReturnsExpectedResult() A.CallTo(() => _identity.IdentityId).Returns(userId); A.CallTo(() => _userRepository.GetOwnCompanyAppUsersPaginationSourceAsync(A._, A._, A>._, A>._, A._)) - .Returns((skip, take) => Task.FromResult(new Pagination.Source(companyUsers.Count(), companyUsers.Skip(skip).Take(take)))); + .Returns((skip, take) => Task.FromResult?>(new Pagination.Source(companyUsers.Count(), companyUsers.Skip(skip).Take(take)))); var sut = new UserBusinessLogic(null!, null!, null!, _portalRepositories, _identityService, null!, null!, null!, A.Fake>()); // Act @@ -1541,16 +1561,22 @@ private void SetupFakesForUserCreation(bool isBulkUserCreation) } A.CallTo(() => _userProvisioningService.CreateOwnCompanyIdpUsersAsync(A._, A>._, A>._, A._)) - .ReturnsLazily((CompanyNameIdpAliasData _, IAsyncEnumerable userCreationInfos, Action _, CancellationToken _) => - userCreationInfos.Select(userCreationInfo => _processLine(userCreationInfo))); + .ReturnsLazily((CompanyNameIdpAliasData idpAliasData, IAsyncEnumerable userCreationInfos, Action? onSuccess, CancellationToken _) => + userCreationInfos.Select(userCreationInfo => _processLine(idpAliasData, userCreationInfo, onSuccess))); A.CallTo(() => _userProvisioningService.GetIdentityProviderDisplayName(A._)).Returns(_displayName); - A.CallTo(() => _processLine(A._)).ReturnsLazily( - (UserCreationRoleDataIdpInfo creationInfo) => _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() - .With(x => x.UserName, creationInfo.UserName) - .With(x => x.Error, default(Exception?)) - .Create()); + A.CallTo(() => _processLine(A._, A._, A>._)) + .ReturnsLazily((CompanyNameIdpAliasData aliasData, UserCreationRoleDataIdpInfo creationInfo, Action? onSucess) => + { + var password = aliasData.IsSharedIdp ? _fixture.Create() : null; + onSucess?.Invoke(new(creationInfo, password)); + return _fixture.Build<(Guid CompanyUserId, string UserName, string? Password, Exception? Error)>() + .With(x => x.UserName, creationInfo.UserName) + .With(x => x.Password, password) + .With(x => x.Error, default(Exception?)) + .Create(); + }); } private void SetupFakesForUserDeletion() diff --git a/tests/administration/Administration.Service.Tests/BusinessLogic/UserUploadBusinessLogicTests.cs b/tests/administration/Administration.Service.Tests/BusinessLogic/UserUploadBusinessLogicTests.cs index 7de3560ef4..fdd434b18a 100644 --- a/tests/administration/Administration.Service.Tests/BusinessLogic/UserUploadBusinessLogicTests.cs +++ b/tests/administration/Administration.Service.Tests/BusinessLogic/UserUploadBusinessLogicTests.cs @@ -24,7 +24,6 @@ using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service; using Org.Eclipse.TractusX.Portal.Backend.Framework.IO; using Org.Eclipse.TractusX.Portal.Backend.Framework.Tests.Shared; -using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums; using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Identities; @@ -39,7 +38,6 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.BusinessLog public class UserUploadBusinessLogicTests { private readonly IFixture _fixture; - private readonly IPortalRepositories _portalRepositories; private readonly IUserProvisioningService _userProvisioningService; private readonly IOptions _options; private readonly IFormFile _document; @@ -60,7 +58,6 @@ public UserUploadBusinessLogicTests() _fixture.Behaviors.OfType().ToList() .ForEach(b => _fixture.Behaviors.Remove(b)); _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); - _portalRepositories = A.Fake(); _random = new Random(); @@ -97,7 +94,7 @@ public async Task TestSetup() { SetupFakes([HeaderLine()]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -114,16 +111,16 @@ public async Task TestSetup() [Fact] public async Task TestUserCreationAllSuccess() { - SetupFakes(new[] { + SetupFakes([ HeaderLine(), NextLine(), NextLine(), NextLine(), NextLine(), NextLine() - }); + ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -150,7 +147,7 @@ public async Task TestUserCreationHeaderParsingThrows() NextLine() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); async Task Act() => await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -183,7 +180,7 @@ public async Task TestUserCreationCreationError() .With(x => x.Error, detailError) .Create()); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -225,7 +222,7 @@ public async Task TestUserCreationCreationNoRolesError() NextLine() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -253,7 +250,7 @@ public async Task TestUserCreationParsingError() NextLine() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -283,7 +280,7 @@ public async Task TestUserCreationCreationThrows() A.CallTo(() => _processLine(A._, A.That.Matches(info => CreationInfoMatches(info, creationInfo)), A>._)) .Throws(_error); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanyIdpUsersAsync(_identityProviderId, _document, CancellationToken.None); @@ -307,7 +304,7 @@ public async Task TestSetupSharedIdp() { SetupFakes([HeaderLineSharedIdp()]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -331,7 +328,7 @@ public async Task TestUserCreationSharedIdpAllSuccess() NextLineSharedIdp() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -360,7 +357,7 @@ public async Task TestUserCreationSharedIdpHeaderParsingThrows() NextLineSharedIdp() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); async Task Act() => await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -386,7 +383,7 @@ public async Task TestUserCreationSharedIdpNoRolesError() NextLineSharedIdp(), ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -428,7 +425,7 @@ public async Task TestUserCreationSharedIdpCreationError() .With(x => x.Error, detailError) .Create()); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -468,7 +465,7 @@ public async Task TestUserCreationSharedIdpParsingError() NextLineSharedIdp() ]); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None); @@ -501,7 +498,7 @@ public async Task TestUserCreationSharedIdpCreationThrows() A.CallTo(() => _processLine(A._, A.That.Matches(info => CreationInfoMatchesSharedIdp(info, creationInfo)), A>._)) .Throws(_error); - var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options, _portalRepositories); + var sut = new UserUploadBusinessLogic(_userProvisioningService, _mailingProcessCreation, _identityService, _errorMessageService, _options); var result = await sut.UploadOwnCompanySharedIdpUsersAsync(_document, CancellationToken.None);