|
| 1 | +using Bit.Core.AdminConsole.Entities; |
| 2 | +using Bit.Core.Context; |
| 3 | +using Bit.Core.Exceptions; |
| 4 | +using Bit.Core.Repositories; |
| 5 | +using Bit.Core.Services; |
| 6 | +using Bit.Test.Common.AutoFixture; |
| 7 | +using Bit.Test.Common.AutoFixture.Attributes; |
| 8 | +using NSubstitute; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.Organizations; |
| 12 | + |
| 13 | +[SutProviderCustomize] |
| 14 | +public class OrganizationUpdateKeysCommandTests |
| 15 | +{ |
| 16 | + [Theory, BitAutoData] |
| 17 | + public async Task UpdateOrganizationKeysAsync_WithoutManageResetPasswordPermission_ThrowsUnauthorizedException( |
| 18 | + Guid orgId, string publicKey, string privateKey, SutProvider<OrganizationUpdateKeysCommand> sutProvider) |
| 19 | + { |
| 20 | + sutProvider.GetDependency<ICurrentContext>() |
| 21 | + .ManageResetPassword(orgId) |
| 22 | + .Returns(false); |
| 23 | + |
| 24 | + await Assert.ThrowsAsync<UnauthorizedAccessException>( |
| 25 | + () => sutProvider.Sut.UpdateOrganizationKeysAsync(orgId, publicKey, privateKey)); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory, BitAutoData] |
| 29 | + public async Task UpdateOrganizationKeysAsync_WhenKeysAlreadyExist_ThrowsBadRequestException( |
| 30 | + Organization organization, string publicKey, string privateKey, |
| 31 | + SutProvider<OrganizationUpdateKeysCommand> sutProvider) |
| 32 | + { |
| 33 | + organization.PublicKey = "existingPublicKey"; |
| 34 | + organization.PrivateKey = "existingPrivateKey"; |
| 35 | + |
| 36 | + sutProvider.GetDependency<ICurrentContext>() |
| 37 | + .ManageResetPassword(organization.Id) |
| 38 | + .Returns(true); |
| 39 | + |
| 40 | + sutProvider.GetDependency<IOrganizationRepository>() |
| 41 | + .GetByIdAsync(organization.Id) |
| 42 | + .Returns(organization); |
| 43 | + |
| 44 | + var exception = await Assert.ThrowsAsync<BadRequestException>( |
| 45 | + () => sutProvider.Sut.UpdateOrganizationKeysAsync(organization.Id, publicKey, privateKey)); |
| 46 | + |
| 47 | + Assert.Equal(OrganizationUpdateKeysCommand.OrganizationKeysAlreadyExistErrorMessage, exception.Message); |
| 48 | + } |
| 49 | + |
| 50 | + [Theory, BitAutoData] |
| 51 | + public async Task UpdateOrganizationKeysAsync_WhenKeysDoNotExist_UpdatesOrganization( |
| 52 | + Organization organization, string publicKey, string privateKey, |
| 53 | + SutProvider<OrganizationUpdateKeysCommand> sutProvider) |
| 54 | + { |
| 55 | + organization.PublicKey = null; |
| 56 | + organization.PrivateKey = null; |
| 57 | + |
| 58 | + sutProvider.GetDependency<ICurrentContext>() |
| 59 | + .ManageResetPassword(organization.Id) |
| 60 | + .Returns(true); |
| 61 | + |
| 62 | + sutProvider.GetDependency<IOrganizationRepository>() |
| 63 | + .GetByIdAsync(organization.Id) |
| 64 | + .Returns(organization); |
| 65 | + |
| 66 | + var result = await sutProvider.Sut.UpdateOrganizationKeysAsync(organization.Id, publicKey, privateKey); |
| 67 | + |
| 68 | + Assert.Equal(publicKey, result.PublicKey); |
| 69 | + Assert.Equal(privateKey, result.PrivateKey); |
| 70 | + |
| 71 | + await sutProvider.GetDependency<IOrganizationService>() |
| 72 | + .Received(1) |
| 73 | + .UpdateAsync(organization); |
| 74 | + } |
| 75 | +} |
0 commit comments