2020-10-29 12:17:09 +02:00

103 lines
3.6 KiB
C#

using System.IO;
using System.Threading.Tasks;
using MalwareMultiScan.Api.Controllers;
using MalwareMultiScan.Api.Data.Models;
using MalwareMultiScan.Api.Services.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
namespace MalwareMultiScan.Tests
{
public class ControllersTests
{
private DownloadController _downloadController;
private QueueController _queueController;
private ScanResultsController _scanResultsController;
private Mock<IScanResultService> _scanResultServiceMock;
[SetUp]
public void SetUp()
{
_scanResultServiceMock = new Mock<IScanResultService>();
_scanResultServiceMock
.Setup(x => x.ObtainFile("existing"))
.Returns(Task.FromResult(new MemoryStream() as Stream));
_scanResultServiceMock
.Setup(x => x.ObtainFile("non-existing"))
.Returns(Task.FromResult((Stream) null));
_scanResultServiceMock
.Setup(x => x.GetScanResult("existing"))
.Returns(Task.FromResult(new ScanResult
{
Id = "existing"
}));
_scanResultServiceMock
.Setup(x => x.GetScanResult("non-existing"))
.Returns(Task.FromResult((ScanResult) null));
_scanResultServiceMock
.Setup(x => x.ObtainFile("non-existing"))
.Returns(Task.FromResult((Stream) null));
_scanResultServiceMock
.Setup(x => x.CreateScanResult())
.Returns(Task.FromResult(new ScanResult()));
_downloadController = new DownloadController(_scanResultServiceMock.Object);
_scanResultsController = new ScanResultsController(_scanResultServiceMock.Object);
_queueController = new QueueController(_scanResultServiceMock.Object)
{
Url = Mock.Of<IUrlHelper>()
};
}
[Test]
public async Task TestFileDownload()
{
Assert.True(await _downloadController.Index("existing") is FileStreamResult);
Assert.True(await _downloadController.Index("non-existing") is NotFoundResult);
}
[Test]
public async Task TestScanResults()
{
Assert.True(
await _scanResultsController.Index("existing") is OkObjectResult okObjectResult &&
okObjectResult.Value is ScanResult scanResult && scanResult.Id == "existing");
Assert.True(await _scanResultsController.Index("non-existing") is NotFoundResult);
}
[Test]
public async Task TestFileQueue()
{
Assert.True(await _queueController.ScanFile(
Mock.Of<IFormFile>(x =>
x.FileName == "test.exe" &&
x.OpenReadStream() == new MemoryStream())
) is CreatedAtActionResult);
_scanResultServiceMock.Verify(
x => x.StoreFile("test.exe", It.IsAny<Stream>()), Times.Once);
_scanResultServiceMock.Verify(
x => x.QueueUrlScan(It.IsAny<ScanResult>(), It.IsAny<string>()), Times.Once);
}
[Test]
public async Task TestUrlQueue()
{
Assert.True(await _queueController.ScanUrl("http://test.com") is CreatedAtActionResult);
_scanResultServiceMock.Verify(
x => x.QueueUrlScan(It.IsAny<ScanResult>(), "http://test.com"), Times.Once);
}
}
}