mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-21 22:31:46 +00:00
chore: apply prettier on the entire project
This commit is contained in:
@@ -2,10 +2,18 @@
|
||||
* User Profile & Settings integration tests.
|
||||
* Covers PROFILE-001 to PROFILE-015.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import { createApp } from '../../src/app';
|
||||
import { runMigrations } from '../../src/db/migrations';
|
||||
import { createTables } from '../../src/db/schema';
|
||||
import { loginAttempts, mfaAttempts } from '../../src/routes/auth';
|
||||
import { authCookie } from '../helpers/auth';
|
||||
import { createUser, createAdmin, createTrip } from '../helpers/factories';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
|
||||
import type { Application } from 'express';
|
||||
import path from 'path';
|
||||
import request from 'supertest';
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
|
||||
const { testDb, dbMock } = vi.hoisted(() => {
|
||||
const Database = require('better-sqlite3');
|
||||
@@ -18,13 +26,29 @@ const { testDb, dbMock } = vi.hoisted(() => {
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
getPlaceWithTags: (placeId: number) => {
|
||||
const place: any = db.prepare(`SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon FROM places p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?`).get(placeId);
|
||||
const place: any = db
|
||||
.prepare(
|
||||
`SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon FROM places p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?`,
|
||||
)
|
||||
.get(placeId);
|
||||
if (!place) return null;
|
||||
const tags = db.prepare(`SELECT t.* FROM tags t JOIN place_tags pt ON t.id = pt.tag_id WHERE pt.place_id = ?`).all(placeId);
|
||||
return { ...place, category: place.category_id ? { id: place.category_id, name: place.category_name, color: place.category_color, icon: place.category_icon } : null, tags };
|
||||
const tags = db
|
||||
.prepare(`SELECT t.* FROM tags t JOIN place_tags pt ON t.id = pt.tag_id WHERE pt.place_id = ?`)
|
||||
.all(placeId);
|
||||
return {
|
||||
...place,
|
||||
category: place.category_id
|
||||
? { id: place.category_id, name: place.category_name, color: place.category_color, icon: place.category_icon }
|
||||
: null,
|
||||
tags,
|
||||
};
|
||||
},
|
||||
canAccessTrip: (tripId: any, userId: number) =>
|
||||
db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId),
|
||||
db
|
||||
.prepare(
|
||||
`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`,
|
||||
)
|
||||
.get(userId, tripId, userId),
|
||||
isOwner: (tripId: any, userId: number) =>
|
||||
!!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
|
||||
};
|
||||
@@ -38,14 +62,6 @@ vi.mock('../../src/config', () => ({
|
||||
updateJwtSecret: () => {},
|
||||
}));
|
||||
|
||||
import { createApp } from '../../src/app';
|
||||
import { createTables } from '../../src/db/schema';
|
||||
import { runMigrations } from '../../src/db/migrations';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import { createUser, createAdmin, createTrip } from '../helpers/factories';
|
||||
import { authCookie } from '../helpers/auth';
|
||||
import { loginAttempts, mfaAttempts } from '../../src/routes/auth';
|
||||
|
||||
const app: Application = createApp();
|
||||
const FIXTURE_JPEG = path.join(__dirname, '../fixtures/small-image.jpg');
|
||||
const FIXTURE_PDF = path.join(__dirname, '../fixtures/test.pdf');
|
||||
@@ -72,9 +88,7 @@ afterAll(() => {
|
||||
describe('PROFILE-001 — Get current user profile', () => {
|
||||
it('returns user object with expected fields', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const res = await request(app)
|
||||
.get('/api/auth/me')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const res = await request(app).get('/api/auth/me').set('Cookie', authCookie(user.id));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.user).toMatchObject({
|
||||
id: user.id,
|
||||
@@ -113,19 +127,12 @@ describe('Avatar', () => {
|
||||
it('PROFILE-005 — DELETE /api/auth/avatar clears avatar_url', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
// Upload first
|
||||
await request(app)
|
||||
.post('/api/auth/avatar')
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.attach('avatar', FIXTURE_JPEG);
|
||||
await request(app).post('/api/auth/avatar').set('Cookie', authCookie(user.id)).attach('avatar', FIXTURE_JPEG);
|
||||
|
||||
const res = await request(app)
|
||||
.delete('/api/auth/avatar')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const res = await request(app).delete('/api/auth/avatar').set('Cookie', authCookie(user.id));
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const me = await request(app)
|
||||
.get('/api/auth/me')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const me = await request(app).get('/api/auth/me').set('Cookie', authCookie(user.id));
|
||||
expect(me.body.user.avatar_url).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -170,19 +177,14 @@ describe('Settings', () => {
|
||||
.send({ key: 'dark_mode', value: 'dark' });
|
||||
expect(put.status).toBe(200);
|
||||
|
||||
const get = await request(app)
|
||||
.get('/api/settings')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const get = await request(app).get('/api/settings').set('Cookie', authCookie(user.id));
|
||||
expect(get.status).toBe(200);
|
||||
expect(get.body.settings).toHaveProperty('dark_mode', 'dark');
|
||||
});
|
||||
|
||||
it('PROFILE-009 — PUT /api/settings without key returns 400', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const res = await request(app)
|
||||
.put('/api/settings')
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.send({ value: 'dark' });
|
||||
const res = await request(app).put('/api/settings').set('Cookie', authCookie(user.id)).send({ value: 'dark' });
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
@@ -196,9 +198,7 @@ describe('Settings', () => {
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.success).toBe(true);
|
||||
|
||||
const get = await request(app)
|
||||
.get('/api/settings')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const get = await request(app).get('/api/settings').set('Cookie', authCookie(user.id));
|
||||
expect(get.body.settings).toHaveProperty('theme', 'dark');
|
||||
expect(get.body.settings).toHaveProperty('language', 'fr');
|
||||
expect(get.body.settings).toHaveProperty('timezone', 'Europe/Paris');
|
||||
@@ -209,24 +209,18 @@ describe('Account deletion', () => {
|
||||
it('PROFILE-013 — DELETE /api/auth/me removes account, subsequent login fails', async () => {
|
||||
const { user, password } = createUser(testDb);
|
||||
|
||||
const del = await request(app)
|
||||
.delete('/api/auth/me')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const del = await request(app).delete('/api/auth/me').set('Cookie', authCookie(user.id));
|
||||
expect(del.status).toBe(200);
|
||||
|
||||
// Should not be able to log in
|
||||
const login = await request(app)
|
||||
.post('/api/auth/login')
|
||||
.send({ email: user.email, password });
|
||||
const login = await request(app).post('/api/auth/login').send({ email: user.email, password });
|
||||
expect(login.status).toBe(401);
|
||||
});
|
||||
|
||||
it('PROFILE-013 — admin cannot delete their own account', async () => {
|
||||
const { user: admin } = createAdmin(testDb);
|
||||
// Admins are protected from self-deletion
|
||||
const res = await request(app)
|
||||
.delete('/api/auth/me')
|
||||
.set('Cookie', authCookie(admin.id));
|
||||
const res = await request(app).delete('/api/auth/me').set('Cookie', authCookie(admin.id));
|
||||
// deleteAccount returns 400 when the user is the last admin
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
@@ -241,9 +235,7 @@ describe('Travel stats', () => {
|
||||
end_date: '2024-06-05',
|
||||
});
|
||||
|
||||
const res = await request(app)
|
||||
.get('/api/auth/travel-stats')
|
||||
.set('Cookie', authCookie(user.id));
|
||||
const res = await request(app).get('/api/auth/travel-stats').set('Cookie', authCookie(user.id));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('totalTrips');
|
||||
expect(res.body.totalTrips).toBeGreaterThanOrEqual(1);
|
||||
@@ -253,9 +245,11 @@ describe('Travel stats', () => {
|
||||
describe('Demo mode protections', () => {
|
||||
it('PROFILE-015 — demo user cannot upload avatar (demoUploadBlock)', async () => {
|
||||
// demoUploadBlock checks for email === 'demo@nomad.app'
|
||||
testDb.prepare(
|
||||
"INSERT INTO users (username, email, password_hash, role) VALUES ('demo', 'demo@nomad.app', 'x', 'user')"
|
||||
).run();
|
||||
testDb
|
||||
.prepare(
|
||||
"INSERT INTO users (username, email, password_hash, role) VALUES ('demo', 'demo@nomad.app', 'x', 'user')",
|
||||
)
|
||||
.run();
|
||||
const demoUser = testDb.prepare('SELECT id FROM users WHERE email = ?').get('demo@nomad.app') as { id: number };
|
||||
process.env.DEMO_MODE = 'true';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user