feat(auth): add signInUseCase (test red until DI lands)

This commit is contained in:
2026-05-05 00:39:11 +02:00
parent 95e80ef62c
commit bc430ea5a0
2 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import { beforeEach, describe, expect, it } from "vitest";
import { authContainer } from "@/di/container";
import { AUTH_SYMBOLS } from "@/di/symbols";
import { MockUsersRepository } from "@/infrastructure/repositories/mock-users.repository";
import { MockAuthenticationService } from "@/infrastructure/services/mock-authentication.service";
import type { IUsersRepository } from "@/application/repositories/users-repository.interface";
import type { IAuthenticationService } from "@/application/services/authentication-service.interface";
import { AuthenticationError } from "@/entities/errors";
import { signInUseCase } from "./sign-in.use-case";
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({
username: "alice",
password: "password_alice",
});
expect(result.session.userId).toBe("1");
expect(result.cookie.name).toBe("session");
});
it("throws AuthenticationError when user does not exist", async () => {
await expect(
signInUseCase({ username: "ghost", password: "anything" }),
).rejects.toBeInstanceOf(AuthenticationError);
});
it("throws AuthenticationError on wrong password", async () => {
await expect(
signInUseCase({ username: "alice", password: "wrong" }),
).rejects.toBeInstanceOf(AuthenticationError);
});
});

View File

@@ -0,0 +1,34 @@
import { AuthenticationError } from "../../entities/errors";
import type { Cookie } from "../../entities/cookie";
import type { Session } from "../../entities/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,
);
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");
}
return await authService.createSession(existingUser);
}