302 lines
13 KiB
C#
302 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using Xunit;
|
|
using LCU.Api.Internal.FileOperations.Controllers;
|
|
using LCU.Api.Internal.FileOperations.Services;
|
|
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
using LCU.Api.Internal.FileOperations.Services.QueueServices;
|
|
namespace LCU.Api.Internal.FileOperationsTests.Controllers;
|
|
|
|
public class FileOperationsControllerTest
|
|
{
|
|
private readonly FileOperationsController _controller;
|
|
private readonly Mock<IQueueFileOperationsService> _mockFileOperationsService;
|
|
private readonly Mock<IActionService<MoveFileRequest>> _mockMoveFileService;
|
|
private readonly Mock<IActionService<CopyFileRequest>> _mockCopyFileService;
|
|
private readonly Mock<IActionService<ModifyFileRequest>> _mockModifyFileService;
|
|
private readonly Mock<IActionService<DeleteFileRequest>> _mockDeleteFileService;
|
|
private readonly Mock<IActionService<RenameFileRequest>> _mockRenameFileService;
|
|
private readonly Mock<IActionService<ZipDirectoryRequest>> _mockZipDirectoryService;
|
|
private readonly Mock<IActionService<UnzipFileRequest>> _mockUnzipFileService;
|
|
private readonly Mock<IActionService<UnzipPasswordProtectedFileRequest>> _mockUnzipPasswordProtectedFileService;
|
|
private readonly Mock<IActionService<CreateDirectoryRequest>> _mockCreateDirectoryService;
|
|
private readonly Mock<IActionService<RenameDirectoryRequest>> _mockRenameDirectoryService;
|
|
private readonly Mock<IActionService<BulkOperationRequest<MoveFileRequest>>> _mockBulkMoveFileService;
|
|
private readonly Mock<IActionService<EncryptFileDataRequest>> _mockEncryptFileDataService;
|
|
private readonly Mock<IActionService<DecryptFileDataRequest>> _mockDecryptFileDataService;
|
|
private readonly Mock<IActionService<WriteFileRequest>> _mockWriteFileDataService;
|
|
private readonly Mock<IActionService<DeleteDirectoryRequest>> _mockDeleteDirectoryService;
|
|
private readonly Mock<IActionService<DeleteEmptyDirectoryRequest>> _mockDeleteEmptyDirectoriesService;
|
|
private readonly Mock<IDirectFileService> _mockDirectFileService;
|
|
|
|
public FileOperationsControllerTest()
|
|
{
|
|
_mockFileOperationsService = new Mock<IQueueFileOperationsService>();
|
|
_mockMoveFileService = new Mock<IActionService<MoveFileRequest>>();
|
|
_mockCopyFileService = new Mock<IActionService<CopyFileRequest>>();
|
|
_mockModifyFileService = new Mock<IActionService<ModifyFileRequest>>();
|
|
_mockDeleteFileService = new Mock<IActionService<DeleteFileRequest>>();
|
|
_mockRenameFileService = new Mock<IActionService<RenameFileRequest>>();
|
|
_mockZipDirectoryService = new Mock<IActionService<ZipDirectoryRequest>>();
|
|
_mockUnzipFileService = new Mock<IActionService<UnzipFileRequest>>();
|
|
_mockUnzipPasswordProtectedFileService = new Mock<IActionService<UnzipPasswordProtectedFileRequest>>();
|
|
_mockCreateDirectoryService = new Mock<IActionService<CreateDirectoryRequest>>();
|
|
_mockRenameDirectoryService = new Mock<IActionService<RenameDirectoryRequest>>();
|
|
_mockBulkMoveFileService = new Mock<IActionService<BulkOperationRequest<MoveFileRequest>>>();
|
|
_mockEncryptFileDataService = new Mock<IActionService<EncryptFileDataRequest>>();
|
|
_mockDecryptFileDataService = new Mock<IActionService<DecryptFileDataRequest>>();
|
|
_mockWriteFileDataService = new Mock<IActionService<WriteFileRequest>>();
|
|
_mockDeleteDirectoryService = new Mock<IActionService<DeleteDirectoryRequest>>();
|
|
_mockDeleteEmptyDirectoriesService = new Mock<IActionService<DeleteEmptyDirectoryRequest>>();
|
|
_mockDirectFileService = new Mock<IDirectFileService>();
|
|
|
|
_controller = new FileOperationsController(
|
|
_mockFileOperationsService.Object,
|
|
_mockMoveFileService.Object,
|
|
_mockCopyFileService.Object,
|
|
_mockModifyFileService.Object,
|
|
_mockDeleteFileService.Object,
|
|
_mockRenameFileService.Object,
|
|
_mockZipDirectoryService.Object,
|
|
_mockUnzipFileService.Object,
|
|
_mockUnzipPasswordProtectedFileService.Object,
|
|
_mockCreateDirectoryService.Object,
|
|
_mockRenameDirectoryService.Object,
|
|
_mockBulkMoveFileService.Object,
|
|
_mockEncryptFileDataService.Object,
|
|
_mockDecryptFileDataService.Object,
|
|
_mockWriteFileDataService.Object,
|
|
_mockDeleteDirectoryService.Object,
|
|
_mockDeleteEmptyDirectoriesService.Object,
|
|
_mockDirectFileService.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetFile_OK()
|
|
{
|
|
var bytes = new byte[1];
|
|
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>())).Returns(bytes);
|
|
|
|
// act
|
|
var result = _controller.GetFile("\\\\fakeit\\file.csv");
|
|
Assert.NotNull(result);
|
|
var okResult = result as FileContentResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetFile_BadRequest_Pre()
|
|
{
|
|
var bytes = new byte[1];
|
|
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>())).Returns(bytes);
|
|
|
|
// act
|
|
var result = _controller.GetFile(" ");
|
|
Assert.NotNull(result);
|
|
var badResult = result as BadRequestObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(badResult);
|
|
Assert.Equal(400, badResult.StatusCode);
|
|
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetFile_BadRequest_Post()
|
|
{
|
|
_mockDirectFileService.Setup(service => service.GetFileBytes(It.IsAny<string>()))
|
|
.Throws<ArgumentException>();
|
|
|
|
// act
|
|
var result = _controller.GetFile("\\\\fakeit\\file.csv");
|
|
Assert.NotNull(result);
|
|
var badResult = result as BadRequestObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(badResult);
|
|
Assert.Equal(400, badResult.StatusCode);
|
|
_mockDirectFileService.Verify(service => service.GetFileBytes(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task MoveFilePost_OK()
|
|
{
|
|
// arrange
|
|
var request = new MoveFileRequest()
|
|
{
|
|
SourcePath = "",
|
|
SourceFile = "",
|
|
DestinationPath = ""
|
|
};
|
|
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
|
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
|
|
|
// act
|
|
var result = await _controller.MoveFile(request);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task MoveFilePost_InvalidRequest()
|
|
{
|
|
// arrange
|
|
var request = new MoveFileRequest() { };
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
|
_mockMoveFileService.Setup(service => service.PreQueueValidation(It.IsAny<MoveFileRequest>())).Returns(validRequestResponse);
|
|
// act
|
|
var result = await _controller.MoveFile(request);
|
|
|
|
Assert.NotNull(result);
|
|
var badRequest = result as BadRequestObjectResult;
|
|
Assert.Equal(400, badRequest.StatusCode);
|
|
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task CopyFilePost_OK()
|
|
{
|
|
// arrange
|
|
var request = new CopyFileRequest()
|
|
{
|
|
SourcePath = "",
|
|
SourceFile = "",
|
|
DestinationPath = ""
|
|
};
|
|
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
|
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
|
|
|
// act
|
|
var result = await _controller.CopyFile(request);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task CopyFilePost_InvalidRequest()
|
|
{
|
|
// arrange
|
|
var request = new CopyFileRequest() { };
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
|
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
|
// act
|
|
var result = await _controller.CopyFile(request);
|
|
|
|
Assert.NotNull(result);
|
|
var badRequest = result as BadRequestObjectResult;
|
|
Assert.Equal(400, badRequest.StatusCode);
|
|
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task UnzipPasswordProtectedFilePost_OK()
|
|
{
|
|
// arrange
|
|
var request = new UnzipPasswordProtectedFileRequest()
|
|
{
|
|
SourceDirectory = "",
|
|
PasswordProtectedZipFileName = "",
|
|
Password = "",
|
|
DestinationPath = ""
|
|
};
|
|
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
|
_mockUnzipPasswordProtectedFileService.Setup(service => service.PreQueueValidation(It.IsAny<UnzipPasswordProtectedFileRequest>())).Returns(validRequestResponse);
|
|
|
|
// act
|
|
var result = await _controller.UnzipPasswordProtectedFile(request);
|
|
|
|
// assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(200, (result as OkResult).StatusCode);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task UnzipPasswordProtectedFilePost_InvalidRequest()
|
|
{
|
|
// arrange
|
|
var request = new UnzipPasswordProtectedFileRequest() { };
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>() { "Invalid" }, IsSuccessful = false };
|
|
_mockUnzipPasswordProtectedFileService.Setup(service => service.PreQueueValidation(It.IsAny<UnzipPasswordProtectedFileRequest>())).Returns(validRequestResponse);
|
|
// act
|
|
var result = await _controller.UnzipPasswordProtectedFile(request);
|
|
|
|
Assert.NotNull(result);
|
|
var badRequest = result as BadRequestObjectResult;
|
|
Assert.Equal(400, badRequest.StatusCode);
|
|
Assert.Equal($"Request is invalid. Response: {String.Join("\n ", validRequestResponse.MessageStack.ToArray())}", badRequest.Value);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task HandleRequestResponsePost_CopyFileRequest_OK()
|
|
{
|
|
// arrange
|
|
var request = new CopyFileRequest()
|
|
{
|
|
SourcePath = "",
|
|
SourceFile = "",
|
|
DestinationPath = ""
|
|
};
|
|
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
|
_mockCopyFileService.Setup(service => service.PreQueueValidation(It.IsAny<CopyFileRequest>())).Returns(validRequestResponse);
|
|
|
|
// act
|
|
var result = await _controller.HandleRequestResponse<CopyFileRequest>(request, FileOperationType.Copy, _mockCopyFileService.Object);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
}
|
|
|
|
// Post tests
|
|
[Fact]
|
|
public async Task HandleRequestResponsePost_CreateDirectoryRequest_OK()
|
|
{
|
|
// arrange
|
|
var request = new CreateDirectoryRequest()
|
|
{
|
|
DirectoryName = "",
|
|
DirectoryPath = ""
|
|
};
|
|
|
|
var validRequestResponse = new RequestResponse() { MessageStack = new List<string>(), IsSuccessful = true };
|
|
_mockCreateDirectoryService.Setup(service => service.PreQueueValidation(It.IsAny<CreateDirectoryRequest>())).Returns(validRequestResponse);
|
|
|
|
// act
|
|
var result = await _controller.HandleRequestResponse<CreateDirectoryRequest>(request, FileOperationType.CreateDirectory,
|
|
_mockCreateDirectoryService.Object);
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
}
|
|
} |