using Bit.Core.Entities; using Bit.Core.Repositories; using Bit.Core.SecretsManager.Commands.EnableAccessSecretsManager; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Bit.Test.Common.Helpers; using NSubstitute; using Xunit; namespace Bit.Core.Test.SecretsManager.Commands.EnableAccessSecretsManager; [SutProviderCustomize] public class EnableAccessSecretsManagerCommandTests { [Theory] [BitAutoData] public async Task EnableUsers_UsersAlreadyEnabled_DoesNotCallRepository( SutProvider sutProvider, ICollection data) { foreach (var item in data) { item.AccessSecretsManager = true; } var result = await sutProvider.Sut.EnableUsersAsync(data); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .ReplaceManyAsync(default); Assert.Equal(data.Count, result.Count); Assert.Equal(data.Count, result.Where(x => x.error == "User already has access to Secrets Manager").ToList().Count); } [Theory] [BitAutoData] public async Task EnableUsers_OneUserNotEnabled_CallsRepositoryForOne( SutProvider sutProvider, ICollection data) { var firstUser = new List(); foreach (var item in data) { if (item == data.First()) { item.AccessSecretsManager = false; firstUser.Add(item); } else { item.AccessSecretsManager = true; } } var result = await sutProvider.Sut.EnableUsersAsync(data); await sutProvider.GetDependency().Received(1) .ReplaceManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(firstUser))); Assert.Equal(data.Count, result.Count); Assert.Equal(data.Count - 1, result.Where(x => x.error == "User already has access to Secrets Manager").ToList().Count); } [Theory] [BitAutoData] public async Task EnableUsers_Success( SutProvider sutProvider, ICollection data) { foreach (var item in data) { item.AccessSecretsManager = false; } var result = await sutProvider.Sut.EnableUsersAsync(data); await sutProvider.GetDependency().Received(1) .ReplaceManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(data))); Assert.Equal(data.Count, result.Count); } }