using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Exceptions; using Bit.Core.Models.Data; using Bit.Core.Repositories; using Bit.Core.Services; using Bit.Core.Test.AutoFixture.OrganizationFixtures; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using NSubstitute; using Xunit; namespace Bit.Core.Test.Services; [SutProviderCustomize] [OrganizationCustomize] public class CollectionServiceTest { [Theory, BitAutoData] public async Task SaveAsync_DefaultId_CreatesCollectionInTheRepository(Collection collection, Organization organization, SutProvider sutProvider) { collection.Id = default; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; await sutProvider.Sut.SaveAsync(collection); await sutProvider.GetDependency().Received().CreateAsync(collection); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); } [Theory, BitAutoData] public async Task SaveAsync_DefaultIdWithGroups_CreateCollectionWithGroupsInRepository(Collection collection, IEnumerable groups, Organization organization, SutProvider sutProvider) { collection.Id = default; organization.UseGroups = true; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; await sutProvider.Sut.SaveAsync(collection, groups); await sutProvider.GetDependency().Received().CreateAsync(collection, groups); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); } [Theory, BitAutoData] public async Task SaveAsync_NonDefaultId_ReplacesCollectionInRepository(Collection collection, Organization organization, SutProvider sutProvider) { var creationDate = collection.CreationDate; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; await sutProvider.Sut.SaveAsync(collection); await sutProvider.GetDependency().Received().ReplaceAsync(collection); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Updated); Assert.Equal(collection.CreationDate, creationDate); Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); } [Theory, BitAutoData] public async Task SaveAsync_OrganizationNotUseGroup_CreateCollectionWithoutGroupsInRepository(Collection collection, IEnumerable groups, Organization organization, SutProvider sutProvider) { collection.Id = default; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; await sutProvider.Sut.SaveAsync(collection, groups); await sutProvider.GetDependency().Received().CreateAsync(collection); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); } [Theory, BitAutoData] public async Task SaveAsync_DefaultIdWithUserId_UpdateUserInCollectionRepository(Collection collection, Organization organization, OrganizationUser organizationUser, SutProvider sutProvider) { collection.Id = default; organizationUser.Status = OrganizationUserStatusType.Confirmed; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); sutProvider.GetDependency().GetByOrganizationAsync(organization.Id, organizationUser.Id) .Returns(organizationUser); var utcNow = DateTime.UtcNow; await sutProvider.Sut.SaveAsync(collection, null, organizationUser.Id); await sutProvider.GetDependency().Received().CreateAsync(collection); await sutProvider.GetDependency().Received() .GetByOrganizationAsync(organization.Id, organizationUser.Id); await sutProvider.GetDependency().Received().UpdateUsersAsync(collection.Id, Arg.Any>()); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); } [Theory, BitAutoData] public async Task SaveAsync_NonExistingOrganizationId_ThrowsBadRequest(Collection collection, SutProvider sutProvider) { var ex = await Assert.ThrowsAsync(() => sutProvider.Sut.SaveAsync(collection)); Assert.Contains("Organization not found", ex.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default, default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().ReplaceAsync(default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().LogCollectionEventAsync(default, default); } [Theory, BitAutoData] public async Task SaveAsync_ExceedsOrganizationMaxCollections_ThrowsBadRequest(Collection collection, Organization organization, SutProvider sutProvider) { collection.Id = default; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); sutProvider.GetDependency().GetCountByOrganizationIdAsync(organization.Id) .Returns(organization.MaxCollections.Value); var ex = await Assert.ThrowsAsync(() => sutProvider.Sut.SaveAsync(collection)); Assert.Equal($@"You have reached the maximum number of collections ({organization.MaxCollections.Value}) for this organization.", ex.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default, default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().ReplaceAsync(default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().LogCollectionEventAsync(default, default); } [Theory, BitAutoData] public async Task DeleteUserAsync_DeletesValidUserWhoBelongsToCollection(Collection collection, Organization organization, OrganizationUser organizationUser, SutProvider sutProvider) { collection.OrganizationId = organization.Id; organizationUser.OrganizationId = organization.Id; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); sutProvider.GetDependency().GetByIdAsync(organizationUser.Id) .Returns(organizationUser); await sutProvider.Sut.DeleteUserAsync(collection, organizationUser.Id); await sutProvider.GetDependency().Received() .DeleteUserAsync(collection.Id, organizationUser.Id); await sutProvider.GetDependency().Received().LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Updated); } [Theory, BitAutoData] public async Task DeleteUserAsync_InvalidUser_ThrowsNotFound(Collection collection, Organization organization, OrganizationUser organizationUser, SutProvider sutProvider) { collection.OrganizationId = organization.Id; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); sutProvider.GetDependency().GetByIdAsync(organizationUser.Id) .Returns(organizationUser); // user not in organization await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteUserAsync(collection, organizationUser.Id)); // invalid user await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteUserAsync(collection, Guid.NewGuid())); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().DeleteUserAsync(default, default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .LogOrganizationUserEventAsync(default, default); } }