refactor(auth): factory-style use cases + controllers + real Payload impls

- Use cases (sign-in, sign-up, sign-out) → factory functions with I*UseCase aliases
- Controllers → factory functions with I*Controller aliases
- DI symbols + module updated with .toDynamicValue() bindings for factories
- New: real UsersRepository (Payload-backed, SanitizedConfig, contract-tested)
- New: real AuthenticationService (node:crypto hashing/UUIDs; createSession/
  validateSession/invalidateSession deferred — see refactor log §7)
- bindProductionAuth swaps both mocks for real impls (was a no-op before)
- Tests refactored to construct mocks and inject directly (no container rebinding)
- Feature test constructs full chain via direct factory injection

Refactor log: §2, §4.1, §4.2, §5.1, §5.2, §6.1, §7
Spec: §6.1, §7

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-06 00:01:11 +02:00
parent aa325f91cc
commit 780d5cb83b
24 changed files with 694 additions and 349 deletions

View File

@@ -1,52 +1,47 @@
import { beforeEach, describe, expect, it } from "vitest";
import { authContainer } from "@/di/container";
import { AUTH_SYMBOLS } from "@/di/symbols";
import { describe, it, expect } from "vitest";
import { signInUseCase } from "@/application/use-cases/sign-in.use-case";
import { MockUsersRepository } from "@/infrastructure/repositories/users.repository.mock";
import { MockAuthenticationService } from "@/infrastructure/services/authentication.service.mock";
import type { IUsersRepository } from "@/application/repositories/users.repository.interface";
import type { IAuthenticationService } from "@/application/services/authentication.service.interface";
import { AuthenticationError } from "@/entities/errors/auth";
import { signInUseCase } from "./sign-in.use-case";
import { userFactory } from "@/__factories__/user.factory";
describe("signInUseCase", () => {
let usersRepo: MockUsersRepository;
let authService: MockAuthenticationService;
beforeEach(() => {
if (authContainer.isBound(AUTH_SYMBOLS.IUsersRepository)) {
authContainer.unbind(AUTH_SYMBOLS.IUsersRepository);
}
if (authContainer.isBound(AUTH_SYMBOLS.IAuthenticationService)) {
authContainer.unbind(AUTH_SYMBOLS.IAuthenticationService);
}
usersRepo = new MockUsersRepository();
authService = new MockAuthenticationService(usersRepo);
authContainer
.bind<IUsersRepository>(AUTH_SYMBOLS.IUsersRepository)
.toConstantValue(usersRepo);
authContainer
.bind<IAuthenticationService>(AUTH_SYMBOLS.IAuthenticationService)
.toConstantValue(authService);
});
it("returns a session + cookie on valid credentials", async () => {
const result = await signInUseCase({
const users = new MockUsersRepository([]);
const auth = new MockAuthenticationService(users);
const seedUser = userFactory.build({
username: "alice",
password: "password_alice",
passwordHash: "hashed_testpassword",
});
expect(result.session.userId).toBe("1");
await users.createUser(seedUser);
const useCase = signInUseCase(users, auth);
const result = await useCase({ username: "alice", password: "testpassword" });
expect(result.session.userId).toBe(seedUser.id);
expect(result.cookie.name).toBe("session");
});
it("throws AuthenticationError when user does not exist", async () => {
const users = new MockUsersRepository([]);
const auth = new MockAuthenticationService(users);
const useCase = signInUseCase(users, auth);
await expect(
signInUseCase({ username: "ghost", password: "anything" }),
useCase({ username: "ghost", password: "anything" }),
).rejects.toBeInstanceOf(AuthenticationError);
});
it("throws AuthenticationError on wrong password", async () => {
const users = new MockUsersRepository([]);
const auth = new MockAuthenticationService(users);
await users.createUser(
userFactory.build({ username: "alice", passwordHash: "hashed_correctpassword" }),
);
const useCase = signInUseCase(users, auth);
await expect(
signInUseCase({ username: "alice", password: "wrong" }),
useCase({ username: "alice", password: "wrong" }),
).rejects.toBeInstanceOf(AuthenticationError);
});
});