@useatlas/create 0.0.1
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 +231 -0
- package/index.ts +829 -0
- package/package.json +38 -0
- package/templates/docker/.env.example +67 -0
- package/templates/docker/Dockerfile +52 -0
- package/templates/docker/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/docker/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/docker/bin/__tests__/eval.test.ts +434 -0
- package/templates/docker/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/docker/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/docker/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/docker/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/docker/bin/__tests__/query.test.ts +240 -0
- package/templates/docker/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/docker/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/docker/bin/atlas.ts +5044 -0
- package/templates/docker/bin/benchmark.ts +695 -0
- package/templates/docker/bin/enrich.ts +559 -0
- package/templates/docker/bin/eval.ts +770 -0
- package/templates/docker/bin/smoke.ts +438 -0
- package/templates/docker/data/.gitkeep +0 -0
- package/templates/docker/data/cybersec.sql +1961 -0
- package/templates/docker/data/demo-semantic/catalog.yml +40 -0
- package/templates/docker/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/docker/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/docker/data/demo-semantic/entities/people.yml +145 -0
- package/templates/docker/data/demo-semantic/glossary.yml +22 -0
- package/templates/docker/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/docker/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/docker/data/demo.sql +373 -0
- package/templates/docker/data/ecommerce.sql +1690 -0
- package/templates/docker/data/init-demo-db.sql +8 -0
- package/templates/docker/docker-compose.yml +34 -0
- package/templates/docker/docs/deploy.md +390 -0
- package/templates/docker/eslint.config.mjs +18 -0
- package/templates/docker/gitignore +5 -0
- package/templates/docker/next.config.ts +9 -0
- package/templates/docker/package.json +59 -0
- package/templates/docker/postcss.config.mjs +8 -0
- package/templates/docker/public/.gitkeep +0 -0
- package/templates/docker/public/favicon.svg +4 -0
- package/templates/docker/railway.json +13 -0
- package/templates/docker/render.yaml +34 -0
- package/templates/docker/semantic/catalog.yml +5 -0
- package/templates/docker/semantic/entities/.gitkeep +0 -0
- package/templates/docker/semantic/glossary.yml +6 -0
- package/templates/docker/semantic/metrics/.gitkeep +0 -0
- package/templates/docker/sidecar/Dockerfile +28 -0
- package/templates/docker/sidecar/railway.json +14 -0
- package/templates/docker/sidecar/server.ts +188 -0
- package/templates/docker/src/api/__tests__/actions.test.ts +683 -0
- package/templates/docker/src/api/__tests__/admin.test.ts +820 -0
- package/templates/docker/src/api/__tests__/auth.test.ts +165 -0
- package/templates/docker/src/api/__tests__/chat.test.ts +376 -0
- package/templates/docker/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/docker/src/api/__tests__/cors.test.ts +135 -0
- package/templates/docker/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/docker/src/api/__tests__/health.test.ts +261 -0
- package/templates/docker/src/api/__tests__/query.test.ts +891 -0
- package/templates/docker/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/docker/src/api/__tests__/slack.test.ts +847 -0
- package/templates/docker/src/api/index.ts +117 -0
- package/templates/docker/src/api/routes/actions.ts +274 -0
- package/templates/docker/src/api/routes/admin.ts +757 -0
- package/templates/docker/src/api/routes/auth.ts +48 -0
- package/templates/docker/src/api/routes/chat.ts +465 -0
- package/templates/docker/src/api/routes/conversations.ts +266 -0
- package/templates/docker/src/api/routes/health.ts +287 -0
- package/templates/docker/src/api/routes/openapi.ts +390 -0
- package/templates/docker/src/api/routes/query.ts +318 -0
- package/templates/docker/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/docker/src/api/routes/slack.ts +611 -0
- package/templates/docker/src/api/server.ts +226 -0
- package/templates/docker/src/app/api/[...route]/route.ts +33 -0
- package/templates/docker/src/app/error.tsx +24 -0
- package/templates/docker/src/app/globals.css +126 -0
- package/templates/docker/src/app/layout.tsx +19 -0
- package/templates/docker/src/app/page.tsx +14 -0
- package/templates/docker/src/global.d.ts +1 -0
- package/templates/docker/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/docker/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/docker/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/docker/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/docker/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/docker/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/docker/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/docker/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/docker/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/docker/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/docker/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/docker/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/docker/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/docker/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/docker/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/docker/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/docker/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/docker/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/docker/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/docker/src/lib/action-types.ts +95 -0
- package/templates/docker/src/lib/agent-query.ts +178 -0
- package/templates/docker/src/lib/agent.ts +505 -0
- package/templates/docker/src/lib/api-url.ts +2 -0
- package/templates/docker/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/docker/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/docker/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/docker/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/docker/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/docker/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/docker/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/docker/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/docker/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/docker/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/docker/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/docker/src/lib/auth/audit.ts +89 -0
- package/templates/docker/src/lib/auth/byot.ts +158 -0
- package/templates/docker/src/lib/auth/client.ts +35 -0
- package/templates/docker/src/lib/auth/detect.ts +83 -0
- package/templates/docker/src/lib/auth/managed.ts +73 -0
- package/templates/docker/src/lib/auth/middleware.ts +208 -0
- package/templates/docker/src/lib/auth/migrate.ts +111 -0
- package/templates/docker/src/lib/auth/permissions.ts +156 -0
- package/templates/docker/src/lib/auth/server.ts +142 -0
- package/templates/docker/src/lib/auth/simple-key.ts +92 -0
- package/templates/docker/src/lib/auth/types.ts +49 -0
- package/templates/docker/src/lib/config.ts +704 -0
- package/templates/docker/src/lib/conversation-types.ts +29 -0
- package/templates/docker/src/lib/conversations.ts +270 -0
- package/templates/docker/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/docker/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/docker/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/docker/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/docker/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/docker/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/docker/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/docker/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/docker/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/docker/src/lib/db/connection.ts +753 -0
- package/templates/docker/src/lib/db/duckdb.ts +122 -0
- package/templates/docker/src/lib/db/internal.ts +273 -0
- package/templates/docker/src/lib/db/salesforce.ts +342 -0
- package/templates/docker/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/docker/src/lib/errors.ts +154 -0
- package/templates/docker/src/lib/logger.ts +98 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/docker/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/docker/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/docker/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/docker/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/docker/src/lib/plugins/hooks.ts +162 -0
- package/templates/docker/src/lib/plugins/index.ts +9 -0
- package/templates/docker/src/lib/plugins/migrate.ts +309 -0
- package/templates/docker/src/lib/plugins/registry.ts +231 -0
- package/templates/docker/src/lib/plugins/tools.ts +39 -0
- package/templates/docker/src/lib/plugins/wiring.ts +291 -0
- package/templates/docker/src/lib/providers.ts +102 -0
- package/templates/docker/src/lib/rls.ts +321 -0
- package/templates/docker/src/lib/scheduled-task-types.ts +132 -0
- package/templates/docker/src/lib/scheduled-tasks.ts +475 -0
- package/templates/docker/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/docker/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/delivery.ts +248 -0
- package/templates/docker/src/lib/scheduler/engine.ts +317 -0
- package/templates/docker/src/lib/scheduler/executor.ts +73 -0
- package/templates/docker/src/lib/scheduler/format-email.ts +109 -0
- package/templates/docker/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/docker/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/docker/src/lib/scheduler/index.ts +7 -0
- package/templates/docker/src/lib/security.ts +11 -0
- package/templates/docker/src/lib/semantic-index.ts +503 -0
- package/templates/docker/src/lib/semantic.ts +387 -0
- package/templates/docker/src/lib/sidecar-types.ts +16 -0
- package/templates/docker/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/docker/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/docker/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/docker/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/docker/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/docker/src/lib/slack/api.ts +102 -0
- package/templates/docker/src/lib/slack/format.ts +209 -0
- package/templates/docker/src/lib/slack/store.ts +107 -0
- package/templates/docker/src/lib/slack/threads.ts +64 -0
- package/templates/docker/src/lib/slack/verify.ts +71 -0
- package/templates/docker/src/lib/startup.ts +730 -0
- package/templates/docker/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/docker/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/docker/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/docker/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/docker/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/docker/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/docker/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/docker/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/docker/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/docker/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/docker/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/docker/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/docker/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/docker/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/docker/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/docker/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/docker/src/lib/tools/actions/audit.ts +47 -0
- package/templates/docker/src/lib/tools/actions/email.ts +191 -0
- package/templates/docker/src/lib/tools/actions/handler.ts +591 -0
- package/templates/docker/src/lib/tools/actions/index.ts +23 -0
- package/templates/docker/src/lib/tools/actions/jira.ts +220 -0
- package/templates/docker/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/docker/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/docker/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/docker/src/lib/tools/explore.ts +379 -0
- package/templates/docker/src/lib/tools/registry.ts +221 -0
- package/templates/docker/src/lib/tools/salesforce.ts +138 -0
- package/templates/docker/src/lib/tools/soql-validation.ts +172 -0
- package/templates/docker/src/lib/tools/sql.ts +680 -0
- package/templates/docker/src/lib/tracing.ts +40 -0
- package/templates/docker/src/lib/utils.ts +6 -0
- package/templates/docker/src/test-setup.ts +38 -0
- package/templates/docker/src/types/vercel-sandbox.d.ts +54 -0
- package/templates/docker/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/docker/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/docker/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/docker/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/docker/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/docker/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/docker/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/docker/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/docker/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/docker/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/docker/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/docker/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/docker/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/docker/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/docker/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/docker/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/docker/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/docker/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/docker/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/docker/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/docker/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/docker/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/docker/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/docker/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/docker/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/docker/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/docker/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/docker/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/docker/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/docker/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/docker/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/docker/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/docker/src/ui/context.tsx +78 -0
- package/templates/docker/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/docker/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/docker/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/docker/src/ui/lib/action-types.ts +63 -0
- package/templates/docker/src/ui/lib/helpers.ts +104 -0
- package/templates/docker/src/ui/lib/types.ts +145 -0
- package/templates/docker/tsconfig.json +41 -0
- package/templates/docker/vercel.json +3 -0
- package/templates/nextjs-standalone/.env.example +68 -0
- package/templates/nextjs-standalone/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/nextjs-standalone/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/nextjs-standalone/bin/__tests__/eval.test.ts +434 -0
- package/templates/nextjs-standalone/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/nextjs-standalone/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/nextjs-standalone/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/nextjs-standalone/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/nextjs-standalone/bin/__tests__/query.test.ts +240 -0
- package/templates/nextjs-standalone/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/nextjs-standalone/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/nextjs-standalone/bin/atlas.ts +5044 -0
- package/templates/nextjs-standalone/bin/benchmark.ts +695 -0
- package/templates/nextjs-standalone/bin/enrich.ts +559 -0
- package/templates/nextjs-standalone/bin/eval.ts +770 -0
- package/templates/nextjs-standalone/bin/smoke.ts +438 -0
- package/templates/nextjs-standalone/data/.gitkeep +0 -0
- package/templates/nextjs-standalone/data/cybersec.sql +1961 -0
- package/templates/nextjs-standalone/data/demo-semantic/catalog.yml +40 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/people.yml +145 -0
- package/templates/nextjs-standalone/data/demo-semantic/glossary.yml +22 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/nextjs-standalone/data/demo.sql +373 -0
- package/templates/nextjs-standalone/data/ecommerce.sql +1690 -0
- package/templates/nextjs-standalone/data/init-demo-db.sql +8 -0
- package/templates/nextjs-standalone/docs/deploy.md +390 -0
- package/templates/nextjs-standalone/eslint.config.mjs +18 -0
- package/templates/nextjs-standalone/gitignore +5 -0
- package/templates/nextjs-standalone/next.config.ts +10 -0
- package/templates/nextjs-standalone/package.json +63 -0
- package/templates/nextjs-standalone/postcss.config.mjs +8 -0
- package/templates/nextjs-standalone/semantic/catalog.yml +5 -0
- package/templates/nextjs-standalone/semantic/entities/.gitkeep +0 -0
- package/templates/nextjs-standalone/semantic/glossary.yml +6 -0
- package/templates/nextjs-standalone/semantic/metrics/.gitkeep +0 -0
- package/templates/nextjs-standalone/src/api/__tests__/actions.test.ts +683 -0
- package/templates/nextjs-standalone/src/api/__tests__/admin.test.ts +820 -0
- package/templates/nextjs-standalone/src/api/__tests__/auth.test.ts +165 -0
- package/templates/nextjs-standalone/src/api/__tests__/chat.test.ts +376 -0
- package/templates/nextjs-standalone/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/nextjs-standalone/src/api/__tests__/cors.test.ts +135 -0
- package/templates/nextjs-standalone/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/nextjs-standalone/src/api/__tests__/health.test.ts +261 -0
- package/templates/nextjs-standalone/src/api/__tests__/query.test.ts +891 -0
- package/templates/nextjs-standalone/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/nextjs-standalone/src/api/__tests__/slack.test.ts +847 -0
- package/templates/nextjs-standalone/src/api/index.ts +117 -0
- package/templates/nextjs-standalone/src/api/routes/actions.ts +274 -0
- package/templates/nextjs-standalone/src/api/routes/admin.ts +757 -0
- package/templates/nextjs-standalone/src/api/routes/auth.ts +48 -0
- package/templates/nextjs-standalone/src/api/routes/chat.ts +465 -0
- package/templates/nextjs-standalone/src/api/routes/conversations.ts +266 -0
- package/templates/nextjs-standalone/src/api/routes/health.ts +287 -0
- package/templates/nextjs-standalone/src/api/routes/openapi.ts +390 -0
- package/templates/nextjs-standalone/src/api/routes/query.ts +318 -0
- package/templates/nextjs-standalone/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/nextjs-standalone/src/api/routes/slack.ts +611 -0
- package/templates/nextjs-standalone/src/api/server.ts +226 -0
- package/templates/nextjs-standalone/src/app/api/[...route]/route.ts +33 -0
- package/templates/nextjs-standalone/src/app/error.tsx +24 -0
- package/templates/nextjs-standalone/src/app/global-error.tsx +68 -0
- package/templates/nextjs-standalone/src/app/globals.css +126 -0
- package/templates/nextjs-standalone/src/app/layout.tsx +19 -0
- package/templates/nextjs-standalone/src/app/page.tsx +14 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/nextjs-standalone/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/nextjs-standalone/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/nextjs-standalone/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/nextjs-standalone/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/nextjs-standalone/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/nextjs-standalone/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/nextjs-standalone/src/lib/action-types.ts +95 -0
- package/templates/nextjs-standalone/src/lib/agent-query.ts +178 -0
- package/templates/nextjs-standalone/src/lib/agent.ts +505 -0
- package/templates/nextjs-standalone/src/lib/api-url.ts +3 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/nextjs-standalone/src/lib/auth/audit.ts +89 -0
- package/templates/nextjs-standalone/src/lib/auth/byot.ts +158 -0
- package/templates/nextjs-standalone/src/lib/auth/client.ts +23 -0
- package/templates/nextjs-standalone/src/lib/auth/detect.ts +83 -0
- package/templates/nextjs-standalone/src/lib/auth/managed.ts +73 -0
- package/templates/nextjs-standalone/src/lib/auth/middleware.ts +208 -0
- package/templates/nextjs-standalone/src/lib/auth/migrate.ts +111 -0
- package/templates/nextjs-standalone/src/lib/auth/permissions.ts +156 -0
- package/templates/nextjs-standalone/src/lib/auth/server.ts +142 -0
- package/templates/nextjs-standalone/src/lib/auth/simple-key.ts +92 -0
- package/templates/nextjs-standalone/src/lib/auth/types.ts +49 -0
- package/templates/nextjs-standalone/src/lib/config.ts +704 -0
- package/templates/nextjs-standalone/src/lib/conversation-types.ts +29 -0
- package/templates/nextjs-standalone/src/lib/conversations.ts +270 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/nextjs-standalone/src/lib/db/connection.ts +753 -0
- package/templates/nextjs-standalone/src/lib/db/duckdb.ts +122 -0
- package/templates/nextjs-standalone/src/lib/db/internal.ts +273 -0
- package/templates/nextjs-standalone/src/lib/db/salesforce.ts +342 -0
- package/templates/nextjs-standalone/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/nextjs-standalone/src/lib/errors.ts +154 -0
- package/templates/nextjs-standalone/src/lib/logger.ts +98 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/nextjs-standalone/src/lib/plugins/hooks.ts +162 -0
- package/templates/nextjs-standalone/src/lib/plugins/index.ts +9 -0
- package/templates/nextjs-standalone/src/lib/plugins/migrate.ts +309 -0
- package/templates/nextjs-standalone/src/lib/plugins/registry.ts +231 -0
- package/templates/nextjs-standalone/src/lib/plugins/tools.ts +39 -0
- package/templates/nextjs-standalone/src/lib/plugins/wiring.ts +291 -0
- package/templates/nextjs-standalone/src/lib/providers.ts +102 -0
- package/templates/nextjs-standalone/src/lib/rls.ts +321 -0
- package/templates/nextjs-standalone/src/lib/scheduled-task-types.ts +132 -0
- package/templates/nextjs-standalone/src/lib/scheduled-tasks.ts +475 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/delivery.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/engine.ts +317 -0
- package/templates/nextjs-standalone/src/lib/scheduler/executor.ts +73 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-email.ts +109 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/nextjs-standalone/src/lib/scheduler/index.ts +7 -0
- package/templates/nextjs-standalone/src/lib/security.ts +11 -0
- package/templates/nextjs-standalone/src/lib/semantic-index.ts +503 -0
- package/templates/nextjs-standalone/src/lib/semantic.ts +387 -0
- package/templates/nextjs-standalone/src/lib/sidecar-types.ts +16 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/nextjs-standalone/src/lib/slack/api.ts +102 -0
- package/templates/nextjs-standalone/src/lib/slack/format.ts +209 -0
- package/templates/nextjs-standalone/src/lib/slack/store.ts +107 -0
- package/templates/nextjs-standalone/src/lib/slack/threads.ts +64 -0
- package/templates/nextjs-standalone/src/lib/slack/verify.ts +71 -0
- package/templates/nextjs-standalone/src/lib/startup.ts +730 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/audit.ts +47 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/email.ts +191 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/handler.ts +591 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/index.ts +23 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/jira.ts +220 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/nextjs-standalone/src/lib/tools/explore.ts +379 -0
- package/templates/nextjs-standalone/src/lib/tools/registry.ts +221 -0
- package/templates/nextjs-standalone/src/lib/tools/salesforce.ts +138 -0
- package/templates/nextjs-standalone/src/lib/tools/soql-validation.ts +172 -0
- package/templates/nextjs-standalone/src/lib/tools/sql.ts +680 -0
- package/templates/nextjs-standalone/src/lib/tracing.ts +40 -0
- package/templates/nextjs-standalone/src/lib/utils.ts +6 -0
- package/templates/nextjs-standalone/src/test-setup.ts +38 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/nextjs-standalone/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/nextjs-standalone/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/nextjs-standalone/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/nextjs-standalone/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/nextjs-standalone/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/nextjs-standalone/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/nextjs-standalone/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/nextjs-standalone/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/nextjs-standalone/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/nextjs-standalone/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/nextjs-standalone/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/nextjs-standalone/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/nextjs-standalone/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/nextjs-standalone/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/nextjs-standalone/src/ui/context.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/nextjs-standalone/src/ui/lib/action-types.ts +63 -0
- package/templates/nextjs-standalone/src/ui/lib/helpers.ts +104 -0
- package/templates/nextjs-standalone/src/ui/lib/types.ts +145 -0
- package/templates/nextjs-standalone/tsconfig.json +32 -0
- package/templates/nextjs-standalone/vercel.json +4 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test";
|
|
2
|
+
import { validateManaged } from "../managed";
|
|
3
|
+
import { _setAuthInstance } from "../server";
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
const mockGetSession = mock((): Promise<any> => Promise.resolve(null));
|
|
7
|
+
|
|
8
|
+
describe("validateManaged()", () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
mockGetSession.mockReset();
|
|
11
|
+
// Inject a fake auth instance whose api.getSession is our mock
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
_setAuthInstance({ api: { getSession: mockGetSession } } as any);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
_setAuthInstance(null);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
function makeRequest(headers?: Record<string, string>): Request {
|
|
21
|
+
return new Request("http://localhost/api/chat", {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: headers ?? {},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
it("returns authenticated with user when session exists", async () => {
|
|
28
|
+
mockGetSession.mockResolvedValueOnce({
|
|
29
|
+
user: { id: "usr_123", email: "alice@example.com", name: "Alice" },
|
|
30
|
+
session: { id: "sess_abc", userId: "usr_123" },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const result = await validateManaged(makeRequest());
|
|
34
|
+
|
|
35
|
+
expect(result).toMatchObject({
|
|
36
|
+
authenticated: true,
|
|
37
|
+
mode: "managed",
|
|
38
|
+
user: {
|
|
39
|
+
id: "usr_123",
|
|
40
|
+
mode: "managed",
|
|
41
|
+
label: "alice@example.com",
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
// Verify claims are populated from session user
|
|
45
|
+
if (result.authenticated && result.user) {
|
|
46
|
+
expect(result.user.claims).toBeDefined();
|
|
47
|
+
expect(result.user.claims!.sub).toBe("usr_123");
|
|
48
|
+
expect(result.user.claims!.email).toBe("alice@example.com");
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns 401 when no session", async () => {
|
|
53
|
+
mockGetSession.mockResolvedValueOnce(null);
|
|
54
|
+
|
|
55
|
+
const result = await validateManaged(makeRequest());
|
|
56
|
+
|
|
57
|
+
expect(result).toEqual({
|
|
58
|
+
authenticated: false,
|
|
59
|
+
mode: "managed",
|
|
60
|
+
status: 401,
|
|
61
|
+
error: "Not signed in",
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("passes request headers to getSession", async () => {
|
|
66
|
+
mockGetSession.mockResolvedValueOnce(null);
|
|
67
|
+
|
|
68
|
+
const req = makeRequest({ Authorization: "Bearer some-token" });
|
|
69
|
+
await validateManaged(req);
|
|
70
|
+
|
|
71
|
+
expect(mockGetSession).toHaveBeenCalledTimes(1);
|
|
72
|
+
const calls = mockGetSession.mock.calls as unknown as Array<[{ headers: Headers }]>;
|
|
73
|
+
expect(calls[0][0].headers.get("authorization")).toBe("Bearer some-token");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("returns 500 when session exists but user.id is missing", async () => {
|
|
77
|
+
mockGetSession.mockResolvedValueOnce({
|
|
78
|
+
user: { email: "ghost@example.com" },
|
|
79
|
+
session: { id: "sess_456" },
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const result = await validateManaged(makeRequest());
|
|
83
|
+
|
|
84
|
+
expect(result).toEqual({
|
|
85
|
+
authenticated: false,
|
|
86
|
+
mode: "managed",
|
|
87
|
+
status: 500,
|
|
88
|
+
error: "Session data is incomplete",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("returns 500 when session exists but user.id is empty string", async () => {
|
|
93
|
+
mockGetSession.mockResolvedValueOnce({
|
|
94
|
+
user: { id: "", email: "empty@example.com" },
|
|
95
|
+
session: { id: "sess_789" },
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const result = await validateManaged(makeRequest());
|
|
99
|
+
|
|
100
|
+
expect(result).toEqual({
|
|
101
|
+
authenticated: false,
|
|
102
|
+
mode: "managed",
|
|
103
|
+
status: 500,
|
|
104
|
+
error: "Session data is incomplete",
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("propagates errors from auth instance", async () => {
|
|
109
|
+
mockGetSession.mockRejectedValueOnce(new Error("DB connection failed"));
|
|
110
|
+
|
|
111
|
+
await expect(validateManaged(makeRequest())).rejects.toThrow(
|
|
112
|
+
"DB connection failed",
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("role extraction from session", () => {
|
|
117
|
+
it("session with user.role: 'admin' propagates to user", async () => {
|
|
118
|
+
mockGetSession.mockResolvedValueOnce({
|
|
119
|
+
user: { id: "usr_123", email: "alice@example.com", role: "admin" },
|
|
120
|
+
session: { id: "sess_abc", userId: "usr_123" },
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const result = await validateManaged(makeRequest());
|
|
124
|
+
|
|
125
|
+
expect(result.authenticated).toBe(true);
|
|
126
|
+
if (result.authenticated && result.user) {
|
|
127
|
+
expect(result.user.role).toBe("admin");
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("session with user.role: 'invalid' falls back — no role on user", async () => {
|
|
132
|
+
mockGetSession.mockResolvedValueOnce({
|
|
133
|
+
user: { id: "usr_123", email: "alice@example.com", role: "invalid" },
|
|
134
|
+
session: { id: "sess_abc", userId: "usr_123" },
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const result = await validateManaged(makeRequest());
|
|
138
|
+
|
|
139
|
+
expect(result.authenticated).toBe(true);
|
|
140
|
+
if (result.authenticated && result.user) {
|
|
141
|
+
expect(result.user.role).toBeUndefined();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("session without role field — no role on user", async () => {
|
|
146
|
+
mockGetSession.mockResolvedValueOnce({
|
|
147
|
+
user: { id: "usr_123", email: "alice@example.com" },
|
|
148
|
+
session: { id: "sess_abc", userId: "usr_123" },
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const result = await validateManaged(makeRequest());
|
|
152
|
+
|
|
153
|
+
expect(result.authenticated).toBe(true);
|
|
154
|
+
if (result.authenticated && result.user) {
|
|
155
|
+
expect(result.user.role).toBeUndefined();
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("session with non-string role (number) is ignored", async () => {
|
|
160
|
+
mockGetSession.mockResolvedValueOnce({
|
|
161
|
+
user: { id: "usr_123", email: "alice@example.com", role: 42 },
|
|
162
|
+
session: { id: "sess_abc", userId: "usr_123" },
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const result = await validateManaged(makeRequest());
|
|
166
|
+
|
|
167
|
+
expect(result.authenticated).toBe(true);
|
|
168
|
+
if (result.authenticated && result.user) {
|
|
169
|
+
expect(result.user.role).toBeUndefined();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, afterAll, mock } from "bun:test";
|
|
2
|
+
import type { AuthResult } from "../types";
|
|
3
|
+
import { resetAuthModeCache } from "../detect";
|
|
4
|
+
import {
|
|
5
|
+
authenticateRequest,
|
|
6
|
+
checkRateLimit,
|
|
7
|
+
resetRateLimits,
|
|
8
|
+
getClientIP,
|
|
9
|
+
_stopCleanup,
|
|
10
|
+
_setValidatorOverrides,
|
|
11
|
+
} from "../middleware";
|
|
12
|
+
|
|
13
|
+
// Mock validators — injected via _setValidatorOverrides (no mock.module needed)
|
|
14
|
+
const mockValidateManaged = mock((): Promise<AuthResult> =>
|
|
15
|
+
Promise.resolve({
|
|
16
|
+
authenticated: false as const,
|
|
17
|
+
mode: "managed" as const,
|
|
18
|
+
status: 401 as const,
|
|
19
|
+
error: "Not signed in",
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const mockValidateBYOT = mock((): Promise<AuthResult> =>
|
|
24
|
+
Promise.resolve({
|
|
25
|
+
authenticated: false as const,
|
|
26
|
+
mode: "byot" as const,
|
|
27
|
+
status: 401 as const,
|
|
28
|
+
error: "Invalid or expired token",
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
describe("authenticateRequest()", () => {
|
|
33
|
+
const origJwks = process.env.ATLAS_AUTH_JWKS_URL;
|
|
34
|
+
const origBetterAuth = process.env.BETTER_AUTH_SECRET;
|
|
35
|
+
const origApiKey = process.env.ATLAS_API_KEY;
|
|
36
|
+
const origAuthMode = process.env.ATLAS_AUTH_MODE;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
delete process.env.ATLAS_AUTH_JWKS_URL;
|
|
40
|
+
delete process.env.BETTER_AUTH_SECRET;
|
|
41
|
+
delete process.env.ATLAS_API_KEY;
|
|
42
|
+
delete process.env.ATLAS_AUTH_MODE;
|
|
43
|
+
resetAuthModeCache();
|
|
44
|
+
_setValidatorOverrides({
|
|
45
|
+
managed: mockValidateManaged,
|
|
46
|
+
byot: mockValidateBYOT,
|
|
47
|
+
});
|
|
48
|
+
mockValidateManaged.mockReset();
|
|
49
|
+
mockValidateManaged.mockResolvedValue({
|
|
50
|
+
authenticated: false as const,
|
|
51
|
+
mode: "managed" as const,
|
|
52
|
+
status: 401 as const,
|
|
53
|
+
error: "Not signed in",
|
|
54
|
+
});
|
|
55
|
+
mockValidateBYOT.mockReset();
|
|
56
|
+
mockValidateBYOT.mockResolvedValue({
|
|
57
|
+
authenticated: false as const,
|
|
58
|
+
mode: "byot" as const,
|
|
59
|
+
status: 401 as const,
|
|
60
|
+
error: "Invalid or expired token",
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(() => {
|
|
65
|
+
if (origJwks !== undefined) process.env.ATLAS_AUTH_JWKS_URL = origJwks;
|
|
66
|
+
else delete process.env.ATLAS_AUTH_JWKS_URL;
|
|
67
|
+
|
|
68
|
+
if (origBetterAuth !== undefined) process.env.BETTER_AUTH_SECRET = origBetterAuth;
|
|
69
|
+
else delete process.env.BETTER_AUTH_SECRET;
|
|
70
|
+
|
|
71
|
+
if (origApiKey !== undefined) process.env.ATLAS_API_KEY = origApiKey;
|
|
72
|
+
else delete process.env.ATLAS_API_KEY;
|
|
73
|
+
|
|
74
|
+
if (origAuthMode !== undefined) process.env.ATLAS_AUTH_MODE = origAuthMode;
|
|
75
|
+
else delete process.env.ATLAS_AUTH_MODE;
|
|
76
|
+
|
|
77
|
+
resetAuthModeCache();
|
|
78
|
+
_setValidatorOverrides({ managed: null, byot: null });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function makeRequest(headers?: Record<string, string>): Request {
|
|
82
|
+
return new Request("http://localhost/api/chat", {
|
|
83
|
+
method: "POST",
|
|
84
|
+
headers: headers ?? {},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
it("mode 'none' passes through with no user", async () => {
|
|
89
|
+
const result = await authenticateRequest(makeRequest());
|
|
90
|
+
expect(result.authenticated).toBe(true);
|
|
91
|
+
expect(result).toEqual({
|
|
92
|
+
authenticated: true,
|
|
93
|
+
user: undefined,
|
|
94
|
+
mode: "none",
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("mode 'simple-key' with valid key succeeds", async () => {
|
|
99
|
+
process.env.ATLAS_API_KEY = "test-secret-key";
|
|
100
|
+
resetAuthModeCache();
|
|
101
|
+
|
|
102
|
+
const result = await authenticateRequest(
|
|
103
|
+
makeRequest({ Authorization: "Bearer test-secret-key" }),
|
|
104
|
+
);
|
|
105
|
+
expect(result).toEqual(
|
|
106
|
+
expect.objectContaining({
|
|
107
|
+
authenticated: true,
|
|
108
|
+
user: expect.objectContaining({ mode: "simple-key" }),
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("mode 'simple-key' with wrong key returns 401", async () => {
|
|
114
|
+
process.env.ATLAS_API_KEY = "test-secret-key";
|
|
115
|
+
resetAuthModeCache();
|
|
116
|
+
|
|
117
|
+
const result = await authenticateRequest(
|
|
118
|
+
makeRequest({ Authorization: "Bearer wrong-key" }),
|
|
119
|
+
);
|
|
120
|
+
expect(result.authenticated).toBe(false);
|
|
121
|
+
if (!result.authenticated) {
|
|
122
|
+
expect(result.status).toBe(401);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("mode 'simple-key' with no header returns 401", async () => {
|
|
127
|
+
process.env.ATLAS_API_KEY = "test-secret-key";
|
|
128
|
+
resetAuthModeCache();
|
|
129
|
+
|
|
130
|
+
const result = await authenticateRequest(makeRequest());
|
|
131
|
+
expect(result.authenticated).toBe(false);
|
|
132
|
+
if (!result.authenticated) {
|
|
133
|
+
expect(result.status).toBe(401);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("mode 'managed' with valid session returns authenticated", async () => {
|
|
138
|
+
process.env.BETTER_AUTH_SECRET = "some-secret-for-managed-auth-32chars!!";
|
|
139
|
+
resetAuthModeCache();
|
|
140
|
+
|
|
141
|
+
mockValidateManaged.mockResolvedValueOnce({
|
|
142
|
+
authenticated: true as const,
|
|
143
|
+
mode: "managed" as const,
|
|
144
|
+
user: { id: "usr_1", mode: "managed" as const, label: "alice@test.com" },
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const result = await authenticateRequest(makeRequest());
|
|
148
|
+
expect(result.authenticated).toBe(true);
|
|
149
|
+
if (result.authenticated) {
|
|
150
|
+
expect(result.user?.mode).toBe("managed");
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("mode 'managed' with no session returns 401", async () => {
|
|
155
|
+
process.env.BETTER_AUTH_SECRET = "some-secret-for-managed-auth-32chars!!";
|
|
156
|
+
resetAuthModeCache();
|
|
157
|
+
|
|
158
|
+
const result = await authenticateRequest(makeRequest());
|
|
159
|
+
expect(result.authenticated).toBe(false);
|
|
160
|
+
if (!result.authenticated) {
|
|
161
|
+
expect(result.status).toBe(401);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("mode 'managed' with unexpected error returns 500", async () => {
|
|
166
|
+
process.env.BETTER_AUTH_SECRET = "some-secret-for-managed-auth-32chars!!";
|
|
167
|
+
resetAuthModeCache();
|
|
168
|
+
|
|
169
|
+
mockValidateManaged.mockRejectedValueOnce(new Error("DB crashed"));
|
|
170
|
+
|
|
171
|
+
const result = await authenticateRequest(makeRequest());
|
|
172
|
+
expect(result.authenticated).toBe(false);
|
|
173
|
+
if (!result.authenticated) {
|
|
174
|
+
expect(result.status).toBe(500);
|
|
175
|
+
expect(result.error).toContain("Authentication service error");
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("mode 'managed' with non-Error rejection returns 500", async () => {
|
|
180
|
+
process.env.BETTER_AUTH_SECRET = "some-secret-for-managed-auth-32chars!!";
|
|
181
|
+
resetAuthModeCache();
|
|
182
|
+
|
|
183
|
+
mockValidateManaged.mockRejectedValueOnce("something went wrong");
|
|
184
|
+
|
|
185
|
+
const result = await authenticateRequest(makeRequest());
|
|
186
|
+
expect(result.authenticated).toBe(false);
|
|
187
|
+
if (!result.authenticated) {
|
|
188
|
+
expect(result.status).toBe(500);
|
|
189
|
+
expect(result.error).toContain("Authentication service error");
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("mode 'byot' with valid token returns authenticated", async () => {
|
|
194
|
+
process.env.ATLAS_AUTH_JWKS_URL = "https://example.com/.well-known/jwks.json";
|
|
195
|
+
resetAuthModeCache();
|
|
196
|
+
|
|
197
|
+
mockValidateBYOT.mockResolvedValueOnce({
|
|
198
|
+
authenticated: true as const,
|
|
199
|
+
mode: "byot" as const,
|
|
200
|
+
user: { id: "usr_ext", mode: "byot" as const, label: "ext@idp.com" },
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const result = await authenticateRequest(makeRequest());
|
|
204
|
+
expect(result.authenticated).toBe(true);
|
|
205
|
+
if (result.authenticated) {
|
|
206
|
+
expect(result.user?.mode).toBe("byot");
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("mode 'byot' with invalid token returns 401", async () => {
|
|
211
|
+
process.env.ATLAS_AUTH_JWKS_URL = "https://example.com/.well-known/jwks.json";
|
|
212
|
+
resetAuthModeCache();
|
|
213
|
+
|
|
214
|
+
const result = await authenticateRequest(makeRequest());
|
|
215
|
+
expect(result.authenticated).toBe(false);
|
|
216
|
+
if (!result.authenticated) {
|
|
217
|
+
expect(result.status).toBe(401);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("mode 'byot' with unexpected error returns 500", async () => {
|
|
222
|
+
process.env.ATLAS_AUTH_JWKS_URL = "https://example.com/.well-known/jwks.json";
|
|
223
|
+
resetAuthModeCache();
|
|
224
|
+
|
|
225
|
+
mockValidateBYOT.mockRejectedValueOnce(new Error("JWKS fetch failed"));
|
|
226
|
+
|
|
227
|
+
const result = await authenticateRequest(makeRequest());
|
|
228
|
+
expect(result.authenticated).toBe(false);
|
|
229
|
+
if (!result.authenticated) {
|
|
230
|
+
expect(result.status).toBe(500);
|
|
231
|
+
expect(result.error).toContain("Authentication service error");
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("mode 'byot' with non-Error rejection returns 500", async () => {
|
|
236
|
+
process.env.ATLAS_AUTH_JWKS_URL = "https://example.com/.well-known/jwks.json";
|
|
237
|
+
resetAuthModeCache();
|
|
238
|
+
|
|
239
|
+
mockValidateBYOT.mockRejectedValueOnce("connection lost");
|
|
240
|
+
|
|
241
|
+
const result = await authenticateRequest(makeRequest());
|
|
242
|
+
expect(result.authenticated).toBe(false);
|
|
243
|
+
if (!result.authenticated) {
|
|
244
|
+
expect(result.status).toBe(500);
|
|
245
|
+
expect(result.error).toContain("Authentication service error");
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// ---------------------------------------------------------------------------
|
|
251
|
+
// Rate limiting
|
|
252
|
+
// ---------------------------------------------------------------------------
|
|
253
|
+
|
|
254
|
+
describe("checkRateLimit()", () => {
|
|
255
|
+
const origRpm = process.env.ATLAS_RATE_LIMIT_RPM;
|
|
256
|
+
|
|
257
|
+
beforeEach(() => {
|
|
258
|
+
resetRateLimits();
|
|
259
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "5"; // low limit for tests
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
afterEach(() => {
|
|
263
|
+
if (origRpm !== undefined) process.env.ATLAS_RATE_LIMIT_RPM = origRpm;
|
|
264
|
+
else delete process.env.ATLAS_RATE_LIMIT_RPM;
|
|
265
|
+
resetRateLimits();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Stop the cleanup timer once after all rate limit tests
|
|
269
|
+
afterAll(() => {
|
|
270
|
+
_stopCleanup();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("allows requests under the limit", () => {
|
|
274
|
+
for (let i = 0; i < 4; i++) {
|
|
275
|
+
expect(checkRateLimit("user1").allowed).toBe(true);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("blocks at the limit and returns retryAfterMs > 0", () => {
|
|
280
|
+
for (let i = 0; i < 5; i++) {
|
|
281
|
+
checkRateLimit("user2");
|
|
282
|
+
}
|
|
283
|
+
const result = checkRateLimit("user2");
|
|
284
|
+
expect(result.allowed).toBe(false);
|
|
285
|
+
expect(result.retryAfterMs).toBeGreaterThan(0);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("allows again after window expires", () => {
|
|
289
|
+
// Manually inject old timestamps to simulate expired window
|
|
290
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "2";
|
|
291
|
+
resetRateLimits();
|
|
292
|
+
|
|
293
|
+
// First two allowed
|
|
294
|
+
expect(checkRateLimit("user3").allowed).toBe(true);
|
|
295
|
+
expect(checkRateLimit("user3").allowed).toBe(true);
|
|
296
|
+
// Third blocked
|
|
297
|
+
expect(checkRateLimit("user3").allowed).toBe(false);
|
|
298
|
+
|
|
299
|
+
// Reset and re-check — simulates window expiry
|
|
300
|
+
resetRateLimits();
|
|
301
|
+
expect(checkRateLimit("user3").allowed).toBe(true);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("sliding window evicts stale timestamps after 60s", () => {
|
|
305
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "2";
|
|
306
|
+
resetRateLimits();
|
|
307
|
+
|
|
308
|
+
// Use up all 2 slots
|
|
309
|
+
expect(checkRateLimit("window-user").allowed).toBe(true);
|
|
310
|
+
expect(checkRateLimit("window-user").allowed).toBe(true);
|
|
311
|
+
expect(checkRateLimit("window-user").allowed).toBe(false);
|
|
312
|
+
|
|
313
|
+
// Advance time past the 60s window
|
|
314
|
+
const originalNow = Date.now;
|
|
315
|
+
Date.now = () => originalNow() + 61_000;
|
|
316
|
+
try {
|
|
317
|
+
// Old timestamps should be evicted — requests allowed again
|
|
318
|
+
expect(checkRateLimit("window-user").allowed).toBe(true);
|
|
319
|
+
expect(checkRateLimit("window-user").allowed).toBe(true);
|
|
320
|
+
// Third should be blocked again
|
|
321
|
+
expect(checkRateLimit("window-user").allowed).toBe(false);
|
|
322
|
+
} finally {
|
|
323
|
+
Date.now = originalNow;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("resetRateLimits() clears all state", () => {
|
|
328
|
+
for (let i = 0; i < 5; i++) {
|
|
329
|
+
checkRateLimit("user4");
|
|
330
|
+
}
|
|
331
|
+
expect(checkRateLimit("user4").allowed).toBe(false);
|
|
332
|
+
|
|
333
|
+
resetRateLimits();
|
|
334
|
+
expect(checkRateLimit("user4").allowed).toBe(true);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("always allows when ATLAS_RATE_LIMIT_RPM=0", () => {
|
|
338
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "0";
|
|
339
|
+
|
|
340
|
+
for (let i = 0; i < 100; i++) {
|
|
341
|
+
expect(checkRateLimit("user5").allowed).toBe(true);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("always allows when ATLAS_RATE_LIMIT_RPM is not set", () => {
|
|
346
|
+
delete process.env.ATLAS_RATE_LIMIT_RPM;
|
|
347
|
+
|
|
348
|
+
for (let i = 0; i < 100; i++) {
|
|
349
|
+
expect(checkRateLimit("user6").allowed).toBe(true);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("tracks separate keys independently", () => {
|
|
354
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "2";
|
|
355
|
+
resetRateLimits();
|
|
356
|
+
|
|
357
|
+
expect(checkRateLimit("a").allowed).toBe(true);
|
|
358
|
+
expect(checkRateLimit("a").allowed).toBe(true);
|
|
359
|
+
expect(checkRateLimit("a").allowed).toBe(false);
|
|
360
|
+
|
|
361
|
+
// Different key should still be allowed
|
|
362
|
+
expect(checkRateLimit("b").allowed).toBe(true);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("treats non-numeric ATLAS_RATE_LIMIT_RPM as disabled", () => {
|
|
366
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "abc";
|
|
367
|
+
resetRateLimits();
|
|
368
|
+
|
|
369
|
+
for (let i = 0; i < 100; i++) {
|
|
370
|
+
expect(checkRateLimit("user7").allowed).toBe(true);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("treats negative ATLAS_RATE_LIMIT_RPM as disabled", () => {
|
|
375
|
+
process.env.ATLAS_RATE_LIMIT_RPM = "-5";
|
|
376
|
+
resetRateLimits();
|
|
377
|
+
|
|
378
|
+
for (let i = 0; i < 100; i++) {
|
|
379
|
+
expect(checkRateLimit("user8").allowed).toBe(true);
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
// getClientIP
|
|
386
|
+
// ---------------------------------------------------------------------------
|
|
387
|
+
|
|
388
|
+
describe("getClientIP()", () => {
|
|
389
|
+
const origTrustProxy = process.env.ATLAS_TRUST_PROXY;
|
|
390
|
+
|
|
391
|
+
afterEach(() => {
|
|
392
|
+
if (origTrustProxy !== undefined) process.env.ATLAS_TRUST_PROXY = origTrustProxy;
|
|
393
|
+
else delete process.env.ATLAS_TRUST_PROXY;
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
function req(headers: Record<string, string>): Request {
|
|
397
|
+
return new Request("http://localhost/api/chat", {
|
|
398
|
+
method: "POST",
|
|
399
|
+
headers,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
it("returns the single IP from X-Forwarded-For when proxy is trusted", () => {
|
|
404
|
+
process.env.ATLAS_TRUST_PROXY = "true";
|
|
405
|
+
expect(getClientIP(req({ "x-forwarded-for": "1.2.3.4" }))).toBe("1.2.3.4");
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("returns the first IP when X-Forwarded-For has multiple and proxy is trusted", () => {
|
|
409
|
+
process.env.ATLAS_TRUST_PROXY = "1";
|
|
410
|
+
expect(
|
|
411
|
+
getClientIP(req({ "x-forwarded-for": "1.2.3.4, 5.6.7.8, 9.10.11.12" })),
|
|
412
|
+
).toBe("1.2.3.4");
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("ignores X-Forwarded-For when ATLAS_TRUST_PROXY is not set", () => {
|
|
416
|
+
delete process.env.ATLAS_TRUST_PROXY;
|
|
417
|
+
expect(getClientIP(req({ "x-forwarded-for": "1.2.3.4" }))).toBeNull();
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("ignores X-Forwarded-For when ATLAS_TRUST_PROXY is false", () => {
|
|
421
|
+
process.env.ATLAS_TRUST_PROXY = "false";
|
|
422
|
+
expect(getClientIP(req({ "x-forwarded-for": "1.2.3.4" }))).toBeNull();
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("returns null for X-Real-IP when proxy is untrusted", () => {
|
|
426
|
+
delete process.env.ATLAS_TRUST_PROXY;
|
|
427
|
+
expect(getClientIP(req({ "x-real-ip": "10.0.0.1" }))).toBeNull();
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("returns X-Real-IP when proxy is trusted", () => {
|
|
431
|
+
process.env.ATLAS_TRUST_PROXY = "true";
|
|
432
|
+
expect(getClientIP(req({ "x-real-ip": "10.0.0.1" }))).toBe("10.0.0.1");
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("returns null when proxy is untrusted even with XFF and X-Real-IP present", () => {
|
|
436
|
+
delete process.env.ATLAS_TRUST_PROXY;
|
|
437
|
+
expect(
|
|
438
|
+
getClientIP(
|
|
439
|
+
req({ "x-forwarded-for": "1.2.3.4", "x-real-ip": "10.0.0.1" }),
|
|
440
|
+
),
|
|
441
|
+
).toBeNull();
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("returns null when no IP headers present", () => {
|
|
445
|
+
expect(getClientIP(req({}))).toBeNull();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it("X-Forwarded-For takes precedence over X-Real-IP when proxy is trusted", () => {
|
|
449
|
+
process.env.ATLAS_TRUST_PROXY = "true";
|
|
450
|
+
expect(
|
|
451
|
+
getClientIP(
|
|
452
|
+
req({ "x-forwarded-for": "1.2.3.4", "x-real-ip": "10.0.0.1" }),
|
|
453
|
+
),
|
|
454
|
+
).toBe("1.2.3.4");
|
|
455
|
+
});
|
|
456
|
+
});
|