feat(setup): add a setup token for first-run setup (#3437)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-13 00:17:24 +02:00
1 parent 8e39380ee6
commit 78272521bb
135 files changed
+3045 -771

No files matched your search

@@ -12,7 +12,8 @@ import {
import type {ApiTestHarness} from '@app/api/test/ApiTestHarness';
import {generateUniquePassword, HTTP_STATUS} from '@app/api/test/TestConstants';
import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';
import {UserRepository} from '@app/api/user/repositories/UserRepository';
import {afterAll, beforeAll, beforeEach, describe, expect, it, vi} from 'vitest';
describe('Password reset flow', () => {
let harness: ApiTestHarness;
@@ -104,6 +105,61 @@ describe('Password reset flow', () => {
const login = await loginUser(harness, {email: account.email, password: newPassword});
expect('token' in login && login.token.length > 0).toBe(true);
});
it('accepts a reset token for only one of several concurrent resets', async () => {
const account = await createTestAccount(harness);
await createBuilderWithoutAuth(harness)
.post('/auth/forgot')
.body({email: account.email})
.expect(HTTP_STATUS.NO_CONTENT)
.execute();
const emails = await listTestEmails(harness, {recipient: account.email});
const token = findLastTestEmail(emails, 'password_reset')!.metadata!.token!;
const results = await Promise.all(
Array.from({length: 4}, () =>
createBuilderWithoutAuth(harness)
.post('/auth/reset')
.body({token, password: generateUniquePassword()})
.executeRaw(),
),
);
const statuses = results.map(({response}) => response.status).sort();
expect(statuses).toEqual(
[HTTP_STATUS.OK, HTTP_STATUS.BAD_REQUEST, HTTP_STATUS.BAD_REQUEST, HTTP_STATUS.BAD_REQUEST].sort(),
);
});
it('keeps a reset token usable when the reset fails before the password is written', async () => {
const account = await createTestAccount(harness);
await createBuilderWithoutAuth(harness)
.post('/auth/forgot')
.body({email: account.email})
.expect(HTTP_STATUS.NO_CONTENT)
.execute();
const emails = await listTestEmails(harness, {recipient: account.email});
const token = findLastTestEmail(emails, 'password_reset')!.metadata!.token!;
const patchUpsert = vi
.spyOn(UserRepository.prototype, 'patchUpsert')
.mockRejectedValueOnce(new Error('the user row write failed'));
try {
await createBuilderWithoutAuth(harness)
.post('/auth/reset')
.body({token, password: generateUniquePassword()})
.expect(HTTP_STATUS.INTERNAL_SERVER_ERROR)
.execute();
} finally {
patchUpsert.mockRestore();
}
const newPassword = generateUniquePassword();
const resetResp = await createBuilderWithoutAuth<LoginSuccessResponse>(harness)
.post('/auth/reset')
.body({token, password: newPassword})
.execute();
expect(resetResp.token.length).toBeGreaterThan(0);
await createBuilderWithoutAuth(harness)
.post('/auth/reset')
.body({token, password: generateUniquePassword()})
.expect(HTTP_STATUS.BAD_REQUEST)
.execute();
});
it('rejects invalid reset token', async () => {
await createTestAccount(harness);
await createBuilderWithoutAuth(harness)