test: add e2e jwt smoke tests and load env for jest
This commit is contained in:
parent
bc3e929759
commit
bb4fa9cfb3
@ -69,11 +69,14 @@
|
|||||||
"json",
|
"json",
|
||||||
"ts"
|
"ts"
|
||||||
],
|
],
|
||||||
"rootDir": "src",
|
"rootDir": ".",
|
||||||
"testRegex": ".*\\.spec\\.ts$",
|
"testRegex": "test/.*\\.(spec|e2e-spec)\\.ts$",
|
||||||
"transform": {
|
"transform": {
|
||||||
"^.+\\.(t|j)s$": "ts-jest"
|
"^.+\\.(t|j)s$": "ts-jest"
|
||||||
},
|
},
|
||||||
|
"setupFiles": [
|
||||||
|
"<rootDir>/test/jest.setup.ts"
|
||||||
|
],
|
||||||
"collectCoverageFrom": [
|
"collectCoverageFrom": [
|
||||||
"**/*.(t|j)s"
|
"**/*.(t|j)s"
|
||||||
],
|
],
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
"rootDir": ".",
|
"rootDir": ".",
|
||||||
"testEnvironment": "node",
|
"testEnvironment": "node",
|
||||||
"testRegex": ".e2e-spec.ts$",
|
"testRegex": ".e2e-spec.ts$",
|
||||||
|
"setupFiles": ["<rootDir>/jest.setup.ts"],
|
||||||
"transform": {
|
"transform": {
|
||||||
"^.+\\.(t|j)s$": "ts-jest"
|
"^.+\\.(t|j)s$": "ts-jest"
|
||||||
}
|
}
|
||||||
|
|||||||
3
test/jest.setup.ts
Normal file
3
test/jest.setup.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import * as dotenv from 'dotenv';
|
||||||
|
|
||||||
|
dotenv.config({ path: '/opt/crm/.env' });
|
||||||
51
test/jwt.e2e-spec.ts
Normal file
51
test/jwt.e2e-spec.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { Test } from '@nestjs/testing';
|
||||||
|
import { INestApplication } from '@nestjs/common';
|
||||||
|
import request from 'supertest';
|
||||||
|
import { AppModule } from '../src/app.module';
|
||||||
|
|
||||||
|
describe('Auth/JWT (e2e)', () => {
|
||||||
|
let app: INestApplication;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const moduleRef = await Test.createTestingModule({
|
||||||
|
imports: [AppModule],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
app = moduleRef.createNestApplication();
|
||||||
|
await app.init();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await app.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('/health (GET) should be public', async () => {
|
||||||
|
await request(app.getHttpServer()).get('/health').expect(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('/auth/login (POST) should return tokens', async () => {
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.post('/auth/login')
|
||||||
|
.send({ email: 'admin@crm.local', password: 'admin123' })
|
||||||
|
.expect(201);
|
||||||
|
|
||||||
|
expect(res.body).toHaveProperty('accessToken');
|
||||||
|
expect(res.body).toHaveProperty('refreshToken');
|
||||||
|
expect(res.body).toHaveProperty('user');
|
||||||
|
expect(res.body.user).toHaveProperty('email', 'admin@crm.local');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('/users (GET) should be protected', async () => {
|
||||||
|
const login = await request(app.getHttpServer())
|
||||||
|
.post('/auth/login')
|
||||||
|
.send({ email: 'admin@crm.local', password: 'admin123' })
|
||||||
|
.expect(201);
|
||||||
|
|
||||||
|
const token = login.body.accessToken as string;
|
||||||
|
|
||||||
|
await request(app.getHttpServer())
|
||||||
|
.get('/users')
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.expect(200);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user