users.e2e-spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 { HttpStatus, ValidationPipe } from '@nestjs/common';
  6. import { JwtAuthGuard } from '../../src/auth/jwt-auth.guard';
  7. const users = [
  8. {
  9. id: 1,
  10. name: 'name #1',
  11. username: 'username #1',
  12. email: 'test1@example.com',
  13. password: 'pass123',
  14. },
  15. ];
  16. const updateProfileUserDto = {
  17. name: 'name#1 update',
  18. username: 'username#1 update',
  19. email: 'test@example.it',
  20. };
  21. describe('App (e2e)', () => {
  22. let app;
  23. let accessTokenJwt: string;
  24. beforeAll(async () => {
  25. const moduleFixture: TestingModule = await Test.createTestingModule({
  26. imports: [AppModule],
  27. })
  28. .overrideProvider(MailerService)
  29. .useValue({
  30. sendMail: jest.fn(() => true),
  31. })
  32. .overrideGuard(JwtAuthGuard)
  33. .useValue({ canActivate: () => true })
  34. .compile();
  35. app = moduleFixture.createNestApplication();
  36. app.setGlobalPrefix('api');
  37. app.useGlobalPipes(
  38. new ValidationPipe({
  39. whitelist: true,
  40. transform: true,
  41. forbidNonWhitelisted: true,
  42. transformOptions: {
  43. enableImplicitConversion: true,
  44. },
  45. }),
  46. );
  47. await app.init();
  48. });
  49. describe('UserController (e2e)', () => {
  50. describe('should sign in and get a "live" JWT', () => {
  51. it('should authenticates user with valid credentials and provides a jwt token', () => {
  52. return request(app.getHttpServer())
  53. .post('/api/auth/login')
  54. .send({
  55. email: 'test@example.com',
  56. password: 'pass123',
  57. })
  58. .then(({ body }) => {
  59. accessTokenJwt = body.accessToken;
  60. expect(accessTokenJwt).toMatch(
  61. /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/,
  62. );
  63. expect(body).toEqual({
  64. sub: 1,
  65. expiresIn: '3600',
  66. audience: '127.0.0.1:3001',
  67. issuer: '127.0.0.1:3001',
  68. accessToken: accessTokenJwt,
  69. user: { name: 'name #1', email: 'test@example.com', id: 1 },
  70. });
  71. expect(HttpStatus.OK);
  72. });
  73. });
  74. });
  75. describe('Get all users [GET /api/users]', () => {
  76. it('should get all users', async () => {
  77. return await request(app.getHttpServer())
  78. .get('/api/users')
  79. .set('Authorization', `Bearer ${accessTokenJwt}`)
  80. .expect(HttpStatus.OK)
  81. .then(({ body }) => {
  82. expect(body).toEqual([
  83. {
  84. id: 1,
  85. name: 'name #1',
  86. username: 'username #1',
  87. email: 'test@example.com',
  88. password: body[0].password,
  89. },
  90. ]);
  91. });
  92. });
  93. });
  94. describe('Get one user [GET /api/users/:id]', () => {
  95. it('should get one user', async () => {
  96. return await request(app.getHttpServer())
  97. .get('/api/users/1')
  98. .set('Authorization', `Bearer ${accessTokenJwt}`)
  99. .expect(HttpStatus.OK)
  100. .then(({ body }) => {
  101. expect(body).toEqual({
  102. id: 1,
  103. name: 'name #1',
  104. username: 'username #1',
  105. email: 'test@example.com',
  106. password: body.password,
  107. });
  108. });
  109. });
  110. it('should return an incorrect request if it does not find the id', async () => {
  111. return await request(app.getHttpServer())
  112. .get('/api/users/30')
  113. .set('Authorization', `Bearer ${accessTokenJwt}`)
  114. .then(({ body }) => {
  115. expect(body).toEqual({
  116. error: 'Not Found',
  117. message: 'User #30 not found',
  118. statusCode: HttpStatus.NOT_FOUND,
  119. });
  120. });
  121. });
  122. });
  123. describe('Get one user profile [GET /api/users/:id/profile]', () => {
  124. it('should get one user profile', async () => {
  125. return await request(app.getHttpServer())
  126. .get('/api/users/1/profile')
  127. .set('Authorization', `Bearer ${accessTokenJwt}`)
  128. .expect(HttpStatus.OK)
  129. .then(({ body }) => {
  130. expect(body).toEqual({
  131. user: {
  132. id: 1,
  133. name: 'name #1',
  134. username: 'username #1',
  135. email: 'test@example.com',
  136. password: body.user.password,
  137. },
  138. status: HttpStatus.OK,
  139. });
  140. });
  141. });
  142. it('should return an incorrect request if it does not find the user profile id', async () => {
  143. return await request(app.getHttpServer())
  144. .get('/api/users/20/profile')
  145. .set('Authorization', `Bearer ${accessTokenJwt}`)
  146. .expect(HttpStatus.NOT_FOUND);
  147. });
  148. });
  149. describe('Update one user profile [PUT /api/users/:id/profile]', () => {
  150. it('should update one user profile by id', async () => {
  151. return await request(app.getHttpServer())
  152. .put('/api/users/1/profile')
  153. .set('Authorization', `Bearer ${accessTokenJwt}`)
  154. .send({
  155. name: 'name #1',
  156. username: 'username #1',
  157. email: 'test@example.com',
  158. })
  159. .expect(HttpStatus.OK)
  160. .then(({ body }) => {
  161. expect(body).toEqual({
  162. message: 'User Updated successfully!',
  163. status: HttpStatus.OK,
  164. });
  165. });
  166. });
  167. it('should return an incorrect request if it does not find the id', async () => {
  168. return await request(app.getHttpServer())
  169. .put('/api/users/10/profile')
  170. .set('Authorization', `Bearer ${accessTokenJwt}`)
  171. .send(updateProfileUserDto)
  172. .expect(HttpStatus.BAD_REQUEST);
  173. });
  174. });
  175. //
  176. describe('Update one user [PUT /api/users/:id]', () => {
  177. it('should update one user', async () => {
  178. return await request(app.getHttpServer())
  179. .put('/api/users/1')
  180. .set('Authorization', `Bearer ${accessTokenJwt}`)
  181. .send({
  182. name: 'name #1',
  183. username: 'username #1',
  184. email: 'test@example.com',
  185. password:
  186. '$2b$10$hgJzgGh2tkqqIYpIYQI9pO0Q1S9Vd.OXnJcsm1oA1nYvd9yet8sxi',
  187. })
  188. .expect(HttpStatus.OK)
  189. .then(({ body }) => {
  190. expect(body).toEqual({
  191. message: 'User Updated successfully!',
  192. status: HttpStatus.OK,
  193. });
  194. });
  195. });
  196. it('should return an incorrect request if it does not find the id', async () => {
  197. return await request(app.getHttpServer())
  198. .put('/api/users/10')
  199. .set('Authorization', `Bearer ${accessTokenJwt}`)
  200. .send(null)
  201. .expect(HttpStatus.BAD_REQUEST);
  202. });
  203. });
  204. describe('Delete on user [DELETE /api/users/:id]', () => {
  205. it('should delete one user by id', async () => {
  206. return await request(app.getHttpServer())
  207. .delete('/api/users/1')
  208. .set('Authorization', `Bearer ${accessTokenJwt}`)
  209. .expect(HttpStatus.OK)
  210. .then(() => {
  211. return request(app.getHttpServer())
  212. .get('/users/1')
  213. .expect(HttpStatus.NOT_FOUND);
  214. });
  215. });
  216. it('should return an incorrect request if it does not find the id', () => {
  217. return request(app.getHttpServer())
  218. .delete('/api/users/10')
  219. .set('Authorization', `Bearer ${accessTokenJwt}`)
  220. .expect(HttpStatus.NOT_FOUND);
  221. });
  222. });
  223. });
  224. afterAll(async () => {
  225. await app.close();
  226. });
  227. });