@rebasepro/server-core 0.0.1-canary.09e5ec5

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 (300) 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 +56 -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 +58 -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 +22 -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-DXVBFp5V.js +37 -0
  78. package/dist/index-DXVBFp5V.js.map +1 -0
  79. package/dist/index.es.js +49934 -0
  80. package/dist/index.es.js.map +1 -0
  81. package/dist/index.umd.js +49968 -0
  82. package/dist/index.umd.js.map +1 -0
  83. package/dist/server-core/src/api/ast-schema-editor.d.ts +21 -0
  84. package/dist/server-core/src/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
  85. package/dist/server-core/src/api/errors.d.ts +35 -0
  86. package/dist/server-core/src/api/graphql/graphql-schema-generator.d.ts +35 -0
  87. package/dist/server-core/src/api/graphql/index.d.ts +1 -0
  88. package/dist/server-core/src/api/index.d.ts +9 -0
  89. package/dist/server-core/src/api/openapi-generator.d.ts +16 -0
  90. package/dist/server-core/src/api/rest/api-generator.d.ts +64 -0
  91. package/dist/server-core/src/api/rest/index.d.ts +1 -0
  92. package/dist/server-core/src/api/rest/query-parser.d.ts +9 -0
  93. package/dist/server-core/src/api/schema-editor-routes.d.ts +3 -0
  94. package/dist/server-core/src/api/server.d.ts +40 -0
  95. package/dist/server-core/src/api/types.d.ts +90 -0
  96. package/dist/server-core/src/auth/admin-routes.d.ts +16 -0
  97. package/dist/server-core/src/auth/apple-oauth.d.ts +30 -0
  98. package/dist/server-core/src/auth/bitbucket-oauth.d.ts +11 -0
  99. package/dist/server-core/src/auth/discord-oauth.d.ts +14 -0
  100. package/dist/server-core/src/auth/facebook-oauth.d.ts +14 -0
  101. package/dist/server-core/src/auth/github-oauth.d.ts +15 -0
  102. package/dist/server-core/src/auth/gitlab-oauth.d.ts +13 -0
  103. package/dist/server-core/src/auth/google-oauth.d.ts +14 -0
  104. package/dist/server-core/src/auth/index.d.ts +23 -0
  105. package/dist/server-core/src/auth/interfaces.d.ts +309 -0
  106. package/dist/server-core/src/auth/jwt.d.ts +43 -0
  107. package/dist/server-core/src/auth/linkedin-oauth.d.ts +18 -0
  108. package/dist/server-core/src/auth/microsoft-oauth.d.ts +16 -0
  109. package/dist/server-core/src/auth/middleware.d.ts +81 -0
  110. package/dist/server-core/src/auth/password.d.ts +22 -0
  111. package/dist/server-core/src/auth/rate-limiter.d.ts +31 -0
  112. package/dist/server-core/src/auth/routes.d.ts +27 -0
  113. package/dist/server-core/src/auth/slack-oauth.d.ts +12 -0
  114. package/dist/server-core/src/auth/spotify-oauth.d.ts +12 -0
  115. package/dist/server-core/src/auth/twitter-oauth.d.ts +18 -0
  116. package/dist/server-core/src/bootstrappers/index.d.ts +0 -0
  117. package/dist/server-core/src/collections/BackendCollectionRegistry.d.ts +13 -0
  118. package/dist/server-core/src/collections/loader.d.ts +5 -0
  119. package/dist/server-core/src/cron/cron-loader.d.ts +17 -0
  120. package/dist/server-core/src/cron/cron-routes.d.ts +14 -0
  121. package/dist/server-core/src/cron/cron-scheduler.d.ts +61 -0
  122. package/dist/server-core/src/cron/cron-store.d.ts +32 -0
  123. package/dist/server-core/src/cron/index.d.ts +6 -0
  124. package/dist/server-core/src/db/interfaces.d.ts +18 -0
  125. package/dist/server-core/src/email/index.d.ts +6 -0
  126. package/dist/server-core/src/email/smtp-email-service.d.ts +25 -0
  127. package/dist/server-core/src/email/templates.d.ts +42 -0
  128. package/dist/server-core/src/email/types.d.ts +107 -0
  129. package/dist/server-core/src/functions/function-loader.d.ts +17 -0
  130. package/dist/server-core/src/functions/function-routes.d.ts +10 -0
  131. package/dist/server-core/src/functions/index.d.ts +3 -0
  132. package/dist/server-core/src/history/history-routes.d.ts +23 -0
  133. package/dist/server-core/src/history/index.d.ts +1 -0
  134. package/dist/server-core/src/index.d.ts +29 -0
  135. package/dist/server-core/src/init.d.ts +159 -0
  136. package/dist/server-core/src/serve-spa.d.ts +30 -0
  137. package/dist/server-core/src/services/driver-registry.d.ts +78 -0
  138. package/dist/server-core/src/singleton.d.ts +35 -0
  139. package/dist/server-core/src/storage/LocalStorageController.d.ts +46 -0
  140. package/dist/server-core/src/storage/S3StorageController.d.ts +36 -0
  141. package/dist/server-core/src/storage/index.d.ts +25 -0
  142. package/dist/server-core/src/storage/routes.d.ts +38 -0
  143. package/dist/server-core/src/storage/storage-registry.d.ts +78 -0
  144. package/dist/server-core/src/storage/types.d.ts +103 -0
  145. package/dist/server-core/src/types/index.d.ts +11 -0
  146. package/dist/server-core/src/utils/dev-port.d.ts +35 -0
  147. package/dist/server-core/src/utils/logger.d.ts +31 -0
  148. package/dist/server-core/src/utils/logging.d.ts +9 -0
  149. package/dist/server-core/src/utils/request-logger.d.ts +19 -0
  150. package/dist/server-core/src/utils/sql.d.ts +27 -0
  151. package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
  152. package/dist/types/src/controllers/auth.d.ts +119 -0
  153. package/dist/types/src/controllers/client.d.ts +170 -0
  154. package/dist/types/src/controllers/collection_registry.d.ts +45 -0
  155. package/dist/types/src/controllers/customization_controller.d.ts +60 -0
  156. package/dist/types/src/controllers/data.d.ts +168 -0
  157. package/dist/types/src/controllers/data_driver.d.ts +160 -0
  158. package/dist/types/src/controllers/database_admin.d.ts +11 -0
  159. package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
  160. package/dist/types/src/controllers/effective_role.d.ts +4 -0
  161. package/dist/types/src/controllers/email.d.ts +34 -0
  162. package/dist/types/src/controllers/index.d.ts +18 -0
  163. package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
  164. package/dist/types/src/controllers/navigation.d.ts +213 -0
  165. package/dist/types/src/controllers/registry.d.ts +54 -0
  166. package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
  167. package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
  168. package/dist/types/src/controllers/snackbar.d.ts +24 -0
  169. package/dist/types/src/controllers/storage.d.ts +171 -0
  170. package/dist/types/src/index.d.ts +4 -0
  171. package/dist/types/src/rebase_context.d.ts +105 -0
  172. package/dist/types/src/types/backend.d.ts +536 -0
  173. package/dist/types/src/types/builders.d.ts +15 -0
  174. package/dist/types/src/types/chips.d.ts +5 -0
  175. package/dist/types/src/types/collections.d.ts +856 -0
  176. package/dist/types/src/types/cron.d.ts +102 -0
  177. package/dist/types/src/types/data_source.d.ts +64 -0
  178. package/dist/types/src/types/entities.d.ts +145 -0
  179. package/dist/types/src/types/entity_actions.d.ts +98 -0
  180. package/dist/types/src/types/entity_callbacks.d.ts +173 -0
  181. package/dist/types/src/types/entity_link_builder.d.ts +7 -0
  182. package/dist/types/src/types/entity_overrides.d.ts +10 -0
  183. package/dist/types/src/types/entity_views.d.ts +61 -0
  184. package/dist/types/src/types/export_import.d.ts +21 -0
  185. package/dist/types/src/types/index.d.ts +23 -0
  186. package/dist/types/src/types/locales.d.ts +4 -0
  187. package/dist/types/src/types/modify_collections.d.ts +5 -0
  188. package/dist/types/src/types/plugins.d.ts +279 -0
  189. package/dist/types/src/types/properties.d.ts +1176 -0
  190. package/dist/types/src/types/property_config.d.ts +70 -0
  191. package/dist/types/src/types/relations.d.ts +336 -0
  192. package/dist/types/src/types/slots.d.ts +252 -0
  193. package/dist/types/src/types/translations.d.ts +870 -0
  194. package/dist/types/src/types/user_management_delegate.d.ts +121 -0
  195. package/dist/types/src/types/websockets.d.ts +78 -0
  196. package/dist/types/src/users/index.d.ts +2 -0
  197. package/dist/types/src/users/roles.d.ts +22 -0
  198. package/dist/types/src/users/user.d.ts +46 -0
  199. package/history_diff.log +385 -0
  200. package/jest.config.cjs +16 -0
  201. package/package.json +86 -0
  202. package/scratch.ts +9 -0
  203. package/src/api/ast-schema-editor.ts +289 -0
  204. package/src/api/collections_for_test/callbacks_test_collection.ts +60 -0
  205. package/src/api/errors.ts +179 -0
  206. package/src/api/graphql/graphql-schema-generator.ts +336 -0
  207. package/src/api/graphql/index.ts +2 -0
  208. package/src/api/index.ts +11 -0
  209. package/src/api/openapi-generator.ts +715 -0
  210. package/src/api/rest/api-generator.ts +472 -0
  211. package/src/api/rest/index.ts +2 -0
  212. package/src/api/rest/query-parser.ts +155 -0
  213. package/src/api/schema-editor-routes.ts +41 -0
  214. package/src/api/server.ts +248 -0
  215. package/src/api/types.ts +90 -0
  216. package/src/auth/admin-routes.ts +529 -0
  217. package/src/auth/apple-oauth.ts +130 -0
  218. package/src/auth/bitbucket-oauth.ts +82 -0
  219. package/src/auth/discord-oauth.ts +83 -0
  220. package/src/auth/facebook-oauth.ts +72 -0
  221. package/src/auth/github-oauth.ts +110 -0
  222. package/src/auth/gitlab-oauth.ts +70 -0
  223. package/src/auth/google-oauth.ts +48 -0
  224. package/src/auth/index.ts +34 -0
  225. package/src/auth/interfaces.ts +363 -0
  226. package/src/auth/jwt.ts +181 -0
  227. package/src/auth/linkedin-oauth.ts +81 -0
  228. package/src/auth/microsoft-oauth.ts +88 -0
  229. package/src/auth/middleware.ts +384 -0
  230. package/src/auth/password.ts +77 -0
  231. package/src/auth/rate-limiter.ts +129 -0
  232. package/src/auth/routes.ts +788 -0
  233. package/src/auth/slack-oauth.ts +71 -0
  234. package/src/auth/spotify-oauth.ts +67 -0
  235. package/src/auth/twitter-oauth.ts +120 -0
  236. package/src/bootstrappers/index.ts +1 -0
  237. package/src/collections/BackendCollectionRegistry.ts +20 -0
  238. package/src/collections/loader.ts +49 -0
  239. package/src/cron/cron-loader.ts +89 -0
  240. package/src/cron/cron-routes.test.ts +265 -0
  241. package/src/cron/cron-routes.ts +85 -0
  242. package/src/cron/cron-scheduler.test.ts +421 -0
  243. package/src/cron/cron-scheduler.ts +413 -0
  244. package/src/cron/cron-store.ts +163 -0
  245. package/src/cron/index.ts +6 -0
  246. package/src/db/interfaces.ts +60 -0
  247. package/src/email/index.ts +18 -0
  248. package/src/email/smtp-email-service.ts +91 -0
  249. package/src/email/templates.ts +388 -0
  250. package/src/email/types.ts +105 -0
  251. package/src/functions/function-loader.ts +119 -0
  252. package/src/functions/function-routes.ts +31 -0
  253. package/src/functions/index.ts +3 -0
  254. package/src/history/history-routes.ts +129 -0
  255. package/src/history/index.ts +2 -0
  256. package/src/index.ts +66 -0
  257. package/src/init.ts +727 -0
  258. package/src/serve-spa.ts +81 -0
  259. package/src/services/driver-registry.ts +182 -0
  260. package/src/singleton.test.ts +28 -0
  261. package/src/singleton.ts +70 -0
  262. package/src/storage/LocalStorageController.ts +365 -0
  263. package/src/storage/S3StorageController.ts +298 -0
  264. package/src/storage/index.ts +43 -0
  265. package/src/storage/routes.ts +264 -0
  266. package/src/storage/storage-registry.ts +187 -0
  267. package/src/storage/types.ts +134 -0
  268. package/src/types/index.ts +27 -0
  269. package/src/utils/dev-port.ts +176 -0
  270. package/src/utils/logger.ts +143 -0
  271. package/src/utils/logging.ts +38 -0
  272. package/src/utils/request-logger.ts +66 -0
  273. package/src/utils/sql.ts +38 -0
  274. package/test/admin-routes.test.ts +640 -0
  275. package/test/api-generator.test.ts +501 -0
  276. package/test/ast-schema-editor.test.ts +63 -0
  277. package/test/auth-middleware-hono.test.ts +556 -0
  278. package/test/auth-routes.test.ts +1047 -0
  279. package/test/driver-registry.test.ts +282 -0
  280. package/test/error-propagation.test.ts +226 -0
  281. package/test/errors-hono.test.ts +133 -0
  282. package/test/errors.test.ts +155 -0
  283. package/test/jwt-security.test.ts +182 -0
  284. package/test/jwt.test.ts +324 -0
  285. package/test/middleware.test.ts +300 -0
  286. package/test/password.test.ts +165 -0
  287. package/test/query-parser.test.ts +263 -0
  288. package/test/rate-limiter.test.ts +102 -0
  289. package/test/safe-compare.test.ts +66 -0
  290. package/test/singleton.test.ts +59 -0
  291. package/test/storage-local.test.ts +271 -0
  292. package/test/storage-registry.test.ts +282 -0
  293. package/test/storage-routes.test.ts +222 -0
  294. package/test/storage-s3.test.ts +304 -0
  295. package/test-ast.ts +28 -0
  296. package/test.ts +6 -0
  297. package/test_output.txt +1133 -0
  298. package/tsconfig.json +49 -0
  299. package/tsconfig.prod.json +20 -0
  300. package/vite.config.ts +80 -0
