register.e2e-spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 { UserCreateDto } from '../users/dto/user-create.dto';
  11. import { HashingService } from '../../src/shared/hashing/hashing.service';
  12. const user = {
  13. name: 'name #1',
  14. username: 'username #1',
  15. email: 'test@example.com',
  16. password: 'pass123',
  17. };
  18. describe('App (e2e)', () => {
  19. let app;
  20. beforeAll(async () => {
  21. const moduleFixture: TestingModule = await Test.createTestingModule({
  22. imports: [AppModule],
  23. providers: [
  24. {
  25. provide: HashingService,
  26. useValue: {
  27. hash: jest.fn(() => 'pass123'),
  28. },
  29. },
  30. ],
  31. })
  32. .overrideProvider(MailerService)
  33. .useValue({
  34. sendMail: jest.fn(() => true),
  35. })
  36. .compile();
  37. app = moduleFixture.createNestApplication();
  38. app.setGlobalPrefix('api');
  39. app.useGlobalPipes(
  40. new ValidationPipe({
  41. whitelist: true,
  42. transform: true,
  43. forbidNonWhitelisted: true,
  44. transformOptions: {
  45. enableImplicitConversion: true,
  46. },
  47. }),
  48. );
  49. await app.init();
  50. });
  51. describe('RegisterController (e2e) - [POST /api/auth/register]', () => {
  52. it('should register user', async () => {
  53. return await request(app.getHttpServer())
  54. .post('/api/auth/register')
  55. .send(user as UserCreateDto)
  56. .then(({ body }) => {
  57. expect(body).toEqual({
  58. message: 'User registration successfully!',
  59. status: 201,
  60. });
  61. expect(HttpStatus.CREATED);
  62. });
  63. });
  64. it('should throw an error for a bad email', async () => {
  65. return await request(app.getHttpServer())
  66. .post('/api/auth/register')
  67. .send({
  68. name: 'name#1 register',
  69. username: 'username#1 register',
  70. password: '123456789',
  71. })
  72. .then(({ body }) => {
  73. expect(body).toEqual({
  74. error: 'Bad Request',
  75. message: [
  76. 'email should not be empty',
  77. 'email must be a string',
  78. 'email must be an email',
  79. ],
  80. statusCode: 400,
  81. });
  82. expect(HttpStatus.BAD_REQUEST);
  83. expect(new BadRequestException());
  84. });
  85. });
  86. it('should throw an error for a bad name', async () => {
  87. return await request(app.getHttpServer())
  88. .post('/api/auth/register')
  89. .send({
  90. username: 'username#1 register',
  91. email: 'test@example.it',
  92. password: '123456789',
  93. })
  94. .expect(HttpStatus.BAD_REQUEST)
  95. .then(({ body }) => {
  96. expect(body).toEqual({
  97. error: 'Bad Request',
  98. message: [
  99. 'name must be shorter than or equal to 30 characters',
  100. 'name must be a string',
  101. ],
  102. statusCode: 400,
  103. });
  104. expect(new BadRequestException());
  105. });
  106. });
  107. it('should throw an error for a bad username', async () => {
  108. return await request(app.getHttpServer())
  109. .post('/api/auth/register')
  110. .send({
  111. name: 'name#1 register',
  112. email: 'test@example.it',
  113. password: '123456789',
  114. })
  115. .then(({ body }) => {
  116. expect(body).toEqual({
  117. error: 'Bad Request',
  118. message: [
  119. 'username must be shorter than or equal to 40 characters',
  120. 'username must be a string',
  121. ],
  122. statusCode: 400,
  123. });
  124. expect(HttpStatus.BAD_REQUEST);
  125. expect(new BadRequestException());
  126. });
  127. });
  128. it('should throw an error for a bad password', async () => {
  129. return await request(app.getHttpServer())
  130. .post('/api/auth/register')
  131. .send({
  132. name: 'name#1 register',
  133. username: 'username#1 register',
  134. email: 'test@example.it',
  135. })
  136. .then(({ body }) => {
  137. expect(body).toEqual({
  138. error: 'Bad Request',
  139. message: [
  140. 'password must be shorter than or equal to 60 characters',
  141. 'password must be a string',
  142. 'password should not be empty',
  143. ],
  144. statusCode: 400,
  145. });
  146. expect(HttpStatus.BAD_REQUEST);
  147. expect(new BadRequestException());
  148. });
  149. });
  150. });
  151. afterAll(async () => {
  152. await app.close();
  153. });
  154. });