feat(auth): add sign-in/sign-up/sign-out controllers with Zod validation

This commit is contained in:
2026-05-05 07:19:19 +02:00
parent 2ad6b21f17
commit 29dea75a62
6 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
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 { InputParseError } from "@/entities/errors";
import { signInController } from "./sign-in.controller";
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({
username: "alice",
password: "password_alice",
});
expect(cookie.name).toBe("session");
});
it("throws InputParseError on missing username", async () => {
await expect(
signInController({ password: "anything" }),
).rejects.toBeInstanceOf(InputParseError);
});
it("throws InputParseError on too-short password", async () => {
await expect(
signInController({ username: "alice", password: "abc" }),
).rejects.toBeInstanceOf(InputParseError);
});
});