Skip to content

Commit

Permalink
fix(unittests): remove some redundancy from ServiceAccountBusinessLog…
Browse files Browse the repository at this point in the history
…ic unit-tests (#1023)

* chore(serviceaccounts): streamline and remove redundant unit-tests
* fix encoding of migration (set to utf8)
  • Loading branch information
ntruchsess authored Sep 20, 2024
1 parent 402bf6f commit 786a38e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 86 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
********************************************************************************/

using Microsoft.EntityFrameworkCore.Migrations;
using System;

#nullable disable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Encryption;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand Down Expand Up @@ -219,6 +218,11 @@ public async Task GetOwnCompanyServiceAccountDetailsAsync_WithValidInput_GetsAll
// Assert
result.Should().NotBeNull();
result.IamClientAuthMethod.Should().Be(IamClientAuthMethod.SECRET);
result.Secret.Should().Be("topsecret");
result.AuthenticationServiceUrl.Should().Be("https://auth.test/auth");
result.CompanyServiceAccountKindId.Should().Be(CompanyServiceAccountKindId.INTERNAL);
A.CallTo(() => _provisioningManager.GetIdOfCentralClientAsync(A<string>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => _provisioningManager.GetCentralClientAuthDataAsync(A<string>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -235,58 +239,12 @@ public async Task GetOwnCompanyServiceAccountDetailsAsync_WithValidInputAndDimCo
result.Should().NotBeNull();
result.Secret.Should().Be("test");
result.IamClientAuthMethod.Should().Be(IamClientAuthMethod.SECRET);
result.AuthenticationServiceUrl.Should().Be("https://example.org/auth");
result.CompanyServiceAccountKindId.Should().Be(CompanyServiceAccountKindId.EXTERNAL);
A.CallTo(() => _provisioningManager.GetIdOfCentralClientAsync(A<string>._)).MustNotHaveHappened();
A.CallTo(() => _provisioningManager.GetCentralClientAuthDataAsync(A<string>._)).MustNotHaveHappened();
}

[Fact]
public async Task GetOwnCompanyServiceAccountDetailsAsync_WithValidUserTypeInternal_AuthenticationUrl()
{
// Arrange
SetupGetOwnComapnyServiceAccountInternalType();
var sut = new ServiceAccountBusinessLogic(_provisioningManager, _portalRepositories, _options, null!, _identityService, _serviceAccountManagement);

// Act
var result = await sut.GetOwnCompanyServiceAccountDetailsAsync(ValidServiceAccountId);

// Assert
result.Should().NotBeNull();
result.CompanyServiceAccountKindId.Should().Be(CompanyServiceAccountKindId.INTERNAL);
result.AuthenticationServiceUrl.Should().Be("https://auth.test/auth");
}

[Fact]
public async Task GetOwnCompanyServiceAccountDetailsAsync_WithValidUserTypeExternal_AuthenticationUrl()
{
// Arrange
SetupGetOwnComapnyServiceAccountExternalType();
var sut = new ServiceAccountBusinessLogic(_provisioningManager, _portalRepositories, _options, null!, _identityService, _serviceAccountManagement);

// Act
var result = await sut.GetOwnCompanyServiceAccountDetailsAsync(ValidServiceAccountId);

// Assert
result.Should().NotBeNull();
result.CompanyServiceAccountKindId.Should().Be(CompanyServiceAccountKindId.EXTERNAL);
result.AuthenticationServiceUrl.Should().Be("https://test.org/auth");
}

[Fact]
public async Task GetOwnCompanyServiceAccountDetailsAsync_WithInValidUserTypeInternal_AuthenticationUrl()
{
// Arrange
SetupGetOwnCompanyServiceAccountDetails();
var sut = new ServiceAccountBusinessLogic(_provisioningManager, _portalRepositories, _options, null!, _identityService, _serviceAccountManagement);

// Act
var result = await sut.GetOwnCompanyServiceAccountDetailsAsync(ValidServiceAccountId);

// Assert
result.Should().NotBeNull();
result.CompanyServiceAccountKindId.Should().NotBe(CompanyServiceAccountKindId.INTERNAL);
result.AuthenticationServiceUrl.Should().NotBe("https://auth.test/auth");
}

[Fact]
public async Task GetOwnCompanyServiceAccountDetailsAsync_WithInvalidCompany_NotFoundException()
{
Expand Down Expand Up @@ -839,16 +797,23 @@ private void SetupUpdateOwnCompanyServiceAccountDetails()

private void SetupGetOwnCompanyServiceAccount()
{
var data = _fixture.Build<CompanyServiceAccountDetailedData>()
.With(x => x.Status, UserStatusId.ACTIVE)
.With(x => x.DimServiceAccountData, default(DimServiceAccountData?))
.With(x => x.CompanyServiceAccountKindId, CompanyServiceAccountKindId.INTERNAL)
.Create();

var cryptoHelper = _options.Value.EncryptionConfigs.GetCryptoHelper(_options.Value.EncryptionConfigIndex);
var (secret, initializationVector) = cryptoHelper.Encrypt("test");

var dimServiceAccountData = new DimServiceAccountData("https://example.org/auth", secret, initializationVector, _options.Value.EncryptionConfigIndex);
var dataWithDim = _fixture.Build<CompanyServiceAccountDetailedData>()
.With(x => x.DimServiceAccountData, dimServiceAccountData)
.With(x => x.CompanyServiceAccountKindId, CompanyServiceAccountKindId.EXTERNAL)
.Create();

A.CallTo(() => _serviceAccountRepository.GetOwnCompanyServiceAccountDetailedDataUntrackedAsync(ValidServiceAccountId, ValidCompanyId))
.Returns(dataWithDim);
.Returns(data);
A.CallTo(() => _serviceAccountRepository.GetOwnCompanyServiceAccountDetailedDataUntrackedAsync(ValidServiceAccountWithDimDataId, ValidCompanyId))
.Returns(dataWithDim);
A.CallTo(() => _serviceAccountRepository.GetOwnCompanyServiceAccountDetailedDataUntrackedAsync(
Expand All @@ -858,35 +823,6 @@ private void SetupGetOwnCompanyServiceAccount()
.Returns<CompanyServiceAccountDetailedData?>(null);
}

private void SetupGetOwnComapnyServiceAccountInternalType()
{
var data = _fixture.Build<CompanyServiceAccountDetailedData>()
.With(x => x.Status, UserStatusId.ACTIVE)
.With(x => x.CompanyServiceAccountKindId, CompanyServiceAccountKindId.INTERNAL)
.With(x => x.DimServiceAccountData, default(DimServiceAccountData?))
.Create();

A.CallTo(() => _serviceAccountRepository.GetOwnCompanyServiceAccountDetailedDataUntrackedAsync(ValidServiceAccountId, ValidCompanyId))
.Returns(data);
}

private void SetupGetOwnComapnyServiceAccountExternalType()
{
var cryptoHelper = _options.Value.EncryptionConfigs.GetCryptoHelper(_options.Value.EncryptionConfigIndex);
var (secret, initializationVector) = cryptoHelper.Encrypt("test");

var dimServiceAccountData = new DimServiceAccountData("https://test.org/auth", secret, initializationVector, _options.Value.EncryptionConfigIndex);

var externalData = _fixture.Build<CompanyServiceAccountDetailedData>()
.With(x => x.Status, UserStatusId.ACTIVE)
.With(x => x.CompanyServiceAccountKindId, CompanyServiceAccountKindId.EXTERNAL)
.With(x => x.DimServiceAccountData, dimServiceAccountData)
.Create();

A.CallTo(() => _serviceAccountRepository.GetOwnCompanyServiceAccountDetailedDataUntrackedAsync(ValidServiceAccountId, ValidCompanyId))
.Returns(externalData);
}

private void SetupDeleteOwnCompanyServiceAccount(Connector? connector = null, Identity? identity = null, Guid? processId = null)
{
var serviceAccount = new CompanyServiceAccount(Guid.NewGuid(), Guid.NewGuid(), "test-sa", "test", CompanyServiceAccountTypeId.OWN, CompanyServiceAccountKindId.INTERNAL);
Expand Down

0 comments on commit 786a38e

Please sign in to comment.