diff --git a/package.json b/package.json index e4347fb..bef5b38 100644 --- a/package.json +++ b/package.json @@ -69,11 +69,14 @@ "json", "ts" ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", + "rootDir": ".", + "testRegex": "test/.*\\.(spec|e2e-spec)\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, + "setupFiles": [ + "/test/jest.setup.ts" + ], "collectCoverageFrom": [ "**/*.(t|j)s" ], diff --git a/test/jest-e2e.json b/test/jest-e2e.json index e9d912f..5255cad 100644 --- a/test/jest-e2e.json +++ b/test/jest-e2e.json @@ -3,6 +3,7 @@ "rootDir": ".", "testEnvironment": "node", "testRegex": ".e2e-spec.ts$", + "setupFiles": ["/jest.setup.ts"], "transform": { "^.+\\.(t|j)s$": "ts-jest" } diff --git a/test/jest.setup.ts b/test/jest.setup.ts new file mode 100644 index 0000000..4eaf681 --- /dev/null +++ b/test/jest.setup.ts @@ -0,0 +1,3 @@ +import * as dotenv from 'dotenv'; + +dotenv.config({ path: '/opt/crm/.env' }); diff --git a/test/jwt.e2e-spec.ts b/test/jwt.e2e-spec.ts new file mode 100644 index 0000000..22bce41 --- /dev/null +++ b/test/jwt.e2e-spec.ts @@ -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); + }); +});