Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/API.Core/Messages/Commands/CompanyDeletionCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,13 +22,21 @@ public CompanyDeletionCommandHandler(
_relationshipGraph = relationshipGraph ?? throw new ArgumentNullException(nameof(relationshipGraph));
}

public async Task<Unit> 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<Unit> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand All @@ -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<IEasyStores>().Resolve<CompanyMongoEntity, long>();
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()
{
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/API.FunctionalTests/Seed/CompanyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ namespace Fossa.API.Core.Messages.Commands
public class CompanyDeletionCommandHandler : MediatR.IRequestHandler<Fossa.API.Core.Messages.Commands.CompanyDeletionCommand, MediatR.Unit>
{
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<MediatR.Unit> Handle(Fossa.API.Core.Messages.Commands.CompanyDeletionCommand request, System.Threading.CancellationToken cancellationToken) { }
}
public class CompanyModificationCommand : Fossa.API.Core.Messages.Commands.EntityTenantCommand<Fossa.API.Core.Entities.CompanyEntity, Fossa.API.Core.Entities.CompanyId, System.Guid>, System.IEquatable<Fossa.API.Core.Messages.Commands.CompanyModificationCommand>
Expand Down