From 10faa9cde4f5b16e3db4d2a87e628aa2c6ccb708 Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Fri, 16 Jan 2026 11:47:04 +0000 Subject: [PATCH] Refactor CiphersControllerTests to use GetOrganizationDetailsByIdAsync for repository calls Updated tests in CiphersControllerTests to replace GetByIdAsync with GetOrganizationDetailsByIdAsync for fetching cipher details. Adjusted assertions in delete and restore admin methods to ensure correct organization context is maintained. This change enhances clarity and consistency in the test suite. --- .../Controllers/CiphersControllerTests.cs | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/test/Api.Test/Vault/Controllers/CiphersControllerTests.cs b/test/Api.Test/Vault/Controllers/CiphersControllerTests.cs index 238053464c..e4d159e896 100644 --- a/test/Api.Test/Vault/Controllers/CiphersControllerTests.cs +++ b/test/Api.Test/Vault/Controllers/CiphersControllerTests.cs @@ -171,7 +171,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency().GetManyByOrganizationIdAsync(organization.Id).Returns(new List { cipherDetails }); @@ -212,7 +212,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency() .GetManyByUserIdAsync(userId) .Returns(new List @@ -229,7 +229,8 @@ public class CiphersControllerTests await sutProvider.Sut.DeleteAdmin(cipherDetails.Id); - await sutProvider.GetDependency().Received(1).DeleteAsync(cipherDetails, userId, true); + await sutProvider.GetDependency().Received(1) + .DeleteAsync(Arg.Is(c => c.Id == cipherDetails.Id && c.OrganizationId == cipherDetails.OrganizationId), userId, true); } [Theory] @@ -249,7 +250,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency() .GetManyByUserIdAsync(userId) .Returns(new List @@ -282,7 +283,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency() .GetManyUnassignedOrganizationDetailsByOrganizationIdAsync(organization.Id) .Returns(new List @@ -299,7 +300,8 @@ public class CiphersControllerTests await sutProvider.Sut.DeleteAdmin(cipherDetails.Id); - await sutProvider.GetDependency().Received(1).DeleteAsync(cipherDetails, userId, true); + await sutProvider.GetDependency().Received(1) + .DeleteAsync(Arg.Is(c => c.Id == cipherDetails.Id && c.OrganizationId == cipherDetails.OrganizationId), userId, true); } [Theory] @@ -314,7 +316,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency().GetManyByOrganizationIdAsync(organization.Id).Returns(new List { cipherDetails }); sutProvider.GetDependency().GetOrganizationAbilityAsync(organization.Id).Returns(new OrganizationAbility { @@ -324,7 +326,8 @@ public class CiphersControllerTests await sutProvider.Sut.DeleteAdmin(cipherDetails.Id); - await sutProvider.GetDependency().Received(1).DeleteAsync(cipherDetails, userId, true); + await sutProvider.GetDependency().Received(1) + .DeleteAsync(Arg.Is(c => c.Id == cipherDetails.Id && c.OrganizationId == cipherDetails.OrganizationId), userId, true); } [Theory] @@ -339,49 +342,46 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipherDetails.Id, userId).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); sutProvider.GetDependency().GetManyByOrganizationIdAsync(organization.Id).Returns(new List { cipherDetails }); await sutProvider.Sut.DeleteAdmin(cipherDetails.Id); - await sutProvider.GetDependency().Received(1).DeleteAsync(cipherDetails, userId, true); + await sutProvider.GetDependency().Received(1) + .DeleteAsync(Arg.Is(c => c.Id == cipherDetails.Id && c.OrganizationId == cipherDetails.OrganizationId), userId, true); } [Theory] [BitAutoData] public async Task DeleteAdmin_WithCustomUser_WithEditAnyCollectionFalse_ThrowsNotFoundException( - Cipher cipher, Guid userId, + CipherDetails cipherDetails, Guid userId, CurrentContextOrganization organization, SutProvider sutProvider) { - cipher.OrganizationId = organization.Id; + cipherDetails.OrganizationId = organization.Id; organization.Type = OrganizationUserType.Custom; organization.Permissions.EditAnyCollection = false; sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByIdAsync(cipher.Id).Returns(cipher); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); - await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteAdmin(cipher.Id)); + await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteAdmin(cipherDetails.Id)); } [Theory] [BitAutoData] public async Task DeleteAdmin_WithProviderUser_ThrowsNotFoundException( - Cipher cipher, Guid userId, SutProvider sutProvider) + CipherDetails cipherDetails, Guid userId, SutProvider sutProvider) { - cipher.OrganizationId = Guid.NewGuid(); + cipherDetails.OrganizationId = Guid.NewGuid(); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); - sutProvider.GetDependency().ProviderUserForOrgAsync(cipher.OrganizationId.Value).Returns(true); - sutProvider.GetDependency().GetByIdAsync(cipher.Id).Returns(cipher); + sutProvider.GetDependency().ProviderUserForOrgAsync(cipherDetails.OrganizationId.Value).Returns(true); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); - await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteAdmin(cipher.Id)); + await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteAdmin(cipherDetails.Id)); } - - - - [Theory] [BitAutoData(OrganizationUserType.Owner)] [BitAutoData(OrganizationUserType.Admin)] @@ -606,7 +606,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherOrgDetails.Id).Returns(cipherOrgDetails); sutProvider.GetDependency() .GetManyByUserIdAsync(userId) .Returns(new List @@ -621,7 +621,7 @@ public class CiphersControllerTests LimitItemDeletion = true }); - await sutProvider.Sut.PutDeleteAdmin(cipherDetails.Id); + await sutProvider.Sut.PutDeleteAdmin(cipherOrgDetails.Id); await sutProvider.GetDependency().Received(1).SoftDeleteAsync( Arg.Is(c => c.OrganizationId.Equals(cipherOrgDetails.OrganizationId)), userId, true); @@ -631,11 +631,13 @@ public class CiphersControllerTests [BitAutoData(OrganizationUserType.Owner)] [BitAutoData(OrganizationUserType.Admin)] public async Task PutDeleteAdmin_WithOwnerOrAdmin_WithoutManagePermission_ThrowsNotFoundException( - OrganizationUserType organizationUserType, CipherDetails cipherDetails, Guid userId, + OrganizationUserType organizationUserType, CipherOrganizationDetails cipherOrgDetails, Guid userId, CurrentContextOrganization organization, SutProvider sutProvider) { - cipherDetails.UserId = null; - cipherDetails.OrganizationId = organization.Id; + cipherOrgDetails.UserId = null; + cipherOrgDetails.OrganizationId = organization.Id; + + var cipherDetails = new CipherDetails(cipherOrgDetails); cipherDetails.Edit = true; cipherDetails.Manage = false; @@ -644,6 +646,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherOrgDetails.Id).Returns(cipherOrgDetails); sutProvider.GetDependency() .GetManyByUserIdAsync(userId) .Returns(new List @@ -658,7 +661,7 @@ public class CiphersControllerTests LimitItemDeletion = true }); - await Assert.ThrowsAsync(() => sutProvider.Sut.PutDeleteAdmin(cipherDetails.Id)); + await Assert.ThrowsAsync(() => sutProvider.Sut.PutDeleteAdmin(cipherOrgDetails.Id)); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() @@ -764,7 +767,7 @@ public class CiphersControllerTests sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(userId); sutProvider.GetDependency().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(new User { Id = userId }); sutProvider.GetDependency().GetOrganization(organization.Id).Returns(organization); - sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherDetails.Id).Returns(cipherDetails); + sutProvider.GetDependency().GetOrganizationDetailsByIdAsync(cipherOrgDetails.Id).Returns(cipherOrgDetails); sutProvider.GetDependency() .GetManyByUserIdAsync(userId) .Returns(new List { cipherDetails }); @@ -1139,7 +1142,7 @@ public class CiphersControllerTests LimitItemDeletion = true }); - await Assert.ThrowsAsync(() => sutProvider.Sut.PutRestoreAdmin(cipherDetails.Id)); + await Assert.ThrowsAsync(() => sutProvider.Sut.PutRestoreAdmin(cipherOrgDetails.Id)); } [Theory]