feat: add 2FA, secure cookies and publishing tools
All checks were successful
CI / Validate frontend and API (push) Successful in 3m5s
CI / Build and publish Docker images (push) Successful in 1m42s

This commit is contained in:
Cauê Faleiros
2026-11-06 10:29:37 -03:00
parent fff63f5efa
commit 50905eefd7
40 changed files with 3493 additions and 215 deletions

33
tests/auth.test.ts Normal file
View File

@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { createRawToken, hashToken } from '../server/src/auth/account-tokens';
import { hashPassword, verifyPassword } from '../server/src/auth/passwords';
describe('account tokens', () => {
it('produces unique URL-safe tokens', () => {
const first = createRawToken();
const second = createRawToken();
expect(first).not.toBe(second);
expect(first).toMatch(/^[A-Za-z0-9_-]+$/);
expect(first.length).toBeGreaterThan(30);
});
it('hashes deterministically without exposing the token', () => {
const token = 'example-token';
expect(hashToken(token)).toBe(hashToken(token));
expect(hashToken(token)).not.toBe(token);
expect(hashToken(token)).toMatch(/^[a-f0-9]{64}$/);
});
});
describe('password hashing', () => {
it('accepts the correct password and rejects others', async () => {
const hash = await hashPassword('Compor@123');
expect(hash).not.toContain('Compor@123');
await expect(verifyPassword('Compor@123', hash)).resolves.toBe(true);
await expect(verifyPassword('wrong-password', hash)).resolves.toBe(false);
});
it('rejects malformed hashes', async () => {
await expect(verifyPassword('Compor@123', 'not-a-hash')).resolves.toBe(false);
});
});

44
tests/format.test.ts Normal file
View File

@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { formatMinutes, initials, lessonDuration, plural, shortDate, totalDuration } from '../lib/format';
import { Course } from '../types';
const course = (duration: string): Course => ({
id: duration,
title: 'Curso',
category: 'Área',
thumbnail: '',
duration,
description: '',
lessons: [],
});
describe('formatting helpers', () => {
it('pluralizes counts', () => {
expect(plural(1, 'aula', 'aulas')).toBe('1 aula');
expect(plural(3, 'aula', 'aulas')).toBe('3 aulas');
});
it('formats minutes with and without hours', () => {
expect(formatMinutes(0)).toBe('0min');
expect(formatMinutes(45)).toBe('45min');
expect(formatMinutes(60)).toBe('1h');
expect(formatMinutes(125)).toBe('2h 05min');
});
it('builds initials and lesson durations', () => {
expect(initials('Ana Maria Silva')).toBe('AM');
expect(lessonDuration({ id: '1', title: 'Aula', duration: '—' } as never)).toBe('');
expect(lessonDuration({ id: '1', title: 'Aula', duration: '12min' } as never)).toBe('12min');
});
it('sums course durations', () => {
expect(totalDuration([course('1h 30min'), course('45min')])).toBe('2h 15min');
expect(totalDuration([])).toBe('');
});
it('formats short dates and rejects invalid values', () => {
expect(shortDate('2026-11-05T12:00:00Z')).toContain('5');
expect(shortDate('invalid')).toBe('');
expect(shortDate(null)).toBe('');
});
});