1+ //During the test the env variable is set to test
2+ process . env . NODE_ENV = 'test' ;
3+
4+ const User = require ( '../src/models/user' ) ;
5+
6+ const chai = require ( 'chai' ) ;
7+ const chaiHttp = require ( 'chai-http' ) ;
8+ const server = require ( '../src/app' ) ;
9+ const should = chai . should ( ) ;
10+
11+ const postAPI = '/user' ;
12+
13+
14+ describe ( 'Users' , ( ) => {
15+ beforeEach ( ( done ) => {
16+ User . remove ( { } , ( err ) => {
17+ done ( ) ;
18+ } ) ;
19+ } ) ;
20+
21+
22+ describe ( '/POST user' , ( ) => {
23+ let user400 = {
24+ emailAddress : 'wre23sd' ,
25+ password : 'password' ,
26+ fullName : 'jerry springer'
27+ } ;
28+
29+ it ( 'should not POST if email address supplied is invalid email address "wre23sd"' , ( done ) => {
30+ user400 . email = "wre23sd" ;
31+ postUser400 ( user400 , done ) ;
32+ } ) ;
33+ it ( 'should not POST if email address supplied is invalid email address "garbage@moreGarbage."' , ( done ) => {
34+ user400 . email = "garbage@moreGarbage." ;
35+ postUser400 ( user400 , done ) ;
36+ } ) ;
37+ it ( 'should not POST if email address supplied is invalid email address ""test@owiejadskla""' , ( done ) => {
38+ user400 . email = "test@owiejadskla" ;
39+ postUser400 ( user400 , done ) ;
40+ } ) ;
41+
42+
43+
44+ it ( 'should not POST if email Address already exists in DB' , ( done ) => {
45+ let user = {
46+ emailAddress : 'test@gmail.com' ,
47+ password : 'password' ,
48+ fullName : 'jerry springer'
49+ } ;
50+ chai . request ( server )
51+ . post ( postAPI )
52+ . send ( user )
53+ . end ( ( err , res ) => {
54+ res . should . have . status ( 201 ) ;
55+ chai . request ( server )
56+ . post ( postAPI )
57+ . send ( user )
58+ . end ( ( err , res ) => {
59+ err . message . should . be . equal ( 'test' ) ;
60+ res . should . have . status ( 500 ) ;
61+ done ( ) ;
62+ } ) ;
63+ } ) ;
64+ } ) ;
65+
66+ it ( 'should not POST a user supplied with no data' , ( done ) => {
67+ let user = { } ;
68+ postUser ( user , done ) ;
69+ } ) ;
70+ it ( 'should not POST a user missing email address' , ( done ) => {
71+ let user = {
72+ password : 'password' ,
73+ fullName : 'jerry springer'
74+ } ;
75+ postUser ( user , done ) ;
76+ } ) ;
77+ it ( 'should not POST a user missing Full Name' , ( done ) => {
78+ let user = {
79+ emailAddress : 'test@gmail.com' ,
80+ password : 'password'
81+ } ;
82+ postUser ( user , done ) ;
83+ } ) ;
84+ it ( 'should not POST a user missing Password' , ( done ) => {
85+ let user = {
86+ emailAddress : 'test@gmail.com' ,
87+ fullName : 'jerry springer'
88+ } ;
89+ postUser ( user , done ) ;
90+ } ) ;
91+ function postUser ( user , done ) {
92+ chai . request ( server )
93+ . post ( postAPI )
94+ . send ( user )
95+ . end ( ( err , res ) => {
96+ res . should . have . status ( 422 ) ;
97+ done ( ) ;
98+ } ) ;
99+ }
100+ function postUser400 ( user , done ) {
101+ chai . request ( server )
102+ . post ( postAPI )
103+ . send ( user )
104+ . end ( ( err , res ) => {
105+ res . should . have . status ( 400 ) ;
106+ res . body . should . have . property ( 'message' ) . equal ( "Malformed Email Supplied" ) ;
107+ done ( ) ;
108+ } ) ;
109+ }
110+ } ) ;
111+ } ) ;
0 commit comments