From 4d5884380b902fc92d6ccb283b9e514667eace0e Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Fri, 11 Oct 2024 23:22:52 -0500 Subject: [PATCH 1/4] Update CompanyControllerWithSystemLicense.cs --- .../CompanyControllerWithSystemLicense.cs | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/API.FunctionalTests/ControllerApis/CompanyControllerWithSystemLicense.cs b/tests/API.FunctionalTests/ControllerApis/CompanyControllerWithSystemLicense.cs index 9f78a4e6..ff91da6d 100644 --- a/tests/API.FunctionalTests/ControllerApis/CompanyControllerWithSystemLicense.cs +++ b/tests/API.FunctionalTests/ControllerApis/CompanyControllerWithSystemLicense.cs @@ -1,9 +1,12 @@ using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; +using EasyDoubles; using Fossa.API.FunctionalTests.Seed; +using Fossa.API.Persistence.Mongo.Entities; using Fossa.API.Web; using Fossa.API.Web.ApiModels; +using Microsoft.Extensions.DependencyInjection; using Shouldly; namespace Fossa.API.FunctionalTests.ControllerApis; @@ -27,7 +30,7 @@ public async Task DeleteCompanyWithoutAccessTokenAsync() } [Fact] - public async Task DeleteExistingCompanyWithAdministratorAccessTokenAsync() + public async Task DeleteExistingCompanyWithDependenciesWithAdministratorAccessTokenAsync() { var client = _factory.CreateClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "01J9SJ94KK62JSRNQD7H70NCF7.Tenant1.ADMIN1"); @@ -36,6 +39,21 @@ public async Task DeleteExistingCompanyWithAdministratorAccessTokenAsync() response.StatusCode.ShouldBe(HttpStatusCode.FailedDependency); } + [Fact] + public async Task DeleteExistingCompanyWithoutDependenciesWithAdministratorAccessTokenAsync() + { + var client = _factory.CreateClient(); + var companyEasyStore = _factory.Services.GetRequiredService().Resolve(); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "01J9SJ94KK62JSRNQD7H70NCF7.Tenant2.ADMIN1"); + + companyEasyStore.Entities.ContainsKey(200L).ShouldBeTrue(); + + var response = await client.DeleteAsync("/api/1.0/Company"); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + companyEasyStore.Entities.ContainsKey(200L).ShouldBeFalse(); + } + [Fact] public async Task DeleteExistingCompanyWithUserAccessTokenAsync() { @@ -46,6 +64,16 @@ public async Task DeleteExistingCompanyWithUserAccessTokenAsync() response.StatusCode.ShouldBe(HttpStatusCode.Forbidden); } + [Fact] + public async Task DeleteMissingCompanyWithAdministratorAccessTokenAsync() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "01J9SJ94KK62JSRNQD7H70NCF7.Tenant1000.ADMIN1"); + var response = await client.DeleteAsync("/api/1.0/Company"); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + } + public Task DisposeAsync() => Task.CompletedTask; public async Task InitializeAsync() From 3293fc3f0098a43fdb7262ea12c29df2b46f21da Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Fri, 11 Oct 2024 23:45:45 -0500 Subject: [PATCH 2/4] Update CompanyDeletionCommandHandler.cs --- .../Commands/CompanyDeletionCommandHandler.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/API.Core/Messages/Commands/CompanyDeletionCommandHandler.cs b/src/API.Core/Messages/Commands/CompanyDeletionCommandHandler.cs index e118799c..58308f99 100644 --- a/src/API.Core/Messages/Commands/CompanyDeletionCommandHandler.cs +++ b/src/API.Core/Messages/Commands/CompanyDeletionCommandHandler.cs @@ -1,4 +1,5 @@ -using Fossa.API.Core.Extensions; +using Fossa.API.Core.Entities; +using Fossa.API.Core.Extensions; using Fossa.API.Core.Relationship; using Fossa.API.Core.Repositories; using MediatR; @@ -21,13 +22,21 @@ public CompanyDeletionCommandHandler( _relationshipGraph = relationshipGraph ?? throw new ArgumentNullException(nameof(relationshipGraph)); } - public async Task Handle( - CompanyDeletionCommand request, + public async Task DeleteCompanyAsync( + CompanyEntity entity, CancellationToken cancellationToken) { - var entity = await _companyQueryRepository.GetByTenantIdAsync(request.TenantID, cancellationToken).ConfigureAwait(false); await _relationshipGraph.ThrowIfHasDependentEntitiesAsync(entity.ID, cancellationToken).ConfigureAwait(false); await _companyRepository.RemoveAsync(entity, cancellationToken).ConfigureAwait(false); + } + + public async Task Handle( + CompanyDeletionCommand request, + CancellationToken cancellationToken) + { + var entityMaybe = await _companyQueryRepository.FindByTenantIdAsync(request.TenantID, cancellationToken).ConfigureAwait(false); + await entityMaybe.IfSomeAsync( + entity => DeleteCompanyAsync(entity, cancellationToken)).ConfigureAwait(false); return Unit.Value; } } From 8412d55b1364d1a1a958b033d08ea5138a10c21f Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Fri, 11 Oct 2024 23:45:50 -0500 Subject: [PATCH 3/4] Update CompanyExtensions.cs --- tests/API.FunctionalTests/Seed/CompanyExtensions.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/API.FunctionalTests/Seed/CompanyExtensions.cs b/tests/API.FunctionalTests/Seed/CompanyExtensions.cs index c0510f19..40ad6941 100644 --- a/tests/API.FunctionalTests/Seed/CompanyExtensions.cs +++ b/tests/API.FunctionalTests/Seed/CompanyExtensions.cs @@ -20,5 +20,12 @@ await companyRepository.TryAddAsync(new CompanyMongoEntity TenantID = Guid.Parse("53ade3c2-8e36-52f2-88cf-d068b1ab247a"), Name = "Company1", }, cancellationToken).ConfigureAwait(false); + + await companyRepository.TryAddAsync(new CompanyMongoEntity + { + ID = 200L, + TenantID = Guid.Parse("cf59d3dd-5258-5a20-88ab-0169cf128440"), + Name = "Company2", + }, cancellationToken).ConfigureAwait(false); } } From 11373cbe9e8e23f6f2827dc6d3d50803f9268555 Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Fri, 11 Oct 2024 23:45:53 -0500 Subject: [PATCH 4/4] Update PublicAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt --- ...cAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/API.UnitTests/PublicAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt b/tests/API.UnitTests/PublicAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt index 9728d9f4..69af5644 100644 --- a/tests/API.UnitTests/PublicAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt +++ b/tests/API.UnitTests/PublicAPITests.CoreAssemblyHasNoPublicAPIChangesAsync.verified.txt @@ -259,6 +259,7 @@ namespace Fossa.API.Core.Messages.Commands public class CompanyDeletionCommandHandler : MediatR.IRequestHandler { public CompanyDeletionCommandHandler(Fossa.API.Core.Repositories.ICompanyQueryRepository companyQueryRepository, Fossa.API.Core.Repositories.ICompanyRepository companyRepository, Fossa.API.Core.Relationship.IRelationshipGraph relationshipGraph) { } + public System.Threading.Tasks.Task DeleteCompanyAsync(Fossa.API.Core.Entities.CompanyEntity entity, System.Threading.CancellationToken cancellationToken) { } public System.Threading.Tasks.Task Handle(Fossa.API.Core.Messages.Commands.CompanyDeletionCommand request, System.Threading.CancellationToken cancellationToken) { } } public class CompanyModificationCommand : Fossa.API.Core.Messages.Commands.EntityTenantCommand, System.IEquatable