1- import { Test , TestingModule } from '@nestjs/testing' ;
2- import * as request from 'supertest' ;
1+ import { Test } from '@nestjs/testing' ;
2+ import request from 'supertest' ;
33import { AppModule } from './../src/app.module' ;
4- import { INestApplication , ValidationPipe } from '@nestjs/common' ;
4+ import { INestApplication , ValidationPipe , HttpStatus } from '@nestjs/common' ;
55import { Sequelize } from 'sequelize-typescript' ;
6- import { ConfigService } from 'src/shared/config/config.service' ;
6+ import { ConfigService } from './../ src/shared/config/config.service' ;
77import { Post } from './../src/posts/post.entity' ;
88import { User } from './../src/users/user.entity' ;
9+ import {
10+ createUserDto1 ,
11+ createUserDto2 ,
12+ userLoginResponseDto1 ,
13+ createUserDto3 ,
14+ createUserDto4 ,
15+ createUserDto5 ,
16+ userLoginRequestDto1 ,
17+ userLoginRequestDto2 ,
18+ userLoginRequestDto3 ,
19+ } from './test-data' ;
920
1021describe ( '/' , ( ) => {
1122 let app : INestApplication ;
1223 let sequelize : Sequelize ;
24+ let userId : string ;
25+ let token : string ;
1326
1427 beforeAll ( async ( ) => {
1528 const module = await Test . createTestingModule ( {
@@ -36,6 +49,93 @@ describe('/', () => {
3649 await app . init ( ) ;
3750 } ) ;
3851
52+ describe ( '/users' , ( ) => {
53+ describe ( 'POST /register' , ( ) => {
54+ it ( 'should return 400 if email is not valid' , ( ) => {
55+ return request ( app . getHttpServer ( ) )
56+ . post ( '/users/register' )
57+ . send ( createUserDto3 )
58+ . expect ( HttpStatus . BAD_REQUEST ) ;
59+ } ) ;
60+
61+ it ( 'should return 400 if birthday is not ISO 8601 date string' , ( ) => {
62+ return request ( app . getHttpServer ( ) )
63+ . post ( '/users/register' )
64+ . send ( createUserDto4 )
65+ . expect ( HttpStatus . BAD_REQUEST ) ;
66+ } ) ;
67+
68+ it ( 'should return 400 if gender is not a valid enum value' , ( ) => {
69+ return request ( app . getHttpServer ( ) )
70+ . post ( '/users/register' )
71+ . send ( createUserDto5 )
72+ . expect ( HttpStatus . BAD_REQUEST ) ;
73+ } ) ;
74+
75+ it ( 'should return 400 if any of the required fields is missing' , ( ) => {
76+ return request ( app . getHttpServer ( ) )
77+ . post ( '/users/register' )
78+ . send ( createUserDto2 )
79+ . expect ( HttpStatus . BAD_REQUEST ) ;
80+ } ) ;
81+
82+ it ( 'should return 201 if user is created' , ( ) => {
83+ return request ( app . getHttpServer ( ) )
84+ . post ( '/users/register' )
85+ . send ( createUserDto1 )
86+ . expect ( HttpStatus . CREATED )
87+ . expect ( res => {
88+ userId = res . body . id ;
89+ userLoginResponseDto1 . id = res . body . id ;
90+ userLoginResponseDto1 . token = res . body . token ;
91+ expect ( res . body ) . toEqual ( userLoginResponseDto1 ) ;
92+ } ) ;
93+ } ) ;
94+
95+ it ( 'should return 409 if user with given email already exists' , ( ) => {
96+ return request ( app . getHttpServer ( ) )
97+ . post ( '/users/register' )
98+ . send ( createUserDto1 )
99+ . expect ( HttpStatus . CONFLICT ) ;
100+ } ) ;
101+ } ) ;
102+
103+ describe ( 'POST /login' , ( ) => {
104+ it ( 'should return 200 and jwt token' , ( ) => {
105+ return request ( app . getHttpServer ( ) )
106+ . post ( '/users/login' )
107+ . send ( userLoginRequestDto1 )
108+ . expect ( HttpStatus . OK )
109+ . expect ( res => {
110+ token = res . body . token ;
111+ userLoginResponseDto1 . id = res . body . id ;
112+ userLoginResponseDto1 . token = token ;
113+ expect ( res . body ) . toEqual ( userLoginResponseDto1 ) ;
114+ } ) ;
115+ } ) ;
116+
117+ it ( 'should return 400 when user with given email not found' , ( ) => {
118+ return request ( app . getHttpServer ( ) )
119+ . post ( '/users/login' )
120+ . send ( userLoginRequestDto2 )
121+ . expect ( HttpStatus . BAD_REQUEST ) ;
122+ } ) ;
123+
124+ it ( 'should return 400 when wrong password inserted' , ( ) => {
125+ return request ( app . getHttpServer ( ) )
126+ . post ( '/users/login' )
127+ . send ( userLoginRequestDto3 )
128+ . expect ( HttpStatus . BAD_REQUEST ) ;
129+ } ) ;
130+ it ( 'should return 400 when no data sent' , ( ) => {
131+ return request ( app . getHttpServer ( ) )
132+ . post ( '/users/login' )
133+ . send ( { } )
134+ . expect ( HttpStatus . BAD_REQUEST ) ;
135+ } ) ;
136+ } ) ;
137+ } ) ;
138+
39139 afterAll ( async done => {
40140 await app . close ( ) ;
41141 await sequelize . drop ( ) ;
0 commit comments