Browse Source
* Renamed and split up class to only query for an organization key * Added a command class to create an organization api key * Updated service registration and controller to include new changes * Updated test cases to reflect refactor * fixed lint issues * Fixed PR commentpull/2448/head
9 changed files with 97 additions and 55 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
using Bit.Core.Entities; |
||||
using Bit.Core.Enums; |
||||
using Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces; |
||||
using Bit.Core.Repositories; |
||||
using Bit.Core.Utilities; |
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys; |
||||
|
||||
public class CreateOrganizationApiKeyCommand : ICreateOrganizationApiKeyCommand |
||||
{ |
||||
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository; |
||||
|
||||
public CreateOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository) |
||||
{ |
||||
_organizationApiKeyRepository = organizationApiKeyRepository; |
||||
} |
||||
|
||||
public async Task<OrganizationApiKey> CreateAsync(Guid organizationId, |
||||
OrganizationApiKeyType organizationApiKeyType) |
||||
{ |
||||
var apiKey = new OrganizationApiKey |
||||
{ |
||||
OrganizationId = organizationId, |
||||
Type = organizationApiKeyType, |
||||
ApiKey = CoreHelpers.SecureRandomString(30), |
||||
RevisionDate = DateTime.UtcNow, |
||||
}; |
||||
|
||||
await _organizationApiKeyRepository.CreateAsync(apiKey); |
||||
return apiKey; |
||||
} |
||||
} |
||||
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
using Bit.Core.Entities; |
||||
using Bit.Core.Enums; |
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces; |
||||
|
||||
public interface ICreateOrganizationApiKeyCommand |
||||
{ |
||||
Task<OrganizationApiKey> CreateAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType); |
||||
} |
||||
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
using Bit.Core.Entities; |
||||
using Bit.Core.Enums; |
||||
using Bit.Core.OrganizationFeatures.OrganizationApiKeys; |
||||
using Bit.Core.Repositories; |
||||
using Bit.Test.Common.AutoFixture; |
||||
using Bit.Test.Common.AutoFixture.Attributes; |
||||
using NSubstitute; |
||||
using NSubstitute.ReceivedExtensions; |
||||
using Xunit; |
||||
|
||||
namespace Bit.Core.Test.OrganizationFeatures.OrganizationApiKeys; |
||||
|
||||
[SutProviderCustomize] |
||||
public class CreateOrganizationApiKeyCommandTest |
||||
{ |
||||
[Theory] |
||||
[BitAutoData] |
||||
public async Task CreateAsync_CreatesOrganizationApiKey(SutProvider<CreateOrganizationApiKeyCommand> sutProvider, |
||||
Guid organizationId, OrganizationApiKeyType keyType) |
||||
{ |
||||
await sutProvider.Sut.CreateAsync(organizationId, keyType); |
||||
|
||||
await sutProvider.GetDependency<IOrganizationApiKeyRepository>().Received(1) |
||||
.CreateAsync(Arg.Is<OrganizationApiKey>(o => o.OrganizationId == organizationId |
||||
&& o.Type == keyType)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue