@rebasepro/server-core 0.0.1-canary.4d4fb3e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +40 -0
  3. package/build-errors.txt +52 -0
  4. package/coverage/clover.xml +3739 -0
  5. package/coverage/coverage-final.json +31 -0
  6. package/coverage/lcov-report/base.css +224 -0
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/index.html +266 -0
  10. package/coverage/lcov-report/prettify.css +1 -0
  11. package/coverage/lcov-report/prettify.js +2 -0
  12. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  13. package/coverage/lcov-report/sorter.js +210 -0
  14. package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +952 -0
  15. package/coverage/lcov-report/src/api/errors.ts.html +472 -0
  16. package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +1069 -0
  17. package/coverage/lcov-report/src/api/graphql/index.html +116 -0
  18. package/coverage/lcov-report/src/api/index.html +176 -0
  19. package/coverage/lcov-report/src/api/openapi-generator.ts.html +565 -0
  20. package/coverage/lcov-report/src/api/rest/api-generator.ts.html +994 -0
  21. package/coverage/lcov-report/src/api/rest/index.html +131 -0
  22. package/coverage/lcov-report/src/api/rest/query-parser.ts.html +550 -0
  23. package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +202 -0
  24. package/coverage/lcov-report/src/api/server.ts.html +823 -0
  25. package/coverage/lcov-report/src/auth/admin-routes.ts.html +973 -0
  26. package/coverage/lcov-report/src/auth/index.html +176 -0
  27. package/coverage/lcov-report/src/auth/jwt.ts.html +574 -0
  28. package/coverage/lcov-report/src/auth/middleware.ts.html +745 -0
  29. package/coverage/lcov-report/src/auth/password.ts.html +310 -0
  30. package/coverage/lcov-report/src/auth/services.ts.html +2074 -0
  31. package/coverage/lcov-report/src/collections/index.html +116 -0
  32. package/coverage/lcov-report/src/collections/loader.ts.html +232 -0
  33. package/coverage/lcov-report/src/db/auth-schema.ts.html +523 -0
  34. package/coverage/lcov-report/src/db/data-transformer.ts.html +1753 -0
  35. package/coverage/lcov-report/src/db/entityService.ts.html +700 -0
  36. package/coverage/lcov-report/src/db/index.html +146 -0
  37. package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +4048 -0
  38. package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +883 -0
  39. package/coverage/lcov-report/src/db/services/RelationService.ts.html +3121 -0
  40. package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +442 -0
  41. package/coverage/lcov-report/src/db/services/index.html +176 -0
  42. package/coverage/lcov-report/src/db/services/index.ts.html +124 -0
  43. package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +1960 -0
  44. package/coverage/lcov-report/src/index.html +116 -0
  45. package/coverage/lcov-report/src/services/driver-registry.ts.html +631 -0
  46. package/coverage/lcov-report/src/services/index.html +131 -0
  47. package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +3025 -0
  48. package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +1189 -0
  49. package/coverage/lcov-report/src/storage/S3StorageController.ts.html +970 -0
  50. package/coverage/lcov-report/src/storage/index.html +161 -0
  51. package/coverage/lcov-report/src/storage/storage-registry.ts.html +646 -0
  52. package/coverage/lcov-report/src/storage/types.ts.html +451 -0
  53. package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +3082 -0
  54. package/coverage/lcov-report/src/utils/index.html +116 -0
  55. package/coverage/lcov.info +7179 -0
  56. package/dist/common/src/collections/CollectionRegistry.d.ts +48 -0
  57. package/dist/common/src/collections/index.d.ts +1 -0
  58. package/dist/common/src/data/buildRebaseData.d.ts +14 -0
  59. package/dist/common/src/index.d.ts +3 -0
  60. package/dist/common/src/util/builders.d.ts +57 -0
  61. package/dist/common/src/util/callbacks.d.ts +6 -0
  62. package/dist/common/src/util/collections.d.ts +11 -0
  63. package/dist/common/src/util/common.d.ts +2 -0
  64. package/dist/common/src/util/conditions.d.ts +26 -0
  65. package/dist/common/src/util/entities.d.ts +36 -0
  66. package/dist/common/src/util/enums.d.ts +3 -0
  67. package/dist/common/src/util/index.d.ts +16 -0
  68. package/dist/common/src/util/navigation_from_path.d.ts +34 -0
  69. package/dist/common/src/util/navigation_utils.d.ts +20 -0
  70. package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
  71. package/dist/common/src/util/paths.d.ts +14 -0
  72. package/dist/common/src/util/permissions.d.ts +5 -0
  73. package/dist/common/src/util/references.d.ts +2 -0
  74. package/dist/common/src/util/relations.d.ts +12 -0
  75. package/dist/common/src/util/resolutions.d.ts +72 -0
  76. package/dist/common/src/util/storage.d.ts +24 -0
  77. package/dist/index-BeMqpmfQ.js +239 -0
  78. package/dist/index-BeMqpmfQ.js.map +1 -0
  79. package/dist/index-bl4J3lNb.js +55823 -0
  80. package/dist/index-bl4J3lNb.js.map +1 -0
  81. package/dist/index.es.js +58 -0
  82. package/dist/index.es.js.map +1 -0
  83. package/dist/index.umd.js +56062 -0
  84. package/dist/index.umd.js.map +1 -0
  85. package/dist/server-core/src/api/ast-schema-editor.d.ts +21 -0
  86. package/dist/server-core/src/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
  87. package/dist/server-core/src/api/errors.d.ts +35 -0
  88. package/dist/server-core/src/api/graphql/graphql-schema-generator.d.ts +35 -0
  89. package/dist/server-core/src/api/graphql/index.d.ts +1 -0
  90. package/dist/server-core/src/api/index.d.ts +9 -0
  91. package/dist/server-core/src/api/openapi-generator.d.ts +2 -0
  92. package/dist/server-core/src/api/rest/api-generator.d.ts +64 -0
  93. package/dist/server-core/src/api/rest/index.d.ts +1 -0
  94. package/dist/server-core/src/api/rest/query-parser.d.ts +9 -0
  95. package/dist/server-core/src/api/schema-editor-routes.d.ts +3 -0
  96. package/dist/server-core/src/api/server.d.ts +40 -0
  97. package/dist/server-core/src/api/types.d.ts +90 -0
  98. package/dist/server-core/src/auth/admin-routes.d.ts +7 -0
  99. package/dist/server-core/src/auth/google-oauth.d.ts +20 -0
  100. package/dist/server-core/src/auth/index.d.ts +12 -0
  101. package/dist/server-core/src/auth/interfaces.d.ts +270 -0
  102. package/dist/server-core/src/auth/jwt.d.ts +42 -0
  103. package/dist/server-core/src/auth/middleware.d.ts +56 -0
  104. package/dist/server-core/src/auth/password.d.ts +22 -0
  105. package/dist/server-core/src/auth/rate-limiter.d.ts +31 -0
  106. package/dist/server-core/src/auth/routes.d.ts +17 -0
  107. package/dist/server-core/src/bootstrappers/index.d.ts +0 -0
  108. package/dist/server-core/src/collections/BackendCollectionRegistry.d.ts +13 -0
  109. package/dist/server-core/src/collections/loader.d.ts +5 -0
  110. package/dist/server-core/src/db/interfaces.d.ts +18 -0
  111. package/dist/server-core/src/email/index.d.ts +6 -0
  112. package/dist/server-core/src/email/smtp-email-service.d.ts +25 -0
  113. package/dist/server-core/src/email/templates.d.ts +33 -0
  114. package/dist/server-core/src/email/types.d.ts +110 -0
  115. package/dist/server-core/src/functions/function-loader.d.ts +17 -0
  116. package/dist/server-core/src/functions/function-routes.d.ts +10 -0
  117. package/dist/server-core/src/functions/index.d.ts +3 -0
  118. package/dist/server-core/src/history/history-routes.d.ts +23 -0
  119. package/dist/server-core/src/history/index.d.ts +1 -0
  120. package/dist/server-core/src/index.d.ts +24 -0
  121. package/dist/server-core/src/init.d.ts +49 -0
  122. package/dist/server-core/src/serve-spa.d.ts +30 -0
  123. package/dist/server-core/src/services/driver-registry.d.ts +78 -0
  124. package/dist/server-core/src/storage/LocalStorageController.d.ts +46 -0
  125. package/dist/server-core/src/storage/S3StorageController.d.ts +36 -0
  126. package/dist/server-core/src/storage/index.d.ts +18 -0
  127. package/dist/server-core/src/storage/routes.d.ts +38 -0
  128. package/dist/server-core/src/storage/storage-registry.d.ts +78 -0
  129. package/dist/server-core/src/storage/types.d.ts +91 -0
  130. package/dist/server-core/src/types/index.d.ts +11 -0
  131. package/dist/server-core/src/utils/logging.d.ts +9 -0
  132. package/dist/server-core/src/utils/sql.d.ts +27 -0
  133. package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
  134. package/dist/types/src/controllers/auth.d.ts +117 -0
  135. package/dist/types/src/controllers/client.d.ts +58 -0
  136. package/dist/types/src/controllers/collection_registry.d.ts +44 -0
  137. package/dist/types/src/controllers/customization_controller.d.ts +54 -0
  138. package/dist/types/src/controllers/data.d.ts +141 -0
  139. package/dist/types/src/controllers/data_driver.d.ts +168 -0
  140. package/dist/types/src/controllers/database_admin.d.ts +11 -0
  141. package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
  142. package/dist/types/src/controllers/effective_role.d.ts +4 -0
  143. package/dist/types/src/controllers/index.d.ts +17 -0
  144. package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
  145. package/dist/types/src/controllers/navigation.d.ts +213 -0
  146. package/dist/types/src/controllers/registry.d.ts +51 -0
  147. package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
  148. package/dist/types/src/controllers/side_entity_controller.d.ts +89 -0
  149. package/dist/types/src/controllers/snackbar.d.ts +24 -0
  150. package/dist/types/src/controllers/storage.d.ts +173 -0
  151. package/dist/types/src/index.d.ts +4 -0
  152. package/dist/types/src/rebase_context.d.ts +101 -0
  153. package/dist/types/src/types/backend.d.ts +533 -0
  154. package/dist/types/src/types/builders.d.ts +14 -0
  155. package/dist/types/src/types/chips.d.ts +5 -0
  156. package/dist/types/src/types/collections.d.ts +812 -0
  157. package/dist/types/src/types/data_source.d.ts +64 -0
  158. package/dist/types/src/types/entities.d.ts +145 -0
  159. package/dist/types/src/types/entity_actions.d.ts +98 -0
  160. package/dist/types/src/types/entity_callbacks.d.ts +173 -0
  161. package/dist/types/src/types/entity_link_builder.d.ts +7 -0
  162. package/dist/types/src/types/entity_overrides.d.ts +9 -0
  163. package/dist/types/src/types/entity_views.d.ts +61 -0
  164. package/dist/types/src/types/export_import.d.ts +21 -0
  165. package/dist/types/src/types/index.d.ts +22 -0
  166. package/dist/types/src/types/locales.d.ts +4 -0
  167. package/dist/types/src/types/modify_collections.d.ts +5 -0
  168. package/dist/types/src/types/plugins.d.ts +225 -0
  169. package/dist/types/src/types/properties.d.ts +1091 -0
  170. package/dist/types/src/types/property_config.d.ts +70 -0
  171. package/dist/types/src/types/relations.d.ts +336 -0
  172. package/dist/types/src/types/slots.d.ts +228 -0
  173. package/dist/types/src/types/translations.d.ts +826 -0
  174. package/dist/types/src/types/user_management_delegate.d.ts +120 -0
  175. package/dist/types/src/types/websockets.d.ts +78 -0
  176. package/dist/types/src/users/index.d.ts +2 -0
  177. package/dist/types/src/users/roles.d.ts +22 -0
  178. package/dist/types/src/users/user.d.ts +46 -0
  179. package/history_diff.log +385 -0
  180. package/jest.config.cjs +16 -0
  181. package/package.json +86 -0
  182. package/scratch.ts +8 -0
  183. package/src/api/ast-schema-editor.ts +289 -0
  184. package/src/api/collections_for_test/callbacks_test_collection.ts +57 -0
  185. package/src/api/errors.ts +155 -0
  186. package/src/api/graphql/graphql-schema-generator.ts +334 -0
  187. package/src/api/graphql/index.ts +2 -0
  188. package/src/api/index.ts +11 -0
  189. package/src/api/openapi-generator.ts +160 -0
  190. package/src/api/rest/api-generator.ts +466 -0
  191. package/src/api/rest/index.ts +2 -0
  192. package/src/api/rest/query-parser.ts +155 -0
  193. package/src/api/schema-editor-routes.ts +39 -0
  194. package/src/api/server.ts +245 -0
  195. package/src/api/types.ts +90 -0
  196. package/src/auth/admin-routes.ts +488 -0
  197. package/src/auth/google-oauth.ts +60 -0
  198. package/src/auth/index.ts +21 -0
  199. package/src/auth/interfaces.ts +316 -0
  200. package/src/auth/jwt.ts +164 -0
  201. package/src/auth/middleware.ts +235 -0
  202. package/src/auth/password.ts +75 -0
  203. package/src/auth/rate-limiter.ts +129 -0
  204. package/src/auth/routes.ts +730 -0
  205. package/src/bootstrappers/index.ts +1 -0
  206. package/src/collections/BackendCollectionRegistry.ts +20 -0
  207. package/src/collections/loader.ts +49 -0
  208. package/src/db/interfaces.ts +60 -0
  209. package/src/email/index.ts +17 -0
  210. package/src/email/smtp-email-service.ts +88 -0
  211. package/src/email/templates.ts +301 -0
  212. package/src/email/types.ts +112 -0
  213. package/src/functions/function-loader.ts +91 -0
  214. package/src/functions/function-routes.ts +31 -0
  215. package/src/functions/index.ts +3 -0
  216. package/src/history/history-routes.ts +128 -0
  217. package/src/history/index.ts +2 -0
  218. package/src/index.ts +56 -0
  219. package/src/init.ts +309 -0
  220. package/src/serve-spa.ts +81 -0
  221. package/src/services/driver-registry.ts +182 -0
  222. package/src/storage/LocalStorageController.ts +368 -0
  223. package/src/storage/S3StorageController.ts +295 -0
  224. package/src/storage/index.ts +32 -0
  225. package/src/storage/routes.ts +247 -0
  226. package/src/storage/storage-registry.ts +187 -0
  227. package/src/storage/types.ts +122 -0
  228. package/src/types/index.ts +27 -0
  229. package/src/utils/logging.ts +35 -0
  230. package/src/utils/sql.ts +38 -0
  231. package/test/admin-routes.test.ts +591 -0
  232. package/test/api-generator.test.ts +458 -0
  233. package/test/ast-schema-editor.test.ts +61 -0
  234. package/test/auth-middleware-hono.test.ts +321 -0
  235. package/test/auth-routes.test.ts +868 -0
  236. package/test/driver-registry.test.ts +280 -0
  237. package/test/errors-hono.test.ts +133 -0
  238. package/test/errors.test.ts +150 -0
  239. package/test/jwt-security.test.ts +173 -0
  240. package/test/jwt.test.ts +311 -0
  241. package/test/middleware.test.ts +295 -0
  242. package/test/password.test.ts +165 -0
  243. package/test/query-parser.test.ts +258 -0
  244. package/test/rate-limiter.test.ts +102 -0
  245. package/test/storage-local.test.ts +278 -0
  246. package/test/storage-registry.test.ts +280 -0
  247. package/test/storage-routes.test.ts +218 -0
  248. package/test/storage-s3.test.ts +301 -0
  249. package/test-ast.ts +28 -0
  250. package/test_output.txt +1133 -0
  251. package/tsconfig.json +49 -0
  252. package/tsconfig.prod.json +20 -0
  253. package/vite.config.ts +78 -0
  254. package/vite.config.ts.timestamp-1775065397568-8a853255edf6e.mjs +46 -0
