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,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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,34 +1,32 @@
|
||||
import { AuthenticationError } from "../../entities/errors/auth";
|
||||
import type { Cookie } from "../../entities/models/cookie";
|
||||
import type { Session } from "../../entities/models/session";
|
||||
import { authContainer } from "../../di/container";
|
||||
import { AUTH_SYMBOLS } from "../../di/symbols";
|
||||
import type { IUsersRepository } from "../repositories/users.repository.interface";
|
||||
import type { IAuthenticationService } from "../services/authentication.service.interface";
|
||||
|
||||
export async function signInUseCase(input: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{ session: Session; cookie: Cookie }> {
|
||||
const usersRepository = authContainer.get<IUsersRepository>(
|
||||
AUTH_SYMBOLS.IUsersRepository,
|
||||
);
|
||||
const authService = authContainer.get<IAuthenticationService>(
|
||||
AUTH_SYMBOLS.IAuthenticationService,
|
||||
);
|
||||
export type ISignInUseCase = ReturnType<typeof signInUseCase>;
|
||||
|
||||
const existingUser = await usersRepository.getUserByUsername(input.username);
|
||||
if (!existingUser) {
|
||||
throw new AuthenticationError("User does not exist");
|
||||
}
|
||||
export const signInUseCase =
|
||||
(
|
||||
usersRepository: IUsersRepository,
|
||||
authenticationService: IAuthenticationService,
|
||||
) =>
|
||||
async (input: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{ session: Session; cookie: Cookie }> => {
|
||||
const existingUser = await usersRepository.getUserByUsername(input.username);
|
||||
if (!existingUser) {
|
||||
throw new AuthenticationError("User does not exist");
|
||||
}
|
||||
|
||||
const validPassword = await authService.verifyPassword(
|
||||
existingUser.passwordHash,
|
||||
input.password,
|
||||
);
|
||||
if (!validPassword) {
|
||||
throw new AuthenticationError("Incorrect username or password");
|
||||
}
|
||||
const validPassword = await authenticationService.verifyPassword(
|
||||
existingUser.passwordHash,
|
||||
input.password,
|
||||
);
|
||||
if (!validPassword) {
|
||||
throw new AuthenticationError("Incorrect username or password");
|
||||
}
|
||||
|
||||
return await authService.createSession(existingUser);
|
||||
}
|
||||
return await authenticationService.createSession(existingUser);
|
||||
};
|
||||
|
||||
@@ -1,35 +1,15 @@
|
||||
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 { signOutUseCase } from "@/application/use-cases/sign-out.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 { signOutUseCase } from "./sign-out.use-case";
|
||||
|
||||
describe("signOutUseCase", () => {
|
||||
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 blank cookie", async () => {
|
||||
const result = await signOutUseCase("session_1");
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signOutUseCase(auth);
|
||||
|
||||
const result = await useCase("session_1");
|
||||
expect(result.blankCookie.name).toBe("session");
|
||||
expect(result.blankCookie.value).toBe("");
|
||||
});
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import type { Cookie } from "../../entities/models/cookie";
|
||||
import { authContainer } from "../../di/container";
|
||||
import { AUTH_SYMBOLS } from "../../di/symbols";
|
||||
import type { IAuthenticationService } from "../services/authentication.service.interface";
|
||||
|
||||
export async function signOutUseCase(
|
||||
sessionId: string,
|
||||
): Promise<{ blankCookie: Cookie }> {
|
||||
const authService = authContainer.get<IAuthenticationService>(
|
||||
AUTH_SYMBOLS.IAuthenticationService,
|
||||
);
|
||||
return await authService.invalidateSession(sessionId);
|
||||
}
|
||||
export type ISignOutUseCase = ReturnType<typeof signOutUseCase>;
|
||||
|
||||
export const signOutUseCase =
|
||||
(authenticationService: IAuthenticationService) =>
|
||||
async (sessionId: string): Promise<{ blankCookie: Cookie }> => {
|
||||
return await authenticationService.invalidateSession(sessionId);
|
||||
};
|
||||
|
||||
@@ -1,47 +1,34 @@
|
||||
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 { signUpUseCase } from "@/application/use-cases/sign-up.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 { signUpUseCase } from "./sign-up.use-case";
|
||||
import { userFactory } from "@/__factories__/user.factory";
|
||||
|
||||
describe("signUpUseCase", () => {
|
||||
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 and returns session + cookie + user", async () => {
|
||||
const result = await signUpUseCase({
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
|
||||
const result = await useCase({
|
||||
username: "carol",
|
||||
password: "secret_password",
|
||||
});
|
||||
|
||||
expect(result.user.username).toBe("carol");
|
||||
expect(result.session.userId).toBe(result.user.id);
|
||||
expect(result.cookie.name).toBe("session");
|
||||
});
|
||||
|
||||
it("throws AuthenticationError when username taken", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
await users.createUser(userFactory.build({ username: "alice" }));
|
||||
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
await expect(
|
||||
signUpUseCase({ username: "alice", password: "secret_password" }),
|
||||
useCase({ username: "alice", password: "secret_password" }),
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,45 +2,43 @@ import { AuthenticationError } from "../../entities/errors/auth";
|
||||
import type { Cookie } from "../../entities/models/cookie";
|
||||
import type { Session } from "../../entities/models/session";
|
||||
import type { User } from "../../entities/models/user";
|
||||
import { authContainer } from "../../di/container";
|
||||
import { AUTH_SYMBOLS } from "../../di/symbols";
|
||||
import type { IUsersRepository } from "../repositories/users.repository.interface";
|
||||
import type { IAuthenticationService } from "../services/authentication.service.interface";
|
||||
|
||||
export async function signUpUseCase(input: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{
|
||||
session: Session;
|
||||
cookie: Cookie;
|
||||
user: Pick<User, "id" | "username">;
|
||||
}> {
|
||||
const usersRepository = authContainer.get<IUsersRepository>(
|
||||
AUTH_SYMBOLS.IUsersRepository,
|
||||
);
|
||||
const authService = authContainer.get<IAuthenticationService>(
|
||||
AUTH_SYMBOLS.IAuthenticationService,
|
||||
);
|
||||
export type ISignUpUseCase = ReturnType<typeof signUpUseCase>;
|
||||
|
||||
const existingUser = await usersRepository.getUserByUsername(input.username);
|
||||
if (existingUser) {
|
||||
throw new AuthenticationError("Username taken");
|
||||
}
|
||||
export const signUpUseCase =
|
||||
(
|
||||
usersRepository: IUsersRepository,
|
||||
authenticationService: IAuthenticationService,
|
||||
) =>
|
||||
async (input: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<{
|
||||
session: Session;
|
||||
cookie: Cookie;
|
||||
user: Pick<User, "id" | "username">;
|
||||
}> => {
|
||||
const existingUser = await usersRepository.getUserByUsername(input.username);
|
||||
if (existingUser) {
|
||||
throw new AuthenticationError("Username taken");
|
||||
}
|
||||
|
||||
const passwordHash = await authService.hashPassword(input.password);
|
||||
const userId = authService.generateUserId();
|
||||
const passwordHash = await authenticationService.hashPassword(input.password);
|
||||
const userId = authenticationService.generateUserId();
|
||||
|
||||
const newUser = await usersRepository.createUser({
|
||||
id: userId,
|
||||
username: input.username,
|
||||
passwordHash,
|
||||
});
|
||||
const newUser = await usersRepository.createUser({
|
||||
id: userId,
|
||||
username: input.username,
|
||||
passwordHash,
|
||||
});
|
||||
|
||||
const { cookie, session } = await authService.createSession(newUser);
|
||||
const { cookie, session } = await authenticationService.createSession(newUser);
|
||||
|
||||
return {
|
||||
cookie,
|
||||
session,
|
||||
user: { id: newUser.id, username: newUser.username },
|
||||
return {
|
||||
cookie,
|
||||
session,
|
||||
user: { id: newUser.id, username: newUser.username },
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user