crm-system/test/jwt.e2e-spec.ts

52 lines
1.4 KiB
TypeScript

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);
});
});