Skip to content

Commit

Permalink
fix(serviceAccount): fix naming of service account (#351)
Browse files Browse the repository at this point in the history
Refs: CPLP-2574
Reviewed-by: Norbert Truchsess <[email protected]>
  • Loading branch information
Phil91 authored Apr 27, 2023
1 parent f85b253 commit 8190305
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public ServiceAccountCreation(
}

/// <inheritdoc />
public async Task<(string clientId, ServiceAccountData serviceAccountData, Guid serviceAccountId, List<UserRoleData> userRoleData)> CreateServiceAccountAsync(
async Task<(string clientId, ServiceAccountData serviceAccountData, Guid serviceAccountId, List<UserRoleData> userRoleData)> IServiceAccountCreation.CreateServiceAccountAsync(
ServiceAccountCreationInfo creationData,
Guid companyId,
IEnumerable<string> bpns,
CompanyServiceAccountTypeId companyServiceAccountTypeId,
bool enhanceTechnicalUserName,
Action<CompanyServiceAccount>? setOptionalParameter = null)
Action<CompanyServiceAccount>? setOptionalParameter)
{
var (name, description, iamClientAuthMethod, userRoleIds) = creationData;
var serviceAccountsRepository = _portalRepositories.GetInstance<IServiceAccountRepository>();
Expand All @@ -83,8 +83,8 @@ public ServiceAccountCreation(
}
}

var (clientId, id) = await GetNextServiceAccountClientIdWithIdAsync().ConfigureAwait(false);
var enhancedName = enhanceTechnicalUserName ? $"{name}{id}" : name;
var clientId = await GetNextServiceAccountClientIdWithIdAsync().ConfigureAwait(false);
var enhancedName = enhanceTechnicalUserName ? $"{clientId}-{name}" : name;
var serviceAccountData = await _provisioningManager.SetupCentralServiceAccountClientAsync(
clientId,
new ClientConfigRolesData(
Expand Down Expand Up @@ -126,9 +126,9 @@ public ServiceAccountCreation(
return (clientId, serviceAccountData, serviceAccount.Id, userRoleData);
}

private async Task<(string clientId, string id)> GetNextServiceAccountClientIdWithIdAsync()
private async Task<string> GetNextServiceAccountClientIdWithIdAsync()
{
var id = await _provisioningDbAccess.GetNextClientSequenceAsync().ConfigureAwait(false);
return ($"{_settings.ServiceAccountClientPrefix}{id}", id.ToString());
return $"{_settings.ServiceAccountClientPrefix}{id}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ServiceAccountCreationTests
private readonly IProvisioningManager _provisioningManager;
private readonly IPortalRepositories _portalRepositories;
private readonly IProvisioningDBAccess _provisioningDbAccess;
private readonly ServiceAccountCreation _sut;
private readonly IServiceAccountCreation _sut;

public ServiceAccountCreationTests()
{
Expand Down Expand Up @@ -124,13 +124,14 @@ public async Task CreateServiceAccountAsync_WithValidData_ReturnsExpected()
public async Task CreateServiceAccountAsync_WithNameSetAndValidData_ReturnsExpected()
{
// Arrange
var serviceAccounts = new List<CompanyServiceAccount>();
var creationData = new ServiceAccountCreationInfo("testName", "abc", IamClientAuthMethod.SECRET, new []{ _validUserRoleId });
var bpns = new[]
{
Bpn
};
Setup();
Setup(serviceAccounts);

// Act
var result = await _sut.CreateServiceAccountAsync(creationData, _companyId, bpns, CompanyServiceAccountTypeId.OWN, true).ConfigureAwait(false);

Expand All @@ -139,25 +140,40 @@ public async Task CreateServiceAccountAsync_WithNameSetAndValidData_ReturnsExpec
result.serviceAccountData.InternalClientId.Should().Be("internal-sa1");
result.serviceAccountData.UserEntityId.Should().Be(_iamUserId);
result.serviceAccountData.AuthData.IamClientAuthMethod.Should().Be(IamClientAuthMethod.SECRET);
A.CallTo(() => _provisioningManager.SetupCentralServiceAccountClientAsync(A<string>._, A<ClientConfigRolesData>.That.Matches(x => x.Name == "sa1-testName"))).MustHaveHappenedOnceExactly();
A.CallTo(() => _provisioningManager.AddBpnAttributetoUserAsync(_iamUserId, bpns)).MustHaveHappenedOnceExactly();
A.CallTo(() => _provisioningManager.AddProtocolMapperAsync("internal-sa1")).MustHaveHappenedOnceExactly();
A.CallTo(() => _serviceAccountRepository.CreateCompanyServiceAccountAssignedRole(_serviceAccountId, A<Guid>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => _serviceAccountRepository.CreateIamServiceAccount("internal-sa1", A<string>._, _iamUserId, _serviceAccountId)).MustHaveHappenedOnceExactly();
A.CallTo(() => _portalRepositories.SaveAsync()).MustNotHaveHappened();
serviceAccounts.Should().ContainSingle().Which.Name.Should().Be("sa1-testName");
}

#region Setup

private void Setup()
private void Setup(ICollection<CompanyServiceAccount>? serviceAccounts = null)
{
A.CallTo(() => _provisioningDbAccess.GetNextClientSequenceAsync())
.ReturnsLazily(() => 1);

A.CallTo(() => _provisioningManager.SetupCentralServiceAccountClientAsync(A<string>._, A<ClientConfigRolesData>._))
.ReturnsLazily(() => new ServiceAccountData("internal-sa1", _iamUserId, new ClientAuthData(IamClientAuthMethod.SECRET)));

A.CallTo(() => _serviceAccountRepository.CreateCompanyServiceAccount(_companyId, CompanyServiceAccountStatusId.ACTIVE, A<string>._, A<string>._, A<CompanyServiceAccountTypeId>._, null))
.ReturnsLazily(() => new CompanyServiceAccount(_serviceAccountId, default, CompanyServiceAccountStatusId.ACTIVE, null!, null!, default, default));
A.CallTo(() => _serviceAccountRepository.CreateCompanyServiceAccount(_companyId, CompanyServiceAccountStatusId.ACTIVE, A<string>._, A<string>._, A<CompanyServiceAccountTypeId>._, A<Action<CompanyServiceAccount>>._))
.Invokes((Guid companyId, CompanyServiceAccountStatusId companyServiceAccountStatusId, string name, string description, CompanyServiceAccountTypeId companyServiceAccountTypeId, Action<CompanyServiceAccount>? setOptionalParameters) =>
{
var sa = new CompanyServiceAccount(
Guid.NewGuid(),
companyId,
companyServiceAccountStatusId,
name,
description,
DateTimeOffset.UtcNow,
companyServiceAccountTypeId);
setOptionalParameters?.Invoke(sa);
serviceAccounts?.Add(sa);
})
.ReturnsLazily(() => new CompanyServiceAccount(_serviceAccountId, Guid.Empty, CompanyServiceAccountStatusId.ACTIVE, null!, null!, default, default));

A.CallTo(() => _userRolesRepository.GetUserRoleDataUntrackedAsync(A<IEnumerable<Guid>>.That.Matches(x => x.Count(y => y == _validUserRoleId) == 1)))
.ReturnsLazily(() => new[] {new UserRoleData(_validUserRoleId, Guid.NewGuid().ToString(), "UserRole")}.ToAsyncEnumerable());
Expand Down

0 comments on commit 8190305

Please sign in to comment.