8b1703dce9
CI / Generate TypeScript types (push) Successful in 10s
CI / Generate TypeScript types (pull_request) Successful in 11s
CI / Server unit tests (push) Failing after 24s
CI / Build server (push) Successful in 24s
CI / Server unit tests (pull_request) Failing after 18s
CI / Build client (push) Successful in 31s
CI / Build server (pull_request) Successful in 25s
CI / Build client (pull_request) Successful in 32s
CI / Playwright E2E tests (push) Successful in 1m17s
CI / Build and push Docker image (push) Has been skipped
CI / Playwright E2E tests (pull_request) Successful in 1m9s
CI / Notify (push) Successful in 2s
CI / Build and push Docker image (pull_request) Has been skipped
CI / Notify (pull_request) Has been skipped
- odstraněn .woodpecker/workflow.yaml (CI přesunuto na Gitea Actions) - tsconfig.json: exclude src/tests/**/* (feat/tests verze) - jest.config.js: testEnvironment node + master cesty - auth/pizza/voting tests: union obou větví, použit resetMemoryStorage() - service.test.ts: jest.useFakeTimers místo MOCK_DATA=true - všechny testy: 167/167 PASS
84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
import { generateToken, verify, getLogin, getTrusted } from '../auth';
|
|
|
|
const VALID_SECRET = 'test-jwt-secret-ktery-ma-alespon-32-znaku';
|
|
const SHORT_SECRET = 'kratky';
|
|
|
|
beforeEach(() => {
|
|
process.env.JWT_SECRET = VALID_SECRET;
|
|
});
|
|
|
|
afterEach(() => {
|
|
delete process.env.JWT_SECRET;
|
|
});
|
|
|
|
describe('generateToken', () => {
|
|
test('vrátí token pro platný login', () => {
|
|
const token = generateToken('alice');
|
|
expect(typeof token).toBe('string');
|
|
expect(token.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
test('vyhodí chybu bez JWT_SECRET', () => {
|
|
delete process.env.JWT_SECRET;
|
|
expect(() => generateToken('alice')).toThrow('JWT_SECRET');
|
|
});
|
|
|
|
test('vyhodí chybu pro příliš krátký JWT_SECRET', () => {
|
|
process.env.JWT_SECRET = SHORT_SECRET;
|
|
expect(() => generateToken('alice')).toThrow('32');
|
|
});
|
|
|
|
test('vyhodí chybu pro prázdný login', () => {
|
|
expect(() => generateToken('')).toThrow('login');
|
|
});
|
|
|
|
test('vyhodí chybu pro login obsahující jen mezery', () => {
|
|
expect(() => generateToken(' ')).toThrow('login');
|
|
});
|
|
|
|
test('vyhodí chybu pro chybějící login', () => {
|
|
expect(() => generateToken(undefined)).toThrow('login');
|
|
});
|
|
});
|
|
|
|
describe('verify', () => {
|
|
test('vrátí true pro platný token', () => {
|
|
const token = generateToken('alice');
|
|
expect(verify(token)).toBe(true);
|
|
});
|
|
|
|
test('vrátí false pro podvrženou signaturu', () => {
|
|
const token = generateToken('alice');
|
|
const tampered = token.slice(0, -5) + 'XXXXX';
|
|
expect(verify(tampered)).toBe(false);
|
|
});
|
|
|
|
test('vrátí false pro token podepsaný jiným secret', () => {
|
|
process.env.JWT_SECRET = 'other-secret-min-32-chars-bbbbb!';
|
|
const tokenOther = generateToken('alice');
|
|
process.env.JWT_SECRET = VALID_SECRET;
|
|
expect(verify(tokenOther)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('getLogin / getTrusted', () => {
|
|
test('round-trip: getLogin vrátí správný login', () => {
|
|
const token = generateToken('bob');
|
|
expect(getLogin(token)).toBe('bob');
|
|
});
|
|
|
|
test('trusted=false je výchozí hodnota', () => {
|
|
const token = generateToken('alice');
|
|
expect(getTrusted(token)).toBe(false);
|
|
});
|
|
|
|
test('trusted=true je zachováno', () => {
|
|
const token = generateToken('alice', true);
|
|
expect(getTrusted(token)).toBe(true);
|
|
});
|
|
|
|
test('getLogin vyhodí chybu pro chybějící token', () => {
|
|
expect(() => getLogin(undefined)).toThrow('token');
|
|
});
|
|
});
|