@@ -0,0 +1,868 @@
1
+ /**
2
+ * Auth Routes — Integration Tests
3
+ *
4
+ * Tests the full Hono request → route handler → JSON response cycle.
5
+ * Services are mocked so we can exercise the HTTP layer in isolation while
6
+ * still verifying business logic (first-user bootstrap, token rotation, etc.).
7
+ */
8
+
9
+ import { Hono } from "hono";
10
+ import type { HonoEnv } from "../src/api/types";
11
+ import { errorHandler } from "../src/api/errors";
12
+ import { createAuthRoutes, AuthModuleConfig } from "../src/auth/routes";
13
+ import type { AuthRepository } from "../src/auth/interfaces";
14
+ import { configureJwt, generateAccessToken, hashRefreshToken } from "../src/auth/jwt";
15
+
16
+ // ── Mocks ───────────────────────────────────────────────────────────────────
17
+
18
+ jest.mock("../src/auth/password");
19
+ jest.mock("../src/auth/google-oauth");
20
+
21
+ // Bypass rate limiters — they share state across tests and cause 429s
22
+ jest.mock("../src/auth/rate-limiter", () => {
23
+ const passthrough = async (_c: unknown, next: () => Promise<void>) => next();
24
+ return {
25
+ createRateLimiter: () => passthrough,
26
+ defaultAuthLimiter: passthrough,
27
+ strictAuthLimiter: passthrough,
28
+ };
29
+ });
30
+
31
+ import { hashPassword, verifyPassword, validatePasswordStrength } from "../src/auth/password";
32
+ import { verifyGoogleIdToken, isGoogleOAuthConfigured } from "../src/auth/google-oauth";
33
+
34
+ // ── Helpers ─────────────────────────────────────────────────────────────────
35
+
36
+ const TEST_SECRET = "integration-test-secret-key-that-is-definitely-32-chars-long!!";
37
+
38
+ function mockUser(overrides: Partial<{ id: string; email: string; passwordHash: string | null; displayName: string | null; photoUrl: string | null; provider: string; emailVerified: boolean; emailVerificationToken: string | null }> = {}) {
39
+ return {
40
+ id: overrides.id ?? "user-1",
41
+ email: overrides.email ?? "test@example.com",
42
+ passwordHash: "passwordHash" in overrides ? overrides.passwordHash : "salt:hash",
43
+ displayName: overrides.displayName ?? "Test User",
44
+ photoUrl: overrides.photoUrl ?? null,
45
+ provider: overrides.provider ?? "email",
46
+ googleId: null,
47
+ emailVerified: overrides.emailVerified ?? false,
48
+ emailVerificationToken: overrides.emailVerificationToken ?? null,
49
+ emailVerificationSentAt: null,
50
+ createdAt: new Date(),
51
+ updatedAt: new Date(),
52
+ };
53
+ }
54
+
55
+ function mockRole(id: string, isAdmin = false) {
56
+ return { id, name: id.charAt(0).toUpperCase() + id.slice(1), isAdmin, defaultPermissions: null, collectionPermissions: null, config: null };
57
+ }
58
+
59
+ let mockAuthRepo: jest.Mocked<AuthRepository>;
60
+ let mockEmailService: { send: jest.Mock; isConfigured: jest.Mock };
61
+
62
+ function createApp(opts: { allowRegistration?: boolean; withEmail?: boolean; defaultRole?: string } = {}) {
63
+ // Re-create mocked service instances each time
64
+
65
+ // Wire constructor mocks to return our instances
66
+
67
+ // Default returns for mocked services
68
+
69
+ mockAuthRepo = {
70
+ getUserByEmail: jest.fn().mockResolvedValue(null),
71
+ getUserByGoogleId: jest.fn().mockResolvedValue(null),
72
+ getUserById: jest.fn().mockResolvedValue(null),
73
+ createUser: jest.fn().mockImplementation((data) =>
74
+ Promise.resolve(mockUser({ email: data.email, displayName: data.displayName, passwordHash: data.passwordHash }))
75
+ ),
76
+ listUsers: jest.fn().mockResolvedValue([]),
77
+ getUserRoles: jest.fn().mockResolvedValue([mockRole("editor")]),
78
+ getUserRoleIds: jest.fn().mockResolvedValue(["editor"]),
79
+ assignDefaultRole: jest.fn().mockResolvedValue(undefined),
80
+ setUserRoles: jest.fn().mockResolvedValue(undefined),
81
+ updateUser: jest.fn().mockImplementation((id, data) =>
82
+ Promise.resolve(mockUser({ id, ...data }))
83
+ ),
84
+ deleteUser: jest.fn().mockResolvedValue(undefined),
85
+ updatePassword: jest.fn().mockResolvedValue(undefined),
86
+ setEmailVerified: jest.fn().mockResolvedValue(undefined),
87
+ setVerificationToken: jest.fn().mockResolvedValue(undefined),
88
+ getUserByVerificationToken: jest.fn().mockResolvedValue(null),
89
+ getUserWithRoles: jest.fn().mockImplementation(async (userId) => {
90
+ const user = mockUser({ id: userId });
91
+ return { user, roles: [mockRole("editor")] };
92
+ }),
93
+ createRefreshToken: jest.fn().mockResolvedValue(undefined),
94
+ findRefreshTokenByHash: jest.fn().mockResolvedValue(null),
95
+ deleteRefreshToken: jest.fn().mockResolvedValue(undefined),
96
+ deleteAllRefreshTokensForUser: jest.fn().mockResolvedValue(undefined),
97
+ listRefreshTokensForUser: jest.fn().mockResolvedValue([]),
98
+ deleteRefreshTokenById: jest.fn().mockResolvedValue(undefined),
99
+ createPasswordResetToken: jest.fn().mockResolvedValue(undefined),
100
+ findValidPasswordResetToken: jest.fn().mockResolvedValue(null),
101
+ markPasswordResetTokenUsed: jest.fn().mockResolvedValue(undefined),
102
+ deleteExpiredPasswordResetTokens: jest.fn().mockResolvedValue(undefined)
103
+ } as unknown as jest.Mocked<AuthRepository>;
104
+
105
+
106
+ // Password mocks
107
+ (validatePasswordStrength as jest.Mock).mockReturnValue({ valid: true, errors: [] });
108
+ (hashPassword as jest.Mock).mockResolvedValue("hashed-pw");
109
+ (verifyPassword as jest.Mock).mockResolvedValue(true);
110
+
111
+ // Google mocks
112
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValue(false);
113
+ (verifyGoogleIdToken as jest.Mock).mockResolvedValue(null);
114
+
115
+ // Email mock
116
+ mockEmailService = { send: jest.fn().mockResolvedValue(undefined), isConfigured: jest.fn().mockReturnValue(opts.withEmail ?? false) };
117
+
118
+ const config: AuthModuleConfig = {
119
+ authRepo: mockAuthRepo,
120
+ allowRegistration: opts.allowRegistration ?? true,
121
+ defaultRole: opts.defaultRole,
122
+ emailService: opts.withEmail ? mockEmailService as any : undefined,
123
+ emailConfig: opts.withEmail ? { from: "test@test.com", appName: "TestApp", resetPasswordUrl: "https://app.test", verifyEmailUrl: "https://app.test" } : undefined,
124
+ };
125
+
126
+ const app = new Hono<HonoEnv>();
127
+ app.onError(errorHandler);
128
+ app.route("/auth", createAuthRoutes(config));
129
+ return app;
130
+ }
131
+
132
+ function json(body: Record<string, unknown>) {
133
+ return {
134
+ method: "POST" as const,
135
+ headers: { "Content-Type": "application/json" },
136
+ body: JSON.stringify(body),
137
+ };
138
+ }
139
+
140
+ function authHeader(userId = "user-1", roles = ["editor"]) {
141
+ return { Authorization: `Bearer ${generateAccessToken(userId, roles)}` };
142
+ }
143
+
144
+ // ═══════════════════════════════════════════════════════════════════════════
145
+ // TESTS
146
+ // ═══════════════════════════════════════════════════════════════════════════
147
+
148
+ describe("Auth Routes (Integration)", () => {
149
+ beforeAll(() => {
150
+ configureJwt({ secret: TEST_SECRET, accessExpiresIn: "1h" });
151
+ });
152
+
153
+ beforeEach(() => {
154
+ jest.clearAllMocks();
155
+ });
156
+
157
+ // ── Registration ────────────────────────────────────────────────────
158
+ describe("POST /auth/register", () => {
159
+ it("registers a new user and returns 201 with tokens", async () => {
160
+ const app = createApp();
161
+ // allowRegistration=true → isRegistrationAllowed() returns immediately
162
+ // Only the isFirstUser check calls listUsers
163
+ mockAuthRepo.listUsers
164
+ .mockResolvedValueOnce([mockUser()]); // isFirstUser check
165
+
166
+ const res = await app.request("/auth/register", json({ email: "new@test.com", password: "StrongPass1" }));
167
+ expect(res.status).toBe(201);
168
+ const body = await res.json() as any;
169
+ expect(body.tokens.accessToken).toBeTruthy();
170
+ expect(body.tokens.refreshToken).toBeTruthy();
171
+ expect(body.user.email).toBe("new@test.com");
172
+ });
173
+
174
+ it("first user gets admin role", async () => {
175
+ const app = createApp();
176
+ // allowRegistration=true → isRegistrationAllowed() returns immediately
177
+ // Only the isFirstUser check calls listUsers (after createUser)
178
+ mockAuthRepo.listUsers
179
+ .mockResolvedValueOnce([mockUser()]); // allUsers.length === 1 → isFirstUser
180
+
181
+ await app.request("/auth/register", json({ email: "first@test.com", password: "StrongPass1" }));
182
+ expect(mockAuthRepo.assignDefaultRole).toHaveBeenCalledWith(expect.any(String), "admin");
183
+ });
184
+
185
+ it("second user gets configured default role", async () => {
186
+ const app = createApp({ defaultRole: "editor" });
187
+ // allowRegistration=true → isRegistrationAllowed() returns immediately
188
+ // isFirstUser check: 2 users → not first
189
+ mockAuthRepo.listUsers
190
+ .mockResolvedValueOnce([mockUser(), mockUser({ id: "user-2" })]);
191
+
192
+ await app.request("/auth/register", json({ email: "second@test.com", password: "StrongPass1" }));
193
+ expect(mockAuthRepo.assignDefaultRole).toHaveBeenCalledWith(expect.any(String), "editor");
194
+ });
195
+
196
+ it("second user gets no role by default when not configured", async () => {
197
+ const app = createApp();
198
+ mockAuthRepo.listUsers
199
+ .mockResolvedValueOnce([mockUser(), mockUser({ id: "user-2" })]);
200
+
201
+ await app.request("/auth/register", json({ email: "third@test.com", password: "StrongPass1" }));
202
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalled();
203
+ });
204
+
205
+ it("returns 409 when email already exists", async () => {
206
+ const app = createApp();
207
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
208
+
209
+ const res = await app.request("/auth/register", json({ email: "existing@test.com", password: "StrongPass1" }));
210
+ expect(res.status).toBe(409);
211
+ const body = await res.json() as any;
212
+ expect(body.error.code).toBe("EMAIL_EXISTS");
213
+ });
214
+
215
+ it("returns 400 for weak password", async () => {
216
+ const app = createApp();
217
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false, errors: ["Too short"] });
218
+
219
+ const res = await app.request("/auth/register", json({ email: "new@test.com", password: "weak" }));
220
+ expect(res.status).toBe(400);
221
+ const body = await res.json() as any;
222
+ expect(body.error.code).toBe("WEAK_PASSWORD");
223
+ });
224
+
225
+ it("returns 400 for invalid email (Zod)", async () => {
226
+ const app = createApp();
227
+ const res = await app.request("/auth/register", json({ email: "not-an-email", password: "StrongPass1" }));
228
+ expect(res.status).toBe(400);
229
+ const body = await res.json() as any;
230
+ expect(body.error.code).toBe("INVALID_INPUT");
231
+ });
232
+
233
+ it("returns 400 for missing password", async () => {
234
+ const app = createApp();
235
+ const res = await app.request("/auth/register", json({ email: "a@b.com" }));
236
+ expect(res.status).toBe(400);
237
+ });
238
+
239
+ it("returns 403 when registration is disabled and users exist", async () => {
240
+ const app = createApp({ allowRegistration: false });
241
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]); // users exist
242
+
243
+ const res = await app.request("/auth/register", json({ email: "new@test.com", password: "StrongPass1" }));
244
+ expect(res.status).toBe(403);
245
+ const body = await res.json() as any;
246
+ expect(body.error.code).toBe("REGISTRATION_DISABLED");
247
+ });
248
+
249
+ it("allows first-user registration even when registration is disabled", async () => {
250
+ const app = createApp({ allowRegistration: false });
251
+ mockAuthRepo.listUsers
252
+ .mockResolvedValueOnce([]) // isRegistrationAllowed → empty = allow
253
+ .mockResolvedValueOnce([mockUser()]); // isFirstUser
254
+
255
+ const res = await app.request("/auth/register", json({ email: "first@test.com", password: "StrongPass1" }));
256
+ expect(res.status).toBe(201);
257
+ });
258
+
259
+ it("stores refresh token after registration", async () => {
260
+ const app = createApp();
261
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]);
262
+
263
+ await app.request("/auth/register", json({ email: "a@b.com", password: "StrongPass1" }));
264
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
265
+ });
266
+ });
267
+
268
+ // ── Login ───────────────────────────────────────────────────────────
269
+ describe("POST /auth/login", () => {
270
+ it("returns tokens on successful login", async () => {
271
+ const app = createApp();
272
+ const user = mockUser({ passwordHash: "salt:hash" });
273
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(user);
274
+
275
+ const res = await app.request("/auth/login", json({ email: "test@example.com", password: "ValidPass1" }));
276
+ expect(res.status).toBe(200);
277
+ const body = await res.json() as any;
278
+ expect(body.tokens.accessToken).toBeTruthy();
279
+ expect(body.user.uid).toBe("user-1");
280
+ });
281
+
282
+ it("returns 401 for non-existent email", async () => {
283
+ const app = createApp();
284
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
285
+
286
+ const res = await app.request("/auth/login", json({ email: "nobody@test.com", password: "Any1" }));
287
+ expect(res.status).toBe(401);
288
+ const body = await res.json() as any;
289
+ expect(body.error.code).toBe("INVALID_CREDENTIALS");
290
+ });
291
+
292
+ it("returns 401 for wrong password", async () => {
293
+ const app = createApp();
294
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
295
+ (verifyPassword as jest.Mock).mockResolvedValueOnce(false);
296
+
297
+ const res = await app.request("/auth/login", json({ email: "test@example.com", password: "Wrong1" }));
298
+ expect(res.status).toBe(401);
299
+ });
300
+
301
+ it("returns 401 for user without password hash (Google-only)", async () => {
302
+ const app = createApp();
303
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser({ passwordHash: null }));
304
+
305
+ const res = await app.request("/auth/login", json({ email: "google@test.com", password: "Any1" }));
306
+ expect(res.status).toBe(401);
307
+ });
308
+
309
+ it("returns 400 for missing email field", async () => {
310
+ const app = createApp();
311
+ const res = await app.request("/auth/login", json({ password: "Any1" }));
312
+ expect(res.status).toBe(400);
313
+ });
314
+
315
+ it("stores refresh token on login", async () => {
316
+ const app = createApp();
317
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
318
+
319
+ await app.request("/auth/login", json({ email: "test@example.com", password: "ValidPass1" }));
320
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
321
+ });
322
+ });
323
+
324
+ // ── Google OAuth ────────────────────────────────────────────────────
325
+ describe("POST /auth/google", () => {
326
+ it("returns 503 when Google OAuth is not configured", async () => {
327
+ const app = createApp();
328
+ const res = await app.request("/auth/google", json({ idToken: "google-token" }));
329
+ expect(res.status).toBe(503);
330
+ const body = await res.json() as any;
331
+ expect(body.error.code).toBe("NOT_CONFIGURED");
332
+ });
333
+
334
+ it("returns 401 for invalid Google token", async () => {
335
+ const app = createApp();
336
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValueOnce(true);
337
+ (verifyGoogleIdToken as jest.Mock).mockResolvedValueOnce(null);
338
+
339
+ const res = await app.request("/auth/google", json({ idToken: "bad-token" }));
340
+ expect(res.status).toBe(401);
341
+ const body = await res.json() as any;
342
+ expect(body.error.code).toBe("INVALID_TOKEN");
343
+ });
344
+
345
+ it("creates a new user for new Google sign-in", async () => {
346
+ const app = createApp();
347
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValue(true);
348
+ (verifyGoogleIdToken as jest.Mock).mockResolvedValueOnce({
349
+ googleId: "g-123",
350
+ email: "google@test.com",
351
+ displayName: "Google User",
352
+ photoUrl: "https://photo.url",
353
+ emailVerified: true,
354
+ });
355
+ mockAuthRepo.getUserByGoogleId.mockResolvedValueOnce(null);
356
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
357
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]); // not first user
358
+
359
+ const res = await app.request("/auth/google", json({ idToken: "valid-token" }));
360
+ expect(res.status).toBe(200);
361
+ expect(mockAuthRepo.createUser).toHaveBeenCalledWith(expect.objectContaining({
362
+ email: "google@test.com",
363
+ provider: "google",
364
+ googleId: "g-123",
365
+ }));
366
+ });
367
+
368
+ it("links Google to existing account by email", async () => {
369
+ const app = createApp();
370
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValue(true);
371
+ (verifyGoogleIdToken as jest.Mock).mockResolvedValueOnce({
372
+ googleId: "g-456",
373
+ email: "existing@test.com",
374
+ displayName: "Existing",
375
+ photoUrl: null,
376
+ emailVerified: true,
377
+ });
378
+ const existing = mockUser({ email: "existing@test.com" });
379
+ mockAuthRepo.getUserByGoogleId.mockResolvedValueOnce(null);
380
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(existing);
381
+
382
+ const res = await app.request("/auth/google", json({ idToken: "link-token" }));
383
+ expect(res.status).toBe(200);
384
+ expect(mockAuthRepo.updateUser).toHaveBeenCalledWith(existing.id, { googleId: "g-456" });
385
+ });
386
+
387
+ it("updates profile for returning Google user", async () => {
388
+ const app = createApp();
389
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValue(true);
390
+ const existingUser = mockUser({ id: "g-user-1" });
391
+ (verifyGoogleIdToken as jest.Mock).mockResolvedValueOnce({
392
+ googleId: "g-789",
393
+ email: "returning@test.com",
394
+ displayName: "Updated Name",
395
+ photoUrl: "https://new-photo.url",
396
+ emailVerified: true,
397
+ });
398
+ mockAuthRepo.getUserByGoogleId.mockResolvedValueOnce(existingUser);
399
+
400
+ const res = await app.request("/auth/google", json({ idToken: "returning-token" }));
401
+ expect(res.status).toBe(200);
402
+ expect(mockAuthRepo.updateUser).toHaveBeenCalledWith(existingUser.id, expect.objectContaining({
403
+ displayName: "Updated Name",
404
+ photoUrl: "https://new-photo.url",
405
+ }));
406
+ });
407
+ });
408
+
409
+ // ── Token Refresh ───────────────────────────────────────────────────
410
+ describe("POST /auth/refresh", () => {
411
+ it("returns new tokens on valid refresh", async () => {
412
+ const app = createApp();
413
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
414
+ id: "rt-1",
415
+ userId: "user-1",
416
+ tokenHash: "old-hash",
417
+ expiresAt: new Date(Date.now() + 86400000),
418
+ createdAt: new Date(),
419
+ userAgent: "",
420
+ ipAddress: "",
421
+ });
422
+ mockAuthRepo.getUserRoles.mockResolvedValueOnce([mockRole("editor")]);
423
+
424
+ const res = await app.request("/auth/refresh", json({ refreshToken: "valid-refresh-token" }));
425
+ expect(res.status).toBe(200);
426
+ const body = await res.json() as any;
427
+ expect(body.tokens.accessToken).toBeTruthy();
428
+ expect(body.tokens.refreshToken).toBeTruthy();
429
+ });
430
+
431
+ it("rotates refresh token — deletes old, creates new", async () => {
432
+ const app = createApp();
433
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
434
+ id: "rt-1",
435
+ userId: "user-1",
436
+ tokenHash: "old-hash",
437
+ expiresAt: new Date(Date.now() + 86400000),
438
+ createdAt: new Date(),
439
+ userAgent: "",
440
+ ipAddress: "",
441
+ });
442
+
443
+ await app.request("/auth/refresh", json({ refreshToken: "the-token" }));
444
+ // Old token deleted
445
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalledTimes(1);
446
+ // New token stored
447
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
448
+ });
449
+
450
+ it("returns 401 for unknown refresh token", async () => {
451
+ const app = createApp();
452
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce(null);
453
+
454
+ const res = await app.request("/auth/refresh", json({ refreshToken: "unknown" }));
455
+ expect(res.status).toBe(401);
456
+ const body = await res.json() as any;
457
+ expect(body.error.code).toBe("INVALID_TOKEN");
458
+ });
459
+
460
+ it("returns 401 and deletes expired refresh token", async () => {
461
+ const app = createApp();
462
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
463
+ id: "rt-1",
464
+ userId: "user-1",
465
+ tokenHash: "expired-hash",
466
+ expiresAt: new Date(Date.now() - 1000), // expired
467
+ createdAt: new Date(),
468
+ userAgent: "",
469
+ ipAddress: "",
470
+ });
471
+
472
+ const res = await app.request("/auth/refresh", json({ refreshToken: "expired-token" }));
473
+ expect(res.status).toBe(401);
474
+ const body = await res.json() as any;
475
+ expect(body.error.code).toBe("TOKEN_EXPIRED");
476
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalled();
477
+ });
478
+
479
+ it("returns 400 for missing refreshToken field", async () => {
480
+ const app = createApp();
481
+ const res = await app.request("/auth/refresh", json({}));
482
+ expect(res.status).toBe(400);
483
+ });
484
+ });
485
+
486
+ // ── Logout ──────────────────────────────────────────────────────────
487
+ describe("POST /auth/logout", () => {
488
+ it("deletes refresh token on logout", async () => {
489
+ const app = createApp();
490
+ const res = await app.request("/auth/logout", json({ refreshToken: "rt-to-delete" }));
491
+ expect(res.status).toBe(200);
492
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalledTimes(1);
493
+ });
494
+
495
+ it("returns 200 even without refresh token", async () => {
496
+ const app = createApp();
497
+ const res = await app.request("/auth/logout", json({}));
498
+ expect(res.status).toBe(200);
499
+ expect(mockAuthRepo.deleteRefreshToken).not.toHaveBeenCalled();
500
+ });
501
+ });
502
+
503
+ // ── Forgot Password ─────────────────────────────────────────────────
504
+ describe("POST /auth/forgot-password", () => {
505
+ it("always returns success (timing-safe)", async () => {
506
+ const app = createApp({ withEmail: true });
507
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null); // user doesn't exist
508
+
509
+ const res = await app.request("/auth/forgot-password", json({ email: "nobody@test.com" }));
510
+ expect(res.status).toBe(200);
511
+ const body = await res.json() as any;
512
+ expect(body.success).toBe(true);
513
+ });
514
+
515
+ it("sends reset email when user exists", async () => {
516
+ const app = createApp({ withEmail: true });
517
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
518
+
519
+ await app.request("/auth/forgot-password", json({ email: "test@example.com" }));
520
+ expect(mockAuthRepo.createPasswordResetToken).toHaveBeenCalledTimes(1);
521
+ expect(mockEmailService.send).toHaveBeenCalledTimes(1);
522
+ });
523
+
524
+ it("does not send email when user does not exist", async () => {
525
+ const app = createApp({ withEmail: true });
526
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
527
+
528
+ await app.request("/auth/forgot-password", json({ email: "nobody@test.com" }));
529
+ expect(mockAuthRepo.createPasswordResetToken).not.toHaveBeenCalled();
530
+ expect(mockEmailService.send).not.toHaveBeenCalled();
531
+ });
532
+
533
+ it("returns 503 when email service is not configured", async () => {
534
+ const app = createApp({ withEmail: false });
535
+ const res = await app.request("/auth/forgot-password", json({ email: "test@test.com" }));
536
+ expect(res.status).toBe(503);
537
+ const body = await res.json() as any;
538
+ expect(body.error.code).toBe("EMAIL_NOT_CONFIGURED");
539
+ });
540
+ });
541
+
542
+ // ── Reset Password ──────────────────────────────────────────────────
543
+ describe("POST /auth/reset-password", () => {
544
+ it("resets password with valid token", async () => {
545
+ const app = createApp();
546
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce({
547
+ userId: "user-1",
548
+ expiresAt: new Date(Date.now() + 3600000),
549
+ });
550
+
551
+ const res = await app.request("/auth/reset-password", json({ token: "valid-reset-token", password: "NewStrong1" }));
552
+ expect(res.status).toBe(200);
553
+ expect(mockAuthRepo.updatePassword).toHaveBeenCalledWith("user-1", "hashed-pw");
554
+ expect(mockAuthRepo.markPasswordResetTokenUsed).toHaveBeenCalled();
555
+ });
556
+
557
+ it("invalidates all sessions after password reset", async () => {
558
+ const app = createApp();
559
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce({
560
+ userId: "user-1",
561
+ expiresAt: new Date(Date.now() + 3600000),
562
+ });
563
+
564
+ await app.request("/auth/reset-password", json({ token: "token", password: "NewStrong1" }));
565
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
566
+ });
567
+
568
+ it("returns 400 for invalid/expired token", async () => {
569
+ const app = createApp();
570
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce(null);
571
+
572
+ const res = await app.request("/auth/reset-password", json({ token: "expired", password: "NewStrong1" }));
573
+ expect(res.status).toBe(400);
574
+ const body = await res.json() as any;
575
+ expect(body.error.code).toBe("INVALID_TOKEN");
576
+ });
577
+
578
+ it("returns 400 for weak new password", async () => {
579
+ const app = createApp();
580
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false, errors: ["Too weak"] });
581
+
582
+ const res = await app.request("/auth/reset-password", json({ token: "token", password: "weak" }));
583
+ expect(res.status).toBe(400);
584
+ const body = await res.json() as any;
585
+ expect(body.error.code).toBe("WEAK_PASSWORD");
586
+ });
587
+ });
588
+
589
+ // ── Change Password ─────────────────────────────────────────────────
590
+ describe("POST /auth/change-password", () => {
591
+ it("changes password for authenticated user", async () => {
592
+ const app = createApp();
593
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
594
+
595
+ const res = await app.request("/auth/change-password", {
596
+ ...json({ oldPassword: "OldPass1", newPassword: "NewPass1" }),
597
+ headers: { ...json({}).headers, ...authHeader() },
598
+ });
599
+ expect(res.status).toBe(200);
600
+ expect(mockAuthRepo.updatePassword).toHaveBeenCalled();
601
+ });
602
+
603
+ it("invalidates all sessions after password change", async () => {
604
+ const app = createApp();
605
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
606
+
607
+ await app.request("/auth/change-password", {
608
+ ...json({ oldPassword: "Old1", newPassword: "New1Pass" }),
609
+ headers: { ...json({}).headers, ...authHeader() },
610
+ });
611
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
612
+ });
613
+
614
+ it("returns 401 for wrong old password", async () => {
615
+ const app = createApp();
616
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
617
+ (verifyPassword as jest.Mock).mockResolvedValueOnce(false);
618
+
619
+ const res = await app.request("/auth/change-password", {
620
+ ...json({ oldPassword: "Wrong1", newPassword: "New1Pass" }),
621
+ headers: { ...json({}).headers, ...authHeader() },
622
+ });
623
+ expect(res.status).toBe(401);
624
+ });
625
+
626
+ it("returns 400 for weak new password", async () => {
627
+ const app = createApp();
628
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
629
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false, errors: ["Too short"] });
630
+
631
+ const res = await app.request("/auth/change-password", {
632
+ ...json({ oldPassword: "Old1", newPassword: "x" }),
633
+ headers: { ...json({}).headers, ...authHeader() },
634
+ });
635
+ expect(res.status).toBe(400);
636
+ });
637
+
638
+ it("returns 401 without auth", async () => {
639
+ const app = createApp();
640
+ const res = await app.request("/auth/change-password", json({ oldPassword: "Old1", newPassword: "New1Pass" }));
641
+ expect(res.status).toBe(401);
642
+ });
643
+
644
+ it("returns 400 for user without password (Google-only account)", async () => {
645
+ const app = createApp();
646
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser({ passwordHash: null }));
647
+
648
+ const res = await app.request("/auth/change-password", {
649
+ ...json({ oldPassword: "Old1", newPassword: "New1Pass" }),
650
+ headers: { ...json({}).headers, ...authHeader() },
651
+ });
652
+ expect(res.status).toBe(400);
653
+ const body = await res.json() as any;
654
+ expect(body.error.code).toBe("INVALID_ACCOUNT");
655
+ });
656
+ });
657
+
658
+ // ── Email Verification ──────────────────────────────────────────────
659
+ describe("Email verification", () => {
660
+ describe("POST /auth/send-verification", () => {
661
+ it("sends verification email for authenticated user", async () => {
662
+ const app = createApp({ withEmail: true });
663
+ mockAuthRepo.getUserById.mockResolvedValueOnce(mockUser({ emailVerified: false }));
664
+
665
+ const res = await app.request("/auth/send-verification", {
666
+ method: "POST",
667
+ headers: { ...authHeader() },
668
+ });
669
+ expect(res.status).toBe(200);
670
+ expect(mockAuthRepo.setVerificationToken).toHaveBeenCalled();
671
+ expect(mockEmailService.send).toHaveBeenCalled();
672
+ });
673
+
674
+ it("returns 400 when email is already verified", async () => {
675
+ const app = createApp({ withEmail: true });
676
+ mockAuthRepo.getUserById.mockResolvedValueOnce(mockUser({ emailVerified: true }));
677
+
678
+ const res = await app.request("/auth/send-verification", {
679
+ method: "POST",
680
+ headers: { ...authHeader() },
681
+ });
682
+ expect(res.status).toBe(400);
683
+ const body = await res.json() as any;
684
+ expect(body.error.code).toBe("ALREADY_VERIFIED");
685
+ });
686
+
687
+ it("returns 401 without auth", async () => {
688
+ const app = createApp({ withEmail: true });
689
+ const res = await app.request("/auth/send-verification", { method: "POST" });
690
+ expect(res.status).toBe(401);
691
+ });
692
+
693
+ it("returns 503 when email service is not configured", async () => {
694
+ const app = createApp({ withEmail: false });
695
+ const res = await app.request("/auth/send-verification", {
696
+ method: "POST",
697
+ headers: { ...authHeader() },
698
+ });
699
+ expect(res.status).toBe(503);
700
+ });
701
+ });
702
+
703
+ describe("GET /auth/verify-email", () => {
704
+ it("verifies email with valid token", async () => {
705
+ const app = createApp();
706
+ mockAuthRepo.getUserByVerificationToken.mockResolvedValueOnce(mockUser());
707
+
708
+ const res = await app.request("/auth/verify-email?token=valid-token");
709
+ expect(res.status).toBe(200);
710
+ expect(mockAuthRepo.setEmailVerified).toHaveBeenCalledWith("user-1", true);
711
+ });
712
+
713
+ it("returns 400 for invalid verification token", async () => {
714
+ const app = createApp();
715
+ mockAuthRepo.getUserByVerificationToken.mockResolvedValueOnce(null);
716
+
717
+ const res = await app.request("/auth/verify-email?token=bad-token");
718
+ expect(res.status).toBe(400);
719
+ const body = await res.json() as any;
720
+ expect(body.error.code).toBe("INVALID_TOKEN");
721
+ });
722
+
723
+ it("returns 400 when token is missing", async () => {
724
+ const app = createApp();
725
+ const res = await app.request("/auth/verify-email");
726
+ expect(res.status).toBe(400);
727
+ });
728
+ });
729
+ });
730
+
731
+ // ── User Profile ────────────────────────────────────────────────────
732
+ describe("GET /auth/me", () => {
733
+ it("returns authenticated user with roles", async () => {
734
+ const app = createApp();
735
+ const res = await app.request("/auth/me", {
736
+ headers: { ...authHeader("user-1", ["admin"]) },
737
+ });
738
+ expect(res.status).toBe(200);
739
+ const body = await res.json() as any;
740
+ expect(body.user.uid).toBe("user-1");
741
+ expect(body.user.roles).toBeDefined();
742
+ });
743
+
744
+ it("returns 401 without auth", async () => {
745
+ const app = createApp();
746
+ const res = await app.request("/auth/me");
747
+ expect(res.status).toBe(401);
748
+ });
749
+
750
+ it("returns 404 when user is deleted", async () => {
751
+ const app = createApp();
752
+ mockAuthRepo.getUserWithRoles.mockResolvedValueOnce(null);
753
+
754
+ const res = await app.request("/auth/me", {
755
+ headers: { ...authHeader() },
756
+ });
757
+ expect(res.status).toBe(404);
758
+ });
759
+ });
760
+
761
+ describe("PATCH /auth/me", () => {
762
+ it("updates user profile", async () => {
763
+ const app = createApp();
764
+ mockAuthRepo.updateUser.mockResolvedValueOnce(mockUser({ displayName: "New Name" }));
765
+
766
+ const res = await app.request("/auth/me", {
767
+ method: "PATCH",
768
+ headers: { "Content-Type": "application/json", ...authHeader() },
769
+ body: JSON.stringify({ displayName: "New Name" }),
770
+ });
771
+ expect(res.status).toBe(200);
772
+ expect(mockAuthRepo.updateUser).toHaveBeenCalledWith("user-1", expect.objectContaining({
773
+ displayName: "New Name",
774
+ }));
775
+ });
776
+
777
+ it("returns 401 without auth", async () => {
778
+ const app = createApp();
779
+ const res = await app.request("/auth/me", {
780
+ method: "PATCH",
781
+ headers: { "Content-Type": "application/json" },
782
+ body: JSON.stringify({ displayName: "Name" }),
783
+ });
784
+ expect(res.status).toBe(401);
785
+ });
786
+ });
787
+
788
+ // ── Sessions ────────────────────────────────────────────────────────
789
+ describe("Session management", () => {
790
+ it("GET /auth/sessions lists active sessions", async () => {
791
+ const app = createApp();
792
+ mockAuthRepo.listRefreshTokensForUser.mockResolvedValueOnce([
793
+ { id: "s1", userId: "user-1", tokenHash: "h1", expiresAt: new Date(), createdAt: new Date(), userAgent: "Chrome", ipAddress: "1.2.3.4" },
794
+ ]);
795
+
796
+ const res = await app.request("/auth/sessions", { headers: { ...authHeader() } });
797
+ expect(res.status).toBe(200);
798
+ const body = await res.json() as any;
799
+ expect(body.sessions).toHaveLength(1);
800
+ expect(body.sessions[0].id).toBe("s1");
801
+ });
802
+
803
+ it("DELETE /auth/sessions revokes all sessions", async () => {
804
+ const app = createApp();
805
+ const res = await app.request("/auth/sessions", {
806
+ method: "DELETE",
807
+ headers: { ...authHeader() },
808
+ });
809
+ expect(res.status).toBe(200);
810
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
811
+ });
812
+
813
+ it("DELETE /auth/sessions/:id revokes specific session", async () => {
814
+ const app = createApp();
815
+ const res = await app.request("/auth/sessions/s123", {
816
+ method: "DELETE",
817
+ headers: { ...authHeader() },
818
+ });
819
+ expect(res.status).toBe(200);
820
+ expect(mockAuthRepo.deleteRefreshTokenById).toHaveBeenCalledWith("s123", "user-1");
821
+ });
822
+
823
+ it("sessions endpoints return 401 without auth", async () => {
824
+ const app = createApp();
825
+ const res1 = await app.request("/auth/sessions");
826
+ expect(res1.status).toBe(401);
827
+
828
+ const res2 = await app.request("/auth/sessions", { method: "DELETE" });
829
+ expect(res2.status).toBe(401);
830
+ });
831
+ });
832
+
833
+ // ── Auth Config ─────────────────────────────────────────────────────
834
+ describe("GET /auth/config", () => {
835
+ it("returns setup status when no users exist", async () => {
836
+ const app = createApp();
837
+ mockAuthRepo.listUsers.mockResolvedValueOnce([]);
838
+
839
+ const res = await app.request("/auth/config");
840
+ expect(res.status).toBe(200);
841
+ const body = await res.json() as any;
842
+ expect(body.needsSetup).toBe(true);
843
+ expect(body.registrationEnabled).toBe(true); // always true when needsSetup
844
+ });
845
+
846
+ it("returns correct flags when users exist", async () => {
847
+ const app = createApp({ allowRegistration: false });
848
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]);
849
+
850
+ const res = await app.request("/auth/config");
851
+ expect(res.status).toBe(200);
852
+ const body = await res.json() as any;
853
+ expect(body.needsSetup).toBe(false);
854
+ expect(body.registrationEnabled).toBe(false);
855
+ expect(body.googleEnabled).toBe(false);
856
+ });
857
+
858
+ it("reports Google enabled when configured", async () => {
859
+ const app = createApp();
860
+ (isGoogleOAuthConfigured as jest.Mock).mockReturnValue(true);
861
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]);
862
+
863
+ const res = await app.request("/auth/config");
864
+ const body = await res.json() as any;
865
+ expect(body.googleEnabled).toBe(true);
866
+ });
867
+ });
868
+ });