From 47d7706cd2d63fbb280331ec72aeadce119d9ec9 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 25 Jul 2021 14:08:35 +0300 Subject: [PATCH] Add unit tests for CheckToken --- .../Controllers/TestTokensController.cs | 42 +++++++++++++++++++ .../Controllers/TokensController.cs | 11 +++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs index 0722866..1134ff0 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestTokensController.cs @@ -172,5 +172,47 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers Assert.IsType(result); tokens.Verify(i => i.FindAsync(It.IsAny()), Times.Once); } + + [Fact] + public async Task Test_CheckToken_NotFound() + { + // Arrange + var mapper = TestUtils.GetMapper(); + var feedbackService = new Mock(); + var tokens = new Mock(); + var logger = new Mock>(); + tokens.Setup(i => i.FindAsync(It.IsAny())) + .ReturnsAsync(new List()); + + // 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(result); + Assert.False(checkResult.IsValid); + } + + [Fact] + public async Task Test_CheckToken_Valid() + { + // Arrange + var mapper = TestUtils.GetMapper(); + var feedbackService = new Mock(); + var tokens = new Mock(); + var logger = new Mock>(); + tokens.Setup(i => i.FindAsync(It.IsAny())) + .ReturnsAsync(new List { 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(result); + Assert.True(checkResult.IsValid); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Controllers/TokensController.cs b/Retroactiune.WebAPI/Controllers/TokensController.cs index 09e3cff..c128295 100644 --- a/Retroactiune.WebAPI/Controllers/TokensController.cs +++ b/Retroactiune.WebAPI/Controllers/TokensController.cs @@ -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 }); - } + } } } } \ No newline at end of file