Skip to content

Commit

Permalink
fix(tenantName): remove invalid characters from tenant name
Browse files Browse the repository at this point in the history
Refs: #86 #87
  • Loading branch information
Phil91 committed Aug 20, 2024
1 parent c23dfe1 commit da493c6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Dim.Clients.Api.SubAccounts;

public interface ISubAccountClient
{
Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, string adminMail, string tenantName, Guid directoryId, CancellationToken cancellationToken);
Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, string adminMail, string tenantName, Guid directoryId, string bpn, CancellationToken cancellationToken);
Task CreateServiceManagerBindings(BasicAuthSettings basicAuthSettings, Guid subAccountId, CancellationToken cancellationToken);
Task<ServiceManagementBindingItem> GetServiceManagerBindings(BasicAuthSettings basicAuthSettings, Guid subAccountId, CancellationToken cancellationToken);
}
21 changes: 6 additions & 15 deletions src/clients/Dim.Clients/Api/SubAccounts/SubAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@

namespace Dim.Clients.Api.SubAccounts;

public class SubAccountClient : ISubAccountClient
public class SubAccountClient(IBasicAuthTokenService basicAuthTokenService) : ISubAccountClient
{
private static readonly Regex TenantName = new(@"(?<=[^\w-])|(?<=[^-])[\W_]+|(?<=[^-])$", RegexOptions.Compiled, new TimeSpan(0, 0, 0, 1));
private readonly IBasicAuthTokenService _basicAuthTokenService;

public SubAccountClient(IBasicAuthTokenService basicAuthTokenService)
{
_basicAuthTokenService = basicAuthTokenService;
}

public async Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, string adminMail, string tenantName, Guid directoryId, CancellationToken cancellationToken)
public async Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, string adminMail, string tenantName, Guid directoryId, string bpn, CancellationToken cancellationToken)
{
var subdomain = TenantName.Replace(tenantName, "-").ToLower().TrimStart('-').TrimEnd('-');
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);
var directory = new CreateSubAccountRequest(
false,
$"CX customer sub-account {tenantName}",
Expand All @@ -55,7 +46,7 @@ public async Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, st
directoryId,
"eu10",
Enumerable.Repeat(adminMail, 1),
subdomain,
bpn,
UsedForProduction.USED_FOR_PRODUCTION
);

