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:
@@ -1,51 +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 { signInController } from "@/interface-adapters/controllers/sign-in.controller";
|
||||
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 { signInUseCase } from "@/application/use-cases/sign-in.use-case";
|
||||
import { InputParseError } from "@/entities/errors/common";
|
||||
import { signInController } from "./sign-in.controller";
|
||||
import { userFactory } from "@/__factories__/user.factory";
|
||||
|
||||
describe("signInController", () => {
|
||||
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 cookie on valid credentials", async () => {
|
||||
const cookie = await signInController({
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const seedUser = userFactory.build({
|
||||
username: "alice",
|
||||
password: "password_alice",
|
||||
passwordHash: "hashed_testpassword",
|
||||
});
|
||||
await users.createUser(seedUser);
|
||||
|
||||
const useCase = signInUseCase(users, auth);
|
||||
const controller = signInController(useCase);
|
||||
|
||||
const cookie = await controller({ username: "alice", password: "testpassword" });
|
||||
expect(cookie.name).toBe("session");
|
||||
});
|
||||
|
||||
it("throws InputParseError on missing username", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signInUseCase(users, auth);
|
||||
const controller = signInController(useCase);
|
||||
|
||||
await expect(
|
||||
signInController({ password: "anything" }),
|
||||
controller({ password: "anything" }),
|
||||
).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
|
||||
it("throws InputParseError on too-short password", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signInUseCase(users, auth);
|
||||
const controller = signInController(useCase);
|
||||
|
||||
await expect(
|
||||
signInController({ username: "alice", password: "abc" }),
|
||||
controller({ username: "alice", password: "abc" }),
|
||||
).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,20 +2,22 @@ import { z } from "zod";
|
||||
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
import type { Cookie } from "../../entities/models/cookie";
|
||||
import { signInUseCase } from "../../application/use-cases/sign-in.use-case";
|
||||
import type { ISignInUseCase } from "../../application/use-cases/sign-in.use-case";
|
||||
|
||||
const inputSchema = z.object({
|
||||
username: z.string().min(3).max(31),
|
||||
password: z.string().min(6).max(255),
|
||||
});
|
||||
|
||||
export async function signInController(
|
||||
input: Partial<z.infer<typeof inputSchema>>,
|
||||
): Promise<Cookie> {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid sign-in input", { cause: parsed.error });
|
||||
}
|
||||
const { cookie } = await signInUseCase(parsed.data);
|
||||
return cookie;
|
||||
}
|
||||
export type ISignInController = ReturnType<typeof signInController>;
|
||||
|
||||
export const signInController =
|
||||
(signInUseCase: ISignInUseCase) =>
|
||||
async (input: Partial<z.infer<typeof inputSchema>>): Promise<Cookie> => {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid sign-in input", { cause: parsed.error });
|
||||
}
|
||||
const { cookie } = await signInUseCase(parsed.data);
|
||||
return cookie;
|
||||
};
|
||||
|
||||
@@ -1,40 +1,28 @@
|
||||
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 { signOutController } from "@/interface-adapters/controllers/sign-out.controller";
|
||||
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 { signOutUseCase } from "@/application/use-cases/sign-out.use-case";
|
||||
import { InputParseError } from "@/entities/errors/common";
|
||||
import { signOutController } from "./sign-out.controller";
|
||||
|
||||
describe("signOutController", () => {
|
||||
beforeEach(() => {
|
||||
if (authContainer.isBound(AUTH_SYMBOLS.IUsersRepository)) {
|
||||
authContainer.unbind(AUTH_SYMBOLS.IUsersRepository);
|
||||
}
|
||||
if (authContainer.isBound(AUTH_SYMBOLS.IAuthenticationService)) {
|
||||
authContainer.unbind(AUTH_SYMBOLS.IAuthenticationService);
|
||||
}
|
||||
const usersRepo = new MockUsersRepository();
|
||||
const authService = new MockAuthenticationService(usersRepo);
|
||||
authContainer
|
||||
.bind<IUsersRepository>(AUTH_SYMBOLS.IUsersRepository)
|
||||
.toConstantValue(usersRepo);
|
||||
authContainer
|
||||
.bind<IAuthenticationService>(AUTH_SYMBOLS.IAuthenticationService)
|
||||
.toConstantValue(authService);
|
||||
});
|
||||
|
||||
it("returns a blank cookie", async () => {
|
||||
const cookie = await signOutController("session_anything");
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signOutUseCase(auth);
|
||||
const controller = signOutController(useCase);
|
||||
|
||||
const cookie = await controller("session_anything");
|
||||
expect(cookie.name).toBe("session");
|
||||
expect(cookie.value).toBe("");
|
||||
});
|
||||
|
||||
it("throws InputParseError when sessionId is missing", async () => {
|
||||
await expect(signOutController(undefined)).rejects.toBeInstanceOf(
|
||||
InputParseError,
|
||||
);
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signOutUseCase(auth);
|
||||
const controller = signOutController(useCase);
|
||||
|
||||
await expect(controller(undefined)).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
import type { Cookie } from "../../entities/models/cookie";
|
||||
import { signOutUseCase } from "../../application/use-cases/sign-out.use-case";
|
||||
import type { ISignOutUseCase } from "../../application/use-cases/sign-out.use-case";
|
||||
|
||||
export async function signOutController(
|
||||
sessionId: string | undefined,
|
||||
): Promise<Cookie> {
|
||||
if (!sessionId) {
|
||||
throw new InputParseError("Must provide a session ID");
|
||||
}
|
||||
const { blankCookie } = await signOutUseCase(sessionId);
|
||||
return blankCookie;
|
||||
}
|
||||
export type ISignOutController = ReturnType<typeof signOutController>;
|
||||
|
||||
export const signOutController =
|
||||
(signOutUseCase: ISignOutUseCase) =>
|
||||
async (sessionId: string | undefined): Promise<Cookie> => {
|
||||
if (!sessionId) {
|
||||
throw new InputParseError("Must provide a session ID");
|
||||
}
|
||||
const { blankCookie } = await signOutUseCase(sessionId);
|
||||
return blankCookie;
|
||||
};
|
||||
|
||||
@@ -1,36 +1,19 @@
|
||||
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 { signUpController } from "@/interface-adapters/controllers/sign-up.controller";
|
||||
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 { signUpUseCase } from "@/application/use-cases/sign-up.use-case";
|
||||
import { InputParseError } from "@/entities/errors/common";
|
||||
import { signUpController } from "./sign-up.controller";
|
||||
import { userFactory } from "@/__factories__/user.factory";
|
||||
|
||||
describe("signUpController", () => {
|
||||
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("creates a new user when passwords match", async () => {
|
||||
const result = await signUpController({
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const controller = signUpController(useCase);
|
||||
|
||||
const result = await controller({
|
||||
username: "carol",
|
||||
password: "secret_password",
|
||||
confirmPassword: "secret_password",
|
||||
@@ -39,12 +22,30 @@ describe("signUpController", () => {
|
||||
});
|
||||
|
||||
it("throws InputParseError when passwords do not match", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const controller = signUpController(useCase);
|
||||
|
||||
await expect(
|
||||
signUpController({
|
||||
controller({
|
||||
username: "dave",
|
||||
password: "secret_password",
|
||||
confirmPassword: "different_password",
|
||||
}),
|
||||
).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
|
||||
it("throws InputParseError when username is too short", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
// Pre-seed so we don't hit the taken-username error
|
||||
await users.createUser(userFactory.build({ username: "alice" }));
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const controller = signUpController(useCase);
|
||||
|
||||
await expect(
|
||||
controller({ username: "ab", password: "secret_password", confirmPassword: "secret_password" }),
|
||||
).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
import { signUpUseCase } from "../../application/use-cases/sign-up.use-case";
|
||||
import type { ISignUpUseCase } from "../../application/use-cases/sign-up.use-case";
|
||||
|
||||
const inputSchema = z
|
||||
.object({
|
||||
@@ -24,12 +24,16 @@ const inputSchema = z
|
||||
}
|
||||
});
|
||||
|
||||
export async function signUpController(
|
||||
input: Partial<z.infer<typeof inputSchema>>,
|
||||
): Promise<ReturnType<typeof signUpUseCase>> {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid sign-up input", { cause: parsed.error });
|
||||
}
|
||||
return await signUpUseCase(parsed.data);
|
||||
}
|
||||
export type ISignUpController = ReturnType<typeof signUpController>;
|
||||
|
||||
export const signUpController =
|
||||
(signUpUseCase: ISignUpUseCase) =>
|
||||
async (
|
||||
input: Partial<z.infer<typeof inputSchema>>,
|
||||
): Promise<Awaited<ReturnType<ISignUpUseCase>>> => {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid sign-up input", { cause: parsed.error });
|
||||
}
|
||||
return await signUpUseCase(parsed.data);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user