|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { runSetInitialPassword, type SetPasswordCapableApi } from './set-initial-password.js'; |
| 5 | + |
| 6 | +function makeRequest(body: unknown): Request { |
| 7 | + return new Request('https://example.test/api/v1/auth/set-initial-password', { |
| 8 | + method: 'POST', |
| 9 | + headers: { 'content-type': 'application/json', cookie: 'better-auth.session_token=abc' }, |
| 10 | + body: typeof body === 'string' ? body : JSON.stringify(body), |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +/** Mimic a better-call APIError (`{ statusCode, status, body: { code, message } }`). */ |
| 15 | +function apiError(statusCode: number, code: string, message: string) { |
| 16 | + return Object.assign(new Error(message), { statusCode, status: code, body: { code, message } }); |
| 17 | +} |
| 18 | + |
| 19 | +describe('runSetInitialPassword', () => { |
| 20 | + it('rejects a missing newPassword with 400 invalid_request (no API call)', async () => { |
| 21 | + const api: SetPasswordCapableApi = { setPassword: vi.fn() }; |
| 22 | + const res = await runSetInitialPassword(api, makeRequest({})); |
| 23 | + expect(res.status).toBe(400); |
| 24 | + expect(res.body).toEqual({ success: false, error: { code: 'invalid_request', message: 'newPassword is required' } }); |
| 25 | + expect(api.setPassword).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('rejects a non-JSON body with 400', async () => { |
| 29 | + const api: SetPasswordCapableApi = { setPassword: vi.fn() }; |
| 30 | + const res = await runSetInitialPassword(api, makeRequest('not-json{')); |
| 31 | + expect(res.status).toBe(400); |
| 32 | + expect(res.body.error?.code).toBe('invalid_request'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('forwards newPassword + session headers to better-auth and returns 200 on success', async () => { |
| 36 | + const setPassword = vi.fn().mockResolvedValue({ status: true }); |
| 37 | + const req = makeRequest({ newPassword: 'super-secret-pw' }); |
| 38 | + const res = await runSetInitialPassword({ setPassword }, req); |
| 39 | + |
| 40 | + expect(res).toEqual({ status: 200, body: { success: true } }); |
| 41 | + expect(setPassword).toHaveBeenCalledTimes(1); |
| 42 | + const arg = setPassword.mock.calls[0][0]; |
| 43 | + expect(arg.body).toEqual({ newPassword: 'super-secret-pw' }); |
| 44 | + // The session cookie must ride along so better-auth's session middleware |
| 45 | + // can identify the caller. |
| 46 | + expect(arg.headers.get('cookie')).toContain('better-auth.session_token'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('maps PASSWORD_ALREADY_SET to 409 (use change-password)', async () => { |
| 50 | + const setPassword = vi.fn().mockRejectedValue( |
| 51 | + apiError(400, 'PASSWORD_ALREADY_SET', 'A local password is already set'), |
| 52 | + ); |
| 53 | + const res = await runSetInitialPassword({ setPassword }, makeRequest({ newPassword: 'x'.repeat(12) })); |
| 54 | + expect(res.status).toBe(409); |
| 55 | + expect(res.body.error).toEqual({ code: 'PASSWORD_ALREADY_SET', message: 'A local password is already set' }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('preserves length-validation errors (400 PASSWORD_TOO_SHORT)', async () => { |
| 59 | + const setPassword = vi.fn().mockRejectedValue(apiError(400, 'PASSWORD_TOO_SHORT', 'Password is too short')); |
| 60 | + const res = await runSetInitialPassword({ setPassword }, makeRequest({ newPassword: 'short' })); |
| 61 | + expect(res.status).toBe(400); |
| 62 | + expect(res.body.error?.code).toBe('PASSWORD_TOO_SHORT'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('passes through a 401 when no session is present', async () => { |
| 66 | + const setPassword = vi.fn().mockRejectedValue(apiError(401, 'UNAUTHORIZED', 'Sign in first')); |
| 67 | + const res = await runSetInitialPassword({ setPassword }, makeRequest({ newPassword: 'x'.repeat(12) })); |
| 68 | + expect(res.status).toBe(401); |
| 69 | + expect(res.body.error?.code).toBe('UNAUTHORIZED'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('falls back to 500 internal for a plain (non-APIError) throw', async () => { |
| 73 | + const setPassword = vi.fn().mockRejectedValue(new Error('adapter exploded')); |
| 74 | + const res = await runSetInitialPassword({ setPassword }, makeRequest({ newPassword: 'x'.repeat(12) })); |
| 75 | + expect(res.status).toBe(500); |
| 76 | + expect(res.body.error).toEqual({ code: 'internal', message: 'adapter exploded' }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments