106 lines
4.4 KiB
TypeScript
106 lines
4.4 KiB
TypeScript
process.env.DATABASE_PATH = ':memory:';
|
|
process.env.THEMES_DIR = './themes';
|
|
process.env.FRONTEND_DIST = './frontend/dist';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
import { INestApplication, ValidationPipe } 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';
|
|
|
|
describe('POST /api/v1/auth/change-password', () => {
|
|
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));
|
|
app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: false, transform: true }));
|
|
const httpAdapterHost = app.get(HttpAdapterHost);
|
|
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
|
await app.init();
|
|
await initDb(app);
|
|
|
|
await request(app.getHttpServer())
|
|
.post('/api/v1/auth/register-first-admin')
|
|
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
async function buildLoggedInAgent(): Promise<any> {
|
|
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);
|
|
(agent as any).accessToken = res.body.accessToken as string;
|
|
return agent;
|
|
}
|
|
|
|
it('rejects unauthenticated requests (CSRF or auth guard)', async () => {
|
|
const res = await request(app.getHttpServer())
|
|
.post('/api/v1/auth/change-password')
|
|
.send({ oldPassword: 'x', newPassword: 'Abcdefg1!Xyz', confirmNewPassword: 'Abcdefg1!Xyz' });
|
|
expect([401, 403]).toContain(res.status);
|
|
});
|
|
|
|
it('rejects when new and confirm do not match (VALIDATION_FAILED or PASSWORDS_DO_NOT_MATCH)', async () => {
|
|
const agent = await buildLoggedInAgent();
|
|
const csrf = (agent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
const res = await agent
|
|
.post('/api/v1/auth/change-password')
|
|
.set('Authorization', `Bearer ${agent.accessToken}`)
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ oldPassword: 'Sup3rSecret!Pass', newPassword: 'NewSecret!Pass1', confirmNewPassword: 'Different1!Pass' });
|
|
expect(res.status).toBe(400);
|
|
expect(['PASSWORDS_DO_NOT_MATCH', 'VALIDATION_FAILED']).toContain(res.body.code);
|
|
});
|
|
|
|
it('rejects when old password is wrong (INVALID_OLD_PASSWORD)', async () => {
|
|
const agent = await buildLoggedInAgent();
|
|
const csrf = (agent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
await agent
|
|
.post('/api/v1/auth/change-password')
|
|
.set('Authorization', `Bearer ${agent.accessToken}`)
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ oldPassword: 'WrongOld!Pass1', newPassword: 'NewSecret!Pass1', confirmNewPassword: 'NewSecret!Pass1' })
|
|
.expect(401)
|
|
.expect((r) => {
|
|
expect(r.body.code).toBe('INVALID_OLD_PASSWORD');
|
|
});
|
|
});
|
|
|
|
it('rejects when new password is too weak (PASSWORD_POLICY)', async () => {
|
|
const agent = await buildLoggedInAgent();
|
|
const csrf = (agent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
await agent
|
|
.post('/api/v1/auth/change-password')
|
|
.set('Authorization', `Bearer ${agent.accessToken}`)
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ oldPassword: 'Sup3rSecret!Pass', newPassword: 'short', confirmNewPassword: 'short' })
|
|
.expect(400)
|
|
.expect((r) => {
|
|
expect(r.body.code).toBe('PASSWORD_POLICY');
|
|
});
|
|
});
|
|
});
|