Skip to content

Commit

Permalink
test: add AuthenticationController unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Aug 3, 2021
1 parent 634eaa8 commit 4d6ded9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
4 changes: 0 additions & 4 deletions test/Armory.Api.Test/Armory.Api.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\ArmoryUsers\Authentication" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Armory.Api\Armory.Api.csproj" />
<ProjectReference Include="..\Shared.Test\Shared.Test.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AuthenticateCommand>(), CancellationToken.None),
Times.AtLeastOnce());
}

[Test, Order(1)]
public async Task Authenticate_With_Valid_Username_And_Password()
{
Mediator.Setup(x => x.Send(It.IsAny<GenerateJwtQuery>(), 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<OkObjectResult>(result.Result);

var okResult = result.Result as OkObjectResult;
Assert.IsNotNull(okResult);
Assert.IsInstanceOf<string>(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<AuthenticateCommand>(), CancellationToken.None))
.Throws<ArmoryUserNotAuthenticate>();

var result = await _controller.Authenticate(new AuthenticationRequest
{
UsernameOrEmail = "admin",
Password = "wrong",
IsPersistent = true
});

ShouldHaveAuthenticate();
Assert.IsNotNull(result.Result);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test, Order(3)]
public async Task Authenticate_With_Invalid_Username()
{
Mediator.Setup(x => x.Send(It.IsAny<AuthenticateCommand>(), CancellationToken.None))
.Throws<ArmoryUserNotFound>();

var result = await _controller.Authenticate(new AuthenticationRequest
{
UsernameOrEmail = "none",
Password = "wrong",
IsPersistent = true
});

ShouldHaveAuthenticate();
Assert.IsNotNull(result.Result);
Assert.IsInstanceOf<NotFoundObjectResult>(result.Result);
}
}
}

0 comments on commit 4d6ded9

Please sign in to comment.