test: add unit tests for all auth backend logic
- Register: edge cases for non-Error throws, error message assertions, and verify signUpEmail was called with correct payload - Forgot-password: empty/missing email, Error/non-Error throws - Reset-password: load (missing/expired/valid token) and action (short password, mismatch, throw, success redirect) - Account setup: load (unauthorized, returns user), action (redirect, falsy create, throws, isRedirect passthrough) - Email-verification: correct URL construction, sendMail delegation, throw propagation - Hooks: sets/omits locals, passes building/auth to svelteKitHandler - Fix Svelte 5 warning on setup page by using with one-time guard instead of top-level prop read
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { sendMail } = vi.hoisted(() => ({
|
||||
sendMail: vi.fn()
|
||||
}));
|
||||
|
||||
vi.mock('$env/static/private', () => ({
|
||||
BETTER_AUTH_URL: 'http://localhost:5173'
|
||||
}));
|
||||
|
||||
vi.mock('./resend', () => ({
|
||||
sendMail
|
||||
}));
|
||||
|
||||
import { sendVerificationEmail } from './email-verification';
|
||||
|
||||
describe('sendVerificationEmail', () => {
|
||||
beforeEach(() => {
|
||||
sendMail.mockReset();
|
||||
});
|
||||
|
||||
it('calls sendMail with correct email and template', async () => {
|
||||
sendMail.mockResolvedValue(undefined);
|
||||
|
||||
await sendVerificationEmail({
|
||||
email: 'jane@example.com',
|
||||
token: 'abc123'
|
||||
});
|
||||
|
||||
expect(sendMail).toHaveBeenCalledWith({
|
||||
to: 'jane@example.com',
|
||||
templateId: 'email-verification-email',
|
||||
data: {
|
||||
target_email_address: 'jane@example.com',
|
||||
verification_link:
|
||||
'http://localhost:5173/api/auth/verify-email?token=abc123&callbackURL=/login?verified=true'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('constructs verification URL with the given token', async () => {
|
||||
sendMail.mockResolvedValue(undefined);
|
||||
|
||||
await sendVerificationEmail({
|
||||
email: 'test@example.com',
|
||||
token: 'xyz789'
|
||||
});
|
||||
|
||||
const call = sendMail.mock.calls[0][0];
|
||||
expect(call.data.verification_link).toContain('xyz789');
|
||||
expect(call.data.verification_link).toContain('/login?verified=true');
|
||||
});
|
||||
|
||||
it('throws when sendMail throws', async () => {
|
||||
sendMail.mockRejectedValue(new Error('Resend error'));
|
||||
|
||||
await expect(
|
||||
sendVerificationEmail({
|
||||
email: 'jane@example.com',
|
||||
token: 'abc123'
|
||||
})
|
||||
).rejects.toThrow('Resend error');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user