create-tigra 3.2.0 → 4.0.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 +81 -12
- package/bin/create-tigra.js +495 -472
- package/lib/patchers/email-verification.patcher.js +160 -44
- package/modules/email-verification/client/hooks/useVerification.ts +4 -1
- package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
- package/modules/email-verification/server/verification.controller.ts +3 -1
- package/modules/email-verification/server/verification.service.ts +81 -21
- package/package.json +3 -2
- package/template/.github/dependabot.yml +47 -0
- package/template/.github/workflows/audit.yml +36 -0
- package/template/.github/workflows/ci.yml +79 -0
- package/template/AGENTS.md +18 -9
- package/template/client/.dockerignore +1 -0
- package/template/client/.env.example.production +13 -2
- package/template/client/AGENTS.md +13 -5
- package/template/client/Dockerfile +186 -14
- package/template/client/next.config.ts +1 -1
- package/template/client/package-lock.json +15983 -14181
- package/template/client/package.json +56 -48
- package/template/client/scripts/check-sharp.mjs +53 -0
- package/template/client/scripts/next-dev.cjs +5 -1
- package/template/client/src/__tests__/proxy.test.ts +92 -0
- package/template/client/src/app/healthz/route.ts +16 -0
- package/template/client/src/app/layout.tsx +9 -3
- package/template/client/src/app/page.tsx +2 -2
- package/template/client/src/app/providers.tsx +23 -4
- package/template/client/src/components/layout/Header.tsx +7 -3
- package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
- package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
- package/template/client/src/features/admin/types/admin.types.ts +2 -3
- package/template/client/src/features/auth/components/AuthInitializer.tsx +38 -19
- package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
- package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
- package/template/client/src/features/auth/components/LoginForm.tsx +18 -4
- package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
- package/template/client/src/features/auth/hooks/useAuth.ts +27 -7
- package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
- package/template/client/src/features/auth/services/auth.service.ts +8 -3
- package/template/client/src/features/auth/store/authSlice.ts +7 -2
- package/template/client/src/features/auth/types/auth.types.ts +3 -0
- package/template/client/src/instrumentation-client.ts +9 -1
- package/template/client/src/instrumentation.ts +6 -0
- package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
- package/template/client/src/lib/api/api.types.ts +14 -0
- package/template/client/src/lib/api/axios.config.ts +101 -15
- package/template/client/src/lib/api/hard-redirect.ts +8 -0
- package/template/client/src/lib/api/session-events.ts +29 -0
- package/template/client/src/lib/constants/routes.ts +12 -0
- package/template/client/src/lib/observability/sentry-scrub.ts +71 -0
- package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
- package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
- package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
- package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
- package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
- package/template/client/src/lib/utils/error.ts +86 -1
- package/template/client/src/lib/utils/form.ts +31 -0
- package/template/client/src/lib/utils/paths.ts +18 -0
- package/template/client/src/lib/utils/security.ts +37 -0
- package/template/client/src/lib/utils/session-marker.ts +24 -0
- package/template/client/src/{middleware.ts → proxy.ts} +25 -11
- package/template/client/src/store/__tests__/store.test.ts +44 -0
- package/template/client/src/store/index.ts +26 -8
- package/template/client/vitest.config.mts +48 -0
- package/template/server/.env.example +280 -269
- package/template/server/.env.example.production +283 -236
- package/template/server/AGENTS.md +13 -4
- package/template/server/Dockerfile +181 -14
- package/template/server/docker-compose.yml +4 -2
- package/template/server/docs/deployment.md +63 -0
- package/template/server/docs/logging.md +52 -4
- package/template/server/docs/rate-limits.md +153 -0
- package/template/server/docs/testing.md +63 -0
- package/template/server/eslint.config.mjs +19 -0
- package/template/server/package-lock.json +8784 -8743
- package/template/server/package.json +79 -75
- package/template/server/prisma/migrations/0_init/migration.sql +82 -0
- package/template/server/prisma/migrations/migration_lock.toml +3 -0
- package/template/server/prisma/schema.prisma +19 -6
- package/template/server/prisma/seed.ts +13 -5
- package/template/server/scripts/check-heic.mjs +40 -0
- package/template/server/scripts/check-sharp.mjs +53 -0
- package/template/server/src/__tests__/routes.integration.test.ts +280 -0
- package/template/server/src/app.ts +248 -335
- package/template/server/src/config/__tests__/env.test.ts +219 -0
- package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
- package/template/server/src/config/env.ts +326 -171
- package/template/server/src/config/rate-limit.config.ts +157 -106
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +110 -0
- package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
- package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
- package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +81 -65
- package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
- package/template/server/src/jobs/index.ts +12 -2
- package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
- package/template/server/src/jobs/schedule.ts +50 -0
- package/template/server/src/libs/__tests__/auth.test.ts +168 -0
- package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
- package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +113 -0
- package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
- package/template/server/src/libs/__tests__/email.test.ts +68 -0
- package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
- package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
- package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
- package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
- package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
- package/template/server/src/libs/__tests__/http.test.ts +74 -2
- package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
- package/template/server/src/libs/__tests__/logger.test.ts +50 -0
- package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
- package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
- package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
- package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
- package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
- package/template/server/src/libs/__tests__/redis.test.ts +59 -0
- package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
- package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
- package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -0
- package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
- package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
- package/template/server/src/libs/account-status.ts +52 -0
- package/template/server/src/libs/auth.ts +51 -12
- package/template/server/src/libs/client-ip.ts +158 -77
- package/template/server/src/libs/cloudflare-ranges.ts +132 -0
- package/template/server/src/libs/cookies.ts +91 -24
- package/template/server/src/libs/email-quota.ts +74 -0
- package/template/server/src/libs/email.ts +46 -9
- package/template/server/src/libs/error-handler.ts +111 -0
- package/template/server/src/libs/health-routes.ts +53 -0
- package/template/server/src/libs/http.ts +139 -47
- package/template/server/src/libs/ip-address.ts +37 -0
- package/template/server/src/libs/ip-block.ts +284 -220
- package/template/server/src/libs/logger.ts +41 -12
- package/template/server/src/libs/monitoring.ts +131 -42
- package/template/server/src/libs/observability/sentry.ts +80 -0
- package/template/server/src/libs/origin-check.ts +9 -7
- package/template/server/src/libs/prisma.ts +47 -4
- package/template/server/src/libs/query-counter.ts +71 -59
- package/template/server/src/libs/rate-limiting.ts +110 -0
- package/template/server/src/libs/redis-required.ts +28 -0
- package/template/server/src/libs/redis.ts +108 -18
- package/template/server/src/libs/request-id.ts +18 -0
- package/template/server/src/libs/requestLogger.ts +112 -17
- package/template/server/src/libs/safe-http.ts +101 -0
- package/template/server/src/libs/shutdown.ts +87 -0
- package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +143 -0
- package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
- package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
- package/template/server/src/libs/storage/file-storage.service.ts +104 -83
- package/template/server/src/libs/storage/file-validator.ts +48 -46
- package/template/server/src/libs/storage/image-optimizer.service.ts +78 -72
- package/template/server/src/libs/token-hash.ts +18 -0
- package/template/server/src/libs/url-safety.ts +127 -121
- package/template/server/src/libs/validation-errors.ts +86 -0
- package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
- package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
- package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
- package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
- package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
- package/template/server/src/modules/admin/admin.controller.ts +66 -49
- package/template/server/src/modules/admin/admin.repo.ts +56 -13
- package/template/server/src/modules/admin/admin.routes.ts +48 -2
- package/template/server/src/modules/admin/admin.schemas.ts +18 -6
- package/template/server/src/modules/admin/admin.service.ts +73 -7
- package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
- package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +332 -49
- package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
- package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
- package/template/server/src/modules/auth/auth.controller.ts +133 -128
- package/template/server/src/modules/auth/auth.repo.ts +60 -42
- package/template/server/src/modules/auth/auth.schemas.ts +5 -2
- package/template/server/src/modules/auth/auth.service.ts +197 -109
- package/template/server/src/modules/auth/session.repo.ts +12 -12
- package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
- package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
- package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
- package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
- package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
- package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
- package/template/server/src/modules/users/users.controller.ts +9 -38
- package/template/server/src/modules/users/users.repo.ts +25 -7
- package/template/server/src/modules/users/users.routes.ts +3 -22
- package/template/server/src/modules/users/users.schemas.ts +15 -9
- package/template/server/src/modules/users/users.service.ts +105 -93
- package/template/server/src/scripts/make-admin.ts +48 -0
- package/template/server/src/server.ts +48 -26
- package/template/server/src/shared/errors/AppError.ts +27 -1
- package/template/server/src/shared/errors/errors.ts +42 -5
- package/template/server/src/shared/responses/successResponse.ts +22 -3
- package/template/server/src/shared/types/index.ts +7 -0
- package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
- package/template/server/src/test/integration-helpers.ts +343 -0
- package/template/server/src/test/integration-services.ts +74 -0
- package/template/server/src/test/integration.global-setup.ts +144 -0
- package/template/server/src/test/integration.setup.ts +35 -158
- package/template/server/src/test/setup.ts +23 -8
- package/template/server/tsconfig.build.json +1 -1
- package/template/server/tsconfig.json +1 -0
- package/template/server/vitest.config.ts +17 -4
- package/template/server/vitest.integration.config.ts +18 -13
- package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract of every API route, in the real app against a real MySQL and
|
|
3
|
+
* Redis: who may call it (401 anonymous, 403 the wrong user or role), what
|
|
4
|
+
* wrong input answers (422 VALIDATION_FAILED with the field list), and its
|
|
5
|
+
* success answer. The deeper guarantees of each area live in that area's own
|
|
6
|
+
* integration file. Add rows here for every new route: the last test fails,
|
|
7
|
+
* naming the route, when a registered route is never called in this file and is
|
|
8
|
+
* not in COVERED_ELSEWHERE (routes another file tests, with that file's name).
|
|
9
|
+
*/
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
13
|
+
import type { FastifyInstance } from 'fastify';
|
|
14
|
+
import { integrationServices } from '@/test/integration.setup.js';
|
|
15
|
+
import {
|
|
16
|
+
PASSWORD,
|
|
17
|
+
buildTestApp,
|
|
18
|
+
call,
|
|
19
|
+
image,
|
|
20
|
+
multipartFile,
|
|
21
|
+
registerAdmin,
|
|
22
|
+
registerUser,
|
|
23
|
+
routeCoverage,
|
|
24
|
+
signIn,
|
|
25
|
+
startEmailStub,
|
|
26
|
+
tokenOf,
|
|
27
|
+
uniqueEmail,
|
|
28
|
+
uploadAvatar,
|
|
29
|
+
type EmailStub,
|
|
30
|
+
type TestUser,
|
|
31
|
+
} from '@/test/integration-helpers.js';
|
|
32
|
+
|
|
33
|
+
type Method = 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
34
|
+
|
|
35
|
+
/** A valid UUID that belongs to nobody. */
|
|
36
|
+
const NOBODY = '00000000-0000-4000-8000-000000000000';
|
|
37
|
+
|
|
38
|
+
const ADMIN_ROUTES: [Method, string][] = [
|
|
39
|
+
['GET', '/admin/blocked-ips'],
|
|
40
|
+
['GET', '/admin/client-ip'],
|
|
41
|
+
['POST', '/admin/blocked-ips'],
|
|
42
|
+
['DELETE', '/admin/blocked-ips/203.0.113.9'],
|
|
43
|
+
['GET', '/admin/health'],
|
|
44
|
+
['GET', '/admin/stats'],
|
|
45
|
+
['GET', '/admin/users'],
|
|
46
|
+
['GET', `/admin/users/${NOBODY}`],
|
|
47
|
+
['PATCH', `/admin/users/${NOBODY}/status`],
|
|
48
|
+
['POST', `/admin/users/${NOBODY}/restore`],
|
|
49
|
+
['PATCH', `/admin/users/${NOBODY}/role`],
|
|
50
|
+
['GET', '/admin/sessions'],
|
|
51
|
+
['DELETE', `/admin/sessions/${NOBODY}`],
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
const SIGNED_IN_ROUTES: [Method, string][] = [
|
|
55
|
+
['GET', '/auth/me'],
|
|
56
|
+
['GET', '/auth/sessions'],
|
|
57
|
+
['POST', '/auth/logout-all'],
|
|
58
|
+
['PATCH', '/users/me'],
|
|
59
|
+
['PATCH', '/users/me/password'],
|
|
60
|
+
['DELETE', '/users/me'],
|
|
61
|
+
['POST', '/users/avatar'],
|
|
62
|
+
['DELETE', '/users/avatar'],
|
|
63
|
+
['PATCH', `/users/${NOBODY}`],
|
|
64
|
+
['PATCH', `/users/${NOBODY}/password`],
|
|
65
|
+
['DELETE', `/users/${NOBODY}`],
|
|
66
|
+
['POST', `/users/${NOBODY}/avatar`],
|
|
67
|
+
['DELETE', `/users/${NOBODY}/avatar`],
|
|
68
|
+
['GET', '/files/anything.txt'],
|
|
69
|
+
...ADMIN_ROUTES,
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Routes whose contract lives in another file, with that file. Only the routes
|
|
74
|
+
* this file does not call belong here. The email-verification module's routes
|
|
75
|
+
* exist only in projects that installed it; listing them is harmless elsewhere.
|
|
76
|
+
*/
|
|
77
|
+
const COVERED_ELSEWHERE: Record<string, string> = {
|
|
78
|
+
'GET /api/v1/health': 'src/libs/__tests__/health.integration.test.ts',
|
|
79
|
+
'GET /api/v1/ready': 'src/libs/__tests__/health.integration.test.ts',
|
|
80
|
+
'GET /api/v1/live': 'src/libs/__tests__/health.integration.test.ts',
|
|
81
|
+
'GET /uploads/*': 'src/modules/files/__tests__/files.integration.test.ts',
|
|
82
|
+
'POST /api/v1/auth/send-verification': 'src/modules/auth/__tests__/verification.integration.test.ts (email-verification module)',
|
|
83
|
+
'POST /api/v1/auth/verify-account': 'src/modules/auth/__tests__/verification.integration.test.ts (email-verification module)',
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Records which routes this file calls (the last test checks it). */
|
|
87
|
+
const coverage = routeCoverage();
|
|
88
|
+
|
|
89
|
+
let app: FastifyInstance;
|
|
90
|
+
let mail: EmailStub;
|
|
91
|
+
let admin: TestUser;
|
|
92
|
+
let user: TestUser;
|
|
93
|
+
|
|
94
|
+
beforeAll(async () => {
|
|
95
|
+
mail = await startEmailStub();
|
|
96
|
+
app = await buildTestApp({ onRoute: coverage.onRoute });
|
|
97
|
+
admin = await registerAdmin(app, 'contract-admin');
|
|
98
|
+
user = await registerUser(app, 'contract-user');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
afterAll(async () => {
|
|
102
|
+
await app?.close();
|
|
103
|
+
await mail?.close();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('who may call', () => {
|
|
107
|
+
it.each(SIGNED_IN_ROUTES)('%s %s anonymously → 401', async (method, url) => {
|
|
108
|
+
expect((await call(app, method, url)).status).toBe(401);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it.each(ADMIN_ROUTES)('%s %s as a USER → 403', async (method, url) => {
|
|
112
|
+
expect((await call(app, method, url, { jar: user.jar })).status).toBe(403);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it.each([
|
|
116
|
+
['PATCH', '', { firstName: 'Hacker' }],
|
|
117
|
+
['PATCH', '/password', { currentPassword: PASSWORD, newPassword: 'Other-Passw0rd-1' }],
|
|
118
|
+
['DELETE', '', { password: PASSWORD }],
|
|
119
|
+
['DELETE', '/avatar', undefined],
|
|
120
|
+
] as const)("%s /users/<someone else>%s → 403", async (method, suffix, payload) => {
|
|
121
|
+
const owner = await registerUser(app, 'contract-owner');
|
|
122
|
+
const r = await call(app, method, `/users/${owner.id}${suffix}`, { jar: user.jar, payload });
|
|
123
|
+
expect(r.status).toBe(403);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("POST /users/<someone else>/avatar → 403", async () => {
|
|
127
|
+
const owner = await registerUser(app, 'contract-owner-av');
|
|
128
|
+
expect((await uploadAvatar(app, user.jar, await image('png'), { url: `/users/${owner.id}/avatar` })).status).toBe(403);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('wrong input → 422 VALIDATION_FAILED, naming the fields', () => {
|
|
133
|
+
it.each([
|
|
134
|
+
['POST', '/auth/register', { email: 'x' }, 'none'],
|
|
135
|
+
['POST', '/auth/login', { email: 'x' }, 'none'],
|
|
136
|
+
['POST', '/auth/forgot-password', { email: 'x' }, 'none'],
|
|
137
|
+
['POST', '/auth/reset-password', {}, 'none'],
|
|
138
|
+
['PATCH', '/users/me', {}, 'user'],
|
|
139
|
+
['PATCH', '/users/me/password', {}, 'user'],
|
|
140
|
+
['DELETE', '/users/me', {}, 'user'],
|
|
141
|
+
// As an admin: for a USER the owner-or-admin check runs first (403), so a
|
|
142
|
+
// stranger cannot even probe another path's validation.
|
|
143
|
+
['PATCH', '/users/not-a-uuid', { firstName: 'Valid' }, 'admin'],
|
|
144
|
+
['POST', '/admin/blocked-ips', {}, 'admin'],
|
|
145
|
+
['GET', '/admin/users?limit=abc', undefined, 'admin'],
|
|
146
|
+
['GET', '/admin/users/not-a-uuid', undefined, 'admin'],
|
|
147
|
+
['PATCH', `/admin/users/${NOBODY}/status`, {}, 'admin'],
|
|
148
|
+
['PATCH', `/admin/users/${NOBODY}/role`, { role: 'GOD' }, 'admin'],
|
|
149
|
+
['GET', '/admin/sessions?userId=nope', undefined, 'admin'],
|
|
150
|
+
['DELETE', '/admin/sessions/not-a-uuid', undefined, 'admin'],
|
|
151
|
+
] as const)('%s %s', async (method, url, payload, who) => {
|
|
152
|
+
const jar = who === 'admin' ? admin.jar : who === 'user' ? user.jar : undefined;
|
|
153
|
+
const r = await call(app, method, url, { jar, payload });
|
|
154
|
+
expect([r.status, r.code]).toEqual([422, 'VALIDATION_FAILED']);
|
|
155
|
+
expect(Array.isArray(r.body?.error?.fields)).toBe(true);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('auth routes answer', () => {
|
|
160
|
+
it('register 201 (and signs in), login 200, me 200, sessions 200, refresh 200', async () => {
|
|
161
|
+
const email = uniqueEmail('contract-auth');
|
|
162
|
+
const reg = await call(app, 'POST', '/auth/register', { payload: { email, password: PASSWORD, firstName: 'Con', lastName: 'Tract' } });
|
|
163
|
+
expect([reg.status, reg.body?.data?.user?.email]).toEqual([201, email]);
|
|
164
|
+
const jar = await signIn(app, email);
|
|
165
|
+
expect((await call(app, 'GET', '/auth/me', { jar })).body?.data?.email).toBe(email);
|
|
166
|
+
const sessions = await call(app, 'GET', '/auth/sessions', { jar });
|
|
167
|
+
expect([sessions.status, Array.isArray(sessions.body?.data) && sessions.body?.data.length >= 1]).toEqual([200, true]);
|
|
168
|
+
expect((await call(app, 'POST', '/auth/refresh', { jar })).status).toBe(200);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('logout 200 (also without a session), logout-all 200', async () => {
|
|
172
|
+
const u = await registerUser(app, 'contract-logout');
|
|
173
|
+
expect((await call(app, 'POST', '/auth/logout', { jar: u.jar })).status).toBe(200);
|
|
174
|
+
expect((await call(app, 'POST', '/auth/logout')).status).toBe(200);
|
|
175
|
+
const v = await registerUser(app, 'contract-logoutall');
|
|
176
|
+
expect((await call(app, 'POST', '/auth/logout-all', { jar: v.jar })).status).toBe(200);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('forgot-password 200 for any address; reset-password 200 with the emailed token, 400 with another', async () => {
|
|
180
|
+
const u = await registerUser(app, 'contract-reset');
|
|
181
|
+
expect((await call(app, 'POST', '/auth/forgot-password', { payload: { email: u.email } })).status).toBe(200);
|
|
182
|
+
expect((await call(app, 'POST', '/auth/forgot-password', { payload: { email: uniqueEmail('nobody') } })).status).toBe(200);
|
|
183
|
+
const token = tokenOf(mail.to(u.email).at(-1)?.link ?? null);
|
|
184
|
+
expect((await call(app, 'POST', '/auth/reset-password', { payload: { token, newPassword: 'Reset-Passw0rd-1' } })).status).toBe(200);
|
|
185
|
+
const bad = await call(app, 'POST', '/auth/reset-password', { payload: { token: 'f'.repeat(64), newPassword: 'Reset-Passw0rd-1' } });
|
|
186
|
+
expect([bad.status, bad.code]).toEqual([400, 'INVALID_RESET_TOKEN']);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('users routes answer', () => {
|
|
191
|
+
it('/users/me: PATCH 200, password 200, avatar POST 200 and DELETE 200, DELETE account 200', async () => {
|
|
192
|
+
const u = await registerUser(app, 'contract-me');
|
|
193
|
+
expect((await call(app, 'PATCH', '/users/me', { jar: u.jar, payload: { lastName: 'Renamed' } })).body?.data?.lastName).toBe('Renamed');
|
|
194
|
+
const pw = await call(app, 'PATCH', '/users/me/password', { jar: u.jar, payload: { currentPassword: PASSWORD, newPassword: 'Changed-Passw0rd-1' } });
|
|
195
|
+
expect(pw.status).toBe(200);
|
|
196
|
+
expect((await uploadAvatar(app, u.jar, await image('jpeg'))).status).toBe(200);
|
|
197
|
+
expect((await call(app, 'DELETE', '/users/avatar', { jar: u.jar })).status).toBe(200);
|
|
198
|
+
expect((await call(app, 'DELETE', '/users/me', { jar: u.jar, payload: { password: 'Changed-Passw0rd-1' } })).status).toBe(200);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('/users/:userId as the owner: PATCH 200, password 200, avatar POST 200 and DELETE 200', async () => {
|
|
202
|
+
const u = await registerUser(app, 'contract-byid');
|
|
203
|
+
const base = `/users/${u.id}`;
|
|
204
|
+
expect((await call(app, 'PATCH', base, { jar: u.jar, payload: { firstName: 'Owner' } })).status).toBe(200);
|
|
205
|
+
expect((await call(app, 'PATCH', `${base}/password`, { jar: u.jar, payload: { currentPassword: PASSWORD, newPassword: 'Owner-Passw0rd-1' } })).status).toBe(200);
|
|
206
|
+
const { payload, headers } = multipartFile(await image('png'));
|
|
207
|
+
expect((await call(app, 'POST', `${base}/avatar`, { jar: u.jar, payload, headers })).status).toBe(200);
|
|
208
|
+
expect((await call(app, 'DELETE', `${base}/avatar`, { jar: u.jar })).status).toBe(200);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('/users/:userId as an admin: password 200 (no current password), DELETE 200; an unknown id 404', async () => {
|
|
212
|
+
const u = await registerUser(app, 'contract-adminbyid');
|
|
213
|
+
expect((await call(app, 'PATCH', `/users/${u.id}/password`, { jar: admin.jar, payload: { newPassword: 'Admin-Set-Passw0rd-1' } })).status).toBe(200);
|
|
214
|
+
expect((await call(app, 'DELETE', `/users/${u.id}`, { jar: admin.jar })).status).toBe(200);
|
|
215
|
+
expect((await call(app, 'PATCH', `/users/${NOBODY}`, { jar: admin.jar, payload: { firstName: 'Ghost' } })).status).toBe(404);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('GET /files/:filename: the owner 200, a missing file 404', async () => {
|
|
219
|
+
const dir = path.join(integrationServices.uploadPrivateDir, 'users', user.id);
|
|
220
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
221
|
+
fs.writeFileSync(path.join(dir, 'contract.txt'), 'contract');
|
|
222
|
+
expect((await call(app, 'GET', '/files/contract.txt', { jar: user.jar })).status).toBe(200);
|
|
223
|
+
expect((await call(app, 'GET', '/files/missing.txt', { jar: user.jar })).status).toBe(404);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('admin routes answer', () => {
|
|
228
|
+
it('stats, health, client-ip and blocked-ips lists → 200 with their shapes', async () => {
|
|
229
|
+
const stats = await call(app, 'GET', '/admin/stats', { jar: admin.jar });
|
|
230
|
+
expect(stats.status).toBe(200);
|
|
231
|
+
expect(stats.body?.data).toEqual(expect.objectContaining({ totalUsers: expect.any(Number) }));
|
|
232
|
+
expect((await call(app, 'GET', '/admin/health', { jar: admin.jar })).status).toBe(200);
|
|
233
|
+
expect(Object.keys((await call(app, 'GET', '/admin/client-ip', { jar: admin.jar })).body?.data ?? {}).sort()).toEqual(['ip', 'source']);
|
|
234
|
+
expect(Object.keys((await call(app, 'GET', '/admin/blocked-ips', { jar: admin.jar })).body?.data ?? {}).sort()).toEqual(['autoBlocked', 'permanent']);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('block 201, unblock 200', async () => {
|
|
238
|
+
expect((await call(app, 'POST', '/admin/blocked-ips', { jar: admin.jar, payload: { ip: '203.0.113.99' } })).status).toBe(201);
|
|
239
|
+
expect((await call(app, 'DELETE', '/admin/blocked-ips/203.0.113.99', { jar: admin.jar })).status).toBe(200);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('users list 200 (paginated), detail 200 / unknown 404, status 200, role 200, restore 200 / not deleted 409', async () => {
|
|
243
|
+
const u = await registerUser(app, 'contract-managed');
|
|
244
|
+
const list = await call(app, 'GET', `/admin/users?search=${encodeURIComponent(u.email)}`, { jar: admin.jar });
|
|
245
|
+
expect([list.status, list.body?.data?.items?.[0]?.id]).toEqual([200, u.id]);
|
|
246
|
+
expect((await call(app, 'GET', `/admin/users/${u.id}`, { jar: admin.jar })).status).toBe(200);
|
|
247
|
+
expect((await call(app, 'GET', `/admin/users/${NOBODY}`, { jar: admin.jar })).status).toBe(404);
|
|
248
|
+
expect((await call(app, 'PATCH', `/admin/users/${u.id}/role`, { jar: admin.jar, payload: { role: 'ADMIN' } })).status).toBe(200);
|
|
249
|
+
expect((await call(app, 'PATCH', `/admin/users/${u.id}/role`, { jar: admin.jar, payload: { role: 'USER' } })).status).toBe(200);
|
|
250
|
+
expect((await call(app, 'PATCH', `/admin/users/${u.id}/status`, { jar: admin.jar, payload: { isActive: false } })).status).toBe(200);
|
|
251
|
+
expect((await call(app, 'PATCH', `/admin/users/${u.id}/status`, { jar: admin.jar, payload: { isActive: true } })).status).toBe(200);
|
|
252
|
+
expect((await call(app, 'POST', `/admin/users/${u.id}/restore`, { jar: admin.jar })).status).toBe(409);
|
|
253
|
+
await call(app, 'DELETE', `/users/${u.id}`, { jar: admin.jar });
|
|
254
|
+
expect((await call(app, 'POST', `/admin/users/${u.id}/restore`, { jar: admin.jar })).status).toBe(200);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('sessions list 200, revoke 200 / unknown 404', async () => {
|
|
258
|
+
const u = await registerUser(app, 'contract-session');
|
|
259
|
+
const list = await call(app, 'GET', `/admin/sessions?userId=${u.id}`, { jar: admin.jar });
|
|
260
|
+
expect(list.status).toBe(200);
|
|
261
|
+
const sessionId = list.body?.data?.items?.[0]?.id as string;
|
|
262
|
+
expect((await call(app, 'DELETE', `/admin/sessions/${sessionId}`, { jar: admin.jar })).status).toBe(200);
|
|
263
|
+
expect((await call(app, 'DELETE', `/admin/sessions/${NOBODY}`, { jar: admin.jar })).status).toBe(404);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
/** The parts of a Vitest task this file reads: a suite has `tasks`, a test does not. */
|
|
268
|
+
type TaskLike = { mode: string; tasks?: TaskLike[] };
|
|
269
|
+
const testsIn = (t: TaskLike): TaskLike[] => (t.tasks ? t.tasks.flatMap(testsIn) : [t]);
|
|
270
|
+
|
|
271
|
+
// Keep this last: it reads what every test above called (tests in a file run in order).
|
|
272
|
+
describe('no route is forgotten', () => {
|
|
273
|
+
it('every registered route was called above, or is in COVERED_ELSEWHERE', ({ task, skip }) => {
|
|
274
|
+
// With a name filter (-t) the other tests did not run, so their routes would look uncalled.
|
|
275
|
+
const self = task as unknown as TaskLike;
|
|
276
|
+
const others = testsIn(task.file as unknown as TaskLike).filter((t) => t !== self);
|
|
277
|
+
if (others.some((t) => t.mode === 'skip' || t.mode === 'todo')) skip('needs the whole file to run');
|
|
278
|
+
expect(coverage.uncalled(COVERED_ELSEWHERE)).toEqual([]);
|
|
279
|
+
});
|
|
280
|
+
});
|