2021-06-11 20:06:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoFixture.Xunit2;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-06-27 12:14:34 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-06-11 20:06:35 +00:00
|
|
|
|
using Moq;
|
|
|
|
|
using Retroactiune.Controllers;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Entities;
|
|
|
|
|
using Retroactiune.Core.Interfaces;
|
|
|
|
|
using Retroactiune.Core.Services;
|
2021-06-11 20:06:35 +00:00
|
|
|
|
using Retroactiune.DataTransferObjects;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class TestTokensController
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_GenerateTokens_InexistentFeedbackReceiver()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-06-11 20:06:35 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-11 20:06:35 +00:00
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-11 20:06:35 +00:00
|
|
|
|
var result = await controller.GenerateTokens(new GenerateTokensDto());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task Test_GenerateTokens_Success(FeedbackReceiver randFedFeedbackReceiver)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-11 20:06:35 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-11 20:06:35 +00:00
|
|
|
|
|
|
|
|
|
feedbackService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>(),
|
|
|
|
|
It.IsAny<int?>(),
|
|
|
|
|
It.IsAny<int?>()))
|
|
|
|
|
.ReturnsAsync(new[] {randFedFeedbackReceiver});
|
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-11 20:06:35 +00:00
|
|
|
|
var result = await controller.GenerateTokens(new GenerateTokensDto
|
|
|
|
|
{
|
|
|
|
|
NumberOfTokens = 2,
|
|
|
|
|
FeedbackReceiverId = "froid",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
|
|
|
tokens.Verify(i => i.GenerateTokensAsync(2, "froid", null), Times.Once);
|
|
|
|
|
}
|
2021-06-20 13:53:43 +00:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_Delete_Ok()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var result = await controller.DeleteToken("my_guid");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<NoContentResult>(result);
|
|
|
|
|
tokens.Verify(i => i.DeleteTokens(new[] {"my_guid"}), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_Delete_BadRequest()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
tokens.Setup(i => i.DeleteTokens(It.IsAny<IEnumerable<string>>()))
|
|
|
|
|
.Throws(new GenericServiceException("op fail"));
|
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var result = await controller.DeleteToken("my_guid");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
|
|
|
tokens.Verify(i => i.DeleteTokens(new[] {"my_guid"}), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_DeleteMany_Ok()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var result = await controller.DeleteTokens(new[] {"my_guid", "b"});
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<NoContentResult>(result);
|
|
|
|
|
tokens.Verify(i => i.DeleteTokens(new[] {"my_guid", "b"}), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_DeleteMany_BadRequest()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
2021-06-20 13:53:43 +00:00
|
|
|
|
tokens.Setup(i => i.DeleteTokens(It.IsAny<IEnumerable<string>>()))
|
|
|
|
|
.Throws(new GenericServiceException("op fail"));
|
|
|
|
|
|
|
|
|
|
// Test
|
2021-06-27 12:14:34 +00:00
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
2021-06-20 13:53:43 +00:00
|
|
|
|
var result = await controller.DeleteTokens(new[] {"my_guid", "b"});
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
|
|
|
tokens.Verify(i => i.DeleteTokens(new[] {"my_guid", "b"}), Times.Once);
|
|
|
|
|
}
|
2021-06-30 20:07:39 +00:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_ListTokens_Ok()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-30 20:07:39 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
|
|
|
|
|
|
|
|
|
// Test
|
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
|
|
|
|
var result = await controller.ListTokens(null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
|
|
|
tokens.Verify(i => i.ListTokens(It.IsAny<TokenListFilters>()), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Test_ListTokens_BadRequest()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var mapper = TestUtils.GetMapper();
|
2021-07-04 14:39:07 +00:00
|
|
|
|
var feedbackService = new Mock<IFeedbackReceiversService>();
|
2021-06-30 20:07:39 +00:00
|
|
|
|
var tokens = new Mock<ITokensService>();
|
|
|
|
|
var logger = new Mock<ILogger<TokensController>>();
|
|
|
|
|
tokens.Setup(i => i.ListTokens(It.IsAny<TokenListFilters>()))
|
|
|
|
|
.Throws(new GenericServiceException("op fail"));
|
|
|
|
|
|
|
|
|
|
// Test
|
|
|
|
|
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
|
|
|
|
|
var result = await controller.ListTokens(null);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
|
|
|
tokens.Verify(i => i.ListTokens(It.IsAny<TokenListFilters>()), Times.Once);
|
|
|
|
|
}
|
2021-06-11 20:06:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|