140 lines
5.6 KiB
TypeScript
140 lines
5.6 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { signRefreshToken } from '../../src/lib/jwt.js';
|
|
import { buildAgent, buildApp, createUser, rand, PASSWORD } from '../helpers.js';
|
|
|
|
describe('auth routes', () => {
|
|
it('register: creates user, returns public user + access token + refresh cookie', async () => {
|
|
const app = buildApp();
|
|
const email = `reg-${rand()}@test.local`;
|
|
const res = await app
|
|
.post('/api/v1/auth/register')
|
|
.send({ email, password: PASSWORD, firstName: 'Jan', lastName: 'Kowalski' });
|
|
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.user).toMatchObject({ email, firstName: 'Jan', lastName: 'Kowalski', isAdmin: false });
|
|
expect(res.body.user).not.toHaveProperty('passwordHash');
|
|
expect(typeof res.body.accessToken).toBe('string');
|
|
const setCookie = (res.headers['set-cookie'] ?? [])[0] ?? '';
|
|
expect(setCookie).toContain('fitos_refresh=');
|
|
expect(setCookie).toContain('HttpOnly');
|
|
expect(setCookie).toContain('SameSite=Lax');
|
|
});
|
|
|
|
it('register: lowercases the email', async () => {
|
|
const app = buildApp();
|
|
const raw = `Upper-${rand()}@TEST.LOCAL`;
|
|
const res = await app.post('/api/v1/auth/register').send({ email: raw, password: PASSWORD });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.user.email).toBe(raw.toLowerCase());
|
|
});
|
|
|
|
it('register: rejects a duplicate email with 409', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const res = await app
|
|
.post('/api/v1/auth/register')
|
|
.send({ email: user.email, password: PASSWORD });
|
|
expect(res.status).toBe(409);
|
|
expect(res.body.error.code).toBe('CONFLICT');
|
|
});
|
|
|
|
it('register: rejects a short password with 400 validation error', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/auth/register')
|
|
.send({ email: `short-${rand()}@test.local`, password: 'x' });
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error.code).toBe('BAD_REQUEST');
|
|
expect(res.body.error.details.fieldErrors.password).toBeDefined();
|
|
});
|
|
|
|
it('register: rejects an invalid email with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/auth/register')
|
|
.send({ email: 'not-an-email', password: PASSWORD });
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error.details.fieldErrors.email).toBeDefined();
|
|
});
|
|
|
|
it('login: returns user + access token + refresh cookie', async () => {
|
|
const app = buildApp();
|
|
const { user, password } = await createUser();
|
|
const res = await app.post('/api/v1/auth/login').send({ email: user.email, password });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user.id).toBe(user.id);
|
|
expect(typeof res.body.accessToken).toBe('string');
|
|
expect((res.headers['set-cookie'] ?? [])[0]).toContain('fitos_refresh=');
|
|
});
|
|
|
|
it('login: rejects a wrong password with 401', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const res = await app
|
|
.post('/api/v1/auth/login')
|
|
.send({ email: user.email, password: 'WrongPass123!' });
|
|
expect(res.status).toBe(401);
|
|
expect(res.body.error.code).toBe('UNAUTHORIZED');
|
|
});
|
|
|
|
it('login: rejects an unknown email with 401', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/auth/login')
|
|
.send({ email: `ghost-${rand()}@test.local`, password: PASSWORD });
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('login: rejects a malformed body with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app.post('/api/v1/auth/login').send({ password: 'no-email' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('refresh: exchanges the refresh cookie for a new access token', async () => {
|
|
const { user, password } = await createUser();
|
|
const agent = buildAgent();
|
|
const login = await agent.post('/api/v1/auth/login').send({ email: user.email, password });
|
|
expect(login.status).toBe(200);
|
|
|
|
const refresh = await agent.post('/api/v1/auth/refresh');
|
|
expect(refresh.status).toBe(200);
|
|
expect(typeof refresh.body.accessToken).toBe('string');
|
|
});
|
|
|
|
it('refresh: rejects a missing cookie with 401', async () => {
|
|
const app = buildApp();
|
|
const res = await app.post('/api/v1/auth/refresh');
|
|
expect(res.status).toBe(401);
|
|
expect(res.body.error.message).toBe('Missing refresh token');
|
|
});
|
|
|
|
it('refresh: rejects a revoked token (tokenVersion bump) with 401', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const token = signRefreshToken(user.id, user.tokenVersion);
|
|
await prisma.user.update({ where: { id: user.id }, data: { tokenVersion: { increment: 1 } } });
|
|
const res = await app.post('/api/v1/auth/refresh').set('Cookie', [`fitos_refresh=${token}`]);
|
|
expect(res.status).toBe(401);
|
|
expect(res.body.error.message).toBe('Token revoked');
|
|
});
|
|
|
|
it('refresh: rejects a garbage cookie with 401', async () => {
|
|
const app = buildApp();
|
|
const res = await app.post('/api/v1/auth/refresh').set('Cookie', ['fitos_refresh=garbage']);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('logout: returns 204 and clears the refresh cookie', async () => {
|
|
const { user, password } = await createUser();
|
|
const agent = buildAgent();
|
|
await agent.post('/api/v1/auth/login').send({ email: user.email, password });
|
|
const res = await agent.post('/api/v1/auth/logout');
|
|
expect(res.status).toBe(204);
|
|
const setCookie = (res.headers['set-cookie'] ?? [])[0] ?? '';
|
|
expect(setCookie).toContain('fitos_refresh=');
|
|
expect(setCookie).toMatch(/Expires=Thu, 01 Jan 1970|Max-Age=0/);
|
|
});
|
|
});
|