AI Implementation feature(823): Landing Page and Login/Register Modal (#11)
This commit was merged in pull request #11.
This commit is contained in:
@@ -37,7 +37,12 @@ describe('Admin route protection', () => {
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
|
||||
const csrfPrime = await request(server).get('/api/v1/auth/csrf');
|
||||
const csrfCookie = (csrfPrime.headers['set-cookie'] as unknown as string[] | undefined)?.find((c) => c.startsWith('csrf='));
|
||||
const csrfToken = csrfCookie ? decodeURIComponent(csrfCookie.split(';')[0].split('=')[1]) : '';
|
||||
const login = await request(server).post('/api/v1/auth/login')
|
||||
.set('Cookie', `csrf=${csrfToken}`)
|
||||
.set('X-CSRF-Token', csrfToken)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
adminToken = login.body.accessToken;
|
||||
|
||||
@@ -33,7 +33,12 @@ beforeAll(async () => {
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
|
||||
const csrfPrime = await request(server).get('/api/v1/auth/csrf');
|
||||
const csrfCookie = (csrfPrime.headers['set-cookie'] as unknown as string[] | undefined)?.find((c) => c.startsWith('csrf='));
|
||||
const csrfToken = csrfCookie ? decodeURIComponent(csrfCookie.split(';')[0].split('=')[1]) : '';
|
||||
const login = await request(server).post('/api/v1/auth/login')
|
||||
.set('Cookie', `csrf=${csrfToken}`)
|
||||
.set('X-CSRF-Token', csrfToken)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
adminToken = login.body.accessToken;
|
||||
|
||||
@@ -52,16 +52,30 @@ beforeAll(async () => {
|
||||
expect(cookies?.some((c) => c.startsWith('csrf='))).toBe(true);
|
||||
});
|
||||
|
||||
async function primeCsrfAndLogin(agent: any): Promise<void> {
|
||||
await agent.get('/api/v1/auth/csrf');
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
||||
await agent.post('/api/v1/auth/login')
|
||||
.set('X-CSRF-Token', csrf.value)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
}
|
||||
|
||||
it('login returns an access token and sets the rt refresh cookie', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
await agent.get('/api/v1/auth/csrf');
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
||||
const res = await agent.post('/api/v1/auth/login')
|
||||
.set('X-CSRF-Token', csrf.value)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
expect(res.body.accessToken).toBeDefined();
|
||||
const cookies = res.headers['set-cookie'] as unknown as string[];
|
||||
const rt = cookies.find((c) => c.startsWith('rt='));
|
||||
const setCookies = res.headers['set-cookie'] as unknown as string[];
|
||||
const rt = setCookies.find((c) => c.startsWith('rt='));
|
||||
expect(rt).toBeDefined();
|
||||
expect(rt).toMatch(/HttpOnly/);
|
||||
});
|
||||
@@ -70,9 +84,7 @@ beforeAll(async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
await agent.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
await primeCsrfAndLogin(agent);
|
||||
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const rt = cookies.find((c: any) => c.name === 'rt');
|
||||
@@ -93,9 +105,7 @@ beforeAll(async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
await agent.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
await primeCsrfAndLogin(agent);
|
||||
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const rt = cookies.find((c: any) => c.name === 'rt');
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
process.env.DATABASE_PATH = ':memory:';
|
||||
process.env.THEMES_DIR = './themes';
|
||||
process.env.FRONTEND_DIST = './frontend/dist';
|
||||
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { HttpAdapterHost } from '@nestjs/core';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import * as express from 'express';
|
||||
import request from 'supertest';
|
||||
import { CookieAccessInfo } from 'cookiejar';
|
||||
import { AppModule } from '../../backend/src/app.module';
|
||||
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
|
||||
import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { initDb } from './db-helper';
|
||||
import { SettingsService } from '../../backend/src/modules/settings/settings.module';
|
||||
|
||||
describe('POST /api/v1/auth/register (player self-registration)', () => {
|
||||
let app: INestApplication;
|
||||
let settings: SettingsService;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
||||
app = moduleRef.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.use(express.json({ limit: '1mb' }));
|
||||
const csrfMw = new CsrfMiddleware(app.get(ConfigService));
|
||||
app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next));
|
||||
const httpAdapterHost = app.get(HttpAdapterHost);
|
||||
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
||||
await app.init();
|
||||
await initDb(app);
|
||||
settings = app.get(SettingsService);
|
||||
await settings.set('registrationsEnabled', 'true');
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
async function primeCsrf(agent: any): Promise<string> {
|
||||
const res = await agent.get('/api/v1/auth/csrf');
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
return cookies.find((c: any) => c.name === 'csrf').value;
|
||||
}
|
||||
|
||||
it('rejects without CSRF token', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
await agent.get('/api/v1/auth/csrf');
|
||||
await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.send({ username: 'alice', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' })
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('registers a new player and returns a session', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'alice', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
expect(res.body.accessToken).toBeDefined();
|
||||
expect(res.body.user.username).toBe('alice');
|
||||
expect(res.body.user.role).toBe('player');
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
expect(cookies.find((c: any) => c.name === 'rt')).toBeDefined();
|
||||
});
|
||||
|
||||
it('rejects mismatched password confirmation', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'bob', password: 'Sup3rSecret!Pass', passwordConfirm: 'OtherP4ss!' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe('VALIDATION_FAILED');
|
||||
});
|
||||
|
||||
it('rejects weak password', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'weak', password: 'short', passwordConfirm: 'short' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe('WEAK_PASSWORD');
|
||||
});
|
||||
|
||||
it('rejects duplicate username', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'carol', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
const csrf2 = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf2)
|
||||
.send({ username: 'carol', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' });
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.code).toBe('USERNAME_TAKEN');
|
||||
});
|
||||
|
||||
it('rejects when registrations are disabled', async () => {
|
||||
await settings.set('registrationsEnabled', 'false');
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'dave', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' });
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body.code).toBe('REGISTRATIONS_DISABLED');
|
||||
await settings.set('registrationsEnabled', 'true');
|
||||
});
|
||||
|
||||
it('rejects when per-IP registration rate limit exceeded', async () => {
|
||||
const { RegistrationRateLimitService } = await import('../../backend/src/common/services/registration-rate-limit.service');
|
||||
const rl = app.get(RegistrationRateLimitService);
|
||||
const now = Date.now();
|
||||
for (let i = 0; i < 10; i++) {
|
||||
rl.record('::ffff:127.0.0.1', now + i);
|
||||
rl.record('127.0.0.1', now + i);
|
||||
}
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const csrf = await primeCsrf(agent);
|
||||
const res = await agent
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-CSRF-Token', csrf)
|
||||
.send({ username: 'eve', password: 'Sup3rSecret!Pass', passwordConfirm: 'Sup3rSecret!Pass' });
|
||||
expect(res.status).toBe(429);
|
||||
expect(res.body.code).toBe('RATE_LIMITED');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
process.env.DATABASE_PATH = ':memory:';
|
||||
process.env.THEMES_DIR = './themes';
|
||||
process.env.FRONTEND_DIST = './frontend/dist';
|
||||
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { HttpAdapterHost } from '@nestjs/core';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import * as express from 'express';
|
||||
import request from 'supertest';
|
||||
import { AppModule } from '../../backend/src/app.module';
|
||||
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
|
||||
import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { initDb } from './db-helper';
|
||||
import { BlogPostEntity } from '../../backend/src/database/entities/blog-post.entity';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
|
||||
describe('GET /api/v1/blog/posts (public)', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
||||
app = moduleRef.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.use(express.json({ limit: '1mb' }));
|
||||
const csrfMw = new CsrfMiddleware(app.get(ConfigService));
|
||||
app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next));
|
||||
const httpAdapterHost = app.get(HttpAdapterHost);
|
||||
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
||||
await app.init();
|
||||
await initDb(app);
|
||||
|
||||
const repo = app.get<any>(getRepositoryToken(BlogPostEntity));
|
||||
await repo.insert([
|
||||
{ id: 'p1', title: 'Published A', bodyMd: '# Hello', status: 'published', publishedAt: '2026-07-20T10:00:00.000Z', createdAt: '2026-07-19T00:00:00.000Z', updatedAt: '2026-07-20T10:00:00.000Z' },
|
||||
{ id: 'p2', title: 'Draft B', bodyMd: 'should not appear', status: 'draft', publishedAt: null, createdAt: '2026-07-18T00:00:00.000Z', updatedAt: '2026-07-18T00:00:00.000Z' },
|
||||
{ id: 'p3', title: 'Published C', bodyMd: 'safe content', status: 'published', publishedAt: '2026-07-21T10:00:00.000Z', createdAt: '2026-07-21T00:00:00.000Z', updatedAt: '2026-07-21T10:00:00.000Z' },
|
||||
]);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('returns only published posts, ordered by publishedAt DESC', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const res = await request(server).get('/api/v1/blog/posts').expect(200);
|
||||
expect(res.body.posts).toHaveLength(2);
|
||||
expect(res.body.posts.map((p: any) => p.id)).toEqual(['p3', 'p1']);
|
||||
expect(res.body.posts[0].title).toBe('Published C');
|
||||
});
|
||||
|
||||
it('returns Markdown bodies and does not include HTML fields', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const res = await request(server).get('/api/v1/blog/posts').expect(200);
|
||||
const c = res.body.posts.find((p: any) => p.id === 'p3');
|
||||
expect(c.bodyMd).toBe('safe content');
|
||||
expect(c.bodyHtml).toBeUndefined();
|
||||
const a = res.body.posts.find((p: any) => p.id === 'p1');
|
||||
expect(a.bodyMd).toBe('# Hello');
|
||||
});
|
||||
|
||||
it('does not leak non-public fields', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const res = await request(server).get('/api/v1/blog/posts').expect(200);
|
||||
for (const post of res.body.posts) {
|
||||
expect(post.id).toBeDefined();
|
||||
expect(post.title).toBeDefined();
|
||||
expect(post.bodyMd).toBeDefined();
|
||||
expect(post.publishedAt).toBeDefined();
|
||||
expect(post.status).toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects unrelated endpoints (no admin/scoreboard leakage)', async () => {
|
||||
const server = app.getHttpServer();
|
||||
await request(server).get('/api/v1/blog/posts/drafts').expect(404);
|
||||
await request(server).get('/api/v1/admin/users').expect(401);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
process.env.DATABASE_PATH = ':memory:';
|
||||
process.env.THEMES_DIR = './themes';
|
||||
process.env.FRONTEND_DIST = './frontend/dist';
|
||||
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { HttpAdapterHost } from '@nestjs/core';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import * as express from 'express';
|
||||
import request from 'supertest';
|
||||
import { AppModule } from '../../backend/src/app.module';
|
||||
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
|
||||
import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { initDb } from './db-helper';
|
||||
|
||||
describe('CSRF: login is now protected', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
||||
app = moduleRef.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.use(express.json({ limit: '1mb' }));
|
||||
const csrfMw = new CsrfMiddleware(app.get(ConfigService));
|
||||
app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next));
|
||||
const httpAdapterHost = app.get(HttpAdapterHost);
|
||||
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
||||
await app.init();
|
||||
await initDb(app);
|
||||
|
||||
const server = app.getHttpServer();
|
||||
await request(server).post('/api/v1/auth/register-first-admin')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('rejects POST /api/v1/auth/login without CSRF header', async () => {
|
||||
const server = app.getHttpServer();
|
||||
await request(server)
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('accepts POST /api/v1/auth/login with valid CSRF header', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const prime = await request(server).get('/api/v1/auth/csrf').expect(200);
|
||||
const csrfCookie = (prime.headers['set-cookie'] as unknown as string[] | undefined)?.find((c) => c.startsWith('csrf='));
|
||||
const csrfToken = csrfCookie ? decodeURIComponent(csrfCookie.split(';')[0].split('=')[1]) : '';
|
||||
await request(server)
|
||||
.post('/api/v1/auth/login')
|
||||
.set('Cookie', `csrf=${csrfToken}`)
|
||||
.set('X-CSRF-Token', csrfToken)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('still allows /api/v1/auth/register-first-admin without CSRF', async () => {
|
||||
const server = app.getHttpServer();
|
||||
await request(server)
|
||||
.post('/api/v1/auth/register-first-admin')
|
||||
.send({ username: 'noone', password: 'Sup3rSecret!Pass' })
|
||||
.expect(409);
|
||||
});
|
||||
});
|
||||
@@ -88,7 +88,12 @@ describe('Uploads endpoint integration', () => {
|
||||
await request(server).post('/api/v1/auth/register-first-admin')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
const csrfPrime = await request(server).get('/api/v1/auth/csrf');
|
||||
const csrfCookie = (csrfPrime.headers['set-cookie'] as unknown as string[] | undefined)?.find((c) => c.startsWith('csrf='));
|
||||
const csrfToken = csrfCookie ? decodeURIComponent(csrfCookie.split(';')[0].split('=')[1]) : '';
|
||||
const login = await request(server).post('/api/v1/auth/login')
|
||||
.set('Cookie', `csrf=${csrfToken}`)
|
||||
.set('X-CSRF-Token', csrfToken)
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
adminToken = login.body.accessToken;
|
||||
|
||||
Reference in New Issue
Block a user