@@ -0,0 +1,1047 @@
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
+
20
+ // Bypass rate limiters — they share state across tests and cause 429s
21
+ jest.mock("../src/auth/rate-limiter", () => {
22
+ const passthrough = async (_c: unknown, next: () => Promise<void>) => next();
23
+ return {
24
+ createRateLimiter: () => passthrough,
25
+ defaultAuthLimiter: passthrough,
26
+ strictAuthLimiter: passthrough
27
+ };
28
+ });
29
+
30
+ import { hashPassword, verifyPassword, validatePasswordStrength } from "../src/auth/password";
31
+ import { z } from "zod";
32
+
33
+ // ── Helpers ─────────────────────────────────────────────────────────────────
34
+
35
+ const TEST_SECRET = "integration-test-secret-key-that-is-definitely-32-chars-long!!";
36
+
37
+ function mockUser(overrides: Partial<{ id: string; email: string; passwordHash: string | null; displayName: string | null; photoUrl: string | null; provider: string; emailVerified: boolean; emailVerificationToken: string | null }> = {}) {
38
+ return {
39
+ id: overrides.id ?? "user-1",
40
+ email: overrides.email ?? "test@example.com",
41
+ passwordHash: "passwordHash" in overrides ? overrides.passwordHash : "salt:hash",
42
+ displayName: overrides.displayName ?? "Test User",
43
+ photoUrl: overrides.photoUrl ?? null,
44
+ emailVerified: overrides.emailVerified ?? false,
45
+ emailVerificationToken: overrides.emailVerificationToken ?? null,
46
+ emailVerificationSentAt: null,
47
+ createdAt: new Date(),
48
+ updatedAt: new Date()
49
+ };
50
+ }
51
+
52
+ function mockRole(id: string, isAdmin = false) {
53
+ return { id,
54
+ name: id.charAt(0).toUpperCase() + id.slice(1),
55
+ isAdmin,
56
+ defaultPermissions: null,
57
+ collectionPermissions: null,
58
+ config: null };
59
+ }
60
+
61
+ let mockAuthRepo: jest.Mocked<AuthRepository>;
62
+ let mockEmailService: { send: jest.Mock; isConfigured: jest.Mock };
63
+
64
+ function createApp(opts: { allowRegistration?: boolean; withEmail?: boolean; defaultRole?: string; isBootstrapCompleted?: () => Promise<boolean> } = {}) {
65
+ // Re-create mocked service instances each time
66
+
67
+ // Wire constructor mocks to return our instances
68
+
69
+ // Default returns for mocked services
70
+
71
+ mockAuthRepo = {
72
+ getUserByEmail: jest.fn().mockResolvedValue(null),
73
+ getUserByIdentity: jest.fn().mockResolvedValue(null),
74
+ linkUserIdentity: jest.fn().mockResolvedValue(undefined),
75
+ getUserIdentities: jest.fn().mockResolvedValue([]),
76
+ getUserById: jest.fn().mockResolvedValue(null),
77
+ createUser: jest.fn().mockImplementation((data) =>
78
+ Promise.resolve(mockUser({ email: data.email,
79
+ displayName: data.displayName,
80
+ passwordHash: data.passwordHash }))
81
+ ),
82
+ listUsers: jest.fn().mockResolvedValue([]),
83
+ getUserRoles: jest.fn().mockResolvedValue([mockRole("editor")]),
84
+ getUserRoleIds: jest.fn().mockResolvedValue(["editor"]),
85
+ assignDefaultRole: jest.fn().mockResolvedValue(undefined),
86
+ setUserRoles: jest.fn().mockResolvedValue(undefined),
87
+ updateUser: jest.fn().mockImplementation((id, data) =>
88
+ Promise.resolve(mockUser({ id,
89
+ ...data }))
90
+ ),
91
+ deleteUser: jest.fn().mockResolvedValue(undefined),
92
+ updatePassword: jest.fn().mockResolvedValue(undefined),
93
+ setEmailVerified: jest.fn().mockResolvedValue(undefined),
94
+ setVerificationToken: jest.fn().mockResolvedValue(undefined),
95
+ getUserByVerificationToken: jest.fn().mockResolvedValue(null),
96
+ getUserWithRoles: jest.fn().mockImplementation(async (userId) => {
97
+ const user = mockUser({ id: userId });
98
+ return { user,
99
+ roles: [mockRole("editor")] };
100
+ }),
101
+ createRefreshToken: jest.fn().mockResolvedValue(undefined),
102
+ findRefreshTokenByHash: jest.fn().mockResolvedValue(null),
103
+ deleteRefreshToken: jest.fn().mockResolvedValue(undefined),
104
+ deleteAllRefreshTokensForUser: jest.fn().mockResolvedValue(undefined),
105
+ listRefreshTokensForUser: jest.fn().mockResolvedValue([]),
106
+ deleteRefreshTokenById: jest.fn().mockResolvedValue(undefined),
107
+ createPasswordResetToken: jest.fn().mockResolvedValue(undefined),
108
+ findValidPasswordResetToken: jest.fn().mockResolvedValue(null),
109
+ markPasswordResetTokenUsed: jest.fn().mockResolvedValue(undefined),
110
+ deleteExpiredPasswordResetTokens: jest.fn().mockResolvedValue(undefined)
111
+ } as unknown as jest.Mocked<AuthRepository>;
112
+
113
+
114
+ // Password mocks
115
+ (validatePasswordStrength as jest.Mock).mockReturnValue({ valid: true,
116
+ errors: [] });
117
+ (hashPassword as jest.Mock).mockResolvedValue("hashed-pw");
118
+ (verifyPassword as jest.Mock).mockResolvedValue(true);
119
+
120
+ // Email mock
121
+ mockEmailService = { send: jest.fn().mockResolvedValue(undefined),
122
+ isConfigured: jest.fn().mockReturnValue(opts.withEmail ?? false) };
123
+
124
+ const config: AuthModuleConfig = {
125
+ authRepo: mockAuthRepo,
126
+ allowRegistration: opts.allowRegistration ?? true,
127
+ defaultRole: opts.defaultRole,
128
+ emailService: opts.withEmail ? mockEmailService as any : undefined,
129
+ emailConfig: opts.withEmail ? { from: "test@test.com",
130
+ appName: "TestApp",
131
+ resetPasswordUrl: "https://app.test",
132
+ verifyEmailUrl: "https://app.test" } : undefined,
133
+ oauthProviders: opts.withEmail === false && opts.allowRegistration === false ? [] : [
134
+ {
135
+ id: "google",
136
+ schema: z.object({ idToken: z.string().min(1) }),
137
+ verify: async (payload: any) => {
138
+ const idToken = payload.idToken;
139
+ if (idToken === "bad-token") return null;
140
+ if (idToken === "link-token") return { providerId: "g-456",
141
+ email: "existing@test.com",
142
+ displayName: "Existing",
143
+ photoUrl: null };
144
+ if (idToken === "returning-token") return { providerId: "g-789",
145
+ email: "returning@test.com",
146
+ displayName: "Updated Name",
147
+ photoUrl: "https://new-photo.url" };
148
+ if (idToken === "valid-token") return { providerId: "g-123",
149
+ email: "google@test.com",
150
+ displayName: "Google User",
151
+ photoUrl: "https://photo.url" };
152
+ return null;
153
+ }
154
+ }
155
+ ]
156
+ };
157
+
158
+ if (opts.isBootstrapCompleted) {
159
+ config.isBootstrapCompleted = opts.isBootstrapCompleted;
160
+ }
161
+
162
+
163
+ const app = new Hono<HonoEnv>();
164
+ app.onError(errorHandler);
165
+ app.route("/auth", createAuthRoutes(config));
166
+ return app;
167
+ }
168
+
169
+ function json(body: Record<string, unknown>) {
170
+ return {
171
+ method: "POST" as const,
172
+ headers: { "Content-Type": "application/json" },
173
+ body: JSON.stringify(body)
174
+ };
175
+ }
176
+
177
+ function authHeader(userId = "user-1", roles = ["editor"]) {
178
+ return { Authorization: `Bearer ${generateAccessToken(userId, roles)}` };
179
+ }
180
+
181
+ // ═══════════════════════════════════════════════════════════════════════════
182
+ // TESTS
183
+ // ═══════════════════════════════════════════════════════════════════════════
184
+
185
+ describe("Auth Routes (Integration)", () => {
186
+ beforeAll(() => {
187
+ configureJwt({ secret: TEST_SECRET,
188
+ accessExpiresIn: "1h" });
189
+ });
190
+
191
+ beforeEach(() => {
192
+ jest.clearAllMocks();
193
+ });
194
+
195
+ describe("Configuration Security", () => {
196
+ it("throws an error when defaultRole is set to 'admin'", () => {
197
+ expect(() => createApp({ defaultRole: "admin" })).toThrowError(/CRITICAL SECURITY ERROR/);
198
+ });
199
+ });
200
+
201
+ // ── Registration ────────────────────────────────────────────────────
202
+ describe("POST /auth/register", () => {
203
+ it("registers a new user and returns 201 with tokens", async () => {
204
+ const app = createApp();
205
+
206
+ const res = await app.request("/auth/register", json({ email: "new@test.com",
207
+ password: "StrongPass1" }));
208
+ expect(res.status).toBe(201);
209
+ const body = await res.json() as any;
210
+ expect(body.tokens.accessToken).toBeTruthy();
211
+ expect(body.tokens.refreshToken).toBeTruthy();
212
+ expect(body.user.email).toBe("new@test.com");
213
+ });
214
+
215
+ it("first user does NOT get admin role (must use bootstrap)", async () => {
216
+ const app = createApp();
217
+
218
+ await app.request("/auth/register", json({ email: "first@test.com",
219
+ password: "StrongPass1" }));
220
+ // No admin assignment — users must bootstrap via POST /admin/bootstrap
221
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalled();
222
+ });
223
+
224
+ it("assigns configured default role to new users", async () => {
225
+ const app = createApp({ defaultRole: "editor" });
226
+
227
+ await app.request("/auth/register", json({ email: "second@test.com",
228
+ password: "StrongPass1" }));
229
+ expect(mockAuthRepo.assignDefaultRole).toHaveBeenCalledWith(expect.any(String), "editor");
230
+ });
231
+
232
+ it("does not assign role when defaultRole is not configured", async () => {
233
+ const app = createApp();
234
+
235
+ await app.request("/auth/register", json({ email: "third@test.com",
236
+ password: "StrongPass1" }));
237
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalled();
238
+ });
239
+
240
+ it("returns 409 when email already exists", async () => {
241
+ const app = createApp();
242
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
243
+
244
+ const res = await app.request("/auth/register", json({ email: "existing@test.com",
245
+ password: "StrongPass1" }));
246
+ expect(res.status).toBe(409);
247
+ const body = await res.json() as any;
248
+ expect(body.error.code).toBe("EMAIL_EXISTS");
249
+ });
250
+
251
+ it("returns 400 for weak password", async () => {
252
+ const app = createApp();
253
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false,
254
+ errors: ["Too short"] });
255
+
256
+ const res = await app.request("/auth/register", json({ email: "new@test.com",
257
+ password: "weak" }));
258
+ expect(res.status).toBe(400);
259
+ const body = await res.json() as any;
260
+ expect(body.error.code).toBe("WEAK_PASSWORD");
261
+ });
262
+
263
+ it("returns 400 for invalid email (Zod)", async () => {
264
+ const app = createApp();
265
+ const res = await app.request("/auth/register", json({ email: "not-an-email",
266
+ password: "StrongPass1" }));
267
+ expect(res.status).toBe(400);
268
+ const body = await res.json() as any;
269
+ expect(body.error.code).toBe("INVALID_INPUT");
270
+ });
271
+
272
+ it("returns 400 for missing password", async () => {
273
+ const app = createApp();
274
+ const res = await app.request("/auth/register", json({ email: "a@b.com" }));
275
+ expect(res.status).toBe(400);
276
+ });
277
+
278
+ it("returns 403 when registration is disabled", async () => {
279
+ const app = createApp({ allowRegistration: false });
280
+
281
+ const res = await app.request("/auth/register", json({ email: "new@test.com",
282
+ password: "StrongPass1" }));
283
+ expect(res.status).toBe(403);
284
+ const body = await res.json() as any;
285
+ expect(body.error.code).toBe("REGISTRATION_DISABLED");
286
+ });
287
+
288
+ it("blocks registration even on empty database when allowRegistration=false", async () => {
289
+ const app = createApp({ allowRegistration: false });
290
+ // Even with no users, registration should be denied
291
+ mockAuthRepo.listUsers.mockResolvedValueOnce([]);
292
+
293
+ const res = await app.request("/auth/register", json({ email: "first@test.com",
294
+ password: "StrongPass1" }));
295
+ expect(res.status).toBe(403);
296
+ const body = await res.json() as any;
297
+ expect(body.error.code).toBe("REGISTRATION_DISABLED");
298
+ });
299
+
300
+ it("stores refresh token after registration", async () => {
301
+ const app = createApp();
302
+
303
+ await app.request("/auth/register", json({ email: "a@b.com",
304
+ password: "StrongPass1" }));
305
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
306
+ });
307
+ });
308
+
309
+ // ── Login ───────────────────────────────────────────────────────────
310
+ describe("POST /auth/login", () => {
311
+ it("returns tokens on successful login", async () => {
312
+ const app = createApp();
313
+ const user = mockUser({ passwordHash: "salt:hash" });
314
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(user);
315
+
316
+ const res = await app.request("/auth/login", json({ email: "test@example.com",
317
+ password: "ValidPass1" }));
318
+ expect(res.status).toBe(200);
319
+ const body = await res.json() as any;
320
+ expect(body.tokens.accessToken).toBeTruthy();
321
+ expect(body.user.uid).toBe("user-1");
322
+ });
323
+
324
+ it("returns 401 for non-existent email", async () => {
325
+ const app = createApp();
326
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
327
+
328
+ const res = await app.request("/auth/login", json({ email: "nobody@test.com",
329
+ password: "Any1" }));
330
+ expect(res.status).toBe(401);
331
+ const body = await res.json() as any;
332
+ expect(body.error.code).toBe("INVALID_CREDENTIALS");
333
+ });
334
+
335
+ it("returns 401 for wrong password", async () => {
336
+ const app = createApp();
337
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
338
+ (verifyPassword as jest.Mock).mockResolvedValueOnce(false);
339
+
340
+ const res = await app.request("/auth/login", json({ email: "test@example.com",
341
+ password: "Wrong1" }));
342
+ expect(res.status).toBe(401);
343
+ });
344
+
345
+ it("returns 401 for user without password hash (Google-only)", async () => {
346
+ const app = createApp();
347
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser({ passwordHash: null }));
348
+
349
+ const res = await app.request("/auth/login", json({ email: "google@test.com",
350
+ password: "Any1" }));
351
+ expect(res.status).toBe(401);
352
+ });
353
+
354
+ it("returns 400 for missing email field", async () => {
355
+ const app = createApp();
356
+ const res = await app.request("/auth/login", json({ password: "Any1" }));
357
+ expect(res.status).toBe(400);
358
+ });
359
+
360
+ it("stores refresh token on login", async () => {
361
+ const app = createApp();
362
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
363
+
364
+ await app.request("/auth/login", json({ email: "test@example.com",
365
+ password: "ValidPass1" }));
366
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
367
+ });
368
+ });
369
+
370
+ // ── Google OAuth ────────────────────────────────────────────────────
371
+ describe("POST /auth/google", () => {
372
+ it("returns 404 when OAuth provider is not injected", async () => {
373
+ const app = createApp({ allowRegistration: false,
374
+ withEmail: false }); // Hack to pass empty list of providers
375
+ const res = await app.request("/auth/google", json({ idToken: "google-token" }));
376
+ expect(res.status).toBe(404);
377
+ });
378
+
379
+ it("returns 401 for invalid Google token", async () => {
380
+ const app = createApp();
381
+
382
+ const res = await app.request("/auth/google", json({ idToken: "bad-token" }));
383
+ expect(res.status).toBe(401);
384
+ const body = await res.json() as any;
385
+ expect(body.error.code).toBe("INVALID_TOKEN");
386
+ });
387
+
388
+ it("creates a new user for new Google sign-in", async () => {
389
+ const app = createApp();
390
+ mockAuthRepo.getUserByIdentity.mockResolvedValueOnce(null);
391
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
392
+
393
+ const res = await app.request("/auth/google", json({ idToken: "valid-token" }));
394
+ expect(res.status).toBe(200);
395
+ expect(mockAuthRepo.createUser).toHaveBeenCalledWith(expect.objectContaining({
396
+ email: "google@test.com"
397
+ }));
398
+ expect(mockAuthRepo.linkUserIdentity).toHaveBeenCalledWith(expect.any(String), "google", "g-123", expect.any(Object));
399
+ // Verify no admin auto-assignment
400
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalled();
401
+ });
402
+
403
+ it("links Google to existing account by email", async () => {
404
+ const app = createApp();
405
+ const existing = mockUser({ email: "existing@test.com" });
406
+ mockAuthRepo.getUserByIdentity.mockResolvedValueOnce(null);
407
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(existing);
408
+
409
+ const res = await app.request("/auth/google", json({ idToken: "link-token" }));
410
+ expect(res.status).toBe(200);
411
+ expect(mockAuthRepo.linkUserIdentity).toHaveBeenCalledWith(existing.id, "google", "g-456", expect.any(Object));
412
+ });
413
+
414
+ it("updates profile for returning Google user", async () => {
415
+ const app = createApp();
416
+ const existingUser = mockUser({ id: "g-user-1" });
417
+ mockAuthRepo.getUserByIdentity.mockResolvedValueOnce(existingUser);
418
+
419
+ const res = await app.request("/auth/google", json({ idToken: "returning-token" }));
420
+ expect(res.status).toBe(200);
421
+ expect(mockAuthRepo.updateUser).toHaveBeenCalledWith(existingUser.id, expect.objectContaining({
422
+ displayName: "Updated Name",
423
+ photoUrl: "https://new-photo.url"
424
+ }));
425
+ });
426
+ });
427
+
428
+ // ── Token Refresh ───────────────────────────────────────────────────
429
+ describe("POST /auth/refresh", () => {
430
+ it("returns new tokens on valid refresh", async () => {
431
+ const app = createApp();
432
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
433
+ id: "rt-1",
434
+ userId: "user-1",
435
+ tokenHash: "old-hash",
436
+ expiresAt: new Date(Date.now() + 86400000),
437
+ createdAt: new Date(),
438
+ userAgent: "",
439
+ ipAddress: ""
440
+ });
441
+ mockAuthRepo.getUserRoles.mockResolvedValueOnce([mockRole("editor")]);
442
+
443
+ const res = await app.request("/auth/refresh", json({ refreshToken: "valid-refresh-token" }));
444
+ expect(res.status).toBe(200);
445
+ const body = await res.json() as any;
446
+ expect(body.tokens.accessToken).toBeTruthy();
447
+ expect(body.tokens.refreshToken).toBeTruthy();
448
+ });
449
+
450
+ it("rotates refresh token — deletes old, creates new", async () => {
451
+ const app = createApp();
452
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
453
+ id: "rt-1",
454
+ userId: "user-1",
455
+ tokenHash: "old-hash",
456
+ expiresAt: new Date(Date.now() + 86400000),
457
+ createdAt: new Date(),
458
+ userAgent: "",
459
+ ipAddress: ""
460
+ });
461
+
462
+ await app.request("/auth/refresh", json({ refreshToken: "the-token" }));
463
+ // Old token deleted
464
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalledTimes(1);
465
+ // New token stored
466
+ expect(mockAuthRepo.createRefreshToken).toHaveBeenCalledTimes(1);
467
+ });
468
+
469
+ it("returns 401 for unknown refresh token", async () => {
470
+ const app = createApp();
471
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce(null);
472
+
473
+ const res = await app.request("/auth/refresh", json({ refreshToken: "unknown" }));
474
+ expect(res.status).toBe(401);
475
+ const body = await res.json() as any;
476
+ expect(body.error.code).toBe("INVALID_TOKEN");
477
+ });
478
+
479
+ it("returns 401 and deletes expired refresh token", async () => {
480
+ const app = createApp();
481
+ mockAuthRepo.findRefreshTokenByHash.mockResolvedValueOnce({
482
+ id: "rt-1",
483
+ userId: "user-1",
484
+ tokenHash: "expired-hash",
485
+ expiresAt: new Date(Date.now() - 1000), // expired
486
+ createdAt: new Date(),
487
+ userAgent: "",
488
+ ipAddress: ""
489
+ });
490
+
491
+ const res = await app.request("/auth/refresh", json({ refreshToken: "expired-token" }));
492
+ expect(res.status).toBe(401);
493
+ const body = await res.json() as any;
494
+ expect(body.error.code).toBe("TOKEN_EXPIRED");
495
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalled();
496
+ });
497
+
498
+ it("returns 400 for missing refreshToken field", async () => {
499
+ const app = createApp();
500
+ const res = await app.request("/auth/refresh", json({}));
501
+ expect(res.status).toBe(400);
502
+ });
503
+ });
504
+
505
+ // ── Logout ──────────────────────────────────────────────────────────
506
+ describe("POST /auth/logout", () => {
507
+ it("deletes refresh token on logout", async () => {
508
+ const app = createApp();
509
+ const res = await app.request("/auth/logout", json({ refreshToken: "rt-to-delete" }));
510
+ expect(res.status).toBe(200);
511
+ expect(mockAuthRepo.deleteRefreshToken).toHaveBeenCalledTimes(1);
512
+ });
513
+
514
+ it("returns 200 even without refresh token", async () => {
515
+ const app = createApp();
516
+ const res = await app.request("/auth/logout", json({}));
517
+ expect(res.status).toBe(200);
518
+ expect(mockAuthRepo.deleteRefreshToken).not.toHaveBeenCalled();
519
+ });
520
+ });
521
+
522
+ // ── Forgot Password ─────────────────────────────────────────────────
523
+ describe("POST /auth/forgot-password", () => {
524
+ it("always returns success (timing-safe)", async () => {
525
+ const app = createApp({ withEmail: true });
526
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null); // user doesn't exist
527
+
528
+ const res = await app.request("/auth/forgot-password", json({ email: "nobody@test.com" }));
529
+ expect(res.status).toBe(200);
530
+ const body = await res.json() as any;
531
+ expect(body.success).toBe(true);
532
+ });
533
+
534
+ it("sends reset email when user exists", async () => {
535
+ const app = createApp({ withEmail: true });
536
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(mockUser());
537
+
538
+ await app.request("/auth/forgot-password", json({ email: "test@example.com" }));
539
+ expect(mockAuthRepo.createPasswordResetToken).toHaveBeenCalledTimes(1);
540
+ expect(mockEmailService.send).toHaveBeenCalledTimes(1);
541
+ });
542
+
543
+ it("does not send email when user does not exist", async () => {
544
+ const app = createApp({ withEmail: true });
545
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
546
+
547
+ await app.request("/auth/forgot-password", json({ email: "nobody@test.com" }));
548
+ expect(mockAuthRepo.createPasswordResetToken).not.toHaveBeenCalled();
549
+ expect(mockEmailService.send).not.toHaveBeenCalled();
550
+ });
551
+
552
+ it("returns 503 when email service is not configured", async () => {
553
+ const app = createApp({ withEmail: false });
554
+ const res = await app.request("/auth/forgot-password", json({ email: "test@test.com" }));
555
+ expect(res.status).toBe(503);
556
+ const body = await res.json() as any;
557
+ expect(body.error.code).toBe("EMAIL_NOT_CONFIGURED");
558
+ });
559
+ });
560
+
561
+ // ── Reset Password ──────────────────────────────────────────────────
562
+ describe("POST /auth/reset-password", () => {
563
+ it("resets password with valid token", async () => {
564
+ const app = createApp();
565
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce({
566
+ userId: "user-1",
567
+ expiresAt: new Date(Date.now() + 3600000)
568
+ });
569
+
570
+ const res = await app.request("/auth/reset-password", json({ token: "valid-reset-token",
571
+ password: "NewStrong1" }));
572
+ expect(res.status).toBe(200);
573
+ expect(mockAuthRepo.updatePassword).toHaveBeenCalledWith("user-1", "hashed-pw");
574
+ expect(mockAuthRepo.markPasswordResetTokenUsed).toHaveBeenCalled();
575
+ });
576
+
577
+ it("invalidates all sessions after password reset", async () => {
578
+ const app = createApp();
579
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce({
580
+ userId: "user-1",
581
+ expiresAt: new Date(Date.now() + 3600000)
582
+ });
583
+
584
+ await app.request("/auth/reset-password", json({ token: "token",
585
+ password: "NewStrong1" }));
586
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
587
+ });
588
+
589
+ it("returns 400 for invalid/expired token", async () => {
590
+ const app = createApp();
591
+ mockAuthRepo.findValidPasswordResetToken.mockResolvedValueOnce(null);
592
+
593
+ const res = await app.request("/auth/reset-password", json({ token: "expired",
594
+ password: "NewStrong1" }));
595
+ expect(res.status).toBe(400);
596
+ const body = await res.json() as any;
597
+ expect(body.error.code).toBe("INVALID_TOKEN");
598
+ });
599
+
600
+ it("returns 400 for weak new password", async () => {
601
+ const app = createApp();
602
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false,
603
+ errors: ["Too weak"] });
604
+
605
+ const res = await app.request("/auth/reset-password", json({ token: "token",
606
+ password: "weak" }));
607
+ expect(res.status).toBe(400);
608
+ const body = await res.json() as any;
609
+ expect(body.error.code).toBe("WEAK_PASSWORD");
610
+ });
611
+ });
612
+
613
+ // ── Change Password ─────────────────────────────────────────────────
614
+ describe("POST /auth/change-password", () => {
615
+ it("changes password for authenticated user", async () => {
616
+ const app = createApp();
617
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
618
+
619
+ const res = await app.request("/auth/change-password", {
620
+ ...json({ oldPassword: "OldPass1",
621
+ newPassword: "NewPass1" }),
622
+ headers: { ...json({}).headers,
623
+ ...authHeader() }
624
+ });
625
+ expect(res.status).toBe(200);
626
+ expect(mockAuthRepo.updatePassword).toHaveBeenCalled();
627
+ });
628
+
629
+ it("invalidates all sessions after password change", async () => {
630
+ const app = createApp();
631
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
632
+
633
+ await app.request("/auth/change-password", {
634
+ ...json({ oldPassword: "Old1",
635
+ newPassword: "New1Pass" }),
636
+ headers: { ...json({}).headers,
637
+ ...authHeader() }
638
+ });
639
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
640
+ });
641
+
642
+ it("returns 401 for wrong old password", async () => {
643
+ const app = createApp();
644
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
645
+ (verifyPassword as jest.Mock).mockResolvedValueOnce(false);
646
+
647
+ const res = await app.request("/auth/change-password", {
648
+ ...json({ oldPassword: "Wrong1",
649
+ newPassword: "New1Pass" }),
650
+ headers: { ...json({}).headers,
651
+ ...authHeader() }
652
+ });
653
+ expect(res.status).toBe(401);
654
+ });
655
+
656
+ it("returns 400 for weak new password", async () => {
657
+ const app = createApp();
658
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser());
659
+ (validatePasswordStrength as jest.Mock).mockReturnValueOnce({ valid: false,
660
+ errors: ["Too short"] });
661
+
662
+ const res = await app.request("/auth/change-password", {
663
+ ...json({ oldPassword: "Old1",
664
+ newPassword: "x" }),
665
+ headers: { ...json({}).headers,
666
+ ...authHeader() }
667
+ });
668
+ expect(res.status).toBe(400);
669
+ });
670
+
671
+ it("returns 401 without auth", async () => {
672
+ const app = createApp();
673
+ const res = await app.request("/auth/change-password", json({ oldPassword: "Old1",
674
+ newPassword: "New1Pass" }));
675
+ expect(res.status).toBe(401);
676
+ });
677
+
678
+ it("returns 400 for user without password (Google-only account)", async () => {
679
+ const app = createApp();
680
+ mockAuthRepo.getUserById.mockResolvedValue(mockUser({ passwordHash: null }));
681
+
682
+ const res = await app.request("/auth/change-password", {
683
+ ...json({ oldPassword: "Old1",
684
+ newPassword: "New1Pass" }),
685
+ headers: { ...json({}).headers,
686
+ ...authHeader() }
687
+ });
688
+ expect(res.status).toBe(400);
689
+ const body = await res.json() as any;
690
+ expect(body.error.code).toBe("INVALID_ACCOUNT");
691
+ });
692
+ });
693
+
694
+ // ── Email Verification ──────────────────────────────────────────────
695
+ describe("Email verification", () => {
696
+ describe("POST /auth/send-verification", () => {
697
+ it("sends verification email for authenticated user", async () => {
698
+ const app = createApp({ withEmail: true });
699
+ mockAuthRepo.getUserById.mockResolvedValueOnce(mockUser({ emailVerified: false }));
700
+
701
+ const res = await app.request("/auth/send-verification", {
702
+ method: "POST",
703
+ headers: { ...authHeader() }
704
+ });
705
+ expect(res.status).toBe(200);
706
+ expect(mockAuthRepo.setVerificationToken).toHaveBeenCalled();
707
+ expect(mockEmailService.send).toHaveBeenCalled();
708
+ });
709
+
710
+ it("returns 400 when email is already verified", async () => {
711
+ const app = createApp({ withEmail: true });
712
+ mockAuthRepo.getUserById.mockResolvedValueOnce(mockUser({ emailVerified: true }));
713
+
714
+ const res = await app.request("/auth/send-verification", {
715
+ method: "POST",
716
+ headers: { ...authHeader() }
717
+ });
718
+ expect(res.status).toBe(400);
719
+ const body = await res.json() as any;
720
+ expect(body.error.code).toBe("ALREADY_VERIFIED");
721
+ });
722
+
723
+ it("returns 401 without auth", async () => {
724
+ const app = createApp({ withEmail: true });
725
+ const res = await app.request("/auth/send-verification", { method: "POST" });
726
+ expect(res.status).toBe(401);
727
+ });
728
+
729
+ it("returns 503 when email service is not configured", async () => {
730
+ const app = createApp({ withEmail: false });
731
+ const res = await app.request("/auth/send-verification", {
732
+ method: "POST",
733
+ headers: { ...authHeader() }
734
+ });
735
+ expect(res.status).toBe(503);
736
+ });
737
+ });
738
+
739
+ describe("GET /auth/verify-email", () => {
740
+ it("verifies email with valid token", async () => {
741
+ const app = createApp();
742
+ mockAuthRepo.getUserByVerificationToken.mockResolvedValueOnce(mockUser());
743
+
744
+ const res = await app.request("/auth/verify-email?token=valid-token");
745
+ expect(res.status).toBe(200);
746
+ expect(mockAuthRepo.setEmailVerified).toHaveBeenCalledWith("user-1", true);
747
+ });
748
+
749
+ it("returns 400 for invalid verification token", async () => {
750
+ const app = createApp();
751
+ mockAuthRepo.getUserByVerificationToken.mockResolvedValueOnce(null);
752
+
753
+ const res = await app.request("/auth/verify-email?token=bad-token");
754
+ expect(res.status).toBe(400);
755
+ const body = await res.json() as any;
756
+ expect(body.error.code).toBe("INVALID_TOKEN");
757
+ });
758
+
759
+ it("returns 400 when token is missing", async () => {
760
+ const app = createApp();
761
+ const res = await app.request("/auth/verify-email");
762
+ expect(res.status).toBe(400);
763
+ });
764
+ });
765
+ });
766
+
767
+ // ── User Profile ────────────────────────────────────────────────────
768
+ describe("GET /auth/me", () => {
769
+ it("returns authenticated user with roles", async () => {
770
+ const app = createApp();
771
+ const res = await app.request("/auth/me", {
772
+ headers: { ...authHeader("user-1", ["admin"]) }
773
+ });
774
+ expect(res.status).toBe(200);
775
+ const body = await res.json() as any;
776
+ expect(body.user.uid).toBe("user-1");
777
+ expect(body.user.roles).toBeDefined();
778
+ });
779
+
780
+ it("returns 401 without auth", async () => {
781
+ const app = createApp();
782
+ const res = await app.request("/auth/me");
783
+ expect(res.status).toBe(401);
784
+ });
785
+
786
+ it("returns 404 when user is deleted", async () => {
787
+ const app = createApp();
788
+ mockAuthRepo.getUserWithRoles.mockResolvedValueOnce(null);
789
+
790
+ const res = await app.request("/auth/me", {
791
+ headers: { ...authHeader() }
792
+ });
793
+ expect(res.status).toBe(404);
794
+ });
795
+ });
796
+
797
+ describe("PATCH /auth/me", () => {
798
+ it("updates user profile", async () => {
799
+ const app = createApp();
800
+ mockAuthRepo.updateUser.mockResolvedValueOnce(mockUser({ displayName: "New Name" }));
801
+
802
+ const res = await app.request("/auth/me", {
803
+ method: "PATCH",
804
+ headers: { "Content-Type": "application/json",
805
+ ...authHeader() },
806
+ body: JSON.stringify({ displayName: "New Name" })
807
+ });
808
+ expect(res.status).toBe(200);
809
+ expect(mockAuthRepo.updateUser).toHaveBeenCalledWith("user-1", expect.objectContaining({
810
+ displayName: "New Name"
811
+ }));
812
+ });
813
+
814
+ it("returns 401 without auth", async () => {
815
+ const app = createApp();
816
+ const res = await app.request("/auth/me", {
817
+ method: "PATCH",
818
+ headers: { "Content-Type": "application/json" },
819
+ body: JSON.stringify({ displayName: "Name" })
820
+ });
821
+ expect(res.status).toBe(401);
822
+ });
823
+ });
824
+
825
+ // ── Sessions ────────────────────────────────────────────────────────
826
+ describe("Session management", () => {
827
+ it("GET /auth/sessions lists active sessions", async () => {
828
+ const app = createApp();
829
+ mockAuthRepo.listRefreshTokensForUser.mockResolvedValueOnce([
830
+ { id: "s1",
831
+ userId: "user-1",
832
+ tokenHash: "h1",
833
+ expiresAt: new Date(),
834
+ createdAt: new Date(),
835
+ userAgent: "Chrome",
836
+ ipAddress: "1.2.3.4" }
837
+ ]);
838
+
839
+ const res = await app.request("/auth/sessions", { headers: { ...authHeader() } });
840
+ expect(res.status).toBe(200);
841
+ const body = await res.json() as any;
842
+ expect(body.sessions).toHaveLength(1);
843
+ expect(body.sessions[0].id).toBe("s1");
844
+ });
845
+
846
+ it("DELETE /auth/sessions revokes all sessions", async () => {
847
+ const app = createApp();
848
+ const res = await app.request("/auth/sessions", {
849
+ method: "DELETE",
850
+ headers: { ...authHeader() }
851
+ });
852
+ expect(res.status).toBe(200);
853
+ expect(mockAuthRepo.deleteAllRefreshTokensForUser).toHaveBeenCalledWith("user-1");
854
+ });
855
+
856
+ it("DELETE /auth/sessions/:id revokes specific session", async () => {
857
+ const app = createApp();
858
+ const res = await app.request("/auth/sessions/s123", {
859
+ method: "DELETE",
860
+ headers: { ...authHeader() }
861
+ });
862
+ expect(res.status).toBe(200);
863
+ expect(mockAuthRepo.deleteRefreshTokenById).toHaveBeenCalledWith("s123", "user-1");
864
+ });
865
+
866
+ it("sessions endpoints return 401 without auth", async () => {
867
+ const app = createApp();
868
+ const res1 = await app.request("/auth/sessions");
869
+ expect(res1.status).toBe(401);
870
+
871
+ const res2 = await app.request("/auth/sessions", { method: "DELETE" });
872
+ expect(res2.status).toBe(401);
873
+ });
874
+ });
875
+
876
+ // ── Auth Config ─────────────────────────────────────────────────────
877
+ describe("GET /auth/config", () => {
878
+ it("returns needsSetup=true when isBootstrapCompleted returns false", async () => {
879
+ const app = createApp({ isBootstrapCompleted: async () => false });
880
+
881
+ const res = await app.request("/auth/config");
882
+ expect(res.status).toBe(200);
883
+ const body = await res.json() as any;
884
+ expect(body.needsSetup).toBe(true);
885
+ expect(body.registrationEnabled).toBe(true); // always true when needsSetup
886
+ });
887
+
888
+ it("returns needsSetup=false when isBootstrapCompleted returns true", async () => {
889
+ const app = createApp({ allowRegistration: false, isBootstrapCompleted: async () => true });
890
+
891
+ const res = await app.request("/auth/config");
892
+ expect(res.status).toBe(200);
893
+ const body = await res.json() as any;
894
+ expect(body.needsSetup).toBe(false);
895
+ expect(body.registrationEnabled).toBe(false);
896
+ });
897
+
898
+ it("falls back to user-count check when no isBootstrapCompleted callback", async () => {
899
+ const app = createApp();
900
+ mockAuthRepo.listUsers.mockResolvedValueOnce([]);
901
+
902
+ const res = await app.request("/auth/config");
903
+ expect(res.status).toBe(200);
904
+ const body = await res.json() as any;
905
+ expect(body.needsSetup).toBe(true);
906
+ expect(body.registrationEnabled).toBe(true);
907
+ });
908
+
909
+ it("returns correct flags when users exist and no callback", async () => {
910
+ const app = createApp({ allowRegistration: false,
911
+ withEmail: false });
912
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]);
913
+
914
+ const res = await app.request("/auth/config");
915
+ expect(res.status).toBe(200);
916
+ const body = await res.json() as any;
917
+ expect(body.needsSetup).toBe(false);
918
+ expect(body.registrationEnabled).toBe(false);
919
+ expect(body.googleEnabled).toBe(false);
920
+ });
921
+
922
+ it("reports Google enabled when configured", async () => {
923
+ const app = createApp();
924
+ mockAuthRepo.listUsers.mockResolvedValueOnce([mockUser()]);
925
+
926
+ const res = await app.request("/auth/config");
927
+ const body = await res.json() as any;
928
+ expect(body.googleEnabled).toBe(true);
929
+ });
930
+ });
931
+
932
+ // ═════════════════════════════════════════════════════════════════════
933
+ // SECURITY REGRESSION TESTS — CVE: First-User Admin Privilege Escalation
934
+ // These tests directly verify the exploit scenarios from the security audit.
935
+ // ═════════════════════════════════════════════════════════════════════
936
+
937
+ describe("Security: privilege escalation prevention", () => {
938
+ it("CVE-FIX: registration NEVER assigns admin role, even for first user", async () => {
939
+ const app = createApp({ allowRegistration: true });
940
+ // Simulate first user (empty database before, one user after create)
941
+ mockAuthRepo.listUsers.mockResolvedValueOnce([]);
942
+
943
+ const res = await app.request("/auth/register", json({
944
+ email: "hacker@evil.com",
945
+ password: "HackRebase2026!",
946
+ displayName: "Hacker"
947
+ }));
948
+
949
+ expect(res.status).toBe(201);
950
+ const body = await res.json() as any;
951
+
952
+ // The critical assertion: admin must NOT be in the roles
953
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalledWith(
954
+ expect.any(String), "admin"
955
+ );
956
+ // Verify the user was created but NOT given admin
957
+ expect(mockAuthRepo.createUser).toHaveBeenCalledTimes(1);
958
+ });
959
+
960
+ it("CVE-FIX: OAuth registration NEVER assigns admin role, even for first user", async () => {
961
+ const app = createApp({ allowRegistration: true });
962
+ mockAuthRepo.getUserByIdentity.mockResolvedValueOnce(null);
963
+ mockAuthRepo.getUserByEmail.mockResolvedValueOnce(null);
964
+
965
+ const res = await app.request("/auth/google", json({ idToken: "valid-token" }));
966
+ expect(res.status).toBe(200);
967
+
968
+ // The critical assertion: admin must NOT be assigned
969
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalledWith(
970
+ expect.any(String), "admin"
971
+ );
972
+ });
973
+
974
+ it("CVE-FIX: empty database does NOT bypass allowRegistration=false", async () => {
975
+ const app = createApp({ allowRegistration: false });
976
+ // Even with zero users, the registration endpoint must reject
977
+ mockAuthRepo.listUsers.mockResolvedValueOnce([]);
978
+
979
+ const res = await app.request("/auth/register", json({
980
+ email: "hacker@evil.com",
981
+ password: "HackRebase2026!",
982
+ displayName: "Hacker"
983
+ }));
984
+
985
+ expect(res.status).toBe(403);
986
+ const body = await res.json() as any;
987
+ expect(body.error.code).toBe("REGISTRATION_DISABLED");
988
+ // createUser must NOT have been called
989
+ expect(mockAuthRepo.createUser).not.toHaveBeenCalled();
990
+ });
991
+
992
+ it("CVE-FIX: registration is blocked when allowRegistration defaults to false", async () => {
993
+ const app = createApp({ allowRegistration: false });
994
+
995
+ const res = await app.request("/auth/register", json({
996
+ email: "hacker@evil.com",
997
+ password: "HackRebase2026!",
998
+ displayName: "Hacker"
999
+ }));
1000
+
1001
+ expect(res.status).toBe(403);
1002
+ const body = await res.json() as any;
1003
+ expect(body.error.code).toBe("REGISTRATION_DISABLED");
1004
+ expect(mockAuthRepo.createUser).not.toHaveBeenCalled();
1005
+ });
1006
+
1007
+ it("CVE-FIX: concurrent registration attempts cannot produce multiple admins", async () => {
1008
+ const app = createApp({ allowRegistration: true });
1009
+
1010
+ // Simulate 5 concurrent registration requests
1011
+ const requests = Array.from({ length: 5 }, (_, i) =>
1012
+ app.request("/auth/register", json({
1013
+ email: `concurrent-${i}@evil.com`,
1014
+ password: "StrongPass1",
1015
+ displayName: `Concurrent ${i}`
1016
+ }))
1017
+ );
1018
+
1019
+ const responses = await Promise.all(requests);
1020
+ const successfulRegistrations = responses.filter(r => r.status === 201);
1021
+
1022
+ // Even if multiple registrations succeed, NONE should get admin
1023
+ expect(mockAuthRepo.assignDefaultRole).not.toHaveBeenCalledWith(
1024
+ expect.any(String), "admin"
1025
+ );
1026
+ });
1027
+
1028
+ it("CVE-FIX: defaultRole cannot be set to 'admin' to grant admin via registration", async () => {
1029
+ // Even if someone misconfigures defaultRole as 'admin',
1030
+ // this test documents the current behavior — it would assign admin.
1031
+ // The fix is in the config validation at startup (outside routes).
1032
+ // Here we verify the defaultRole IS what's passed, and the auto-escalation is gone.
1033
+ const app = createApp({ allowRegistration: true, defaultRole: "viewer" });
1034
+
1035
+ await app.request("/auth/register", json({
1036
+ email: "new@test.com",
1037
+ password: "StrongPass1"
1038
+ }));
1039
+
1040
+ // Only the configured default role is assigned, never auto-admin
1041
+ expect(mockAuthRepo.assignDefaultRole).toHaveBeenCalledWith(
1042
+ expect.any(String), "viewer"
1043
+ );
1044
+ expect(mockAuthRepo.assignDefaultRole).toHaveBeenCalledTimes(1);
1045
+ });
1046
+ });
1047
+ });