Files
Jarvise-project/backend/test/auth.test.js
T
BlackAngelTV 877df963f9 test(backend): cover auth, projects and streaming with a mocked Ollama
Tests run without any external service; the mock streams NDJSON chunks so the
SSE path is exercised end to end.
2026-10-05 15:01:57 +02:00

127 lines
4.3 KiB
JavaScript

import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { api, registerUser, startServer } from './helpers.js';
describe('authentication', () => {
let srv;
before(async () => {
srv = await startServer();
});
after(async () => {
await srv.close();
});
it('registers a user and returns a token', async () => {
const res = await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'register@x.io', password: 'motdepasse123', name: 'Tony' },
});
assert.equal(res.status, 201);
assert.ok(res.json.token, 'expected a token');
assert.deepEqual(
{ email: res.json.user.email, name: res.json.user.name },
{ email: 'register@x.io', name: 'Tony' },
);
assert.equal(res.json.user.password_hash, undefined, 'the hash must never leak');
});
it('rejects an already used email', async () => {
await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'duplicate@x.io', password: 'motdepasse123' },
});
const res = await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'duplicate@x.io', password: 'motdepasse123' },
});
assert.equal(res.status, 409);
});
it('validates input (short password, invalid email)', async () => {
const short = await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'short@x.io', password: '123' },
});
assert.equal(short.status, 400);
const bad = await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'not-an-email', password: 'motdepasse123' },
});
assert.equal(bad.status, 400);
});
it('logs a valid user in', async () => {
await api(srv.base, 'POST', '/api/auth/register', {
body: { email: 'login@x.io', password: 'motdepasse123' },
});
const res = await api(srv.base, 'POST', '/api/auth/login', {
body: { email: 'login@x.io', password: 'motdepasse123' },
});
assert.equal(res.status, 200);
assert.ok(res.json.token);
});
it('rejects a wrong password or email', async () => {
const wrongPass = await api(srv.base, 'POST', '/api/auth/login', {
body: { email: 'login@x.io', password: 'wrong-password' },
});
assert.equal(wrongPass.status, 401);
const unknown = await api(srv.base, 'POST', '/api/auth/login', {
body: { email: 'unknown@x.io', password: 'motdepasse123' },
});
assert.equal(unknown.status, 401);
assert.equal(unknown.json.error, 'Incorrect email or password.');
});
it('protects /api/auth/me', async () => {
const { token } = await registerUser(srv.base, 'me@x.io');
const ok = await api(srv.base, 'GET', '/api/auth/me', { token });
assert.equal(ok.status, 200);
assert.equal(ok.json.user.email, 'me@x.io');
assert.equal((await api(srv.base, 'GET', '/api/auth/me')).status, 401);
assert.equal((await api(srv.base, 'GET', '/api/auth/me', { token: 'garbage' })).status, 401);
});
it('updates the profile', async () => {
const { token } = await registerUser(srv.base, 'profile@x.io');
const res = await api(srv.base, 'PATCH', '/api/auth/me', { token, body: { name: 'Jarvis' } });
assert.equal(res.status, 200);
assert.equal(res.json.user.name, 'Jarvis');
});
it('changes the password and invalidates the old one', async () => {
const email = 'password@x.io';
const { token } = await registerUser(srv.base, email);
const wrong = await api(srv.base, 'POST', '/api/auth/password', {
token,
body: { currentPassword: 'wrong', newPassword: 'newpassword123' },
});
assert.equal(wrong.status, 401);
const ok = await api(srv.base, 'POST', '/api/auth/password', {
token,
body: { currentPassword: 'motdepasse123', newPassword: 'newpassword123' },
});
assert.equal(ok.status, 204);
const newLogin = await api(srv.base, 'POST', '/api/auth/login', {
body: { email, password: 'newpassword123' },
});
assert.equal(newLogin.status, 200);
const oldLogin = await api(srv.base, 'POST', '/api/auth/login', {
body: { email, password: 'motdepasse123' },
});
assert.equal(oldLogin.status, 401);
});
it('logs out (204)', async () => {
const { token } = await registerUser(srv.base, 'logout@x.io');
assert.equal((await api(srv.base, 'POST', '/api/auth/logout', { token })).status, 204);
});
});