feat(auth): signUp publishes userSignedUpEvent
signUpUseCase now takes an IEventBus and publishes userSignedUpEvent after creating the user (synthesizing email from username since auth is username-based). Use case mocks-default in module.ts get a fresh InMemoryEventBus per resolution; bind-production / bind-dev-seed wire the shared bus passed by bindAll. Tests updated to inject RecordingEventBus, including a new test that asserts publish on success and silence on failure.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { ZodError } from "zod";
|
||||
import { RecordingEventBus } from "@repo/core-testing/instrumentation";
|
||||
import { signUpUseCase, signUpOutputSchema } from "@/application/use-cases/sign-up.use-case";
|
||||
import { MockUsersRepository } from "@/infrastructure/repositories/users.repository.mock";
|
||||
import { MockAuthenticationService } from "@/infrastructure/services/authentication.service.mock";
|
||||
@@ -11,7 +12,8 @@ describe("signUpUseCase", () => {
|
||||
it("creates a new user and returns session + cookie", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const bus = new RecordingEventBus();
|
||||
const useCase = signUpUseCase(users, auth, bus);
|
||||
|
||||
const result = await useCase({
|
||||
username: "carol",
|
||||
@@ -26,13 +28,50 @@ describe("signUpUseCase", () => {
|
||||
it("throws AuthenticationError when username taken", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const bus = new RecordingEventBus();
|
||||
await users.createUser(userFactory.build({ username: "alice" }));
|
||||
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const useCase = signUpUseCase(users, auth, bus);
|
||||
await expect(
|
||||
useCase({ username: "alice", password: "secret_password", confirmPassword: "secret_password" }),
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
|
||||
it("publishes auth.user.signed-up after creating the user", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const bus = new RecordingEventBus();
|
||||
const useCase = signUpUseCase(users, auth, bus);
|
||||
|
||||
await useCase({
|
||||
username: "dave",
|
||||
password: "secret_password",
|
||||
confirmPassword: "secret_password",
|
||||
});
|
||||
|
||||
expect(bus.published).toHaveLength(1);
|
||||
const published = bus.published[0]!;
|
||||
expect(published.name).toBe("auth.user.signed-up");
|
||||
expect(published.payload).toEqual(
|
||||
expect.objectContaining({
|
||||
userId: expect.any(String),
|
||||
email: expect.stringMatching(/^dave@/),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("does NOT publish when sign-up fails (username taken)", async () => {
|
||||
const users = new MockUsersRepository([]);
|
||||
const auth = new MockAuthenticationService(users);
|
||||
const bus = new RecordingEventBus();
|
||||
await users.createUser(userFactory.build({ username: "eve" }));
|
||||
|
||||
const useCase = signUpUseCase(users, auth, bus);
|
||||
await expect(
|
||||
useCase({ username: "eve", password: "secret_password", confirmPassword: "secret_password" }),
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
expect(bus.published).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("signUpUseCase output validation (R25)", () => {
|
||||
@@ -46,7 +85,8 @@ describe("signUpUseCase output validation (R25)", () => {
|
||||
createSession: async () => ({ session: { id: 123 }, cookie: null }),
|
||||
} as unknown as IAuthenticationService;
|
||||
|
||||
const useCase = signUpUseCase(users, auth);
|
||||
const bus = new RecordingEventBus();
|
||||
const useCase = signUpUseCase(users, auth, bus);
|
||||
await expect(
|
||||
useCase({ username: "carol", password: "secret_password", confirmPassword: "secret_password" }),
|
||||
).rejects.toBeInstanceOf(ZodError);
|
||||
|
||||
Reference in New Issue
Block a user