feat: wip on testing login flows

This commit is contained in:
Stevan Freeborn
2026-02-01 08:07:23 -06:00
parent 725392227e
commit 872eb3e95d
12 changed files with 284 additions and 47 deletions
+14 -6
View File
@@ -27,12 +27,20 @@ internal sealed class AppDbContext(IOptions<AppDbContextOptions> ctxOptions) : D
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.Property(static u => u.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<User>(static eb =>
{
eb.HasMany(static u => u.RefreshTokens)
.WithOne(static t => t.User)
.HasForeignKey(static t => t.UserId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<RefreshToken>()
.Property(static t => t.Id)
.ValueGeneratedOnAdd();
eb.Property(static u => u.Id).ValueGeneratedOnAdd();
eb.Property(static u => u.Username);
});
modelBuilder.Entity<RefreshToken>(static eb =>
{
eb.Property(static t => t.Id);
});
}
}
@@ -3,4 +3,7 @@ namespace FiscalOS.API.Identity;
internal sealed class RefreshToken
{
public Guid Id { get; init; }
public Guid UserId { get; init; }
public User User { get; init; } = new();
}
+3
View File
@@ -3,4 +3,7 @@ namespace FiscalOS.API.Identity;
internal sealed class User
{
public Guid Id { get; init; }
public string Username { get; init; } = string.Empty;
public ICollection<RefreshToken> RefreshTokens { get; init; } = [];
}
+11 -1
View File
@@ -9,7 +9,10 @@ internal static class Endpoint
return app.MapPost(Route, HandleAsync);
}
private static async Task<IResult> HandleAsync([FromBody] LoginRequest loginRequest)
private static async Task<IResult> HandleAsync(
[FromBody] LoginRequest loginRequest,
[FromServices] AppDbContext appDbContext
)
{
const string ADMIN_USERNAME = "Stevan";
const string ADMIN_PASSWORD = "@Password1";
@@ -30,6 +33,13 @@ internal static class Endpoint
// 1. We need a user model
// 2. We need a refresh token model
var user = await appDbContext.Users.SingleOrDefaultAsync(u => u.Username == loginRequest.Username);
if (user is null)
{
return Results.Unauthorized();
}
if (loginRequest.Username is not ADMIN_USERNAME || loginRequest.Password is not ADMIN_PASSWORD)
{
return Results.Unauthorized();
@@ -5,43 +5,43 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace FiscalOS.API.Migrations
{
/// <inheritdoc />
public partial class AddUsersAndRefreshTokens : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
public partial class AddUsersAndRefreshTokens : Migration
{
migrationBuilder.CreateTable(
name: "RefreshTokens",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_RefreshTokens", x => x.Id);
});
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "RefreshTokens",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_RefreshTokens", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "RefreshTokens");
migrationBuilder.DropTable(
name: "Users");
}
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "RefreshTokens");
migrationBuilder.DropTable(
name: "Users");
}
}
}
@@ -0,0 +1,72 @@
// <auto-generated />
using System;
using FiscalOS.API.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FiscalOS.API.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260201125529_AddUsernameToUsers")]
partial class AddUsernameToUsers
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.2");
modelBuilder.Entity("FiscalOS.API.Identity.RefreshToken", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<Guid>("UserId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("RefreshTokens");
});
modelBuilder.Entity("FiscalOS.API.Identity.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("FiscalOS.API.Identity.RefreshToken", b =>
{
b.HasOne("FiscalOS.API.Identity.User", "User")
.WithMany("RefreshTokens")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("FiscalOS.API.Identity.User", b =>
{
b.Navigation("RefreshTokens");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,62 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FiscalOS.API.Migrations
{
/// <inheritdoc />
public partial class AddUsernameToUsers : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Username",
table: "Users",
type: "TEXT",
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<Guid>(
name: "UserId",
table: "RefreshTokens",
type: "TEXT",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
migrationBuilder.CreateIndex(
name: "IX_RefreshTokens_UserId",
table: "RefreshTokens",
column: "UserId");
migrationBuilder.AddForeignKey(
name: "FK_RefreshTokens_Users_UserId",
table: "RefreshTokens",
column: "UserId",
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_RefreshTokens_Users_UserId",
table: "RefreshTokens");
migrationBuilder.DropIndex(
name: "IX_RefreshTokens_UserId",
table: "RefreshTokens");
migrationBuilder.DropColumn(
name: "Username",
table: "Users");
migrationBuilder.DropColumn(
name: "UserId",
table: "RefreshTokens");
}
}
}
@@ -23,8 +23,13 @@ namespace FiscalOS.API.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<Guid>("UserId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("RefreshTokens");
});
@@ -34,10 +39,30 @@ namespace FiscalOS.API.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("FiscalOS.API.Identity.RefreshToken", b =>
{
b.HasOne("FiscalOS.API.Identity.User", "User")
.WithMany("RefreshTokens")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("FiscalOS.API.Identity.User", b =>
{
b.Navigation("RefreshTokens");
});
#pragma warning restore 612, 618
}
}
+1
View File
@@ -1,5 +1,6 @@
global using System.ComponentModel.DataAnnotations;
global using FiscalOS.API.Data;
global using FiscalOS.API.Identity;
global using FiscalOS.API.Login;
+16
View File
@@ -1,3 +1,9 @@
using FiscalOS.API.Data;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace FiscalOS.API.Tests.Infra;
public class TestApi : WebApplicationFactory<Program>
@@ -7,5 +13,15 @@ public class TestApi : WebApplicationFactory<Program>
base.ConfigureWebHost(builder);
builder.ConfigureLogging(static c => c.ClearProviders());
builder.ConfigureTestServices(static c =>
{
var opts = Options.Create(new AppDbContextOptions()
{
DatabaseFilePath = $"{Guid.NewGuid()}.db",
});
c.AddSingleton(opts);
});
}
}
@@ -0,0 +1,39 @@
using FiscalOS.API.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace FiscalOS.API.Tests.Integration;
public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>, IAsyncLifetime
{
protected TestApi TestApi { get; } = testApi;
// TODO: This seems not correct
// would need to dispose of scope
// we need to clean up the test
// database files when the test ends
protected DbContext TestDbContext
{
get
{
var scopeFactory = TestApi.Services.GetRequiredService<IServiceScopeFactory>();
var scope = scopeFactory.CreateScope();
return scope.ServiceProvider.GetRequiredService<AppDbContext>();
}
}
public ValueTask InitializeAsync()
{
return new(TestDbContext.Database.EnsureCreatedAsync());
}
public ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
return new(TestDbContext.Database.EnsureDeletedAsync());
}
}
@@ -1,14 +1,12 @@
namespace FiscalOS.API.Tests.Integration;
public class LoginTests(TestApi testApi) : IClassFixture<TestApi>
public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
{
private readonly TestApi _testApi = testApi;
[Theory]
[ClassData<LoginValidationTestCases>]
public async Task Login_WhenUserSubmitsInvalidRequest_ItShouldReturn400WithProblemDetails(LoginValidationTestCase tc)
{
var client = _testApi.CreateClient();
var client = TestApi.CreateClient();
var req = new
{
@@ -28,7 +26,7 @@ public class LoginTests(TestApi testApi) : IClassFixture<TestApi>
[Fact]
public async Task Login_WhenUserCredentialsAreIncorrect_ItShouldReturn401WithProblemDetails()
{
var client = _testApi.CreateClient();
var client = TestApi.CreateClient();
var req = new
{