Set up PostgreSQL database configuration and enhance application structure
- Added .env.example with database connection details and JWT configuration. - Introduced docker-compose.yml for PostgreSQL service setup with health checks. - Created drizzle.config.ts for database schema and migration management. - Updated nest-cli.json to include Swagger plugin configuration for API documentation. - Enhanced package.json with new database-related scripts and dependencies. - Implemented initial database migrations for user and token management. - Configured application bootstrap process to load environment variables and set up Swagger. - Added shared application configuration in configure-app.ts for consistent setup. - Included unit tests for application configuration and Swagger setup.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { configureApp } from '../src/common/configure-app';
|
||||
|
||||
describe('Auth (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
|
||||
const username = `user_${Date.now()}`;
|
||||
const password = 'password123';
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
configureApp(app, {
|
||||
NODE_ENV: 'test',
|
||||
SWAGGER_ENABLED: 'false',
|
||||
});
|
||||
await app.init();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('GET / remains public', () => {
|
||||
return request(app.getHttpServer())
|
||||
.get('/')
|
||||
.expect(200)
|
||||
.expect('Hello World!');
|
||||
});
|
||||
|
||||
it('GET /auth/me without token returns 401', () => {
|
||||
return request(app.getHttpServer()).get('/auth/me').expect(401);
|
||||
});
|
||||
|
||||
it('register → me → refresh → revoke → me 401', async () => {
|
||||
const register = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username, password })
|
||||
.expect(201);
|
||||
|
||||
const { accessToken, refreshToken } = register.body as {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
};
|
||||
expect(accessToken).toBeDefined();
|
||||
expect(refreshToken).toHaveLength(64);
|
||||
expect(Object.keys(register.body).sort()).toEqual([
|
||||
'accessToken',
|
||||
'refreshToken',
|
||||
]);
|
||||
|
||||
const me = await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${accessToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(me.body).toMatchObject({ username: username.toLowerCase() });
|
||||
|
||||
const refreshed = await request(app.getHttpServer())
|
||||
.post('/auth/refresh')
|
||||
.send({ refreshToken })
|
||||
.expect(200);
|
||||
|
||||
const { accessToken: nextAccess, refreshToken: nextRefresh } =
|
||||
refreshed.body as {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
};
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${nextAccess}`)
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/revoke')
|
||||
.send({ refreshToken: nextRefresh })
|
||||
.expect(204);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get('/auth/me')
|
||||
.set('Authorization', `Bearer ${nextAccess}`)
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('login rejects invalid credentials', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/login')
|
||||
.send({ username, password: 'wrongpass' })
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('duplicate register returns 409', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username, password })
|
||||
.expect(409);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user