| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import { Test, TestingModule } from '@nestjs/testing';
- import * as request from 'supertest';
- import { AppModule } from './../../src/app.module';
- import { MailerService } from '../../src/shared/mailer/mailer.service';
- import { HttpStatus, ValidationPipe } from '@nestjs/common';
- import { JwtAuthGuard } from '../../src/auth/jwt-auth.guard';
- const users = [
- {
- id: 1,
- name: 'name #1',
- username: 'username #1',
- email: 'test1@example.com',
- password: 'pass123',
- },
- ];
- const updateProfileUserDto = {
- name: 'name#1 update',
- username: 'username#1 update',
- email: 'test@example.it',
- };
- describe('App (e2e)', () => {
- let app;
- let accessTokenJwt: string;
- beforeAll(async () => {
- const moduleFixture: TestingModule = await Test.createTestingModule({
- imports: [AppModule],
- })
- .overrideProvider(MailerService)
- .useValue({
- sendMail: jest.fn(() => true),
- })
- .overrideGuard(JwtAuthGuard)
- .useValue({ canActivate: () => true })
- .compile();
- app = moduleFixture.createNestApplication();
- app.setGlobalPrefix('api');
- app.useGlobalPipes(
- new ValidationPipe({
- whitelist: true,
- transform: true,
- forbidNonWhitelisted: true,
- transformOptions: {
- enableImplicitConversion: true,
- },
- }),
- );
- await app.init();
- });
- describe('UserController (e2e)', () => {
- describe('should sign in and get a "live" JWT', () => {
- it('should authenticates user with valid credentials and provides a jwt token', () => {
- return request(app.getHttpServer())
- .post('/api/auth/login')
- .send({
- email: 'test@example.com',
- password: 'pass123',
- })
- .then(({ body }) => {
- accessTokenJwt = body.accessToken;
- expect(accessTokenJwt).toMatch(
- /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/,
- );
- expect(body).toEqual({
- sub: 1,
- expiresIn: '3600',
- audience: '127.0.0.1:3001',
- issuer: '127.0.0.1:3001',
- accessToken: accessTokenJwt,
- user: { name: 'name #1', email: 'test@example.com', id: 1 },
- });
- expect(HttpStatus.OK);
- });
- });
- });
- describe('Get all users [GET /api/users]', () => {
- it('should get all users', async () => {
- return await request(app.getHttpServer())
- .get('/api/users')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.OK)
- .then(({ body }) => {
- expect(body).toEqual([
- {
- id: 1,
- name: 'name #1',
- username: 'username #1',
- email: 'test@example.com',
- password: body[0].password,
- },
- ]);
- });
- });
- });
- describe('Get one user [GET /api/users/:id]', () => {
- it('should get one user', async () => {
- return await request(app.getHttpServer())
- .get('/api/users/1')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.OK)
- .then(({ body }) => {
- expect(body).toEqual({
- id: 1,
- name: 'name #1',
- username: 'username #1',
- email: 'test@example.com',
- password: body.password,
- });
- });
- });
- it('should return an incorrect request if it does not find the id', async () => {
- return await request(app.getHttpServer())
- .get('/api/users/30')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .then(({ body }) => {
- expect(body).toEqual({
- error: 'Not Found',
- message: 'User #30 not found',
- statusCode: HttpStatus.NOT_FOUND,
- });
- });
- });
- });
- describe('Get one user profile [GET /api/users/:id/profile]', () => {
- it('should get one user profile', async () => {
- return await request(app.getHttpServer())
- .get('/api/users/1/profile')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.OK)
- .then(({ body }) => {
- expect(body).toEqual({
- user: {
- id: 1,
- name: 'name #1',
- username: 'username #1',
- email: 'test@example.com',
- password: body.user.password,
- },
- status: HttpStatus.OK,
- });
- });
- });
- it('should return an incorrect request if it does not find the user profile id', async () => {
- return await request(app.getHttpServer())
- .get('/api/users/20/profile')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.NOT_FOUND);
- });
- });
- describe('Update one user profile [PUT /api/users/:id/profile]', () => {
- it('should update one user profile by id', async () => {
- return await request(app.getHttpServer())
- .put('/api/users/1/profile')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .send({
- name: 'name #1',
- username: 'username #1',
- email: 'test@example.com',
- })
- .expect(HttpStatus.OK)
- .then(({ body }) => {
- expect(body).toEqual({
- message: 'User Updated successfully!',
- status: HttpStatus.OK,
- });
- });
- });
- it('should return an incorrect request if it does not find the id', async () => {
- return await request(app.getHttpServer())
- .put('/api/users/10/profile')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .send(updateProfileUserDto)
- .expect(HttpStatus.BAD_REQUEST);
- });
- });
- //
- describe('Update one user [PUT /api/users/:id]', () => {
- it('should update one user', async () => {
- return await request(app.getHttpServer())
- .put('/api/users/1')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .send({
- name: 'name #1',
- username: 'username #1',
- email: 'test@example.com',
- password:
- '$2b$10$hgJzgGh2tkqqIYpIYQI9pO0Q1S9Vd.OXnJcsm1oA1nYvd9yet8sxi',
- })
- .expect(HttpStatus.OK)
- .then(({ body }) => {
- expect(body).toEqual({
- message: 'User Updated successfully!',
- status: HttpStatus.OK,
- });
- });
- });
- it('should return an incorrect request if it does not find the id', async () => {
- return await request(app.getHttpServer())
- .put('/api/users/10')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .send(null)
- .expect(HttpStatus.BAD_REQUEST);
- });
- });
- describe('Delete on user [DELETE /api/users/:id]', () => {
- it('should delete one user by id', async () => {
- return await request(app.getHttpServer())
- .delete('/api/users/1')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.OK)
- .then(() => {
- return request(app.getHttpServer())
- .get('/users/1')
- .expect(HttpStatus.NOT_FOUND);
- });
- });
- it('should return an incorrect request if it does not find the id', () => {
- return request(app.getHttpServer())
- .delete('/api/users/10')
- .set('Authorization', `Bearer ${accessTokenJwt}`)
- .expect(HttpStatus.NOT_FOUND);
- });
- });
- });
- afterAll(async () => {
- await app.close();
- });
- });
|