diff --git a/test/Armory.Api.Test/Armory.Api.Test.csproj b/test/Armory.Api.Test/Armory.Api.Test.csproj index f78a54e6..e90e8d48 100644 --- a/test/Armory.Api.Test/Armory.Api.Test.csproj +++ b/test/Armory.Api.Test/Armory.Api.Test.csproj @@ -13,10 +13,6 @@ - - - - diff --git a/test/Armory.Api.Test/Controllers/ArmoryUsers/Authentication/AuthenticationControllerTest.cs b/test/Armory.Api.Test/Controllers/ArmoryUsers/Authentication/AuthenticationControllerTest.cs new file mode 100644 index 00000000..c2480a1f --- /dev/null +++ b/test/Armory.Api.Test/Controllers/ArmoryUsers/Authentication/AuthenticationControllerTest.cs @@ -0,0 +1,90 @@ +using System.Threading; +using System.Threading.Tasks; +using Armory.Api.Controllers.ArmoryUsers.Authentication; +using Armory.Users.Application.Authenticate; +using Armory.Users.Application.GenerateJwt; +using Armory.Users.Domain; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; + +namespace Armory.Api.Test.Controllers.ArmoryUsers.Authentication +{ + public class AuthenticationControllerTest : ApiTest + { + private AuthenticationController _controller; + + [SetUp] + public void SetUp() + { + _controller = new AuthenticationController(Mediator.Object); + } + + private void ShouldHaveAuthenticate() + { + Mediator.Verify(x => x.Send(It.IsAny(), CancellationToken.None), + Times.AtLeastOnce()); + } + + [Test, Order(1)] + public async Task Authenticate_With_Valid_Username_And_Password() + { + Mediator.Setup(x => x.Send(It.IsAny(), CancellationToken.None)) + .ReturnsAsync("auth_token"); + + var result = await _controller.Authenticate(new AuthenticationRequest + { + UsernameOrEmail = "admin", + Password = "admin", + IsPersistent = true + }); + + ShouldHaveAuthenticate(); + + Assert.IsNotNull(result.Result); + Assert.IsInstanceOf(result.Result); + + var okResult = result.Result as OkObjectResult; + Assert.IsNotNull(okResult); + Assert.IsInstanceOf(okResult.Value); + + Assert.AreEqual("auth_token", okResult.Value as string); + } + + [Test, Order(2)] + public async Task Authenticate_With_Valid_Username_And_Invalid_Password() + { + Mediator.Setup(x => x.Send(It.IsAny(), CancellationToken.None)) + .Throws(); + + var result = await _controller.Authenticate(new AuthenticationRequest + { + UsernameOrEmail = "admin", + Password = "wrong", + IsPersistent = true + }); + + ShouldHaveAuthenticate(); + Assert.IsNotNull(result.Result); + Assert.IsInstanceOf(result.Result); + } + + [Test, Order(3)] + public async Task Authenticate_With_Invalid_Username() + { + Mediator.Setup(x => x.Send(It.IsAny(), CancellationToken.None)) + .Throws(); + + var result = await _controller.Authenticate(new AuthenticationRequest + { + UsernameOrEmail = "none", + Password = "wrong", + IsPersistent = true + }); + + ShouldHaveAuthenticate(); + Assert.IsNotNull(result.Result); + Assert.IsInstanceOf(result.Result); + } + } +}