Skip to content

Commit 15fb749

Browse files
test(auth): add xUnit tests for Authenticate endpoint (400)
1 parent 8cf2030 commit 15fb749

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using FluentAssertions;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Options;
6+
using Moq;
7+
using UserManagementApi.Data;
8+
using UserManagementApi.DTO;
9+
using UserManagementApi.DTO.Auth;
10+
11+
12+
//using FluentAssertions;
13+
//using Microsoft.AspNetCore.Http;
14+
//using Microsoft.AspNetCore.Mvc;
15+
//using Microsoft.EntityFrameworkCore;
16+
using Xunit;
17+
18+
namespace UserManagementApi.Tests.Unit
19+
{
20+
public class LoginControllerTests
21+
{
22+
private static AppDbContext NewDb(string name)
23+
{
24+
var opts = new DbContextOptionsBuilder<AppDbContext>()
25+
.UseInMemoryDatabase(name)
26+
.Options;
27+
return new AppDbContext(opts);
28+
}
29+
30+
[Fact]
31+
public async Task Authenticate_WhenMissingUsernameOrPassword_Returns400WithValidationProblem()
32+
{
33+
using var db = NewDb(nameof(Authenticate_WhenMissingUsernameOrPassword_Returns400WithValidationProblem));
34+
var jwtOptions = new JwtOptions
35+
{
36+
Issuer = "test-issuer",
37+
Audience = "test-audience",
38+
Key = "supersecretkey1234567890"
39+
};
40+
var wrapped = Options.Create(jwtOptions); // returns IOptions<JwtOptions>
41+
42+
var controller = new TestableLoginController(db, wrapped);
43+
44+
var res = await controller.Authenticate(new LoginRequest("", ""));
45+
46+
var bad = res.Result.Should().BeOfType<BadRequestObjectResult>().Which;
47+
bad.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
48+
bad.Value.Should().BeOfType<ValidationProblemDetails>();
49+
var v = (ValidationProblemDetails)bad.Value!;
50+
v.Errors.Should().ContainKey("userName");
51+
v.Errors.Should().ContainKey("password");
52+
}
53+
}
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Extensions.Options;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using UserManagementApi.Contracts.Models;
8+
using UserManagementApi.Controllers;
9+
using UserManagementApi.Data;
10+
using UserManagementApi.DTO;
11+
using UserManagementApi.DTO.Auth;
12+
13+
namespace UserManagementApi.Tests.Unit
14+
{
15+
public sealed class TestableLoginController :UsersController
16+
{
17+
private readonly string _jwtToReturn;
18+
private readonly DateTime _expToReturn;
19+
private readonly UserPermissionsDto _permToReturn;
20+
21+
public TestableLoginController(AppDbContext db,
22+
IOptions<JwtOptions> jwtopts)
23+
: base(db, jwtopts)
24+
{
25+
_jwtToReturn = "fake-jwt";
26+
_expToReturn = DateTime.UtcNow.AddHours(1);
27+
_permToReturn = new UserPermissionsDto(0, "abc", new List<CategoryDto>());
28+
29+
}
30+
protected override Task<UserPermissionsDto> BuildPermissionsForUser(int userId)
31+
=> Task.FromResult(_permToReturn);
32+
33+
protected override string GenerateJwt(AppUser user, List<CategoryDto> Categories, out DateTime expiresAtUtc)
34+
{
35+
expiresAtUtc = _expToReturn;
36+
return _jwtToReturn;
37+
}
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
12+
<PackageReference Include="FluentAssertions" Version="8.7.1" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.9" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
15+
<PackageReference Include="Moq" Version="4.20.72" />
16+
<PackageReference Include="xunit" Version="2.9.2" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\UserManagement.Contracts\UserManagement.Contracts.csproj" />
22+
<ProjectReference Include="..\UserManagementApi\UserManagementApi.csproj" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Using Include="Xunit" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)