148 lines
4.6 KiB
C#
148 lines
4.6 KiB
C#
using EnvelopeGenerator.Application;
|
|
using EnvelopeGenerator.Application.Histories.Commands;
|
|
using EnvelopeGenerator.Application.Histories.Queries;
|
|
using EnvelopeGenerator.Application.Receivers.Commands;
|
|
using EnvelopeGenerator.Domain;
|
|
using EnvelopeGenerator.Infrastructure;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace EnvelopeGenerator.Tests.Application;
|
|
|
|
[TestFixture]
|
|
public class HistoryTests
|
|
{
|
|
private IHost _host;
|
|
|
|
private IMediator Mediator => _host.Services.GetRequiredService<IMediator>();
|
|
|
|
private readonly List<(int Id, string EmailAddress)> _receivers = new();
|
|
|
|
public (int Id, string EmailAddress) Receiver => _receivers.Count == 0
|
|
? throw new InvalidOperationException("Receiver list is empty.")
|
|
: _receivers[Random.Shared.Next(_receivers.Count)];
|
|
|
|
[SetUp]
|
|
public async Task Setup()
|
|
{
|
|
_host = Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
// add appsettings.json
|
|
config.SetBasePath(Directory.GetCurrentDirectory());
|
|
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
IConfiguration configuration = context.Configuration;
|
|
|
|
// add Application and Infrastructure services
|
|
#pragma warning disable CS0618
|
|
services.AddEnvelopeGeneratorServices(configuration);
|
|
services.AddEnvelopeGeneratorInfrastructureServices(
|
|
(sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"),
|
|
context.Configuration
|
|
);
|
|
#pragma warning restore CS0618
|
|
})
|
|
.Build();
|
|
|
|
// set receivers
|
|
var receivers = await Task.WhenAll(
|
|
new[]
|
|
{
|
|
"max.mueller@email.de",
|
|
"anna.schmidt@email.de",
|
|
"lukas.schneider@email.de",
|
|
"sophia.fischer@email.de",
|
|
"jonas.weber@email.de",
|
|
"lea.hoffmann@email.de",
|
|
"felix.wagner@email.de",
|
|
"mia.becker@email.de",
|
|
"paul.schulz@email.de",
|
|
"lena.koch@email.de"
|
|
}
|
|
.Select(async email =>
|
|
{
|
|
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
|
var (Id, AlreadyExists) = await Mediator.Send(cmd);
|
|
return (Id, email);
|
|
})
|
|
);
|
|
_receivers.AddRange(receivers);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_host.Dispose();
|
|
}
|
|
|
|
[Test]
|
|
public async Task CreateHistory_And_ReadHistory_Should_Work()
|
|
{
|
|
// Arrange
|
|
var createCmd = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 1,
|
|
UserReference = "UserA",
|
|
Status = Constants.EnvelopeStatus.EnvelopeCreated,
|
|
Comment = "First create"
|
|
};
|
|
|
|
// Act
|
|
var id = await Mediator.Send(createCmd);
|
|
|
|
// Assert
|
|
Assert.That(id, Is.Not.Null);
|
|
|
|
// ReadHistory query
|
|
var query = new ReadHistoryQuery(1);
|
|
var result = await Mediator.Send(query);
|
|
|
|
Assert.That(result, Is.Not.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReadHistory_Should_Filter_By_Status()
|
|
{
|
|
// Arrange
|
|
var createCmd1 = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 2,
|
|
UserReference = "UserX",
|
|
Status = Constants.EnvelopeStatus.EnvelopeCreated
|
|
};
|
|
|
|
var createCmd2 = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 2,
|
|
UserReference = "UserX",
|
|
Status = Constants.EnvelopeStatus.EnvelopePartlySigned
|
|
};
|
|
|
|
await Mediator.Send(createCmd1);
|
|
await Mediator.Send(createCmd2);
|
|
|
|
// Act
|
|
var result = await Mediator.Send(new ReadHistoryQuery(2, Constants.EnvelopeStatus.EnvelopePartlySigned));
|
|
|
|
// Assert
|
|
Assert.That(result, Has.Exactly(1).Items);
|
|
Assert.That(result, Has.All.Matches<EnvelopeGenerator.Application.Dto.EnvelopeHistory.EnvelopeHistoryDto>(
|
|
r => r.Status == Constants.EnvelopeStatus.EnvelopePartlySigned));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReadHistory_Should_Return_Empty_When_No_Record()
|
|
{
|
|
// Act
|
|
var result = await Mediator.Send(new ReadHistoryQuery(999));
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Empty);
|
|
}
|
|
} |