forgot-password.e2e-spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import * as request from 'supertest';
  3. import { AppModule } from './../../src/app.module';
  4. import { MailerService } from '../../src/shared/mailer/mailer.service';
  5. import {
  6. BadRequestException,
  7. HttpStatus,
  8. ValidationPipe,
  9. } from '@nestjs/common';
  10. import { ForgotPasswordDto } from 'src/iam/forgot-password/dto/forgot-password.dto';
  11. import { UserDto } from '../../src/users/dto/user.dto';
  12. const user = {
  13. email: 'test@example.com',
  14. };
  15. const createUser = {
  16. name: 'name #1',
  17. username: 'username #1',
  18. email: 'test@example.com',
  19. password: 'pass123',
  20. };
  21. describe('App (e2e)', () => {
  22. let app;
  23. beforeAll(async () => {
  24. const moduleFixture: TestingModule = await Test.createTestingModule({
  25. imports: [AppModule],
  26. })
  27. .overrideProvider(MailerService)
  28. .useValue({
  29. sendMail: jest.fn(() => true),
  30. })
  31. .compile();
  32. app = moduleFixture.createNestApplication();
  33. app.setGlobalPrefix('api');
  34. app.useGlobalPipes(
  35. new ValidationPipe({
  36. whitelist: true,
  37. transform: true,
  38. forbidNonWhitelisted: true,
  39. transformOptions: {
  40. enableImplicitConversion: true,
  41. },
  42. }),
  43. );
  44. await app.init();
  45. });
  46. describe('ForgotPassowrdController (e2e) - [POST /api/auth/forgot-password]', () => {
  47. it('should create user', async () => {
  48. return await request(app.getHttpServer())
  49. .post('/api/auth/register')
  50. .send(createUser as UserDto)
  51. .then(({ body }) => {
  52. expect(body).toEqual({
  53. message: 'User registration successfully!',
  54. status: 201,
  55. });
  56. expect(HttpStatus.CREATED);
  57. });
  58. });
  59. it('should generate a new password per user if they have forgotten their password.', () => {
  60. return request(app.getHttpServer())
  61. .post('/api/auth/forgot-password')
  62. .send(user as ForgotPasswordDto)
  63. .then(({ body }) => {
  64. expect(body).toEqual({
  65. message: 'Request Reset Password Successfully!',
  66. status: 200,
  67. });
  68. });
  69. });
  70. });
  71. it('should throw an error for a bad email', () => {
  72. return request(app.getHttpServer())
  73. .post('/api/auth/forgot-password')
  74. .send({
  75. email: 'not correct',
  76. })
  77. .then(({ body }) => {
  78. expect(body).toEqual({
  79. error: 'Bad Request',
  80. message: ['email must be an email'],
  81. statusCode: 400,
  82. });
  83. expect(HttpStatus.BAD_REQUEST);
  84. expect(new BadRequestException());
  85. });
  86. });
  87. afterAll(async () => {
  88. await app.close();
  89. });
  90. });