create-nexwire 4.3.0 → 4.4.0
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.
- package/README.md +10 -5
- package/express/src/framework/facade.ts +1 -0
- package/express/src/framework/http/logger.ts +13 -4
- package/express/src/framework/maker-cli/runtime/core.mjs +1 -2
- package/express/src/framework/notification/channels/mail.ts +8 -6
- package/express/src/middlewares/auth-middleware.ts +1 -2
- package/express/src/middlewares/role-middleware.ts +3 -13
- package/express/src/modules/auth/routes/index.ts +1 -2
- package/express/src/providers/notification.ts +2 -2
- package/express/src/resources/src/components/Switch.vue +60 -65
- package/express/src/resources/src/pages/auth/forgetPassword.vue +23 -30
- package/express/src/resources/src/pages/auth/login.vue +25 -28
- package/express/src/resources/src/pages/auth/register.vue +35 -39
- package/express/src/resources/src/pages/auth/resetPassword.vue +29 -37
- package/express/src/resources/src/pages/auth/verifyEmail.vue +15 -19
- package/express/src/resources/src/plugins/gum.ts +519 -203
- package/express/src/resources/src/stores/auth.ts +9 -6
- package/express/src/storage/database/nexwire.sqlite +0 -0
- package/fastify/.dockerignore +14 -0
- package/fastify/.env +44 -0
- package/fastify/.env.example +42 -0
- package/fastify/.vscode/extensions.json +6 -0
- package/fastify/.vscode/settings.json +37 -0
- package/fastify/LICENSE +21 -0
- package/fastify/README.md +232 -0
- package/fastify/biome.json +99 -0
- package/fastify/drizzle.config.ts +25 -0
- package/fastify/gitignore-stub +31 -0
- package/fastify/package.json +130 -0
- package/fastify/public/.gitkeep +0 -0
- package/fastify/src/config/app.ts +41 -0
- package/fastify/src/config/auth.ts +12 -0
- package/fastify/src/config/cache.ts +16 -0
- package/fastify/src/config/cookie.ts +15 -0
- package/fastify/src/config/cors.ts +12 -0
- package/fastify/src/config/database.ts +48 -0
- package/fastify/src/config/index.ts +83 -0
- package/fastify/src/config/jwt.ts +19 -0
- package/fastify/src/config/logging.ts +13 -0
- package/fastify/src/config/mail.ts +24 -0
- package/fastify/src/config/openapi.ts +36 -0
- package/fastify/src/config/queue.ts +30 -0
- package/fastify/src/config/rateLimit.ts +17 -0
- package/fastify/src/config/realtime.ts +15 -0
- package/fastify/src/config/redis.ts +18 -0
- package/fastify/src/config/security.ts +44 -0
- package/fastify/src/config/session.ts +17 -0
- package/fastify/src/config/storage.ts +22 -0
- package/fastify/src/config/validate.ts +50 -0
- package/fastify/src/database/migrations/sqlite/0000_init.sql +48 -0
- package/fastify/src/database/migrations/sqlite/meta/0000_snapshot.json +324 -0
- package/fastify/src/database/migrations/sqlite/meta/_journal.json +13 -0
- package/fastify/src/database/schema.ts +2 -0
- package/fastify/src/env.ts +76 -0
- package/fastify/src/framework/cache/cache.ts +114 -0
- package/fastify/src/framework/circuit-breaker/cb.ts +88 -0
- package/fastify/src/framework/database/app-schema.ts +37 -0
- package/fastify/src/framework/database/connection.ts +223 -0
- package/fastify/src/framework/database/migrate-hooks.ts +87 -0
- package/fastify/src/framework/database/paginate.ts +226 -0
- package/fastify/src/framework/database/schema.ts +20 -0
- package/fastify/src/framework/database/seed.ts +152 -0
- package/fastify/src/framework/events/dispatcher.ts +61 -0
- package/fastify/src/framework/facade.ts +96 -0
- package/fastify/src/framework/http/app.ts +176 -0
- package/fastify/src/framework/http/cors.ts +13 -0
- package/fastify/src/framework/http/fastify.d.ts +13 -0
- package/fastify/src/framework/http/logger.ts +110 -0
- package/fastify/src/framework/http/metrics.ts +22 -0
- package/fastify/src/framework/http/openapi.ts +48 -0
- package/fastify/src/framework/http/ratelimiter.ts +47 -0
- package/fastify/src/framework/http/security.ts +25 -0
- package/fastify/src/framework/http/static.ts +83 -0
- package/fastify/src/framework/http/validation.ts +49 -0
- package/fastify/src/framework/kernel.ts +43 -0
- package/fastify/src/framework/maker-cli/db/core.mjs +291 -0
- package/fastify/src/framework/maker-cli/db/index.mjs +204 -0
- package/fastify/src/framework/maker-cli/deploy/core.mjs +1046 -0
- package/fastify/src/framework/maker-cli/deploy/index.mjs +91 -0
- package/fastify/src/framework/maker-cli/index.mjs +65 -0
- package/fastify/src/framework/maker-cli/middleware/core.mjs +41 -0
- package/fastify/src/framework/maker-cli/middleware/index.mjs +17 -0
- package/fastify/src/framework/maker-cli/module/core.mjs +1066 -0
- package/fastify/src/framework/maker-cli/module/index.mjs +211 -0
- package/fastify/src/framework/maker-cli/provider/core.mjs +49 -0
- package/fastify/src/framework/maker-cli/provider/index.mjs +12 -0
- package/fastify/src/framework/maker-cli/runtime/build-ui.mjs +16 -0
- package/fastify/src/framework/maker-cli/runtime/core.mjs +560 -0
- package/fastify/src/framework/maker-cli/runtime/index.mjs +120 -0
- package/fastify/src/framework/maker-cli/runtime/redis-commander-patch.cjs +20 -0
- package/fastify/src/framework/maker-cli/stubs/controller/openapi.ts.stub +74 -0
- package/fastify/src/framework/maker-cli/stubs/controller/plain.ts.stub +77 -0
- package/fastify/src/framework/maker-cli/stubs/controller/standalone.ts.stub +51 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.bun.stub +52 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.pnpm.stub +55 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.stub +51 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.yarn.stub +55 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/README.stub +59 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/mysql.server.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/postgres.server.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/sqlite.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/mysql.server.stub +68 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/postgres.server.stub +68 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/sqlite.stub +67 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/scripts/auto-migrate.sh.stub +41 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/README.stub +78 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/noredis.stub +120 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/redis.dev.stub +133 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/redis.stub +131 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/local.example.stub +11 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/noredis.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/redis.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/nginx-vhost/README.stub +16 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/nginx-vhost/app.example.com.stub +13 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/pgadmin/servers.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/redis/redis.conf.stub +7 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/supervisor/noredis.stub +54 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/supervisor/redis.stub +70 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/workflow/local.json.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/workflow/remote.json.stub +24 -0
- package/fastify/src/framework/maker-cli/stubs/facade/name.ts.stub +12 -0
- package/fastify/src/framework/maker-cli/stubs/helper/name.ts.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/job/name.ts.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/middleware/name.local.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/middleware/name.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.mysql.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.postgresql.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.sqlite.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/notification/controller.ts.stub +79 -0
- package/fastify/src/framework/maker-cli/stubs/notification/job.ts.stub +11 -0
- package/fastify/src/framework/maker-cli/stubs/notification/model.ts.stub +29 -0
- package/fastify/src/framework/maker-cli/stubs/notification/provider.ts.stub +174 -0
- package/fastify/src/framework/maker-cli/stubs/notification/route.api.ts.stub +71 -0
- package/fastify/src/framework/maker-cli/stubs/notification/route.plain.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/notification/schema.plain.ts.stub +26 -0
- package/fastify/src/framework/maker-cli/stubs/notification/schema.ts.stub +26 -0
- package/fastify/src/framework/maker-cli/stubs/provider/module-barrel.ts.stub +19 -0
- package/fastify/src/framework/maker-cli/stubs/provider/module.ts.stub +21 -0
- package/fastify/src/framework/maker-cli/stubs/provider/name.ts.stub +21 -0
- package/fastify/src/framework/maker-cli/stubs/route/api.ts.stub +101 -0
- package/fastify/src/framework/maker-cli/stubs/route/plain.ts.stub +23 -0
- package/fastify/src/framework/maker-cli/stubs/route/standalone.api.ts.stub +120 -0
- package/fastify/src/framework/maker-cli/stubs/route/standalone.plain.ts.stub +50 -0
- package/fastify/src/framework/maker-cli/stubs/schedule/name.ts.stub +36 -0
- package/fastify/src/framework/maker-cli/stubs/schema/name.plain.ts.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/schema/name.ts.stub +33 -0
- package/fastify/src/framework/maker-cli/stubs/seeder/name.ts.stub +18 -0
- package/fastify/src/framework/maker-cli/stubs/seeder/standalone.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/stubs/service/name.plain.ts.stub +67 -0
- package/fastify/src/framework/maker-cli/stubs/service/name.ts.stub +65 -0
- package/fastify/src/framework/maker-cli/stubs/test/unit.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/stubs/type/name.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/utils/env-config.mjs +44 -0
- package/fastify/src/framework/maker-cli/utils/env-db.mjs +61 -0
- package/fastify/src/framework/maker-cli/utils/file-ops.mjs +30 -0
- package/fastify/src/framework/maker-cli/utils/flags.mjs +14 -0
- package/fastify/src/framework/maker-cli/utils/help.mjs +379 -0
- package/fastify/src/framework/maker-cli/utils/naming.mjs +119 -0
- package/fastify/src/framework/maker-cli/utils/process.mjs +52 -0
- package/fastify/src/framework/maker-cli/utils/project-commands.mjs +201 -0
- package/fastify/src/framework/modules/discover.ts +57 -0
- package/fastify/src/framework/modules/routes.ts +31 -0
- package/fastify/src/framework/notification/channels/broadcast.ts +15 -0
- package/fastify/src/framework/notification/channels/mail.ts +42 -0
- package/fastify/src/framework/notification/contracts.ts +29 -0
- package/fastify/src/framework/notification/index.ts +59 -0
- package/fastify/src/framework/notification/registry.ts +15 -0
- package/fastify/src/framework/providers/adapter.ts +149 -0
- package/fastify/src/framework/providers/contracts.ts +126 -0
- package/fastify/src/framework/providers/loader.ts +105 -0
- package/fastify/src/framework/providers/registry.ts +88 -0
- package/fastify/src/framework/queue/clear.ts +22 -0
- package/fastify/src/framework/queue/queue.ts +401 -0
- package/fastify/src/framework/queue/ui.ts +194 -0
- package/fastify/src/framework/queue/worker.ts +61 -0
- package/fastify/src/framework/realtime/broadcast.ts +70 -0
- package/fastify/src/framework/realtime/index.ts +2 -0
- package/fastify/src/framework/realtime/socket-cookie.ts +72 -0
- package/fastify/src/framework/realtime/socket.ts +149 -0
- package/fastify/src/framework/realtime/types.ts +6 -0
- package/fastify/src/framework/realtime/ui.ts +16 -0
- package/fastify/src/framework/redis/client.ts +148 -0
- package/fastify/src/framework/runtime/runtime.ts +23 -0
- package/fastify/src/framework/runtime/server-adapter.ts +33 -0
- package/fastify/src/framework/scheduler/lock.ts +128 -0
- package/fastify/src/framework/scheduler/run.ts +48 -0
- package/fastify/src/framework/scheduler/scheduler.ts +155 -0
- package/fastify/src/framework/server.ts +162 -0
- package/fastify/src/framework/session/session.ts +160 -0
- package/fastify/src/framework/storage/storage.ts +744 -0
- package/fastify/src/framework/support/cookie.ts +71 -0
- package/fastify/src/framework/support/jwt.ts +33 -0
- package/fastify/src/framework/support/lifecycle.ts +35 -0
- package/fastify/src/framework/support/logger.ts +148 -0
- package/fastify/src/framework/support/mail.ts +56 -0
- package/fastify/src/framework/support/password.ts +23 -0
- package/fastify/src/framework/support/status-codes.ts +75 -0
- package/fastify/src/framework/support/upload.ts +181 -0
- package/fastify/src/framework/support/url.ts +41 -0
- package/fastify/src/middlewares/auth-middleware.ts +134 -0
- package/fastify/src/middlewares/role-middleware.ts +19 -0
- package/fastify/src/modules/auth/__tests__/forgetpass.test.ts +200 -0
- package/fastify/src/modules/auth/__tests__/login.test.ts +171 -0
- package/fastify/src/modules/auth/__tests__/logout.test.ts +60 -0
- package/fastify/src/modules/auth/__tests__/logoutalldevices.test.ts +49 -0
- package/fastify/src/modules/auth/__tests__/me.test.ts +91 -0
- package/fastify/src/modules/auth/__tests__/refreshtoken.test.ts +305 -0
- package/fastify/src/modules/auth/__tests__/register.test.ts +291 -0
- package/fastify/src/modules/auth/__tests__/resetpass.test.ts +206 -0
- package/fastify/src/modules/auth/__tests__/verifyemail.test.ts +183 -0
- package/fastify/src/modules/auth/controllers/auth.ts +252 -0
- package/fastify/src/modules/auth/controllers/role.ts +35 -0
- package/fastify/src/modules/auth/database/models/role.ts +14 -0
- package/fastify/src/modules/auth/database/models/user.ts +46 -0
- package/fastify/src/modules/auth/database/seeders/role.ts +18 -0
- package/fastify/src/modules/auth/database/seeders/user.ts +35 -0
- package/fastify/src/modules/auth/facade.ts +12 -0
- package/fastify/src/modules/auth/helpers/auth.ts +115 -0
- package/fastify/src/modules/auth/jobs/forgetpass.ts +20 -0
- package/fastify/src/modules/auth/jobs/registeruser.ts +23 -0
- package/fastify/src/modules/auth/jobs/verifyemail.ts +18 -0
- package/fastify/src/modules/auth/routes/index.ts +185 -0
- package/fastify/src/modules/auth/routes/role.ts +56 -0
- package/fastify/src/modules/auth/schemas/auth.ts +116 -0
- package/fastify/src/modules/auth/services/forgetpassword.ts +66 -0
- package/fastify/src/modules/auth/services/login.ts +63 -0
- package/fastify/src/modules/auth/services/logout.ts +25 -0
- package/fastify/src/modules/auth/services/logoutalldevices.ts +19 -0
- package/fastify/src/modules/auth/services/me.ts +31 -0
- package/fastify/src/modules/auth/services/refreshtoken.ts +110 -0
- package/fastify/src/modules/auth/services/register.ts +141 -0
- package/fastify/src/modules/auth/services/resetpassword.ts +69 -0
- package/fastify/src/modules/auth/services/verifyemail.ts +77 -0
- package/fastify/src/modules/auth/types/auth.ts +31 -0
- package/fastify/src/providers/README.md +107 -0
- package/fastify/src/providers/notification.ts +4 -0
- package/fastify/src/resources/index.html +17 -0
- package/fastify/src/resources/src/App.vue +5 -0
- package/fastify/src/resources/src/assets/css/styles.css +14761 -0
- package/fastify/src/resources/src/assets/css/styles.css.map +1 -0
- package/fastify/src/resources/src/assets/fonts/public-sans/public-sans-latin-ext.woff2 +0 -0
- package/fastify/src/resources/src/assets/fonts/public-sans/public-sans-latin.woff2 +0 -0
- package/fastify/src/resources/src/assets/images/favicon/favicon.ico +0 -0
- package/fastify/src/resources/src/assets/images/logo.png +0 -0
- package/fastify/src/resources/src/assets/scss/custom.css +217 -0
- package/fastify/src/resources/src/assets/scss/custom.css.map +1 -0
- package/fastify/src/resources/src/assets/scss/custom.scss +1346 -0
- package/fastify/src/resources/src/components/Button.vue +32 -0
- package/fastify/src/resources/src/components/Checkbox.vue +32 -0
- package/fastify/src/resources/src/components/Datepicker.vue +106 -0
- package/fastify/src/resources/src/components/FeatureButton.vue +53 -0
- package/fastify/src/resources/src/components/FloatButton.vue +33 -0
- package/fastify/src/resources/src/components/Href.vue +32 -0
- package/fastify/src/resources/src/components/Input.vue +243 -0
- package/fastify/src/resources/src/components/InputGroup.vue +147 -0
- package/fastify/src/resources/src/components/InputPasswordToggle.vue +229 -0
- package/fastify/src/resources/src/components/Modal.vue +71 -0
- package/fastify/src/resources/src/components/Pagebar.vue +26 -0
- package/fastify/src/resources/src/components/Refresh.vue +19 -0
- package/fastify/src/resources/src/components/Select.vue +384 -0
- package/fastify/src/resources/src/components/Spinner.vue +42 -0
- package/fastify/src/resources/src/components/Switch.vue +60 -0
- package/fastify/src/resources/src/components/TextArea.vue +117 -0
- package/fastify/src/resources/src/components/Toast.vue +56 -0
- package/fastify/src/resources/src/components/datatable/DataTableSkeleton.vue +98 -0
- package/fastify/src/resources/src/components/datatable/Pagination.vue +147 -0
- package/fastify/src/resources/src/components/datatable/SelectOpption.vue +64 -0
- package/fastify/src/resources/src/components/datatable/index.vue +199 -0
- package/fastify/src/resources/src/composables/useAuth.ts +52 -0
- package/fastify/src/resources/src/helpers/assets.ts +51 -0
- package/fastify/src/resources/src/helpers/nformatter.ts +10 -0
- package/fastify/src/resources/src/helpers/utils.ts +75 -0
- package/fastify/src/resources/src/layouts/AuthLayout.vue +20 -0
- package/fastify/src/resources/src/layouts/Layout/Footer.vue +23 -0
- package/fastify/src/resources/src/layouts/Layout/Header.vue +124 -0
- package/fastify/src/resources/src/layouts/Layout/Sidebar.vue +137 -0
- package/fastify/src/resources/src/layouts/Layout/index.vue +87 -0
- package/fastify/src/resources/src/main.ts +28 -0
- package/fastify/src/resources/src/pages/auth/forgetPassword.vue +76 -0
- package/fastify/src/resources/src/pages/auth/login.vue +96 -0
- package/fastify/src/resources/src/pages/auth/register.vue +129 -0
- package/fastify/src/resources/src/pages/auth/resetPassword.vue +110 -0
- package/fastify/src/resources/src/pages/auth/verifyEmail.vue +64 -0
- package/fastify/src/resources/src/pages/dashboard/index.vue +66 -0
- package/fastify/src/resources/src/plugins/axios.ts +31 -0
- package/fastify/src/resources/src/plugins/browserDetect.ts +55 -0
- package/fastify/src/resources/src/plugins/dialog.ts +216 -0
- package/fastify/src/resources/src/plugins/gum.ts +683 -0
- package/fastify/src/resources/src/plugins/pulse.ts +157 -0
- package/fastify/src/resources/src/plugins/routeProgress.ts +87 -0
- package/fastify/src/resources/src/router/index.ts +85 -0
- package/fastify/src/resources/src/stores/admin-ui.ts +148 -0
- package/fastify/src/resources/src/stores/auth.ts +187 -0
- package/fastify/src/resources/tsconfig.json +19 -0
- package/fastify/src/resources/vite.config.ts +64 -0
- package/fastify/src/storage/app/private/.gitkeep +0 -0
- package/fastify/src/storage/app/public/.gitkeep +0 -0
- package/fastify/src/storage/app/tmp/generated/.gitkeep +0 -0
- package/fastify/src/storage/database/nexwire.sqlite +0 -0
- package/fastify/src/storage/logs/.gitkeep +0 -0
- package/fastify/src/storage/logs/app.log +0 -0
- package/fastify/src/storage/logs/fatal.log +0 -0
- package/fastify/src/storage/tmp/.gitkeep +0 -0
- package/fastify/src/storage/trash/modules/.gitkeep +0 -0
- package/fastify/src/types/global.d.ts +32 -0
- package/fastify/tsconfig.json +19 -0
- package/fastify/vitest.config.ts +38 -0
- package/hono/src/framework/facade.ts +1 -0
- package/hono/src/framework/http/logger.ts +86 -81
- package/hono/src/framework/maker-cli/runtime/core.mjs +1 -2
- package/hono/src/framework/notification/channels/mail.ts +8 -6
- package/hono/src/middlewares/auth-middleware.ts +1 -2
- package/hono/src/middlewares/role-middleware.ts +3 -13
- package/hono/src/modules/auth/routes/index.ts +1 -2
- package/hono/src/providers/notification.ts +2 -2
- package/hono/src/resources/src/components/Switch.vue +60 -65
- package/hono/src/resources/src/pages/auth/forgetPassword.vue +23 -29
- package/hono/src/resources/src/pages/auth/login.vue +26 -29
- package/hono/src/resources/src/pages/auth/register.vue +36 -40
- package/hono/src/resources/src/pages/auth/resetPassword.vue +29 -37
- package/hono/src/resources/src/pages/auth/verifyEmail.vue +15 -19
- package/hono/src/resources/src/plugins/gum.ts +519 -203
- package/hono/src/resources/src/stores/auth.ts +9 -6
- package/hono/src/storage/database/nexwire.sqlite +0 -0
- package/package.json +3 -2
- package/src/index.js +11 -8
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("@/framework/facade.js", () => ({
|
|
4
|
+
db: { query: { users: { findFirst: vi.fn() } } }
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
// NOTE: we do NOT mock "@/modules/auth/helpers/auth.js" — we want the
|
|
8
|
+
// real sanitizeUser so the "no password" test is meaningful.
|
|
9
|
+
|
|
10
|
+
vi.mock("@/modules/auth/database/models/user.js", () => ({
|
|
11
|
+
users: { __table: "users", id: "users.id", email: "users.email" }
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
import { db } from "@/framework/facade.js";
|
|
15
|
+
import { sanitizeUser } from "@/modules/auth/helpers/auth.js";
|
|
16
|
+
import { meService } from "@/modules/auth/services/me.js";
|
|
17
|
+
|
|
18
|
+
/** A user row as it comes from the DB — includes sensitive fields. */
|
|
19
|
+
const dbUser = {
|
|
20
|
+
id: 1,
|
|
21
|
+
email: "user@example.com",
|
|
22
|
+
name: "Jane",
|
|
23
|
+
password: "$2b$10$hashedpassword",
|
|
24
|
+
emailVerifiedAt: new Date("2025-01-01"),
|
|
25
|
+
roleId: 2,
|
|
26
|
+
createdAt: new Date("2024-01-01"),
|
|
27
|
+
updatedAt: new Date("2024-01-01"),
|
|
28
|
+
role: { id: 2, name: "user" }
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe("meService.me", () => {
|
|
32
|
+
beforeEach(() => vi.clearAllMocks());
|
|
33
|
+
|
|
34
|
+
it("returns not_found when the user does not exist", async () => {
|
|
35
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(undefined);
|
|
36
|
+
|
|
37
|
+
const result = await meService.me(999);
|
|
38
|
+
|
|
39
|
+
expect(result).toEqual({
|
|
40
|
+
kind: "not_found",
|
|
41
|
+
message: "User not found"
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("strips sensitive fields from the returned user", async () => {
|
|
46
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(dbUser as any);
|
|
47
|
+
|
|
48
|
+
const result = await meService.me(1);
|
|
49
|
+
|
|
50
|
+
if (result.kind !== "found") throw new Error("expected found");
|
|
51
|
+
|
|
52
|
+
// The real sanitizeUser must not leak the password hash.
|
|
53
|
+
expect(result.data).not.toHaveProperty("password");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("returns exactly what sanitizeUser produces", async () => {
|
|
57
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(dbUser as any);
|
|
58
|
+
|
|
59
|
+
const result = await meService.me(1);
|
|
60
|
+
|
|
61
|
+
if (result.kind !== "found") throw new Error("expected found");
|
|
62
|
+
|
|
63
|
+
expect(result.data).toEqual(sanitizeUser(dbUser as any));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("keeps the fields a client actually needs", async () => {
|
|
67
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(dbUser as any);
|
|
68
|
+
|
|
69
|
+
const result = await meService.me(1);
|
|
70
|
+
|
|
71
|
+
if (result.kind !== "found") throw new Error("expected found");
|
|
72
|
+
|
|
73
|
+
expect(result.data).toMatchObject({
|
|
74
|
+
id: 1,
|
|
75
|
+
email: "user@example.com",
|
|
76
|
+
name: "Jane"
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("returns the found variant with the expected message", async () => {
|
|
81
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(dbUser as any);
|
|
82
|
+
|
|
83
|
+
const result = await meService.me(1);
|
|
84
|
+
|
|
85
|
+
expect(result).toEqual({
|
|
86
|
+
kind: "found",
|
|
87
|
+
message: "Authenticated user fetched successfully",
|
|
88
|
+
data: expect.objectContaining({ id: 1, email: "user@example.com" })
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("@/config/index.js", () => ({
|
|
4
|
+
jwtConfig: { refreshRememberExpirySeconds: 60 * 60 * 24 * 30 }
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
vi.mock("drizzle-orm", () => ({
|
|
8
|
+
eq: vi.fn((col: unknown, val: unknown) => ({ op: "eq", col, val })),
|
|
9
|
+
and: vi.fn((...args: unknown[]) => ({ op: "and", args }))
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
vi.mock("@/framework/facade.js", () => ({
|
|
13
|
+
db: {
|
|
14
|
+
query: {
|
|
15
|
+
refreshTokens: { findFirst: vi.fn() },
|
|
16
|
+
users: { findFirst: vi.fn() }
|
|
17
|
+
},
|
|
18
|
+
delete: vi.fn((_table: unknown) => ({
|
|
19
|
+
where: vi.fn((_where: unknown) => Promise.resolve(undefined))
|
|
20
|
+
})),
|
|
21
|
+
update: vi.fn((_table: unknown) => ({
|
|
22
|
+
set: vi.fn((_values: unknown) => ({
|
|
23
|
+
where: vi.fn((_where: unknown) => ({
|
|
24
|
+
returning: vi.fn((_args?: unknown) => Promise.resolve([]))
|
|
25
|
+
}))
|
|
26
|
+
}))
|
|
27
|
+
}))
|
|
28
|
+
},
|
|
29
|
+
jwt: {
|
|
30
|
+
verifyToken: vi.fn(),
|
|
31
|
+
generateToken: vi.fn()
|
|
32
|
+
}
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
vi.mock("@/modules/auth/helpers/auth.js", () => ({
|
|
36
|
+
sanitizeUser: vi.fn((u: any) => ({ id: u.id, email: u.email, name: u.name }))
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
vi.mock("@/modules/auth/database/models/user.js", () => ({
|
|
40
|
+
users: { __table: "users", id: "users.id", email: "users.email" },
|
|
41
|
+
refreshTokens: {
|
|
42
|
+
__table: "refreshTokens",
|
|
43
|
+
id: "refreshTokens.id",
|
|
44
|
+
jti: "refreshTokens.jti",
|
|
45
|
+
userId: "refreshTokens.userId",
|
|
46
|
+
revoked: "refreshTokens.revoked",
|
|
47
|
+
expiresAt: "refreshTokens.expiresAt"
|
|
48
|
+
}
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
import { db, jwt } from "@/framework/facade.js";
|
|
52
|
+
import { refreshTokenService } from "@/modules/auth/services/refreshtoken.js";
|
|
53
|
+
|
|
54
|
+
const futureDate = new Date(Date.now() + 60_000);
|
|
55
|
+
const pastDate = new Date(Date.now() - 60_000);
|
|
56
|
+
|
|
57
|
+
const storedToken = {
|
|
58
|
+
id: 10,
|
|
59
|
+
jti: "jti-old",
|
|
60
|
+
revoked: 0,
|
|
61
|
+
expiresAt: futureDate
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const fakeUser = {
|
|
65
|
+
id: 1,
|
|
66
|
+
email: "user@example.com",
|
|
67
|
+
name: "Jane",
|
|
68
|
+
role: { id: 2, name: "user" }
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Wire db.update so its returning() resolves with `rows`.
|
|
73
|
+
* Returns handles to inspect the set/where calls.
|
|
74
|
+
*/
|
|
75
|
+
function mockRotate(rows: unknown[]) {
|
|
76
|
+
const returning = vi.fn((_args?: unknown) => Promise.resolve(rows));
|
|
77
|
+
const where = vi.fn((_where: unknown) => ({ returning }));
|
|
78
|
+
const set = vi.fn((_values: unknown) => ({ where }));
|
|
79
|
+
const update = vi.fn((_table: unknown) => ({ set }));
|
|
80
|
+
|
|
81
|
+
vi.mocked(db.update).mockImplementationOnce(update as any);
|
|
82
|
+
|
|
83
|
+
return { update, set, where, returning };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Wire jwt.generateToken to succeed twice (access then refresh). */
|
|
87
|
+
function mockTokenGeneration() {
|
|
88
|
+
vi.mocked(jwt.generateToken)
|
|
89
|
+
.mockResolvedValueOnce({
|
|
90
|
+
token: "new-access",
|
|
91
|
+
exp: 111,
|
|
92
|
+
jti: "access-jti"
|
|
93
|
+
} as any)
|
|
94
|
+
.mockResolvedValueOnce({
|
|
95
|
+
token: "new-refresh",
|
|
96
|
+
exp: 222,
|
|
97
|
+
jti: "refresh-jti"
|
|
98
|
+
} as any);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
describe("refreshTokenService.refreshToken", () => {
|
|
102
|
+
beforeEach(() => vi.clearAllMocks());
|
|
103
|
+
|
|
104
|
+
it("returns invalid_token when jti is missing from the payload", async () => {
|
|
105
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({ id: 1 } as any);
|
|
106
|
+
|
|
107
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
108
|
+
|
|
109
|
+
expect(result).toEqual({
|
|
110
|
+
kind: "invalid_token",
|
|
111
|
+
message: "Invalid refresh token"
|
|
112
|
+
});
|
|
113
|
+
expect(db.query.refreshTokens.findFirst).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("returns invalid_token when jti is not a string", async () => {
|
|
117
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
118
|
+
jti: 12345,
|
|
119
|
+
id: 1
|
|
120
|
+
} as any);
|
|
121
|
+
|
|
122
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
123
|
+
|
|
124
|
+
expect(result.kind).toBe("invalid_token");
|
|
125
|
+
expect(db.query.refreshTokens.findFirst).not.toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("returns invalid_token when id is not a positive integer", async () => {
|
|
129
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
130
|
+
jti: "jti-old",
|
|
131
|
+
id: "not-a-number"
|
|
132
|
+
} as any);
|
|
133
|
+
|
|
134
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
135
|
+
|
|
136
|
+
expect(result.kind).toBe("invalid_token");
|
|
137
|
+
expect(db.query.refreshTokens.findFirst).not.toHaveBeenCalled();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("returns revoked when no stored token matches the jti", async () => {
|
|
141
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
142
|
+
jti: "jti-old",
|
|
143
|
+
id: 1
|
|
144
|
+
} as any);
|
|
145
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce(undefined);
|
|
146
|
+
|
|
147
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
148
|
+
|
|
149
|
+
expect(result).toEqual({ kind: "revoked", message: "Refresh token revoked" });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("returns revoked when the stored token is revoked", async () => {
|
|
153
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
154
|
+
jti: "jti-old",
|
|
155
|
+
id: 1
|
|
156
|
+
} as any);
|
|
157
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce({
|
|
158
|
+
...storedToken,
|
|
159
|
+
revoked: 1
|
|
160
|
+
} as any);
|
|
161
|
+
|
|
162
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
163
|
+
|
|
164
|
+
expect(result.kind).toBe("revoked");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("returns expired and deletes the row when the token has expired", async () => {
|
|
168
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
169
|
+
jti: "jti-old",
|
|
170
|
+
id: 1
|
|
171
|
+
} as any);
|
|
172
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce({
|
|
173
|
+
...storedToken,
|
|
174
|
+
expiresAt: pastDate
|
|
175
|
+
} as any);
|
|
176
|
+
|
|
177
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
178
|
+
|
|
179
|
+
expect(result).toEqual({ kind: "expired", message: "Refresh token expired" });
|
|
180
|
+
expect(db.delete).toHaveBeenCalledTimes(1);
|
|
181
|
+
expect(db.update).not.toHaveBeenCalled();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("returns user_not_found when the user has been deleted", async () => {
|
|
185
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
186
|
+
jti: "jti-old",
|
|
187
|
+
id: 1
|
|
188
|
+
} as any);
|
|
189
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce(storedToken as any);
|
|
190
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(undefined);
|
|
191
|
+
|
|
192
|
+
const result = await refreshTokenService.refreshToken({ refresh_token: "x" });
|
|
193
|
+
|
|
194
|
+
expect(result).toEqual({
|
|
195
|
+
kind: "user_not_found",
|
|
196
|
+
message: "User not found"
|
|
197
|
+
});
|
|
198
|
+
expect(db.update).not.toHaveBeenCalled();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("rotates the token and returns new cookies on the happy path", async () => {
|
|
202
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
203
|
+
jti: "jti-old",
|
|
204
|
+
id: 1,
|
|
205
|
+
remember: false
|
|
206
|
+
} as any);
|
|
207
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce(storedToken as any);
|
|
208
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(fakeUser as any);
|
|
209
|
+
mockTokenGeneration();
|
|
210
|
+
|
|
211
|
+
const { update, set, where } = mockRotate([{ id: 10 }]);
|
|
212
|
+
|
|
213
|
+
const result = await refreshTokenService.refreshToken({
|
|
214
|
+
refresh_token: "old-token"
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(result).toEqual({
|
|
218
|
+
kind: "refreshed",
|
|
219
|
+
message: "Token refreshed successfully",
|
|
220
|
+
data: {
|
|
221
|
+
user: { id: 1, email: "user@example.com", name: "Jane" },
|
|
222
|
+
access_token: "new-access",
|
|
223
|
+
refresh_token: "new-refresh",
|
|
224
|
+
token_type: "Bearer"
|
|
225
|
+
},
|
|
226
|
+
cookies: {
|
|
227
|
+
accessToken: "new-access",
|
|
228
|
+
refreshToken: "new-refresh",
|
|
229
|
+
refreshExpiry: undefined
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Rotation wrote the new jti + expiry, and cleared revoked
|
|
234
|
+
expect(set).toHaveBeenCalledWith({
|
|
235
|
+
jti: "refresh-jti",
|
|
236
|
+
expiresAt: new Date(222 * 1000),
|
|
237
|
+
revoked: 0
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// The CAS where-clause targets both the row id AND the old jti.
|
|
241
|
+
const whereArg = where.mock.calls[0][0] as any;
|
|
242
|
+
expect(whereArg.op).toBe("and");
|
|
243
|
+
|
|
244
|
+
const eqArgs = whereArg.args.filter((a: any) => a?.op === "eq");
|
|
245
|
+
const idEq = eqArgs.find((c: any) => c.col === "refreshTokens.id");
|
|
246
|
+
const jtiEq = eqArgs.find((c: any) => c.col === "refreshTokens.jti");
|
|
247
|
+
|
|
248
|
+
expect(idEq).toBeDefined();
|
|
249
|
+
expect(idEq.val).toBe(10);
|
|
250
|
+
expect(jtiEq).toBeDefined();
|
|
251
|
+
expect(jtiEq.val).toBe("jti-old");
|
|
252
|
+
|
|
253
|
+
expect(update).toHaveBeenCalledTimes(1);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("uses remember-me expiry when the payload has remember=true", async () => {
|
|
257
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
258
|
+
jti: "jti-old",
|
|
259
|
+
id: 1,
|
|
260
|
+
remember: true
|
|
261
|
+
} as any);
|
|
262
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce(storedToken as any);
|
|
263
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(fakeUser as any);
|
|
264
|
+
mockTokenGeneration();
|
|
265
|
+
mockRotate([{ id: 10 }]);
|
|
266
|
+
|
|
267
|
+
const result = await refreshTokenService.refreshToken({
|
|
268
|
+
refresh_token: "old-token"
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
expect(jwt.generateToken).toHaveBeenNthCalledWith(
|
|
272
|
+
2,
|
|
273
|
+
expect.objectContaining({ id: 1, email: "user@example.com", remember: true }),
|
|
274
|
+
"refresh",
|
|
275
|
+
60 * 60 * 24 * 30
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
if (result.kind === "refreshed") {
|
|
279
|
+
expect(result.cookies.refreshExpiry).toBe(60 * 60 * 24 * 30);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("returns revoked when the CAS loses (token already rotated by another request)", async () => {
|
|
284
|
+
vi.mocked(jwt.verifyToken).mockResolvedValueOnce({
|
|
285
|
+
jti: "jti-old",
|
|
286
|
+
id: 1,
|
|
287
|
+
remember: false
|
|
288
|
+
} as any);
|
|
289
|
+
vi.mocked(db.query.refreshTokens.findFirst).mockResolvedValueOnce(storedToken as any);
|
|
290
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(fakeUser as any);
|
|
291
|
+
mockTokenGeneration();
|
|
292
|
+
|
|
293
|
+
// Simulate a losing CAS: the conditional UPDATE matches no rows
|
|
294
|
+
mockRotate([]);
|
|
295
|
+
|
|
296
|
+
const result = await refreshTokenService.refreshToken({
|
|
297
|
+
refresh_token: "old-token"
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
expect(result).toEqual({
|
|
301
|
+
kind: "revoked",
|
|
302
|
+
message: "Refresh token revoked"
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
vi.mock("@/config/index.js", () => ({
|
|
5
|
+
authConfig: { requireEmailVerification: false }
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
vi.mock("@/framework/facade.js", () => ({
|
|
9
|
+
db: {
|
|
10
|
+
query: {
|
|
11
|
+
roles: { findFirst: vi.fn() },
|
|
12
|
+
users: { findFirst: vi.fn() }
|
|
13
|
+
},
|
|
14
|
+
transaction: vi.fn()
|
|
15
|
+
},
|
|
16
|
+
dispatchEvent: vi.fn(),
|
|
17
|
+
password: { hashPassword: vi.fn() },
|
|
18
|
+
urls: { url: vi.fn((p: string) => `https://app.test${p}`) }
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock("@/modules/auth/database/models/role.js", () => ({
|
|
22
|
+
roles: { __table: "roles", name: "roles.name" }
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock("@/modules/auth/database/models/user.js", () => ({
|
|
26
|
+
users: { __table: "users", id: "users.id", email: "users.email" },
|
|
27
|
+
emailVerificationTokens: {
|
|
28
|
+
__table: "emailVerificationTokens",
|
|
29
|
+
email: "emailVerificationTokens.email",
|
|
30
|
+
token: "emailVerificationTokens.token",
|
|
31
|
+
expiresAt: "emailVerificationTokens.expiresAt",
|
|
32
|
+
createdAt: "emailVerificationTokens.createdAt"
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
vi.mock("@/modules/auth/helpers/auth.js", () => ({
|
|
37
|
+
hashEmailVerificationToken: vi.fn((t: string) => `hashed:${t}`),
|
|
38
|
+
makeEmailVerificationToken: vi.fn(() => "plain-verify-token"),
|
|
39
|
+
issueTokens: vi.fn(),
|
|
40
|
+
revokeCurrentRefreshToken: vi.fn(),
|
|
41
|
+
sanitizeUser: vi.fn((u: any) => ({ id: u.id, email: u.email, name: u.name }))
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
import { authConfig } from "@/config/index.js";
|
|
45
|
+
import { db, dispatchEvent, password, urls } from "@/framework/facade.js";
|
|
46
|
+
import { issueTokens, revokeCurrentRefreshToken } from "@/modules/auth/helpers/auth.js";
|
|
47
|
+
import { registerService } from "@/modules/auth/services/register.js";
|
|
48
|
+
|
|
49
|
+
const body = { name: "Jane", email: "jane@example.com", password: "secret123" };
|
|
50
|
+
|
|
51
|
+
const fakeUser = {
|
|
52
|
+
id: 1,
|
|
53
|
+
name: "Jane",
|
|
54
|
+
email: "jane@example.com",
|
|
55
|
+
role: { id: 2, name: "user" }
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function makeReqRes() {
|
|
59
|
+
const req = {} as FastifyRequest;
|
|
60
|
+
const res = {} as FastifyReply;
|
|
61
|
+
return { req, res };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build a fake tx.
|
|
66
|
+
* - insertedRow: what insert().returning() resolves to
|
|
67
|
+
* - refetchedUser: what query.users.findFirst returns inside the tx
|
|
68
|
+
*/
|
|
69
|
+
function buildTx({ insertedRow = { id: 1 } as unknown, refetchedUser = fakeUser as unknown | null } = {}) {
|
|
70
|
+
const insertCalls: { table: unknown; values: unknown }[] = [];
|
|
71
|
+
let currentInsertTable: unknown;
|
|
72
|
+
|
|
73
|
+
const txInsertReturning = vi.fn((_args?: unknown) => Promise.resolve(insertedRow ? [insertedRow] : []));
|
|
74
|
+
const txInsertValues = vi.fn((values: unknown) => {
|
|
75
|
+
insertCalls.push({ table: currentInsertTable, values });
|
|
76
|
+
return { returning: txInsertReturning };
|
|
77
|
+
});
|
|
78
|
+
const txInsert = vi.fn((table: unknown) => {
|
|
79
|
+
currentInsertTable = table;
|
|
80
|
+
return { values: txInsertValues };
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const txDeleteWhere = vi.fn((_where: unknown) => Promise.resolve(undefined));
|
|
84
|
+
const txDelete = vi.fn((_table: unknown) => ({ where: txDeleteWhere }));
|
|
85
|
+
|
|
86
|
+
const txFindFirst = vi.fn((_args: unknown) => Promise.resolve(refetchedUser));
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
tx: {
|
|
90
|
+
insert: txInsert,
|
|
91
|
+
delete: txDelete,
|
|
92
|
+
query: { users: { findFirst: txFindFirst } }
|
|
93
|
+
},
|
|
94
|
+
txInsert,
|
|
95
|
+
txInsertValues,
|
|
96
|
+
txInsertReturning,
|
|
97
|
+
txDelete,
|
|
98
|
+
txDeleteWhere,
|
|
99
|
+
txFindFirst,
|
|
100
|
+
insertCalls
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
describe("registerService.register (fastify)", () => {
|
|
105
|
+
beforeEach(() => {
|
|
106
|
+
vi.clearAllMocks();
|
|
107
|
+
vi.mocked(authConfig).requireEmailVerification = false;
|
|
108
|
+
vi.mocked(password.hashPassword).mockResolvedValue("hashed-secret" as any);
|
|
109
|
+
vi.mocked(db.query.roles.findFirst).mockResolvedValue({
|
|
110
|
+
id: 2,
|
|
111
|
+
name: "user"
|
|
112
|
+
} as any);
|
|
113
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValue(undefined as any);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("returns email_exists when the email is already taken (no hashing, no tx)", async () => {
|
|
117
|
+
vi.mocked(db.query.users.findFirst).mockResolvedValueOnce(fakeUser as any);
|
|
118
|
+
const { req, res } = makeReqRes();
|
|
119
|
+
|
|
120
|
+
const result = await registerService.register(req, res, body);
|
|
121
|
+
|
|
122
|
+
expect(result).toEqual({
|
|
123
|
+
kind: "email_exists",
|
|
124
|
+
message: "Email already exists"
|
|
125
|
+
});
|
|
126
|
+
expect(password.hashPassword).not.toHaveBeenCalled();
|
|
127
|
+
expect(db.transaction).not.toHaveBeenCalled();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("throws if the default 'user' role is missing", async () => {
|
|
131
|
+
vi.mocked(db.query.roles.findFirst).mockResolvedValueOnce(undefined);
|
|
132
|
+
const { req, res } = makeReqRes();
|
|
133
|
+
|
|
134
|
+
await expect(registerService.register(req, res, body)).rejects.toThrow('Default role "user" not found');
|
|
135
|
+
|
|
136
|
+
expect(db.transaction).not.toHaveBeenCalled();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("hashes the password BEFORE opening the transaction", async () => {
|
|
140
|
+
const order: string[] = [];
|
|
141
|
+
vi.mocked(password.hashPassword).mockImplementationOnce(async () => {
|
|
142
|
+
order.push("hash");
|
|
143
|
+
return "hashed-secret" as any;
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const { tx } = buildTx();
|
|
147
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => {
|
|
148
|
+
order.push("tx");
|
|
149
|
+
return fn(tx);
|
|
150
|
+
});
|
|
151
|
+
vi.mocked(issueTokens).mockResolvedValueOnce({
|
|
152
|
+
accessToken: "a",
|
|
153
|
+
refreshToken: "r"
|
|
154
|
+
} as any);
|
|
155
|
+
|
|
156
|
+
const { req, res } = makeReqRes();
|
|
157
|
+
await registerService.register(req, res, body);
|
|
158
|
+
|
|
159
|
+
expect(order).toEqual(["hash", "tx"]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("inserts the user with the hashed password and the default role id", async () => {
|
|
163
|
+
const { tx, insertCalls } = buildTx();
|
|
164
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => fn(tx));
|
|
165
|
+
vi.mocked(issueTokens).mockResolvedValueOnce({
|
|
166
|
+
accessToken: "a",
|
|
167
|
+
refreshToken: "r"
|
|
168
|
+
} as any);
|
|
169
|
+
const { req, res } = makeReqRes();
|
|
170
|
+
|
|
171
|
+
await registerService.register(req, res, body);
|
|
172
|
+
|
|
173
|
+
const inserted = insertCalls[0].values as any;
|
|
174
|
+
expect(inserted).toMatchObject({
|
|
175
|
+
name: "Jane",
|
|
176
|
+
email: "jane@example.com",
|
|
177
|
+
password: "hashed-secret",
|
|
178
|
+
roleId: 2
|
|
179
|
+
});
|
|
180
|
+
expect(inserted.password).not.toBe("secret123");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("registers, issues tokens, and dispatches user:signup WITHOUT the password", async () => {
|
|
184
|
+
const { tx } = buildTx();
|
|
185
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => fn(tx));
|
|
186
|
+
vi.mocked(issueTokens).mockResolvedValueOnce({
|
|
187
|
+
accessToken: "access-123",
|
|
188
|
+
refreshToken: "refresh-456"
|
|
189
|
+
} as any);
|
|
190
|
+
const { req, res } = makeReqRes();
|
|
191
|
+
|
|
192
|
+
const result = await registerService.register(req, res, {
|
|
193
|
+
...body,
|
|
194
|
+
remember: true
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(revokeCurrentRefreshToken).toHaveBeenCalledWith(req);
|
|
198
|
+
expect(issueTokens).toHaveBeenCalledWith(req, res, fakeUser, {
|
|
199
|
+
remember: true
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// The critical assertion: password must NOT be in the event payload
|
|
203
|
+
expect(dispatchEvent).toHaveBeenCalledWith(
|
|
204
|
+
"user:signup",
|
|
205
|
+
{
|
|
206
|
+
userId: 1,
|
|
207
|
+
email: "jane@example.com",
|
|
208
|
+
name: "Jane"
|
|
209
|
+
},
|
|
210
|
+
{ queue: "mail" }
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const [, eventPayload] = vi.mocked(dispatchEvent).mock.calls[0];
|
|
214
|
+
expect(eventPayload).not.toHaveProperty("password");
|
|
215
|
+
|
|
216
|
+
expect(result).toEqual({
|
|
217
|
+
kind: "registered",
|
|
218
|
+
message: "User registered successfully",
|
|
219
|
+
data: {
|
|
220
|
+
user: { id: 1, email: "jane@example.com", name: "Jane" },
|
|
221
|
+
access_token: "access-123",
|
|
222
|
+
refresh_token: "refresh-456",
|
|
223
|
+
token_type: "Bearer"
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("returns verification_required and dispatches user:verify-email when verification is on", async () => {
|
|
229
|
+
vi.mocked(authConfig).requireEmailVerification = true;
|
|
230
|
+
|
|
231
|
+
const { tx, insertCalls } = buildTx();
|
|
232
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => fn(tx));
|
|
233
|
+
const { req, res } = makeReqRes();
|
|
234
|
+
|
|
235
|
+
const result = await registerService.register(req, res, body);
|
|
236
|
+
|
|
237
|
+
expect(result).toEqual({
|
|
238
|
+
kind: "verification_required",
|
|
239
|
+
message: "User registered successfully. Please verify your email before logging in."
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
expect(dispatchEvent).toHaveBeenCalledWith(
|
|
243
|
+
"user:verify-email",
|
|
244
|
+
{
|
|
245
|
+
email: "jane@example.com",
|
|
246
|
+
name: "Jane",
|
|
247
|
+
verifyUrl: "https://app.test/verify-email?token=plain-verify-token&email=jane%40example.com"
|
|
248
|
+
},
|
|
249
|
+
{ queue: "mail" }
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
// No signup event, no tokens
|
|
253
|
+
expect(issueTokens).not.toHaveBeenCalled();
|
|
254
|
+
expect(revokeCurrentRefreshToken).not.toHaveBeenCalled();
|
|
255
|
+
|
|
256
|
+
// Second insert (the token) stored the HASH, not the plain token
|
|
257
|
+
const tokenInsert = insertCalls[1].values as any;
|
|
258
|
+
expect(tokenInsert.token).toBe("hashed:plain-verify-token");
|
|
259
|
+
expect(tokenInsert.token).not.toBe("plain-verify-token");
|
|
260
|
+
expect(tokenInsert.email).toBe("jane@example.com");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("does not dispatch any email if the transaction fails", async () => {
|
|
264
|
+
vi.mocked(db.transaction).mockRejectedValueOnce(new Error("db down"));
|
|
265
|
+
const { req, res } = makeReqRes();
|
|
266
|
+
|
|
267
|
+
await expect(registerService.register(req, res, body)).rejects.toThrow("db down");
|
|
268
|
+
|
|
269
|
+
expect(dispatchEvent).not.toHaveBeenCalled();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("throws when the insert returns no row", async () => {
|
|
273
|
+
const { tx } = buildTx({ insertedRow: null });
|
|
274
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => fn(tx));
|
|
275
|
+
const { req, res } = makeReqRes();
|
|
276
|
+
|
|
277
|
+
await expect(registerService.register(req, res, body)).rejects.toThrow("Failed to insert user");
|
|
278
|
+
|
|
279
|
+
expect(dispatchEvent).not.toHaveBeenCalled();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("throws when the inserted user cannot be re-fetched", async () => {
|
|
283
|
+
const { tx } = buildTx({ refetchedUser: null });
|
|
284
|
+
vi.mocked(db.transaction).mockImplementationOnce(async (fn: any) => fn(tx));
|
|
285
|
+
const { req, res } = makeReqRes();
|
|
286
|
+
|
|
287
|
+
await expect(registerService.register(req, res, body)).rejects.toThrow("Inserted user not found");
|
|
288
|
+
|
|
289
|
+
expect(dispatchEvent).not.toHaveBeenCalled();
|
|
290
|
+
});
|
|
291
|
+
});
|