feat(core): add auth use cases with tests (sign-in, sign-up, sign-out)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import "reflect-metadata";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
destroyContainer,
|
||||
initializeContainer,
|
||||
} from "@/di/container.js";
|
||||
import { signInUseCase } from "@/application/use-cases/auth/sign-in.use-case.js";
|
||||
import { AuthenticationError } from "@/entities/errors/auth.js";
|
||||
|
||||
beforeEach(() => {
|
||||
initializeContainer();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
destroyContainer();
|
||||
});
|
||||
|
||||
describe("signInUseCase", () => {
|
||||
it("returns session and cookie for valid credentials", async () => {
|
||||
const result = await signInUseCase({
|
||||
username: "alice",
|
||||
password: "password_alice",
|
||||
});
|
||||
expect(result).toHaveProperty("session");
|
||||
expect(result).toHaveProperty("cookie");
|
||||
expect(result.session.userId).toBe("1");
|
||||
});
|
||||
|
||||
it("throws AuthenticationError for non-existing user", async () => {
|
||||
await expect(
|
||||
signInUseCase({ username: "non-existing", password: "any" })
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
|
||||
it("throws AuthenticationError for wrong password", async () => {
|
||||
await expect(
|
||||
signInUseCase({ username: "alice", password: "wrong" })
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import "reflect-metadata";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
destroyContainer,
|
||||
initializeContainer,
|
||||
} from "@/di/container.js";
|
||||
import { signOutUseCase } from "@/application/use-cases/auth/sign-out.use-case.js";
|
||||
|
||||
beforeEach(() => {
|
||||
initializeContainer();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
destroyContainer();
|
||||
});
|
||||
|
||||
describe("signOutUseCase", () => {
|
||||
it("returns a blank cookie", async () => {
|
||||
const result = await signOutUseCase("some-session-id");
|
||||
expect(result).toHaveProperty("blankCookie");
|
||||
expect(result.blankCookie.value).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import "reflect-metadata";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
destroyContainer,
|
||||
initializeContainer,
|
||||
} from "@/di/container.js";
|
||||
import { signUpUseCase } from "@/application/use-cases/auth/sign-up.use-case.js";
|
||||
import { AuthenticationError } from "@/entities/errors/auth.js";
|
||||
|
||||
beforeEach(() => {
|
||||
initializeContainer();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
destroyContainer();
|
||||
});
|
||||
|
||||
describe("signUpUseCase", () => {
|
||||
it("creates user and returns session, cookie, and user info", async () => {
|
||||
const result = await signUpUseCase({
|
||||
username: "newuser",
|
||||
password: "securepassword",
|
||||
});
|
||||
expect(result).toHaveProperty("session");
|
||||
expect(result).toHaveProperty("cookie");
|
||||
expect(result).toHaveProperty("user");
|
||||
expect(result.user.username).toBe("newuser");
|
||||
});
|
||||
|
||||
it("throws AuthenticationError if username is taken", async () => {
|
||||
await expect(
|
||||
signUpUseCase({ username: "alice", password: "anypassword" })
|
||||
).rejects.toBeInstanceOf(AuthenticationError);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user