login-regression-matrix.test.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { NextRequest } from "next/server";
  3. const mockValidateKey = vi.hoisted(() => vi.fn());
  4. const mockSetAuthCookie = vi.hoisted(() => vi.fn());
  5. const mockGetSessionTokenMode = vi.hoisted(() => vi.fn());
  6. const mockGetLoginRedirectTarget = vi.hoisted(() => vi.fn());
  7. const mockGetTranslations = vi.hoisted(() => vi.fn());
  8. const mockGetEnvConfig = vi.hoisted(() => vi.fn());
  9. const mockLogger = vi.hoisted(() => ({
  10. warn: vi.fn(),
  11. error: vi.fn(),
  12. info: vi.fn(),
  13. debug: vi.fn(),
  14. }));
  15. vi.mock("@/lib/auth", () => ({
  16. validateKey: mockValidateKey,
  17. setAuthCookie: mockSetAuthCookie,
  18. getSessionTokenMode: mockGetSessionTokenMode,
  19. getLoginRedirectTarget: mockGetLoginRedirectTarget,
  20. toKeyFingerprint: vi.fn().mockResolvedValue("sha256:mock"),
  21. withNoStoreHeaders: (res: any) => {
  22. (res as any).headers.set("Cache-Control", "no-store, no-cache, must-revalidate");
  23. (res as any).headers.set("Pragma", "no-cache");
  24. return res;
  25. },
  26. }));
  27. vi.mock("next-intl/server", () => ({
  28. getTranslations: mockGetTranslations,
  29. }));
  30. vi.mock("@/lib/config/env.schema", () => ({
  31. getEnvConfig: mockGetEnvConfig,
  32. }));
  33. vi.mock("@/lib/logger", () => ({
  34. logger: mockLogger,
  35. }));
  36. vi.mock("@/lib/security/auth-response-headers", () => ({
  37. withAuthResponseHeaders: (res: any) => {
  38. (res as any).headers.set("Cache-Control", "no-store, no-cache, must-revalidate");
  39. (res as any).headers.set("Pragma", "no-cache");
  40. return res;
  41. },
  42. }));
  43. function makeRequest(body: unknown, xForwardedProto = "https"): NextRequest {
  44. return new NextRequest("http://localhost/api/auth/login", {
  45. method: "POST",
  46. headers: {
  47. "Content-Type": "application/json",
  48. "x-forwarded-proto": xForwardedProto,
  49. },
  50. body: JSON.stringify(body),
  51. });
  52. }
  53. const adminSession = {
  54. user: {
  55. id: -1,
  56. name: "Admin Token",
  57. description: "Environment admin session",
  58. role: "admin" as const,
  59. },
  60. key: { canLoginWebUi: true },
  61. };
  62. const dashboardUserSession = {
  63. user: {
  64. id: 1,
  65. name: "Dashboard User",
  66. description: "dashboard",
  67. role: "user" as const,
  68. },
  69. key: { canLoginWebUi: true },
  70. };
  71. const readonlyUserSession = {
  72. user: {
  73. id: 2,
  74. name: "Readonly User",
  75. description: "readonly",
  76. role: "user" as const,
  77. },
  78. key: { canLoginWebUi: false },
  79. };
  80. describe("Login Regression Matrix", () => {
  81. let POST: (request: NextRequest) => Promise<Response>;
  82. beforeEach(async () => {
  83. vi.clearAllMocks();
  84. const mockT = vi.fn((key: string) => `translated:${key}`);
  85. mockGetTranslations.mockResolvedValue(mockT);
  86. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: false });
  87. mockSetAuthCookie.mockResolvedValue(undefined);
  88. mockGetSessionTokenMode.mockReturnValue("legacy");
  89. const mod = await import("@/app/api/auth/login/route");
  90. POST = mod.POST;
  91. });
  92. describe("Success Paths", () => {
  93. it("admin user: redirectTo=/dashboard, loginType=admin", async () => {
  94. mockValidateKey.mockResolvedValue(adminSession);
  95. mockGetLoginRedirectTarget.mockReturnValue("/dashboard");
  96. const res = await POST(makeRequest({ key: "admin-key" }));
  97. expect(res.status).toBe(200);
  98. expect(await res.json()).toEqual({
  99. ok: true,
  100. user: {
  101. id: -1,
  102. name: "Admin Token",
  103. description: "Environment admin session",
  104. role: "admin",
  105. },
  106. redirectTo: "/dashboard",
  107. loginType: "admin",
  108. });
  109. expect(mockSetAuthCookie).toHaveBeenCalledWith("admin-key");
  110. expect(mockGetLoginRedirectTarget).toHaveBeenCalledWith(adminSession);
  111. });
  112. it("dashboard user: redirectTo=/dashboard, loginType=dashboard_user", async () => {
  113. mockValidateKey.mockResolvedValue(dashboardUserSession);
  114. mockGetLoginRedirectTarget.mockReturnValue("/dashboard");
  115. const res = await POST(makeRequest({ key: "dashboard-user-key" }));
  116. expect(res.status).toBe(200);
  117. expect(await res.json()).toEqual({
  118. ok: true,
  119. user: {
  120. id: 1,
  121. name: "Dashboard User",
  122. description: "dashboard",
  123. role: "user",
  124. },
  125. redirectTo: "/dashboard",
  126. loginType: "dashboard_user",
  127. });
  128. expect(mockSetAuthCookie).toHaveBeenCalledWith("dashboard-user-key");
  129. expect(mockGetLoginRedirectTarget).toHaveBeenCalledWith(dashboardUserSession);
  130. });
  131. it("readonly user: redirectTo=/my-usage, loginType=readonly_user", async () => {
  132. mockValidateKey.mockResolvedValue(readonlyUserSession);
  133. mockGetLoginRedirectTarget.mockReturnValue("/my-usage");
  134. const res = await POST(makeRequest({ key: "readonly-user-key" }));
  135. expect(res.status).toBe(200);
  136. expect(await res.json()).toEqual({
  137. ok: true,
  138. user: {
  139. id: 2,
  140. name: "Readonly User",
  141. description: "readonly",
  142. role: "user",
  143. },
  144. redirectTo: "/my-usage",
  145. loginType: "readonly_user",
  146. });
  147. expect(mockSetAuthCookie).toHaveBeenCalledWith("readonly-user-key");
  148. expect(mockGetLoginRedirectTarget).toHaveBeenCalledWith(readonlyUserSession);
  149. });
  150. });
  151. describe("Failure Paths", () => {
  152. it("missing key: 400 + KEY_REQUIRED", async () => {
  153. const res = await POST(makeRequest({}));
  154. expect(res.status).toBe(400);
  155. expect(await res.json()).toEqual({
  156. error: "translated:apiKeyRequired",
  157. errorCode: "KEY_REQUIRED",
  158. });
  159. expect(mockValidateKey).not.toHaveBeenCalled();
  160. expect(mockSetAuthCookie).not.toHaveBeenCalled();
  161. });
  162. it("invalid key: 401 + KEY_INVALID", async () => {
  163. mockValidateKey.mockResolvedValue(null);
  164. const res = await POST(makeRequest({ key: "invalid-key" }));
  165. expect(res.status).toBe(401);
  166. expect(await res.json()).toEqual({
  167. error: "translated:apiKeyInvalidOrExpired",
  168. errorCode: "KEY_INVALID",
  169. });
  170. expect(mockSetAuthCookie).not.toHaveBeenCalled();
  171. });
  172. it("HTTP mismatch: 401 + httpMismatchGuidance", async () => {
  173. mockGetEnvConfig.mockReturnValue({ ENABLE_SECURE_COOKIES: true });
  174. mockValidateKey.mockResolvedValue(null);
  175. const res = await POST(makeRequest({ key: "mismatch-key" }, "http"));
  176. expect(res.status).toBe(401);
  177. expect(await res.json()).toEqual({
  178. error: "translated:apiKeyInvalidOrExpired",
  179. errorCode: "KEY_INVALID",
  180. httpMismatchGuidance: "translated:cookieWarningDescription",
  181. });
  182. expect(mockSetAuthCookie).not.toHaveBeenCalled();
  183. });
  184. it("server error: 500 + SERVER_ERROR", async () => {
  185. mockValidateKey.mockRejectedValue(new Error("DB connection failed"));
  186. const res = await POST(makeRequest({ key: "trigger-server-error" }));
  187. expect(res.status).toBe(500);
  188. expect(await res.json()).toEqual({
  189. error: "translated:serverError",
  190. errorCode: "SERVER_ERROR",
  191. });
  192. expect(mockSetAuthCookie).not.toHaveBeenCalled();
  193. expect(mockLogger.error).toHaveBeenCalled();
  194. });
  195. });
  196. });