diff --git a/EnvelopeGenerator.Tests/Application/DeleteEnvelopeCommandTests.cs b/EnvelopeGenerator.Tests/Application/DeleteEnvelopeCommandTests.cs new file mode 100644 index 00000000..59843417 --- /dev/null +++ b/EnvelopeGenerator.Tests/Application/DeleteEnvelopeCommandTests.cs @@ -0,0 +1,109 @@ +using DigitalData.Core.Exceptions; +using DigitalData.EmailProfilerDispatcher.Abstraction.Entities; +using EnvelopeGenerator.Application.Envelopes.Commands; +using EnvelopeGenerator.Domain.Constants; +using EnvelopeGenerator.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace EnvelopeGenerator.Tests.Application; + +[TestFixture] +public class DeleteEnvelopeCommandTests : TestBase +{ + protected override void ConfigureServices(IServiceCollection services) + { + } + + [Test] + public async Task DeleteEnvelope_DraftEnvelope_ShouldSetDeleted_CreateHistory_AndQueueDeletionEmail() + { + CancellationToken cancel = default; + var reason = "Invalid receiver data"; + + var envelope = this.CreateEnvelope(User.Id); + envelope.Status = EnvelopeStatus.EnvelopeCreated; + envelope = await GetRepository().CreateAsync(envelope, cancel); + + var createReceiverCmd = this.CreateReceiverCommand(); + (var receiver, _) = await Mediator.Send(createReceiverCmd, cancel); + + var envReceiver = this.CreateEnvelopeReceiver(envelope.Id, receiver.Id); + await GetRepository().CreateAsync(envReceiver, cancel); + + var result = await Mediator.Send(new DeleteEnvelopeCommand + { + EnvelopeId = envelope.Id, + Reason = reason + }.WithAuth(User.Id), cancel); + + Assert.That(result.EnvelopeId, Is.EqualTo(envelope.Id)); + Assert.That(result.AppliedStatus, Is.EqualTo(EnvelopeStatus.EnvelopeDeleted)); + Assert.That(result.IsNoOp, Is.False); + + var histories = await GetRepository().Query + .Where(h => h.EnvelopeId == envelope.Id && h.Status == EnvelopeStatus.EnvelopeDeleted) + .ToListAsync(cancel); + Assert.That(histories, Has.Count.EqualTo(1)); + Assert.That(histories[0].Comment, Is.EqualTo(reason)); + + var emails = await GetRepository().Query + .Where(e => e.WfId == (int)EnvelopeStatus.MessageDeletionSent && e.ReferenceId == receiver.Id) + .ToListAsync(cancel); + Assert.That(emails, Has.Count.EqualTo(1)); + Assert.That(emails[0].EmailAddress, Is.EqualTo(receiver.EmailAddress)); + } + + [Test] + public async Task DeleteEnvelope_SentEnvelope_ShouldSetWithdrawn() + { + CancellationToken cancel = default; + + var envelope = this.CreateEnvelope(User.Id); + envelope.Status = EnvelopeStatus.EnvelopeQueued; + envelope = await GetRepository().CreateAsync(envelope, cancel); + + var result = await Mediator.Send(new DeleteEnvelopeCommand + { + EnvelopeId = envelope.Id, + Reason = "Sender requested cancellation" + }.WithAuth(User.Id), cancel); + + Assert.That(result.AppliedStatus, Is.EqualTo(EnvelopeStatus.EnvelopeWithdrawn)); + + var histories = await GetRepository().Query + .Where(h => h.EnvelopeId == envelope.Id && h.Status == EnvelopeStatus.EnvelopeWithdrawn) + .ToListAsync(cancel); + Assert.That(histories, Has.Count.EqualTo(1)); + } + + [Test] + public void DeleteEnvelope_MissingReason_ShouldThrowBadRequest() + { + Assert.ThrowsAsync(async () => + await Mediator.Send(new DeleteEnvelopeCommand + { + EnvelopeId = 123, + Reason = " " + }.WithAuth(User.Id))); + } + + [Test] + public async Task DeleteEnvelope_WrongOwner_ShouldThrowForbidden() + { + CancellationToken cancel = default; + + var envelope = this.CreateEnvelope(User.Id); + envelope = await GetRepository().CreateAsync(envelope, cancel); + + var otherUserCmd = this.CreateUserCommand(); + var otherUser = await GetRepository().CreateAsync(otherUserCmd, cancel); + + Assert.ThrowsAsync(async () => + await Mediator.Send(new DeleteEnvelopeCommand + { + EnvelopeId = envelope.Id, + Reason = "Not allowed" + }.WithAuth(otherUser.Id), cancel)); + } +} diff --git a/EnvelopeGenerator.Tests/Application/Fake.cs b/EnvelopeGenerator.Tests/Application/Fake.cs index 9f424b70..ca15c07d 100644 --- a/EnvelopeGenerator.Tests/Application/Fake.cs +++ b/EnvelopeGenerator.Tests/Application/Fake.cs @@ -52,7 +52,7 @@ public class Fake services.AddEnvelopeGeneratorInfrastructureServices(opt => { - opt.AddDbContext(dbCtxOpt => dbCtxOpt.UseInMemoryDatabase("EnvelopeGeneratorTestDb")); + opt.AddDbContext(dbCtxOpt => dbCtxOpt.UseInMemoryDatabase($"EnvelopeGeneratorTestDb_{Guid.NewGuid():N}")); opt.AddDbTriggerParams(context.Configuration); @@ -308,4 +308,4 @@ public static class Extensions .Select(_ => fake.CreateUserCommand()) .ToList(); #endregion -} \ No newline at end of file +} diff --git a/EnvelopeGenerator.Tests/Application/TestBase.cs b/EnvelopeGenerator.Tests/Application/TestBase.cs index f5b362e6..d4a11c7a 100644 --- a/EnvelopeGenerator.Tests/Application/TestBase.cs +++ b/EnvelopeGenerator.Tests/Application/TestBase.cs @@ -38,7 +38,10 @@ public abstract class TestBase : Faker // Add seed email templates foreach (var temp in SeedEmailTemplates) + { + temp.LangCode ??= "de"; await repo.CreateAsync(temp); + } } [TearDown] @@ -140,4 +143,4 @@ public abstract class TestBase : Faker ChangedWhen = DateTime.Parse("2025-05-12 10:43:44.290") } }; -} \ No newline at end of file +}