From c89feec95357b15deff3eadd2e191e834f1bb1db Mon Sep 17 00:00:00 2001 From: Jimmy Vo Date: Tue, 15 Apr 2025 12:34:48 -0400 Subject: [PATCH] [PM-15621] Fix merge conflicts --- ...teClaimedOrganizationUserAccountCommand.cs | 21 +- ...laimedOrganizationUserAccountValidator.cs} | 4 +- ...laimedOrganizationUserAccountValidator.cs} | 2 +- ...OrganizationServiceCollectionExtensions.cs | 2 +- .../OrganizationUsersControllerTests.cs | 27 +- ...imedOrganizationUserAccountCommandTests.cs | 989 +++++------------- 6 files changed, 304 insertions(+), 741 deletions(-) rename src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/{DeleteManagedOrganizationUserAccountValidator.cs => DeleteClaimedOrganizationUserAccountValidator.cs} (97%) rename src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/{IDeleteManagedOrganizationUserAccountValidator.cs => IDeleteClaimedOrganizationUserAccountValidator.cs} (80%) diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommand.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommand.cs index 67d535491a..f6eac2b293 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommand.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommand.cs @@ -1,5 +1,6 @@ using Bit.Core.AdminConsole.Errors; using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; +using Bit.Core.AdminConsole.Repositories; using Bit.Core.AdminConsole.Shared.Validation; using Bit.Core.Context; using Bit.Core.Entities; @@ -23,16 +24,14 @@ public class DeleteClaimedOrganizationUserAccountCommand : IDeleteClaimedOrganiz { private readonly IUserService _userService; private readonly IEventService _eventService; - private readonly IDeleteManagedOrganizationUserAccountValidator _deleteManagedOrganizationUserAccountValidator; - private readonly IGetOrganizationUsersManagementStatusQuery _getOrganizationUsersManagementStatusQuery; private readonly IGetOrganizationUsersClaimedStatusQuery _getOrganizationUsersClaimedStatusQuery; + private readonly IDeleteClaimedOrganizationUserAccountValidator _deleteManagedOrganizationUserAccountValidator; + private readonly ILogger _logger; private readonly IOrganizationUserRepository _organizationUserRepository; private readonly IUserRepository _userRepository; private readonly ICurrentContext _currentContext; - private readonly ILogger _logger; private readonly IReferenceEventService _referenceEventService; private readonly IPushNotificationService _pushService; - private readonly IOrganizationRepository _organizationRepository; private readonly IProviderUserRepository _providerUserRepository; public DeleteClaimedOrganizationUserAccountCommand( IUserService userService, @@ -41,21 +40,23 @@ public class DeleteClaimedOrganizationUserAccountCommand : IDeleteClaimedOrganiz IOrganizationUserRepository organizationUserRepository, IUserRepository userRepository, ICurrentContext currentContext, - ILogger logger, IReferenceEventService referenceEventService, - IPushNotificationService pushService) + IPushNotificationService pushService, + IProviderUserRepository providerUserRepository, + ILogger logger, + IDeleteClaimedOrganizationUserAccountValidator deleteManagedOrganizationUserAccountValidator) { _userService = userService; _eventService = eventService; - _deleteManagedOrganizationUserAccountValidator = deleteManagedOrganizationUserAccountValidator; - _getOrganizationUsersManagementStatusQuery = _getOrganizationUsersManagementStatusQuery; _getOrganizationUsersClaimedStatusQuery = getOrganizationUsersClaimedStatusQuery; _organizationUserRepository = organizationUserRepository; _userRepository = userRepository; _currentContext = currentContext; - _logger = logger; _referenceEventService = referenceEventService; _pushService = pushService; + _providerUserRepository = providerUserRepository; + _logger = logger; + _deleteManagedOrganizationUserAccountValidator = deleteManagedOrganizationUserAccountValidator; } public async Task> DeleteUserAsync(Guid organizationId, Guid organizationUserId, Guid deletingUserId) @@ -93,7 +94,7 @@ public class DeleteClaimedOrganizationUserAccountCommand : IDeleteClaimedOrganiz { var orgUsers = await _organizationUserRepository.GetManyAsync(orgUserIds); var users = await GetUsersAsync(orgUsers); - var managementStatuses = await _getOrganizationUsersManagementStatusQuery.GetUsersOrganizationManagementStatusAsync(organizationId, orgUserIds); + var managementStatuses = await _getOrganizationUsersClaimedStatusQuery.GetUsersOrganizationClaimedStatusAsync(organizationId, orgUserIds); var requests = CreateRequests(organizationId, deletingUserId, orgUserIds, orgUsers, users, managementStatuses); var results = await _deleteManagedOrganizationUserAccountValidator.ValidateAsync(requests); diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteManagedOrganizationUserAccountValidator.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountValidator.cs similarity index 97% rename from src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteManagedOrganizationUserAccountValidator.cs rename to src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountValidator.cs index 4ed1bcd9f5..03ee67964d 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteManagedOrganizationUserAccountValidator.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountValidator.cs @@ -8,10 +8,10 @@ using Bit.Core.Repositories; namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers; -public class DeleteManagedOrganizationUserAccountValidator( +public class DeleteClaimedOrganizationUserAccountValidator( ICurrentContext currentContext, IOrganizationUserRepository organizationUserRepository, - IProviderUserRepository providerUserRepository) : IDeleteManagedOrganizationUserAccountValidator + IProviderUserRepository providerUserRepository) : IDeleteClaimedOrganizationUserAccountValidator { public async Task> ValidateAsync(List requests) { diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteManagedOrganizationUserAccountValidator.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteClaimedOrganizationUserAccountValidator.cs similarity index 80% rename from src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteManagedOrganizationUserAccountValidator.cs rename to src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteClaimedOrganizationUserAccountValidator.cs index 4eb03c7629..7924187133 100644 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteManagedOrganizationUserAccountValidator.cs +++ b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/Interfaces/IDeleteClaimedOrganizationUserAccountValidator.cs @@ -2,7 +2,7 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; -public interface IDeleteManagedOrganizationUserAccountValidator +public interface IDeleteClaimedOrganizationUserAccountValidator { Task> ValidateAsync(List requests); } diff --git a/src/Core/OrganizationFeatures/OrganizationServiceCollectionExtensions.cs b/src/Core/OrganizationFeatures/OrganizationServiceCollectionExtensions.cs index 95c20c077b..d86074e631 100644 --- a/src/Core/OrganizationFeatures/OrganizationServiceCollectionExtensions.cs +++ b/src/Core/OrganizationFeatures/OrganizationServiceCollectionExtensions.cs @@ -128,7 +128,7 @@ public static class OrganizationServiceCollectionExtensions services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); } diff --git a/test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs b/test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs index 14784f4f28..c0cf9997a7 100644 --- a/test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs +++ b/test/Api.Test/AdminConsole/Controllers/OrganizationUsersControllerTests.cs @@ -360,7 +360,6 @@ public class OrganizationUsersControllerTests sutProvider.Sut.DeleteAccount(orgId, id)); } -<<<<<<< HEAD // [Theory] // [BitAutoData] // public async Task BulkDeleteAccount_WhenUserCanManageUsers_Success( @@ -369,7 +368,7 @@ public class OrganizationUsersControllerTests // { // sutProvider.GetDependency().ManageUsers(orgId).Returns(true); // sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser); - // sutProvider.GetDependency() + // sutProvider.GetDependency() // .DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id) // .Returns(deleteResults); // @@ -377,32 +376,10 @@ public class OrganizationUsersControllerTests // // Assert.Equal(deleteResults.Count, response.Data.Count()); // Assert.True(response.Data.All(r => deleteResults.Any(res => res.Item1 == r.Id && res.Item2 == r.Error))); - // await sutProvider.GetDependency() + // await sutProvider.GetDependency() // .Received(1) // .DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id); // } -======= - [Theory] - [BitAutoData] - public async Task BulkDeleteAccount_WhenUserCanManageUsers_Success( - Guid orgId, OrganizationUserBulkRequestModel model, User currentUser, - List<(Guid, string)> deleteResults, SutProvider sutProvider) - { - sutProvider.GetDependency().ManageUsers(orgId).Returns(true); - sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(currentUser); - sutProvider.GetDependency() - .DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id) - .Returns(deleteResults); - - var response = await sutProvider.Sut.BulkDeleteAccount(orgId, model); - - Assert.Equal(deleteResults.Count, response.Data.Count()); - Assert.True(response.Data.All(r => deleteResults.Any(res => res.Item1 == r.Id && res.Item2 == r.Error))); - await sutProvider.GetDependency() - .Received(1) - .DeleteManyUsersAsync(orgId, model.Ids, currentUser.Id); - } ->>>>>>> main [Theory] [BitAutoData] diff --git a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommandTests.cs b/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommandTests.cs index 87bdd943e7..6508148059 100644 --- a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommandTests.cs +++ b/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommandTests.cs @@ -5,65 +5,258 @@ namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.OrganizationUsers; [SutProviderCustomize] public class DeleteClaimedOrganizationUserAccountCommandTests { -<<<<<<< HEAD:test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteManagedOrganizationUserAccountCommandTests.cs - // [Theory] // [BitAutoData] - // public async Task DeleteUserAsync_WithValidUser_DeletesUserAndLogsEvents( - // SutProvider sutProvider, User user, Guid organizationId, - // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) + // public async Task DeleteUserAsync_WithValidUser_DeletesUserAndLogsEvent( + // SutProvider sutProvider, User user, Guid deletingUserId, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser) // { // // Arrange - // orgUser.OrganizationId = organizationId; - // orgUser.UserId = user.Id; - - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Any>()) - // .Returns(new List { orgUser }); + // organizationUser.UserId = user.Id; // sutProvider.GetDependency() - // .GetManyAsync(Arg.Is>(ids => ids.Contains(user.Id))) - // .Returns(new[] { user }); + // .GetByIdAsync(user.Id) + // .Returns(user); - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(organizationId, Arg.Any>()) - // .Returns(new Dictionary { { orgUser.Id, true } }); + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency() + // .GetUsersOrganizationClaimedStatusAsync( + // organizationUser.OrganizationId, + // Arg.Is>(ids => ids.Contains(organizationUser.Id))) + // .Returns(new Dictionary { { organizationUser.Id, true } }); + + // sutProvider.GetDependency() + // .HasConfirmedOwnersExceptAsync( + // organizationUser.OrganizationId, + // Arg.Is>(ids => ids.Contains(organizationUser.Id)), + // includeProvider: Arg.Any()) + // .Returns(true); // // Act - // await sutProvider.Sut.DeleteUserAsync(organizationId, orgUser.Id, null); + // await sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId); // // Assert - // await sutProvider.GetDependency().Received(1).DeleteManyAsync(Arg.Is>(users => users.Any(u => u.Id == user.Id))); - // await sutProvider.GetDependency().Received(1).LogOrganizationUserEventsAsync( - // Arg.Is>(events => - // events.Count(e => e.Item1.Id == orgUser.Id && e.Item2 == EventType.OrganizationUser_Deleted) == 1)); + // await sutProvider.GetDependency().Received(1).DeleteAsync(user); + // await sutProvider.GetDependency().Received(1) + // .LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Deleted); // } // [Theory] // [BitAutoData] - // public async Task DeleteUserAsync_WhenError_ShouldReturnFailure( - // SutProvider sutProvider, User user, Guid organizationId, - // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) + // public async Task DeleteUserAsync_WithUserNotFound_ThrowsException( + // SutProvider sutProvider, + // Guid organizationId, Guid organizationUserId) // { // // Arrange - // orgUser.OrganizationId = organizationId; - // orgUser.UserId = user.Id; - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Any>()) - // .Returns(new List { }); + // .GetByIdAsync(organizationUserId) + // .Returns((OrganizationUser?)null); // // Act - // // Test is not ready - // await sutProvider.Sut.DeleteUserAsync(organizationId, orgUser.Id, null); + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationId, organizationUserId, null)); // // Assert + // Assert.Equal("Member not found.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_DeletingYourself_ThrowsException( + // SutProvider sutProvider, + // User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser, + // Guid deletingUserId) + // { + // // Arrange + // organizationUser.UserId = user.Id = deletingUserId; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency().GetByIdAsync(user.Id) + // .Returns(user); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); + + // // Assert + // Assert.Equal("You cannot delete yourself.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_WhenUserIsInvited_ThrowsException( + // SutProvider sutProvider, + // [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser organizationUser) + // { + // // Arrange + // organizationUser.UserId = null; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, null)); + + // // Assert + // Assert.Equal("You cannot delete a member with Invited status.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_WhenCustomUserDeletesAdmin_ThrowsException( + // SutProvider sutProvider, User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Admin)] OrganizationUser organizationUser, + // Guid deletingUserId) + // { + // // Arrange + // organizationUser.UserId = user.Id; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency().GetByIdAsync(user.Id) + // .Returns(user); + + // sutProvider.GetDependency() + // .OrganizationCustom(organizationUser.OrganizationId) + // .Returns(true); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); + + // // Assert + // Assert.Equal("Custom users can not delete admins.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_DeletingOwnerWhenNotOwner_ThrowsException( + // SutProvider sutProvider, User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser organizationUser, + // Guid deletingUserId) + // { + // // Arrange + // organizationUser.UserId = user.Id; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency().GetByIdAsync(user.Id) + // .Returns(user); + + // sutProvider.GetDependency() + // .OrganizationOwner(organizationUser.OrganizationId) + // .Returns(false); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); + + // // Assert + // Assert.Equal("Only owners can delete other owners.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_DeletingLastConfirmedOwner_ThrowsException( + // SutProvider sutProvider, User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser organizationUser, + // Guid deletingUserId) + // { + // // Arrange + // organizationUser.UserId = user.Id; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency().GetByIdAsync(user.Id) + // .Returns(user); + + // sutProvider.GetDependency() + // .OrganizationOwner(organizationUser.OrganizationId) + // .Returns(true); + + // sutProvider.GetDependency() + // .HasConfirmedOwnersExceptAsync( + // organizationUser.OrganizationId, + // Arg.Is>(ids => ids.Contains(organizationUser.Id)), + // includeProvider: Arg.Any()) + // .Returns(false); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); + + // // Assert + // Assert.Equal("Organization must have at least one confirmed owner.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteUserAsync_WithUserNotManaged_ThrowsException( + // SutProvider sutProvider, User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser) + // { + // // Arrange + // organizationUser.UserId = user.Id; + + // sutProvider.GetDependency() + // .GetByIdAsync(organizationUser.Id) + // .Returns(organizationUser); + + // sutProvider.GetDependency().GetByIdAsync(user.Id) + // .Returns(user); + + // sutProvider.GetDependency() + // .GetUsersOrganizationClaimedStatusAsync(organizationUser.OrganizationId, Arg.Any>()) + // .Returns(new Dictionary { { organizationUser.Id, false } }); + + // // Act + // var exception = await Assert.ThrowsAsync(() => + // sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, null)); + + // // Assert + // Assert.Equal("Member is not claimed by the organization.", exception.Message); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); // } // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WithValidUsers_DeletesUsersAndLogsEvents( - // SutProvider sutProvider, User user1, User user2, Guid organizationId, + // SutProvider sutProvider, User user1, User user2, Guid organizationId, // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser1, // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser2) // { @@ -80,8 +273,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // .GetManyAsync(Arg.Is>(ids => ids.Contains(user1.Id) && ids.Contains(user2.Id))) // .Returns(new[] { user1, user2 }); - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(organizationId, Arg.Any>()) + // sutProvider.GetDependency() + // .GetUsersOrganizationClaimedStatusAsync(organizationId, Arg.Any>()) // .Returns(new Dictionary { { orgUser1.Id, true }, { orgUser2.Id, true } }); // // Act @@ -90,7 +283,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // // Assert // Assert.Equal(2, results.Count()); - // Assert.All(results, r => Assert.IsType(r.Item2)); + // Assert.All(results, r => Assert.Empty(r.Item2)); // await sutProvider.GetDependency().Received(1).GetManyAsync(userIds); // await sutProvider.GetDependency().Received(1).DeleteManyAsync(Arg.Is>(users => users.Any(u => u.Id == user1.Id) && users.Any(u => u.Id == user2.Id))); @@ -103,7 +296,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WhenUserNotFound_ReturnsErrorMessage( - // SutProvider sutProvider, + // SutProvider sutProvider, // Guid organizationId, // Guid orgUserId) // { @@ -112,13 +305,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // // Assert // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUserId, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("Member not found.", commandResult); - + // Assert.Equal(orgUserId, result.First().Item1); + // Assert.Contains("Member not found.", result.First().Item2); // await sutProvider.GetDependency() // .DidNotReceiveWithAnyArgs() // .DeleteManyAsync(default); @@ -129,7 +317,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WhenDeletingYourself_ReturnsErrorMessage( - // SutProvider sutProvider, + // SutProvider sutProvider, // User user, [OrganizationUser] OrganizationUser orgUser, Guid deletingUserId) // { // // Arrange @@ -148,13 +336,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // // Assert // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("You cannot delete yourself.", commandResult); - + // Assert.Equal(orgUser.Id, result.First().Item1); + // Assert.Contains("You cannot delete yourself.", result.First().Item2); // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); // await sutProvider.GetDependency().Received(0) // .LogOrganizationUserEventsAsync(Arg.Any>()); @@ -163,29 +346,23 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WhenUserIsInvited_ReturnsErrorMessage( - // SutProvider sutProvider, - // User user, + // SutProvider sutProvider, // [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser orgUser) // { // // Arrange - // orgUser.UserId = user.Id; + // orgUser.UserId = null; // sutProvider.GetDependency() // .GetManyAsync(Arg.Any>()) // .Returns(new List { orgUser }); - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Is>(input => input.Contains(user.Id))) - // .Returns(new[] { user }); - // // Act // var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, null); // // Assert // Assert.Single(result); - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - // AssertErrorMessages("You cannot delete a member with Invited status.", result.First().Item2); + // Assert.Equal(orgUser.Id, result.First().Item1); + // Assert.Contains("You cannot delete a member with Invited status.", result.First().Item2); // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); // await sutProvider.GetDependency().Received(0) // .LogOrganizationUserEventsAsync(Arg.Any>()); @@ -194,7 +371,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WhenDeletingOwnerAsNonOwner_ReturnsErrorMessage( - // SutProvider sutProvider, User user, + // SutProvider sutProvider, User user, // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser, // Guid deletingUserId) // { @@ -213,23 +390,49 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // .OrganizationOwner(orgUser.OrganizationId) // .Returns(false); - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(Arg.Any(), Arg.Any>()) - // .Returns(new Dictionary { { orgUser.Id, true } }); + // var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, deletingUserId); + // Assert.Single(result); + // Assert.Equal(orgUser.Id, result.First().Item1); + // Assert.Contains("Only owners can delete other owners.", result.First().Item2); + // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); + // await sutProvider.GetDependency().Received(0) + // .LogOrganizationUserEventsAsync(Arg.Any>()); + // } + + // [Theory] + // [BitAutoData] + // public async Task DeleteManyUsersAsync_WhenDeletingLastOwner_ReturnsErrorMessage( + // SutProvider sutProvider, User user, + // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser, + // Guid deletingUserId) + // { + // // Arrange + // orgUser.UserId = user.Id; + + // sutProvider.GetDependency() + // .GetManyAsync(Arg.Any>()) + // .Returns(new List { orgUser }); + + // sutProvider.GetDependency() + // .GetManyAsync(Arg.Is>(i => i.Contains(user.Id))) + // .Returns(new[] { user }); + + // sutProvider.GetDependency() + // .OrganizationOwner(orgUser.OrganizationId) + // .Returns(true); + + // sutProvider.GetDependency() + // .HasConfirmedOwnersExceptAsync(orgUser.OrganizationId, Arg.Any>(), Arg.Any()) + // .Returns(false); // // Act // var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, deletingUserId); // // Assert // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("Only owners can delete other owners.", commandResult); - + // Assert.Equal(orgUser.Id, result.First().Item1); + // Assert.Contains("Organization must have at least one confirmed owner.", result.First().Item2); // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); // await sutProvider.GetDependency().Received(0) // .LogOrganizationUserEventsAsync(Arg.Any>()); @@ -238,7 +441,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_WhenUserNotManaged_ReturnsErrorMessage( - // SutProvider sutProvider, User user, + // SutProvider sutProvider, User user, // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) // { // // Arrange @@ -252,8 +455,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // .GetManyAsync(Arg.Is>(ids => ids.Contains(orgUser.UserId.Value))) // .Returns(new[] { user }); - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(Arg.Any(), Arg.Any>()) + // sutProvider.GetDependency() + // .GetUsersOrganizationClaimedStatusAsync(Arg.Any(), Arg.Any>()) // .Returns(new Dictionary { { orgUser.Id, false } }); // // Act @@ -261,108 +464,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // // Assert // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("Member is not managed by the organization.", commandResult); - - // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - // await sutProvider.GetDependency().Received(0) - // .LogOrganizationUserEventsAsync(Arg.Any>()); - // } - - - // [Theory] - // [BitAutoData] - // public async Task DeleteManyUsersAsync_WhenUserIsASoleOwner_ReturnsErrorMessage( - // SutProvider sutProvider, User user, - // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) - // { - // // Arrange - // orgUser.UserId = user.Id; - - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Any>()) - // .Returns(new List { orgUser }); - - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Is>(ids => ids.Contains(orgUser.UserId.Value))) - // .Returns(new[] { user }); - - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(Arg.Any(), Arg.Any>()) - // .Returns(new Dictionary { { orgUser.Id, true } }); - - // const int onlyOwnerCount = 1; - - // sutProvider.GetDependency() - // .GetCountByOnlyOwnerAsync(Arg.Is(id => id == user.Id)) - // .Returns(onlyOwnerCount); - - // // Act - // var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, null); - - // // Assert - // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("Cannot delete this user because it is the sole owner of at least one organization. Please delete these organizations or upgrade another user.", commandResult); - - // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - // await sutProvider.GetDependency().Received(0) - // .LogOrganizationUserEventsAsync(Arg.Any>()); - // } - - // [Theory] - // [BitAutoData] - // public async Task DeleteManyUsersAsync_WhenUserIsASoleProvider_ReturnsErrorMessage( - // SutProvider sutProvider, User user, - // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) - // { - // // Arrange - // orgUser.UserId = user.Id; - - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Any>()) - // .Returns(new List { orgUser }); - - // sutProvider.GetDependency() - // .GetManyAsync(Arg.Is>(ids => ids.Contains(orgUser.UserId.Value))) - // .Returns(new[] { user }); - - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(Arg.Any(), Arg.Any>()) - // .Returns(new Dictionary { { orgUser.Id, true } }); - - // const int onlyOwnerCount = 0; - - // sutProvider.GetDependency() - // .GetCountByOnlyOwnerAsync(Arg.Is(id => id == user.Id)) - // .Returns(onlyOwnerCount); - - // const int onlyOwnerProviderCount = 1; - - // sutProvider.GetDependency() - // .GetCountByOnlyOwnerAsync(Arg.Is(id => id == user.Id)) - // .Returns(onlyOwnerProviderCount); - - // // Act - // var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, null); - - // // Assert - // Assert.Single(result); - - // var userId = result.First().Item1; - // Assert.Equal(orgUser.Id, userId); - - // var commandResult = result.First().Item2; - // AssertErrorMessages("Cannot delete this user because it is the sole owner of at least one provider. Please delete these providers or upgrade another user.", commandResult); - + // Assert.Equal(orgUser.Id, result.First().Item1); + // Assert.Contains("Member is not claimed by the organization.", result.First().Item2); // await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); // await sutProvider.GetDependency().Received(0) // .LogOrganizationUserEventsAsync(Arg.Any>()); @@ -371,7 +474,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // [Theory] // [BitAutoData] // public async Task DeleteManyUsersAsync_MixedValidAndInvalidUsers_ReturnsAppropriateResults( - // SutProvider sutProvider, User user1, User user3, + // SutProvider sutProvider, User user1, User user3, // Guid organizationId, // [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser1, // [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser orgUser2, @@ -381,9 +484,7 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // orgUser1.UserId = user1.Id; // orgUser2.UserId = null; // orgUser3.UserId = user3.Id; - // orgUser1.OrganizationId = organizationId; - // orgUser2.OrganizationId = organizationId; - // orgUser3.OrganizationId = organizationId; + // orgUser1.OrganizationId = orgUser2.OrganizationId = orgUser3.OrganizationId = organizationId; // sutProvider.GetDependency() // .GetManyAsync(Arg.Any>()) @@ -393,8 +494,8 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // .GetManyAsync(Arg.Is>(ids => ids.Contains(user1.Id) && ids.Contains(user3.Id))) // .Returns(new[] { user1, user3 }); - // sutProvider.GetDependency() - // .GetUsersOrganizationManagementStatusAsync(organizationId, Arg.Any>()) + // sutProvider.GetDependency() + // .GetUsersOrganizationClaimedStatusAsync(organizationId, Arg.Any>()) // .Returns(new Dictionary { { orgUser1.Id, true }, { orgUser3.Id, false } }); // // Act @@ -402,528 +503,12 @@ public class DeleteClaimedOrganizationUserAccountCommandTests // // Assert // Assert.Equal(3, results.Count()); - - // var orgUser1ErrorMessage = results.First(r => r.Item1 == orgUser1.Id).Item2; - // Assert.Null(orgUser1ErrorMessage); - - // var orgUser2CommandResult = results.First(r => r.Item1 == orgUser2.Id).Item2; - // AssertErrorMessages("Member not found.", orgUser2CommandResult); - - // var orgUser3CommandResult = results.First(r => r.Item1 == orgUser3.Id).Item2; - // AssertErrorMessages("Member is not managed by the organization.", orgUser3CommandResult); + // Assert.Empty(results.First(r => r.Item1 == orgUser1.Id).Item2); + // Assert.Equal("You cannot delete a member with Invited status.", results.First(r => r.Item1 == orgUser2.Id).Item2); + // Assert.Equal("Member is not claimed by the organization.", results.First(r => r.Item1 == orgUser3.Id).Item2); // await sutProvider.GetDependency().Received(1).LogOrganizationUserEventsAsync( // Arg.Is>(events => // events.Count(e => e.Item1.Id == orgUser1.Id && e.Item2 == EventType.OrganizationUser_Deleted) == 1)); // } - - // private static void AssertErrorMessages(string expectedErrorMessage, CommandResult commandResult) => Assert.Contains([expectedErrorMessage], ((Failure)commandResult).ErrorMessages.ToArray()); -======= - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_WithValidUser_DeletesUserAndLogsEvent( - SutProvider sutProvider, User user, Guid deletingUserId, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser) - { - // Arrange - organizationUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetByIdAsync(user.Id) - .Returns(user); - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency() - .GetUsersOrganizationClaimedStatusAsync( - organizationUser.OrganizationId, - Arg.Is>(ids => ids.Contains(organizationUser.Id))) - .Returns(new Dictionary { { organizationUser.Id, true } }); - - sutProvider.GetDependency() - .HasConfirmedOwnersExceptAsync( - organizationUser.OrganizationId, - Arg.Is>(ids => ids.Contains(organizationUser.Id)), - includeProvider: Arg.Any()) - .Returns(true); - - // Act - await sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId); - - // Assert - await sutProvider.GetDependency().Received(1).DeleteAsync(user); - await sutProvider.GetDependency().Received(1) - .LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Deleted); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_WithUserNotFound_ThrowsException( - SutProvider sutProvider, - Guid organizationId, Guid organizationUserId) - { - // Arrange - sutProvider.GetDependency() - .GetByIdAsync(organizationUserId) - .Returns((OrganizationUser?)null); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationId, organizationUserId, null)); - - // Assert - Assert.Equal("Member not found.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_DeletingYourself_ThrowsException( - SutProvider sutProvider, - User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser, - Guid deletingUserId) - { - // Arrange - organizationUser.UserId = user.Id = deletingUserId; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency().GetByIdAsync(user.Id) - .Returns(user); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); - - // Assert - Assert.Equal("You cannot delete yourself.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_WhenUserIsInvited_ThrowsException( - SutProvider sutProvider, - [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser organizationUser) - { - // Arrange - organizationUser.UserId = null; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, null)); - - // Assert - Assert.Equal("You cannot delete a member with Invited status.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_WhenCustomUserDeletesAdmin_ThrowsException( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Admin)] OrganizationUser organizationUser, - Guid deletingUserId) - { - // Arrange - organizationUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency().GetByIdAsync(user.Id) - .Returns(user); - - sutProvider.GetDependency() - .OrganizationCustom(organizationUser.OrganizationId) - .Returns(true); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); - - // Assert - Assert.Equal("Custom users can not delete admins.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_DeletingOwnerWhenNotOwner_ThrowsException( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser organizationUser, - Guid deletingUserId) - { - // Arrange - organizationUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency().GetByIdAsync(user.Id) - .Returns(user); - - sutProvider.GetDependency() - .OrganizationOwner(organizationUser.OrganizationId) - .Returns(false); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); - - // Assert - Assert.Equal("Only owners can delete other owners.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_DeletingLastConfirmedOwner_ThrowsException( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser organizationUser, - Guid deletingUserId) - { - // Arrange - organizationUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency().GetByIdAsync(user.Id) - .Returns(user); - - sutProvider.GetDependency() - .OrganizationOwner(organizationUser.OrganizationId) - .Returns(true); - - sutProvider.GetDependency() - .HasConfirmedOwnersExceptAsync( - organizationUser.OrganizationId, - Arg.Is>(ids => ids.Contains(organizationUser.Id)), - includeProvider: Arg.Any()) - .Returns(false); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, deletingUserId)); - - // Assert - Assert.Equal("Organization must have at least one confirmed owner.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteUserAsync_WithUserNotManaged_ThrowsException( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser organizationUser) - { - // Arrange - organizationUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetByIdAsync(organizationUser.Id) - .Returns(organizationUser); - - sutProvider.GetDependency().GetByIdAsync(user.Id) - .Returns(user); - - sutProvider.GetDependency() - .GetUsersOrganizationClaimedStatusAsync(organizationUser.OrganizationId, Arg.Any>()) - .Returns(new Dictionary { { organizationUser.Id, false } }); - - // Act - var exception = await Assert.ThrowsAsync(() => - sutProvider.Sut.DeleteUserAsync(organizationUser.OrganizationId, organizationUser.Id, null)); - - // Assert - Assert.Equal("Member is not claimed by the organization.", exception.Message); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventAsync(Arg.Any(), Arg.Any(), Arg.Any()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WithValidUsers_DeletesUsersAndLogsEvents( - SutProvider sutProvider, User user1, User user2, Guid organizationId, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser1, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser2) - { - // Arrange - orgUser1.OrganizationId = orgUser2.OrganizationId = organizationId; - orgUser1.UserId = user1.Id; - orgUser2.UserId = user2.Id; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser1, orgUser2 }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(ids => ids.Contains(user1.Id) && ids.Contains(user2.Id))) - .Returns(new[] { user1, user2 }); - - sutProvider.GetDependency() - .GetUsersOrganizationClaimedStatusAsync(organizationId, Arg.Any>()) - .Returns(new Dictionary { { orgUser1.Id, true }, { orgUser2.Id, true } }); - - // Act - var userIds = new[] { orgUser1.Id, orgUser2.Id }; - var results = await sutProvider.Sut.DeleteManyUsersAsync(organizationId, userIds, null); - - // Assert - Assert.Equal(2, results.Count()); - Assert.All(results, r => Assert.Empty(r.Item2)); - - await sutProvider.GetDependency().Received(1).GetManyAsync(userIds); - await sutProvider.GetDependency().Received(1).DeleteManyAsync(Arg.Is>(users => users.Any(u => u.Id == user1.Id) && users.Any(u => u.Id == user2.Id))); - await sutProvider.GetDependency().Received(1).LogOrganizationUserEventsAsync( - Arg.Is>(events => - events.Count(e => e.Item1.Id == orgUser1.Id && e.Item2 == EventType.OrganizationUser_Deleted) == 1 - && events.Count(e => e.Item1.Id == orgUser2.Id && e.Item2 == EventType.OrganizationUser_Deleted) == 1)); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenUserNotFound_ReturnsErrorMessage( - SutProvider sutProvider, - Guid organizationId, - Guid orgUserId) - { - // Act - var result = await sutProvider.Sut.DeleteManyUsersAsync(organizationId, new[] { orgUserId }, null); - - // Assert - Assert.Single(result); - Assert.Equal(orgUserId, result.First().Item1); - Assert.Contains("Member not found.", result.First().Item2); - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .DeleteManyAsync(default); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenDeletingYourself_ReturnsErrorMessage( - SutProvider sutProvider, - User user, [OrganizationUser] OrganizationUser orgUser, Guid deletingUserId) - { - // Arrange - orgUser.UserId = user.Id = deletingUserId; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(ids => ids.Contains(user.Id))) - .Returns(new[] { user }); - - // Act - var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, deletingUserId); - - // Assert - Assert.Single(result); - Assert.Equal(orgUser.Id, result.First().Item1); - Assert.Contains("You cannot delete yourself.", result.First().Item2); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenUserIsInvited_ReturnsErrorMessage( - SutProvider sutProvider, - [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser orgUser) - { - // Arrange - orgUser.UserId = null; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser }); - - // Act - var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, null); - - // Assert - Assert.Single(result); - Assert.Equal(orgUser.Id, result.First().Item1); - Assert.Contains("You cannot delete a member with Invited status.", result.First().Item2); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenDeletingOwnerAsNonOwner_ReturnsErrorMessage( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser, - Guid deletingUserId) - { - // Arrange - orgUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(i => i.Contains(user.Id))) - .Returns(new[] { user }); - - sutProvider.GetDependency() - .OrganizationOwner(orgUser.OrganizationId) - .Returns(false); - - var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, deletingUserId); - - Assert.Single(result); - Assert.Equal(orgUser.Id, result.First().Item1); - Assert.Contains("Only owners can delete other owners.", result.First().Item2); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenDeletingLastOwner_ReturnsErrorMessage( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser, - Guid deletingUserId) - { - // Arrange - orgUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(i => i.Contains(user.Id))) - .Returns(new[] { user }); - - sutProvider.GetDependency() - .OrganizationOwner(orgUser.OrganizationId) - .Returns(true); - - sutProvider.GetDependency() - .HasConfirmedOwnersExceptAsync(orgUser.OrganizationId, Arg.Any>(), Arg.Any()) - .Returns(false); - - // Act - var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, deletingUserId); - - // Assert - Assert.Single(result); - Assert.Equal(orgUser.Id, result.First().Item1); - Assert.Contains("Organization must have at least one confirmed owner.", result.First().Item2); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_WhenUserNotManaged_ReturnsErrorMessage( - SutProvider sutProvider, User user, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser) - { - // Arrange - orgUser.UserId = user.Id; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(ids => ids.Contains(orgUser.UserId.Value))) - .Returns(new[] { user }); - - sutProvider.GetDependency() - .GetUsersOrganizationClaimedStatusAsync(Arg.Any(), Arg.Any>()) - .Returns(new Dictionary { { orgUser.Id, false } }); - - // Act - var result = await sutProvider.Sut.DeleteManyUsersAsync(orgUser.OrganizationId, new[] { orgUser.Id }, null); - - // Assert - Assert.Single(result); - Assert.Equal(orgUser.Id, result.First().Item1); - Assert.Contains("Member is not claimed by the organization.", result.First().Item2); - await sutProvider.GetDependency().Received(0).DeleteAsync(Arg.Any()); - await sutProvider.GetDependency().Received(0) - .LogOrganizationUserEventsAsync(Arg.Any>()); - } - - [Theory] - [BitAutoData] - public async Task DeleteManyUsersAsync_MixedValidAndInvalidUsers_ReturnsAppropriateResults( - SutProvider sutProvider, User user1, User user3, - Guid organizationId, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser1, - [OrganizationUser(OrganizationUserStatusType.Invited, OrganizationUserType.User)] OrganizationUser orgUser2, - [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.User)] OrganizationUser orgUser3) - { - // Arrange - orgUser1.UserId = user1.Id; - orgUser2.UserId = null; - orgUser3.UserId = user3.Id; - orgUser1.OrganizationId = orgUser2.OrganizationId = orgUser3.OrganizationId = organizationId; - - sutProvider.GetDependency() - .GetManyAsync(Arg.Any>()) - .Returns(new List { orgUser1, orgUser2, orgUser3 }); - - sutProvider.GetDependency() - .GetManyAsync(Arg.Is>(ids => ids.Contains(user1.Id) && ids.Contains(user3.Id))) - .Returns(new[] { user1, user3 }); - - sutProvider.GetDependency() - .GetUsersOrganizationClaimedStatusAsync(organizationId, Arg.Any>()) - .Returns(new Dictionary { { orgUser1.Id, true }, { orgUser3.Id, false } }); - - // Act - var results = await sutProvider.Sut.DeleteManyUsersAsync(organizationId, new[] { orgUser1.Id, orgUser2.Id, orgUser3.Id }, null); - - // Assert - Assert.Equal(3, results.Count()); - Assert.Empty(results.First(r => r.Item1 == orgUser1.Id).Item2); - Assert.Equal("You cannot delete a member with Invited status.", results.First(r => r.Item1 == orgUser2.Id).Item2); - Assert.Equal("Member is not claimed by the organization.", results.First(r => r.Item1 == orgUser3.Id).Item2); - - await sutProvider.GetDependency().Received(1).LogOrganizationUserEventsAsync( - Arg.Is>(events => - events.Count(e => e.Item1.Id == orgUser1.Id && e.Item2 == EventType.OrganizationUser_Deleted) == 1)); - } ->>>>>>> main:test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/DeleteClaimedOrganizationUserAccountCommandTests.cs }