Add unit tests for CheckToken

This commit is contained in:
Denis-Cosmin Nutiu 2021-07-25 14:08:35 +03:00
parent 86fbe4d2da
commit 47d7706cd2
2 changed files with 47 additions and 6 deletions

View file

@ -172,5 +172,47 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
Assert.IsType<BadRequestObjectResult>(result);
tokens.Verify(i => i.FindAsync(It.IsAny<TokenListFilters>()), Times.Once);
}
[Fact]
public async Task Test_CheckToken_NotFound()
{
// Arrange
var mapper = TestUtils.GetMapper();
var feedbackService = new Mock<IFeedbackReceiversService>();
var tokens = new Mock<ITokensService>();
var logger = new Mock<ILogger<TokensController>>();
tokens.Setup(i => i.FindAsync(It.IsAny<TokenListFilters>()))
.ReturnsAsync(new List<Token>());
// Test
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
var result = await controller.CheckToken("random");
// Assert
var checkResult = (CheckTokenDto) ((ObjectResult) result).Value;
Assert.IsType<OkObjectResult>(result);
Assert.False(checkResult.IsValid);
}
[Fact]
public async Task Test_CheckToken_Valid()
{
// Arrange
var mapper = TestUtils.GetMapper();
var feedbackService = new Mock<IFeedbackReceiversService>();
var tokens = new Mock<ITokensService>();
var logger = new Mock<ILogger<TokensController>>();
tokens.Setup(i => i.FindAsync(It.IsAny<TokenListFilters>()))
.ReturnsAsync(new List<Token> { new Token() });
// Test
var controller = new TokensController(feedbackService.Object, tokens.Object, logger.Object, mapper);
var result = await controller.CheckToken("random");
// Assert
var checkResult = (CheckTokenDto) ((ObjectResult) result).Value;
Assert.IsType<OkObjectResult>(result);
Assert.True(checkResult.IsValid);
}
}
}

View file

@ -160,13 +160,12 @@ namespace Retroactiune.Controllers
string guid
)
{
// TODO: Unit test.
var response = await _tokensService.FindAsync(new TokenListFilters
{
Ids = new[] {guid}
});
try
{
var response = await _tokensService.FindAsync(new TokenListFilters
{
Ids = new[] {guid}
});
var token = response.ElementAt(0);
return Ok(new CheckTokenDto
{
@ -180,7 +179,7 @@ namespace Retroactiune.Controllers
{
IsValid = false
});
}
}
}
}
}