Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #29

Merged
merged 8 commits into from
Aug 7, 2023
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
3 changes: 3 additions & 0 deletions .github/workflows/build-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test
run: dotnet test --no-restore --verbosity normal

- name: Setup Node
uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SecureSend.Application.Commands.Handlers
{
internal sealed class CreateSecureUploadHandler: ICommandHandler<CreateSecureUpload, Unit>
public sealed class CreateSecureUploadHandler: ICommandHandler<CreateSecureUpload, Unit>
{
private readonly ISecureSendUploadRepository _secureSendUploadRepository;
private readonly ISecureSendUploadFactory _secureSendUploadFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SecureSend.Application.Commands.Handlers
{
internal sealed class DeleteSecureUploadHandler : ICommandHandler<DeleteSecureUpload, Unit>
public sealed class DeleteSecureUploadHandler : ICommandHandler<DeleteSecureUpload, Unit>
{
private readonly ISecureSendUploadRepository _repository;
private readonly IFileService _fileService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SecureSend.Application.Commands.Handlers
{
internal sealed class UploadChunksHandler : IRequestHandler<UploadChunks, Unit>
public sealed class UploadChunksHandler : ICommandHandler<UploadChunks, Unit>
{
private readonly IFileService _fileService;
private readonly ISecureSendUploadRepository _repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SecureSend.Application.Commands.Handlers
{
internal sealed class ViewSecureUploadHandler : ICommandHandler<ViewSecureUpload, SecureUploadDto>
public sealed class ViewSecureUploadHandler : ICommandHandler<ViewSecureUpload, SecureUploadDto>
{
private readonly ISecureSendUploadRepository _repository;

Expand All @@ -25,7 +25,7 @@
{
SecureUploadId = upload.Id,
UploadDate = upload.UploadDate,
ExpiryDate = upload.ExpiryDate,

Check warning on line 28 in SecureSend.Application/Commands/Handlers/ViewSecureUploadHandler.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu-latest

Possible null reference argument for parameter 'secureSendExpiryDate' in 'SecureSendExpiryDate.implicit operator DateTime?(SecureSendExpiryDate secureSendExpiryDate)'.

Check warning on line 28 in SecureSend.Application/Commands/Handlers/ViewSecureUploadHandler.cs

View workflow job for this annotation

GitHub Actions / build-windows-latest

Possible null reference argument for parameter 'secureSendExpiryDate' in 'SecureSendExpiryDate.implicit operator DateTime?(SecureSendExpiryDate secureSendExpiryDate)'.

Check warning on line 28 in SecureSend.Application/Commands/Handlers/ViewSecureUploadHandler.cs

View workflow job for this annotation

GitHub Actions / build-macOS-latest

Possible null reference argument for parameter 'secureSendExpiryDate' in 'SecureSendExpiryDate.implicit operator DateTime?(SecureSendExpiryDate secureSendExpiryDate)'.
Files = upload.Files.Select(f => new SecureFileDto { ContentType = f.ContentType, FileName = f.FileName })

};
Expand Down
2 changes: 1 addition & 1 deletion SecureSend.Domain/ValueObjects/SecureSendUploadDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public SecureSendUploadDate()

public static implicit operator DateTime(SecureSendUploadDate secureSendUploadDate) => secureSendUploadDate.Value;

//public static implicit operator SecureSendUploadDate(DateTime secureSendUploadDate) => new();
public static implicit operator SecureSendUploadDate(DateTime secureSendUploadDate) => new();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using MediatR;
using Moq;
using SecureSend.Application.Commands;
using SecureSend.Application.Commands.Handlers;
using SecureSend.Application.Exceptions;
using SecureSend.Application.Services;
using SecureSend.Domain.Factories;
using SecureSend.Domain.Repositories;

namespace SecureSend.Test.Application.Handlers;

public class CreateSecureUploadHandlerTests
{
#region ARRANGE

private readonly Mock<ISecureSendUploadRepository> _secureSendUploadRepository;
private readonly Mock<ISecureSendUploadFactory> _secureSendUploadFactory;
private readonly Mock<ISecureUploadReadService> _secureUploadReadService;
private readonly ICommandHandler<CreateSecureUpload, Unit> _commandHandler;


public CreateSecureUploadHandlerTests()
{
_secureSendUploadRepository = new Mock<ISecureSendUploadRepository>();
_secureSendUploadFactory = new Mock<ISecureSendUploadFactory>();
_secureUploadReadService = new Mock<ISecureUploadReadService>();
_commandHandler = new CreateSecureUploadHandler(_secureSendUploadRepository.Object, _secureSendUploadFactory.Object,
_secureUploadReadService.Object);
}


#endregion

[Fact]
public async void Handle_Succeeds()
{
var command = new CreateSecureUpload(Guid.NewGuid(), DateTime.Now.AddDays(5));
_secureUploadReadService.Setup(x => x.GetUploadId(command.uploadId, It.IsAny<CancellationToken>()))
.ReturnsAsync(() => Guid.Empty);


var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.Null(exception);
}
[Fact]
public async void Handle_Throws_UploadAlreadyExistsException()
{
var command = new CreateSecureUpload(Guid.NewGuid(), DateTime.Now.AddDays(5));
_secureUploadReadService.Setup(x => x.GetUploadId(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(() => Guid.NewGuid());


var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<UploadAlreadyExistsException>(exception);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using MediatR;
using Moq;
using SecureSend.Application.Commands;
using SecureSend.Application.Commands.Handlers;
using SecureSend.Application.Exceptions;
using SecureSend.Application.Services;
using SecureSend.Domain.Entities;
using SecureSend.Domain.Repositories;

namespace SecureSend.Test.Application.Handlers;

public class DeleteSecureUploadHandlerTest
{
#region ARRANGE

private readonly Mock<ISecureSendUploadRepository> _repository;
private readonly Mock<IFileService> _fileService;
private readonly ICommandHandler<DeleteSecureUpload, Unit> _commandHandler;

public DeleteSecureUploadHandlerTest()
{
_repository = new Mock<ISecureSendUploadRepository>();
_fileService = new Mock<IFileService>();
_commandHandler = new DeleteSecureUploadHandler(_repository.Object, _fileService.Object);
}
#endregion

[Fact]
public async void Handle_Succeeds()
{
var command = new DeleteSecureUpload(Guid.NewGuid());
_repository.Setup(x => x.GetAsync(command.id, It.IsAny<CancellationToken>())).ReturnsAsync(new SecureSendUpload());
var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.Null(exception);
}

[Fact]
public async void Handle_Throws_UploadDoesNotExistException()
{
var command = new DeleteSecureUpload(Guid.NewGuid());
_repository.Setup(x => x.GetAsync(command.id, It.IsAny<CancellationToken>()))
.ReturnsAsync((SecureSendUpload)null);


var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<UploadDoesNotExistException>(exception);

}
}
80 changes: 80 additions & 0 deletions SecureSend.Test/Application/Handlers/UploadChunksHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Moq;
using SecureSend.Application.Commands;
using SecureSend.Application.Commands.Handlers;
using SecureSend.Application.Exceptions;
using SecureSend.Application.Services;
using SecureSend.Domain.Entities;
using SecureSend.Domain.Factories;
using SecureSend.Domain.Repositories;

namespace SecureSend.Test.Application.Handlers;

public class UploadChunksHandlerTests
{
#region ARRANGE

private readonly Mock<ISecureSendUploadRepository> _repository;
private readonly Mock<IFileService> _fileService;
private readonly Mock<IFormFile> _file;
private readonly ISecureSendUploadFactory _factory;
private readonly ICommandHandler<UploadChunks, Unit> _commandHandler;
private readonly SecureSendUpload upload;

public UploadChunksHandlerTests()
{
_repository = new Mock<ISecureSendUploadRepository>();
_fileService = new Mock<IFileService>();
_commandHandler = new UploadChunksHandler(_fileService.Object, _repository.Object);
_factory = new SecureSendUploadFactory();
upload = _factory.CreateSecureSendUpload(Guid.NewGuid(), DateTime.Now, DateTime.Now.AddDays(5), false);
_file = new Mock<IFormFile>();
_file.Setup(x => x.FileName).Returns("file.txt");
_file.Setup(x => x.ContentType).Returns("text/plain");
}
#endregion

[Fact]
public async void Handle_Succeeds()
{
var command = new UploadChunks(Guid.NewGuid(), 5, 5, _file.Object);
_repository.Setup(x => x.GetAsync(command.uploadId, It.IsAny<CancellationToken>()))
.ReturnsAsync(upload);
_fileService.Setup(x => x.GetChunksList(command.uploadId, It.IsAny<string>()))
.Returns(new List<string> { "1", "2", "3", "4", "5" });

var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.Null(exception);
Assert.Single(upload.Files);
}

[Fact]
public async void Handle_Throws_UploadDoesNotExistException()
{
var command = new UploadChunks(Guid.NewGuid(), 5, 5, _file.Object);
_repository.Setup(x => x.GetAsync(command.uploadId, It.IsAny<CancellationToken>()))
.ReturnsAsync(default(SecureSendUpload));


var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<UploadDoesNotExistException>(exception);

}

[Fact]
public async void Handle_Throws_InvalidChunkCountException()
{
var command = new UploadChunks(Guid.NewGuid(), 5, 5, _file.Object);
_repository.Setup(x => x.GetAsync(command.uploadId, It.IsAny<CancellationToken>()))
.ReturnsAsync(upload);
_fileService.Setup(x => x.GetChunksList(It.IsAny<Guid>(), It.IsAny<string>()))
.Returns(new List<string> { "1", "2" });

var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<InvalidChunkCountException>(exception);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Moq;
using SecureSend.Application.Commands;
using SecureSend.Application.Commands.Handlers;
using SecureSend.Application.DTO;
using SecureSend.Application.Exceptions;
using SecureSend.Domain.Entities;
using SecureSend.Domain.Factories;
using SecureSend.Domain.Repositories;

namespace SecureSend.Test.Application.Handlers;

public class ViewSecureUploadHandlerTests
{
#region ARRANGE

private readonly Mock<ISecureSendUploadRepository> _repository;
private readonly ISecureSendUploadFactory _factory;
private readonly ICommandHandler<ViewSecureUpload, SecureUploadDto> _commandHandler;

public ViewSecureUploadHandlerTests()
{
_repository = new Mock<ISecureSendUploadRepository>();
_commandHandler = new ViewSecureUploadHandler(_repository.Object);
_factory = new SecureSendUploadFactory();
}

#endregion

[Fact]
public async void Handle_Throws_UploadDoesNotExistException()
{
var command = new ViewSecureUpload(Guid.NewGuid());
_repository.Setup(x => x.GetAsync(command.id, It.IsAny<CancellationToken>()))!
.ReturnsAsync(default(SecureSendUpload));

var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<UploadDoesNotExistException>(exception);

}

[Fact]
public async void Handle_Throws_UploadExpiredException()
{
var upload = _factory.CreateSecureSendUpload(Guid.NewGuid(), DateTime.Now, DateTime.Now.AddDays(-5), false);
var command = new ViewSecureUpload(Guid.NewGuid());
_repository.Setup(x => x.GetAsync(command.id, It.IsAny<CancellationToken>()))!
.ReturnsAsync(upload);

var exception = await Record.ExceptionAsync(() => _commandHandler.Handle(command, It.IsAny<CancellationToken>()));
Assert.NotNull(exception);
Assert.IsType<UploadExpiredException>(exception);

}

[Fact]
public async void Handle_Succedes()
{
var upload = _factory.CreateSecureSendUpload(Guid.NewGuid(), DateTime.Now, DateTime.Now.AddDays(5), false);
var command = new ViewSecureUpload(Guid.NewGuid());
_repository.Setup(x => x.GetAsync(command.id, It.IsAny<CancellationToken>()))!
.ReturnsAsync(upload);

var result = await _commandHandler.Handle(command, It.IsAny<CancellationToken>());
Assert.True(upload.IsViewed);
Assert.NotNull(result);
Assert.IsType<SecureUploadDto>(result);

}
}
56 changes: 56 additions & 0 deletions SecureSend.Test/Domain/SecureSendUploadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using SecureSend.Domain.Entities;
using SecureSend.Domain.Exceptions;
using SecureSend.Domain.Factories;
using SecureSend.Domain.ValueObjects;

namespace SecureSend.Test.Domain;

public class SecureSendUploadTests
{
#region ARRANGE

private readonly ISecureSendUploadFactory _factory;
private readonly SecureSendUpload upload;

public SecureSendUploadTests()
{
_factory = new SecureSendUploadFactory();
upload = _factory.CreateSecureSendUpload(Guid.NewGuid(), DateTime.Now, DateTime.Now.AddDays(5), false);
}

#endregion

[Fact]
public void AddFile_Succeeds()
{
upload.AddFile(new SecureSendFile("test_file", "application/octet-stream"));
Assert.Single(upload.Files);
}

[Fact]
public void AddMultipleFiles_Succeeds()
{
IList<SecureSendFile> files = new List<SecureSendFile>();
for (int i = 0; i < 5; i++)
{
files.Add(new SecureSendFile($"{i}_test_file", "application/octet-stream"));
}
upload.AddMultipleFiles(files);
Assert.Collection(upload.Files,
x => Assert.Equal("0_test_file", x.FileName),
x => Assert.Equal("1_test_file", x.FileName),
x => Assert.Equal("2_test_file", x.FileName),
x => Assert.Equal("3_test_file", x.FileName),
x => Assert.Equal("4_test_file", x.FileName));

}

[Fact]
public void AddFile_Throws_FileAlreadyExistsException()
{
upload.AddFile(new SecureSendFile("test_file", "application/octet-stream"));
var exception = Record.Exception(() => upload.AddFile(new SecureSendFile("test_file", "application/octet-stream")));
Assert.NotNull(exception);
Assert.IsType<FileAlreadyExistsException>(exception);
}
}
Loading
Loading