login.e2e-spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import * as request from 'supertest';
  3. import { AppModule } from './../../src/app.module';
  4. import {
  5. BadRequestException,
  6. HttpStatus,
  7. ValidationPipe,
  8. } from '@nestjs/common';
  9. describe('App (e2e)', () => {
  10. let app;
  11. beforeAll(async () => {
  12. const moduleFixture: TestingModule = await Test.createTestingModule({
  13. imports: [AppModule],
  14. }).compile();
  15. app = moduleFixture.createNestApplication();
  16. app.setGlobalPrefix('api');
  17. app.useGlobalPipes(
  18. new ValidationPipe({
  19. whitelist: true,
  20. transform: true,
  21. forbidNonWhitelisted: true,
  22. transformOptions: {
  23. enableImplicitConversion: true,
  24. },
  25. }),
  26. );
  27. await app.init();
  28. });
  29. describe('LoginController (e2e) - [POST /api/auth/login]', () => {
  30. let accessTokenJwt: string;
  31. it('should authenticates user with valid credentials and provides a jwt token', () => {
  32. return request(app.getHttpServer())
  33. .post('/api/auth/login')
  34. .send({
  35. email: 'test@example.com',
  36. password: 'pass123',
  37. })
  38. .then(({ body }) => {
  39. accessTokenJwt = body.accessToken;
  40. expect(accessTokenJwt).toMatch(
  41. /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/,
  42. );
  43. expect(body).toEqual({
  44. sub: 1,
  45. expiresIn: '3600',
  46. audience: '127.0.0.1:3001',
  47. issuer: '127.0.0.1:3001',
  48. accessToken: accessTokenJwt,
  49. user: { name: 'name #1', email: 'test@example.com', id: 1 },
  50. });
  51. expect(HttpStatus.OK);
  52. });
  53. });
  54. it('should fails to authenticate user with an incorrect password', async () => {
  55. const response = await request(app.getHttpServer())
  56. .post('/api/auth/login')
  57. .send({ email: 'test@example.com', password: 'wrong' })
  58. .expect(HttpStatus.BAD_REQUEST);
  59. expect(response.body.accessToken).not.toBeDefined();
  60. });
  61. it('should throw an error for a bad email', () => {
  62. return request(app.getHttpServer())
  63. .post('/api/auth/login')
  64. .send({
  65. password: 'pass123',
  66. })
  67. .then(({ body }) => {
  68. expect(body).toEqual({
  69. error: 'Bad Request',
  70. message: [
  71. 'email should not be empty',
  72. 'email must be a string',
  73. 'email must be an email',
  74. ],
  75. statusCode: 400,
  76. });
  77. expect(HttpStatus.BAD_REQUEST);
  78. expect(new BadRequestException());
  79. });
  80. });
  81. it('should throw an error for a bad password', () => {
  82. return request(app.getHttpServer())
  83. .post('/api/auth/login')
  84. .send({
  85. email: 'test@example.it',
  86. })
  87. .then(({ body }) => {
  88. expect(body).toEqual({
  89. error: 'Bad Request',
  90. message: [
  91. 'password must be shorter than or equal to 60 characters',
  92. 'password must be a string',
  93. 'password should not be empty',
  94. ],
  95. statusCode: 400,
  96. });
  97. expect(HttpStatus.BAD_REQUEST);
  98. expect(new BadRequestException());
  99. });
  100. });
  101. });
  102. afterAll(async () => {
  103. await app.close();
  104. });
  105. });