feat(ai-character): 관리자 인증 셸 구현
This commit is contained in:
107
src/features/auth/tests/auth-api.test.ts
Normal file
107
src/features/auth/tests/auth-api.test.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { login, logout } from "@/features/auth/api/auth-api";
|
||||
import { createApiClient } from "@/shared/api/client";
|
||||
import { server } from "@/shared/test/server";
|
||||
|
||||
const apiBaseUrl = "https://api.example.com";
|
||||
|
||||
function createClient(token: string | null = "header.payload.signature") {
|
||||
return createApiClient({
|
||||
getToken: () => token,
|
||||
clearSession: vi.fn(),
|
||||
onAuthExpired: vi.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe("auth API", () => {
|
||||
test("posts email and password JSON only to admin login without Authorization", async () => {
|
||||
// Given
|
||||
vi.stubEnv("VITE_API_BASE_URL", apiBaseUrl);
|
||||
const client = createClient("caller-token");
|
||||
const observedRequests: string[] = [];
|
||||
let authorization: string | null = null;
|
||||
let contentType: string | null = null;
|
||||
let body: unknown = null;
|
||||
server.use(
|
||||
http.post(`${apiBaseUrl}/admin/member/login`, async ({ request }) => {
|
||||
observedRequests.push(new URL(request.url).pathname);
|
||||
authorization = request.headers.get("Authorization");
|
||||
contentType = request.headers.get("Content-Type");
|
||||
body = await request.json();
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
message: null,
|
||||
data: { token: "jwt-token", role: "ADMIN" },
|
||||
errorProperty: null,
|
||||
});
|
||||
}),
|
||||
http.all(`${apiBaseUrl}/refresh`, ({ request }) => {
|
||||
observedRequests.push(new URL(request.url).pathname);
|
||||
return HttpResponse.json({ success: false, message: "unexpected", data: null }, { status: 500 });
|
||||
}),
|
||||
);
|
||||
|
||||
// When
|
||||
const session = await login(client, { email: "admin@test.com", password: "password" });
|
||||
|
||||
// Then
|
||||
expect(session).toEqual({ token: "jwt-token", role: "ADMIN" });
|
||||
expect(authorization).toBeNull();
|
||||
expect(contentType).toContain("application/json");
|
||||
expect(body).toEqual({ email: "admin@test.com", password: "password" });
|
||||
expect(observedRequests).toEqual(["/admin/member/login"]);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ name: "empty token", data: { token: "", role: "ADMIN" } },
|
||||
{ name: "missing token", data: { role: "ADMIN" } },
|
||||
{ name: "missing role", data: { token: "jwt-token" } },
|
||||
{ name: "non ADMIN role", data: { token: "jwt-token", role: "USER" } },
|
||||
])("rejects $name login response", async ({ data }) => {
|
||||
// Given
|
||||
vi.stubEnv("VITE_API_BASE_URL", apiBaseUrl);
|
||||
const client = createClient();
|
||||
server.use(
|
||||
http.post(`${apiBaseUrl}/admin/member/login`, () =>
|
||||
HttpResponse.json({ success: true, message: null, data, errorProperty: null }),
|
||||
),
|
||||
);
|
||||
|
||||
// When
|
||||
const request = login(client, { email: "admin@test.com", password: "password" });
|
||||
|
||||
// Then
|
||||
await expect(request).rejects.toThrow("API 응답 형식이 올바르지 않습니다.");
|
||||
});
|
||||
|
||||
test("posts logout once with Bearer and no body", async () => {
|
||||
// Given
|
||||
vi.stubEnv("VITE_API_BASE_URL", apiBaseUrl);
|
||||
const client = createClient("logout-token");
|
||||
let calls = 0;
|
||||
let authorization: string | null = null;
|
||||
let body = "not-read";
|
||||
server.use(
|
||||
http.post(`${apiBaseUrl}/member/logout`, async ({ request }) => {
|
||||
calls += 1;
|
||||
authorization = request.headers.get("Authorization");
|
||||
body = await request.text();
|
||||
return HttpResponse.json({ success: true, message: null, data: {}, errorProperty: null });
|
||||
}),
|
||||
);
|
||||
|
||||
// When
|
||||
await logout(client);
|
||||
|
||||
// Then
|
||||
expect(calls).toBe(1);
|
||||
expect(authorization).toBe("Bearer logout-token");
|
||||
expect(body).toBe("");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user