Expand Down Expand Up @@ -86,7 +77,7 @@ public async Task<Guid> CreateSubaccount(BasicAuthSettings basicAuthSettings, st

public async Task CreateServiceManagerBindings(BasicAuthSettings basicAuthSettings, Guid subAccountId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);
var data = new
{
name = "accessServiceManager"
Expand All @@ -98,7 +89,7 @@ await client.PostAsJsonAsync($"/accounts/v2/subaccounts/{subAccountId}/serviceMa

public async Task<ServiceManagementBindingItem> GetServiceManagerBindings(BasicAuthSettings basicAuthSettings, Guid subAccountId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<SubAccountClient>(basicAuthSettings, cancellationToken).ConfigureAwait(false);

var result = await client.GetAsync($"/accounts/v2/subaccounts/{subAccountId}/serviceManagerBindings", cancellationToken)
.CatchingIntoServiceExceptionFor("create-subaccount", HttpAsyncResponseMessageExtension.RecoverOptions.ALLWAYS).ConfigureAwait(false);
Expand Down
3 changes: 3 additions & 0 deletions src/database/Dim.DbAccess/Repositories/ITenantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
********************************************************************************/

using Dim.Entities.Entities;
using System;
using System.Threading.Tasks;

namespace Dim.DbAccess.Repositories;

Expand Down Expand Up @@ -47,4 +49,5 @@ public interface ITenantRepository
Task<Guid> GetExternalIdForTechnicalUser(Guid technicalUserId);
void RemoveTechnicalUser(Guid technicalUserId);
Task<bool> IsTenantExisting(string companyName, string bpn);
Task<string> GetTenantBpn(Guid tenantId);
}
8 changes: 8 additions & 0 deletions src/database/Dim.DbAccess/Repositories/TenantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using Dim.Entities;
using Dim.Entities.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;

namespace Dim.DbAccess.Repositories;

Expand Down Expand Up @@ -165,4 +167,10 @@ public void RemoveTechnicalUser(Guid technicalUserId) =>
public Task<bool> IsTenantExisting(string companyName, string bpn) =>
context.Tenants
.AnyAsync(x => x.CompanyName == companyName && x.Bpn == bpn);

public Task<string> GetTenantBpn(Guid tenantId) =>
context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => x.Bpn)
.SingleAsync();
}
3 changes: 2 additions & 1 deletion src/processes/DimProcess.Library/DimProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class DimProcessHandler(
ClientSecret = _settings.ClientsecretCisCentral
};

var subAccountId = await subAccountClient.CreateSubaccount(subAccountAuth, adminMail, tenantName, parentDirectoryId, cancellationToken).ConfigureAwait(false);
var bpn = await dimRepositories.GetInstance<ITenantRepository>().GetTenantBpn(tenantId);
var subAccountId = await subAccountClient.CreateSubaccount(subAccountAuth, adminMail, tenantName, parentDirectoryId, bpn, cancellationToken).ConfigureAwait(false);
dimRepositories.GetInstance<ITenantRepository>().AttachAndModifyTenant(tenantId, tenant =>
{
tenant.SubAccountId = null;
Expand Down
5 changes: 4 additions & 1 deletion src/web/Dim.Web/BusinessLogic/DimBusinessLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Dim.Web.Models;
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using System.Text.RegularExpressions;

namespace Dim.Web.BusinessLogic;

Expand All @@ -38,10 +39,12 @@ public class DimBusinessLogic(
IOptions<DimSettings> options)
: IDimBusinessLogic
{
private static readonly Regex TenantName = new(@"(?<=[^\w-])|(?<=[^-])[\W_]+|(?<=[^-])$", RegexOptions.Compiled, new TimeSpan(0, 0, 0, 1));
private readonly DimSettings _settings = options.Value;

public async Task StartSetupDim(string companyName, string bpn, string didDocumentLocation, bool isIssuer)
{
var tenant = TenantName.Replace(companyName, string.Empty).TrimStart('-').TrimEnd('-').ToLower();
if (await dimRepositories.GetInstance<ITenantRepository>().IsTenantExisting(companyName, bpn).ConfigureAwait(ConfigureAwaitOptions.None))
{
throw new ConflictException($"Tenant {companyName} with Bpn {bpn} already exists");
Expand All @@ -51,7 +54,7 @@ public async Task StartSetupDim(string companyName, string bpn, string didDocume
var processId = processStepRepository.CreateProcess(ProcessTypeId.SETUP_DIM).Id;
processStepRepository.CreateProcessStep(ProcessStepTypeId.CREATE_SUBACCOUNT, ProcessStepStatusId.TODO, processId);

dimRepositories.GetInstance<ITenantRepository>().CreateTenant(companyName, bpn, didDocumentLocation, isIssuer, processId, _settings.OperatorId);
dimRepositories.GetInstance<ITenantRepository>().CreateTenant(tenant, bpn, didDocumentLocation, isIssuer, processId, _settings.OperatorId);

await dimRepositories.SaveAsync().ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task CreateSubaccount_WithValidData_ReturnsExpected()
initialize?.Invoke(tenant);
modify(tenant);
});
A.CallTo(() => _subAccountClient.CreateSubaccount(A<BasicAuthSettings>._, A<string>._, _tenantName, A<Guid>._, A<CancellationToken>._))
A.CallTo(() => _subAccountClient.CreateSubaccount(A<BasicAuthSettings>._, A<string>._, _tenantName, A<Guid>._, A<string>._, A<CancellationToken>._))
.Returns(subAccountId);

// Act
Expand Down
11 changes: 7 additions & 4 deletions tests/web/Dim.Web.Tests/DimBusinessLogicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ public async Task StartSetupDim_WithExisting_ThrowsConflictException()
result.Message.Should().Be($"Tenant testCompany with Bpn BPNL00000001TEST already exists");
}

[Fact]
public async Task StartSetupDim_WithNewData_CreatesExpected()
[Theory]
[InlineData("testCompany", "testcompany")]
[InlineData("-abc123", "abc123")]
[InlineData("äsoidjfül&23948", "äsoidjfül23948")]
public async Task StartSetupDim_WithNewData_CreatesExpected(string companyName, string expectedCompanyName)
{
// Arrange
var processId = Guid.NewGuid();
Expand Down Expand Up @@ -106,14 +109,14 @@ public async Task StartSetupDim_WithNewData_CreatesExpected()
});

// Act
await _sut.StartSetupDim("testCompany", "BPNL00000001TEST", "https://example.org/test", false);
await _sut.StartSetupDim(companyName, "BPNL00000001TEST", "https://example.org/test", false);

// Assert
processes.Should().ContainSingle()
.Which.ProcessTypeId.Should().Be(ProcessTypeId.SETUP_DIM);
processSteps.Should().ContainSingle()
.And.Satisfy(x => x.ProcessId == processId && x.ProcessStepTypeId == ProcessStepTypeId.CREATE_SUBACCOUNT);
tenants.Should().ContainSingle()
.And.Satisfy(x => x.CompanyName == "testCompany" && x.Bpn == "BPNL00000001TEST");
.And.Satisfy(x => x.CompanyName == expectedCompanyName && x.Bpn == "BPNL00000001TEST");
}
}

0 comments on commit da493c6

Please sign in to comment.