|
| 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 | +namespace UserManagementApi.Tests.Unit |
| 12 | +{ |
| 13 | + public class LoginControllerTests |
| 14 | + { |
| 15 | + private static AppDbContext NewDb(string name) |
| 16 | + { |
| 17 | + var opts = new DbContextOptionsBuilder<AppDbContext>() |
| 18 | + .UseInMemoryDatabase(name) |
| 19 | + .Options; |
| 20 | + return new AppDbContext(opts); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task Authenticate_WhenMissingUsernameOrPassword_Returns400WithValidationProblem() |
| 25 | + { |
| 26 | + using var db = NewDb(nameof(Authenticate_WhenMissingUsernameOrPassword_Returns400WithValidationProblem)); |
| 27 | + var jwtOptions = new JwtOptions |
| 28 | + { |
| 29 | + Issuer = "test-issuer", |
| 30 | + Audience = "test-audience", |
| 31 | + Key = "supersecretkey1234567890" |
| 32 | + }; |
| 33 | + var wrapped = Options.Create(jwtOptions); // returns IOptions<JwtOptions> |
| 34 | + |
| 35 | + var controller = new TestableLoginController(db, wrapped); |
| 36 | + |
| 37 | + var res = await controller.Authenticate(new LoginRequest("", "")); |
| 38 | + |
| 39 | + var bad = res.Result.Should().BeOfType<BadRequestObjectResult>().Which; |
| 40 | + bad.StatusCode.Should().Be(StatusCodes.Status400BadRequest); |
| 41 | + bad.Value.Should().BeOfType<ValidationProblemDetails>(); |
| 42 | + var v = (ValidationProblemDetails)bad.Value!; |
| 43 | + v.Errors.Should().ContainKey("userName"); |
| 44 | + v.Errors.Should().ContainKey("password"); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments