refactor(auth): unify use-case I/O schemas + presenter + feature error map
Per Plan 9 (spec R1-R28): - Use cases: input + output schemas (signIn, signUp); input-only for signOut (void output). Use case body validates output via outputSchema.parse before returning. - Controllers: receive `unknown`; safeParse with the use-case schema; presenter (returning cookie) for signIn/signUp; void return for signOut. - New integrations/api/procedures.ts with authProcedure built via defineErrorMiddleware([[InputParseError,"BAD_REQUEST"], [AuthenticationError,"UNAUTHORIZED"], [UnauthenticatedError, "UNAUTHORIZED"], [UnauthorizedError,"FORBIDDEN"]]). - Router uses authProcedure + .input(xInputSchema) for every procedure. - src/index.ts exports schemas + types + IUseCase/IController aliases. - package.json gains ./ui subpath; src/ui/index.ts placeholder (auth has no query builders today). - New tests: R25 output-validation per use case (signIn, signUp); R26 router error-mapping (UNAUTHORIZED on missing user, BAD_REQUEST on schema fail). Refactor log: §1, §2, §3.1, §3.2, §3.3, §5.1, §5.2, §6.1, §6.2 Spec: R1–R6, R8–R15, R18, R19, R22–R26
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { signInUseCase } from "@/application/use-cases/sign-in.use-case";
|
||||
import { signInUseCase, signInOutputSchema } 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 { AuthenticationError } from "@/entities/errors/auth";
|
||||
import type { IAuthenticationService } from "@/application/services/authentication.service.interface";
|
||||
import { userFactory } from "@/__factories__/user.factory";
|
||||
|
||||
describe("signInUseCase", () => {
|
||||
@@ -45,3 +46,29 @@ describe("signInUseCase", () => {
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
});
|
||||
|
||||
describe("signInUseCase output validation (R25)", () => {
|
||||
it("throws when authenticationService returns a malformed session", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const seed = userFactory.build({ username: "alice" });
|
||||
await users.createUser(seed);
|
||||
|
||||
const auth = {
|
||||
verifyPassword: async () => true,
|
||||
// session missing required fields → should fail signInOutputSchema.parse
|
||||
createSession: async () => ({ session: { id: 123 }, cookie: null }),
|
||||
} as unknown as IAuthenticationService;
|
||||
|
||||
const useCase = signInUseCase(users, auth);
|
||||
await expect(useCase({ username: "alice", password: "x" })).rejects.toThrow(/parse|invalid/i);
|
||||
});
|
||||
|
||||
it("exports an output schema that mirrors the success shape", () => {
|
||||
expect(signInOutputSchema).toBeDefined();
|
||||
const parsed = signInOutputSchema.safeParse({
|
||||
session: { id: "s1", userId: "u1", expiresAt: new Date() },
|
||||
cookie: { name: "session", value: "s1", attributes: {} },
|
||||
});
|
||||
expect(parsed.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user