From 3a51e9a85db554e6b0b1e1d3d1cdb0b6a19d9b14 Mon Sep 17 00:00:00 2001 From: Dhirender Singh Date: Fri, 10 Jan 2025 15:38:38 +0100 Subject: [PATCH] fixed the existing test and added new test --- .../BusinessLogic/OfferProviderBusinessLogic.cs | 8 ++++---- .../Models/TriggerProviderInformation.cs | 2 +- .../IOfferSubscriptionsRepository.cs | 2 +- .../OfferSubscriptionsRepository.cs | 11 ++++++----- .../CompanyRepositoryTests.cs | 13 +++++++------ .../OfferSubscriptionRepositoryTest.cs | 17 +++++++++++++++++ .../Data/provider_company_details.unittest.json | 4 ++++ 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/externalsystems/OfferProvider.Library/BusinessLogic/OfferProviderBusinessLogic.cs b/src/externalsystems/OfferProvider.Library/BusinessLogic/OfferProviderBusinessLogic.cs index 720c6a3662..84a8b14add 100644 --- a/src/externalsystems/OfferProvider.Library/BusinessLogic/OfferProviderBusinessLogic.cs +++ b/src/externalsystems/OfferProvider.Library/BusinessLogic/OfferProviderBusinessLogic.cs @@ -68,8 +68,8 @@ public OfferProviderBusinessLogic( { throw new ConflictException("Country should be set for the company"); } - var cryptoConfig = _settings.EncryptionConfigs.SingleOrDefault(x => x.Index == data.AuthDetails.EncryptionMode) ?? throw new ConfigurationException($"EncryptionModeIndex {data.AuthDetails.EncryptionMode} is not configured"); - var secret = CryptoHelper.Decrypt(data.AuthDetails.ClientSecret, data.AuthDetails.InitializationVector, Convert.FromHexString(cryptoConfig.EncryptionKey), cryptoConfig.CipherMode, cryptoConfig.PaddingMode); + var cryptoConfig = _settings.EncryptionConfigs.SingleOrDefault(x => x.Index == data.AuthDetails!.EncryptionMode) ?? throw new ConfigurationException($"EncryptionModeIndex {data.AuthDetails!.EncryptionMode} is not configured"); + var secret = CryptoHelper.Decrypt(data.AuthDetails!.ClientSecret, data.AuthDetails.InitializationVector, Convert.FromHexString(cryptoConfig.EncryptionKey), cryptoConfig.CipherMode, cryptoConfig.PaddingMode); var triggerProvider = !string.IsNullOrWhiteSpace(data.AutoSetupUrl) && !data.IsSingleInstance; if (triggerProvider) @@ -159,8 +159,8 @@ await _offerProviderService technicalUserInfoData, new CallbackClientInfoData(data.ClientId) ); - var cryptoConfig = _settings.EncryptionConfigs.SingleOrDefault(x => x.Index == data.AuthDetails.EncryptionMode) ?? throw new ConfigurationException($"EncryptionModeIndex {data.AuthDetails.EncryptionMode} is not configured"); - var secret = CryptoHelper.Decrypt(data.AuthDetails.ClientSecret, data.AuthDetails.InitializationVector, Convert.FromHexString(cryptoConfig.EncryptionKey), cryptoConfig.CipherMode, cryptoConfig.PaddingMode); + var cryptoConfig = _settings.EncryptionConfigs.SingleOrDefault(x => x.Index == data.AuthDetails!.EncryptionMode) ?? throw new ConfigurationException($"EncryptionModeIndex {data.AuthDetails!.EncryptionMode} is not configured"); + var secret = CryptoHelper.Decrypt(data.AuthDetails!.ClientSecret, data.AuthDetails.InitializationVector, Convert.FromHexString(cryptoConfig.EncryptionKey), cryptoConfig.CipherMode, cryptoConfig.PaddingMode); await _offerProviderService .TriggerOfferProviderCallback(callbackData, data.CallbackUrl, data.AuthDetails.AuthUrl, data.AuthDetails.ClientId, secret, cancellationToken) diff --git a/src/portalbackend/PortalBackend.DBAccess/Models/TriggerProviderInformation.cs b/src/portalbackend/PortalBackend.DBAccess/Models/TriggerProviderInformation.cs index 40bb00ede3..6677e5daf1 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Models/TriggerProviderInformation.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Models/TriggerProviderInformation.cs @@ -25,7 +25,7 @@ public record TriggerProviderInformation( Guid OfferId, string? OfferName, string? AutoSetupUrl, - ProviderAuthInformation AuthDetails, + ProviderAuthInformation? AuthDetails, CompanyInformationData CompanyInformationData, OfferTypeId OfferTypeId, Guid? SalesManagerId, diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs index a9ed33d4f0..8dcce426ce 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs @@ -148,7 +148,7 @@ public interface IOfferSubscriptionsRepository Task GetProcessStepData(Guid offerSubscriptionId, IEnumerable processStepTypeIds); Task GetClientCreationData(Guid offerSubscriptionId); Task GetTechnicalUserCreationData(Guid offerSubscriptionId); - Task<(IEnumerable<(Guid TechnicalUserId, string? TechnicalClientId, TechnicalUserKindId TechnicalUserKindId)> ServiceAccounts, string? ClientId, string? CallbackUrl, ProviderAuthInformation AuthDetails, OfferSubscriptionStatusId Status)> GetTriggerProviderCallbackInformation(Guid offerSubscriptionId); + Task<(IEnumerable<(Guid TechnicalUserId, string? TechnicalClientId, TechnicalUserKindId TechnicalUserKindId)> ServiceAccounts, string? ClientId, string? CallbackUrl, ProviderAuthInformation? AuthDetails, OfferSubscriptionStatusId Status)> GetTriggerProviderCallbackInformation(Guid offerSubscriptionId); OfferSubscriptionProcessData CreateOfferSubscriptionProcessData(Guid offerSubscriptionId, string offerUrl); void RemoveOfferSubscriptionProcessData(Guid offerSubscriptionProcessDataId); IAsyncEnumerable GetProcessStepsForSubscription(Guid offerSubscriptionId); diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs index 825dad52c3..7d92c0159a 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs @@ -311,7 +311,7 @@ public Task GetOfferSubscriptionDataForProcessIdAsync(Guid processId) => /// public Task GetTriggerProviderInformation(Guid offerSubscriptionId) => - dbContext.OfferSubscriptions + dbContext.OfferSubscriptions .Where(x => x.Id == offerSubscriptionId) .Select(x => new { @@ -334,7 +334,7 @@ public Task GetOfferSubscriptionDataForProcessIdAsync(Guid processId) => x.OfferId, x.OfferName, x.AutoSetupUrl, - new ProviderAuthInformation( + x.AuthUrl == null ? null : new ProviderAuthInformation( x.AuthUrl, x.ClientId, x.ClientSecret, @@ -355,6 +355,7 @@ public Task GetOfferSubscriptionDataForProcessIdAsync(Guid processId) => )) .SingleOrDefaultAsync(); + /// public Task GetSubscriptionActivationDataByIdAsync(Guid offerSubscriptionId) => dbContext.OfferSubscriptions @@ -435,14 +436,14 @@ public Task GetOfferSubscriptionDataForProcessIdAsync(Guid processId) => .SingleOrDefaultAsync(); /// - public Task<(IEnumerable<(Guid TechnicalUserId, string? TechnicalClientId, TechnicalUserKindId TechnicalUserKindId)> ServiceAccounts, string? ClientId, string? CallbackUrl, ProviderAuthInformation AuthDetails, OfferSubscriptionStatusId Status)> GetTriggerProviderCallbackInformation(Guid offerSubscriptionId) => + public Task<(IEnumerable<(Guid TechnicalUserId, string? TechnicalClientId, TechnicalUserKindId TechnicalUserKindId)> ServiceAccounts, string? ClientId, string? CallbackUrl, ProviderAuthInformation? AuthDetails, OfferSubscriptionStatusId Status)> GetTriggerProviderCallbackInformation(Guid offerSubscriptionId) => dbContext.OfferSubscriptions .Where(x => x.Id == offerSubscriptionId) - .Select(x => new ValueTuple, string?, string?, ProviderAuthInformation, OfferSubscriptionStatusId>( + .Select(x => new ValueTuple, string?, string?, ProviderAuthInformation?, OfferSubscriptionStatusId>( x.Technicalusers.Select(sa => new ValueTuple(sa.Id, sa.ClientClientId, sa.TechnicalUserKindId)), x.AppSubscriptionDetail!.AppInstance!.IamClient!.ClientClientId, x.Offer!.ProviderCompany!.ProviderCompanyDetail!.AutoSetupCallbackUrl, - new ProviderAuthInformation(x.Offer!.ProviderCompany!.ProviderCompanyDetail!.AuthUrl, + x.Offer.ProviderCompany.ProviderCompanyDetail!.AuthUrl == null ? null : new ProviderAuthInformation(x.Offer!.ProviderCompany!.ProviderCompanyDetail!.AuthUrl, x.Offer!.ProviderCompany!.ProviderCompanyDetail!.ClientId, x.Offer!.ProviderCompany!.ProviderCompanyDetail!.ClientSecret, x.Offer!.ProviderCompany!.ProviderCompanyDetail!.InitializationVector, diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyRepositoryTests.cs b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyRepositoryTests.cs index bb0e796426..11bbbac1e9 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyRepositoryTests.cs +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyRepositoryTests.cs @@ -61,9 +61,10 @@ public async Task CreateServiceProviderCompanyDetail_ReturnsExpectedResult() var (sut, context) = await CreateSut(); const string authUrl = "https://auth-url.com"; const string clientId = "client-id"; - var secret = A._; - var encryptionMode = A._; - var initializationVector = A._; + var secret = Convert.FromHexString("2b7e151628aed2a6abf715892b7e151628aed2a6abf715892b7e151628aed2a6"); + var initializationVector = Convert.FromBase64String("JHcycHPDfRwjT1J1NqBJtQ=="); + var encryptionMode = 5; + // Act var results = sut.CreateProviderCompanyDetail(_validCompanyId, new ProviderDetailsCreationData(url, authUrl, clientId, secret, encryptionMode), entity => @@ -163,7 +164,7 @@ public async Task GetServiceProviderCompanyDetailAsync_WithExistingUser_ReturnsE var (sut, _) = await CreateSut(); // Act - var result = await sut.GetProviderCompanyDetailAsync(A>._, new Guid("3390c2d7-75c1-4169-aa27-6ce00e1f3cdd")); + var result = await sut.GetProviderCompanyDetailAsync([CompanyRoleId.SERVICE_PROVIDER, CompanyRoleId.APP_PROVIDER], new Guid("3390c2d7-75c1-4169-aa27-6ce00e1f3cdd")); // Assert result.Should().NotBe(default); @@ -179,7 +180,7 @@ public async Task GetServiceProviderCompanyDetailAsync_WithNotExistingDetails_Re var (sut, _) = await CreateSut(); // Act - var result = await sut.GetProviderCompanyDetailAsync(A>._, Guid.NewGuid()); + var result = await sut.GetProviderCompanyDetailAsync([CompanyRoleId.SERVICE_PROVIDER, CompanyRoleId.APP_PROVIDER], Guid.NewGuid()); // Assert result.Should().Be(default); @@ -192,7 +193,7 @@ public async Task GetServiceProviderCompanyDetailAsync_WithExistingUserAndNotPro var (sut, _) = await CreateSut(); // Act - var result = await sut.GetProviderCompanyDetailAsync(A>._, new("ac861325-bc54-4583-bcdc-9e9f2a38ff84")); + var result = await sut.GetProviderCompanyDetailAsync([CompanyRoleId.SERVICE_PROVIDER, CompanyRoleId.APP_PROVIDER], new("41fd2ab8-71cd-4546-9bef-a388d91b2542")); // Assert result.Should().NotBe(default); diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs b/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs index 95f6ca272c..7522c20432 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs @@ -428,6 +428,23 @@ public async Task GetTriggerProviderInformation_WithValidData_ReturnsExpectedRes result.IsSingleInstance.Should().BeTrue(); } + [Fact] + public async Task GetTriggerProviderInformation_WithValidData_And_WithProviderDetails_ReturnsExpectedResult() + { + // Arrange + var (sut, _) = await CreateSut(); + + // Act + var result = await sut.GetTriggerProviderInformation(new Guid("81a8211d-6261-4969-8123-a420364ae997")); + + // Assert + result.Should().NotBeNull(); + result!.OfferName.Should().Be("Test App 1"); + result!.AuthDetails.Should().NotBeNull(); + result!.AuthDetails!.AuthUrl.Should().Be("https://auth-url.com"); + result.IsSingleInstance.Should().BeFalse(); + } + [Fact] public async Task GetTriggerProviderInformation_WithNotExistingId_ReturnsNull() { diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/Seeder/Data/provider_company_details.unittest.json b/tests/portalbackend/PortalBackend.DBAccess.Tests/Seeder/Data/provider_company_details.unittest.json index a9de6efd12..19cccd0b13 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/Seeder/Data/provider_company_details.unittest.json +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/Seeder/Data/provider_company_details.unittest.json @@ -3,6 +3,10 @@ "id": "3c0a31d2-7743-402c-af02-f0cc3cc3ca6d", "date_created": "2022-10-10 11:50:54.709881 +00:00", "auto_setup_url": "https://orchestrator.cx.dih-cloud.com", + "auth_url": "https://auth-url.com", + "client_id": "test", + "client_secret": "/UJ0wr5w1HiXaLo25QfxqXWhyq6Pa9w+CvBFNs1782s=", + "encryption_mode": 1, "company_id": "3390c2d7-75c1-4169-aa27-6ce00e1f3cdd" } ] \ No newline at end of file