@@ -4,41 +4,125 @@ import { DatabaseResetCommand } from '../../src/console/DatabaseResetCommand';
44let createdId ;
55const userKeys = [ 'id' , 'firstName' , 'lastName' , 'email' , 'updatedAt' , 'createdAt' ] ;
66
7+ const testUser = {
8+ firstName : 'Hans' ,
9+ lastName : 'Muster' ,
10+ email : 'hans@muster.ch'
11+ } ;
12+
13+ const testUserUpdated = {
14+ firstName : 'Horst' ,
15+ lastName : 'Maier' ,
16+ email : 'horst@maier.ch'
17+ } ;
18+
719beforeAll ( async ( ) => {
820 await DatabaseResetCommand . run ( ) ;
921} ) ;
1022
11- test ( 'POST [/v1/user] - should create a new user' , async ( ) => {
23+ test ( 'GET [/v1/user] - should return a empty list' , async ( ) => {
24+ const res = await api ( 'GET' , '/api/v1/user' ) ;
25+ res . expectJson ( ) ;
26+ res . expectStatusCode ( 200 ) ;
27+ const data = res . getData < any [ ] > ( ) ;
28+ expect ( data . length ) . toBe ( 0 ) ;
29+ } ) ;
30+
31+ test ( 'POST [/v1/user] - should create a new user' , async ( ) => {
1232 const res = await api ( 'POST' , '/api/v1/user' , {
13- body : {
14- firstName : 'Hans' ,
15- lastName : 'Muster' ,
16- email : 'hans@muster.ch'
17- }
33+ body : testUser
1834 } ) ;
1935 res . expectJson ( ) ;
2036 res . expectStatusCode ( 201 ) ;
2137 res . expectData ( userKeys ) ;
2238 createdId = res . getData ( ) [ 'id' ] ;
2339} ) ;
2440
25- test ( 'GET [/v1/user] - should return the api info as a json' , async ( ) => {
41+ test ( 'POST [/v1/user] - should fail because we want to create a empty user' , async ( ) => {
42+ const res = await api ( 'POST' , '/api/v1/user' , {
43+ body : { }
44+ } ) ;
45+ res . expectJson ( ) ;
46+ res . expectStatusCode ( 400 ) ;
47+ } ) ;
48+
49+ test ( 'GET [/v1/user] - should list of users with our new create one' , async ( ) => {
2650 const res = await api ( 'GET' , '/api/v1/user' ) ;
2751 res . expectJson ( ) ;
2852 res . expectStatusCode ( 200 ) ;
2953 res . expectData ( userKeys ) ;
54+ const data = res . getData < any [ ] > ( ) ;
55+ expect ( data . length ) . toBe ( 1 ) ;
56+
57+ const user = data [ 0 ] ;
58+ expect ( user . firstName ) . toBe ( testUser . firstName ) ;
59+ expect ( user . lastName ) . toBe ( testUser . lastName ) ;
60+ expect ( user . email ) . toBe ( testUser . email ) ;
3061} ) ;
3162
32- test ( 'GET [/v1/user/:id] - should return the api info as a json ' , async ( ) => {
63+ test ( 'GET [/v1/user/:id] - should return one user ' , async ( ) => {
3364 const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` ) ;
3465 res . expectJson ( ) ;
3566 res . expectStatusCode ( 200 ) ;
3667 res . expectData ( userKeys ) ;
3768
3869 const user : any = res . getData ( ) ;
39- expect ( user . firstName ) . toBe ( 'Hans' ) ;
40- expect ( user . lastName ) . toBe ( 'Muster' ) ;
41- expect ( user . email ) . toBe ( 'hans@muster.ch' ) ;
70+ expect ( user . firstName ) . toBe ( testUser . firstName ) ;
71+ expect ( user . lastName ) . toBe ( testUser . lastName ) ;
72+ expect ( user . email ) . toBe ( testUser . email ) ;
73+ } ) ;
74+
75+ test ( 'PUT [/v1/user/:id] - should update the user' , async ( ) => {
76+ const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` , {
77+ body : testUserUpdated
78+ } ) ;
79+ res . expectJson ( ) ;
80+ res . expectStatusCode ( 200 ) ;
81+ res . expectData ( userKeys ) ;
82+
83+ const user : any = res . getData ( ) ;
84+ expect ( user . firstName ) . toBe ( testUserUpdated . firstName ) ;
85+ expect ( user . lastName ) . toBe ( testUserUpdated . lastName ) ;
86+ expect ( user . email ) . toBe ( testUserUpdated . email ) ;
87+ } ) ;
88+
89+ test ( 'PUT [/v1/user/:id] - should fail because we want to update the user with a invalid email' , async ( ) => {
90+ const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` , {
91+ body : {
92+ email : 'abc'
93+ }
94+ } ) ;
95+ res . expectJson ( ) ;
96+ res . expectStatusCode ( 400 ) ;
97+ } ) ;
98+
99+ test ( 'DELETE [/v1/user/:id] - should delete the user' , async ( ) => {
100+ const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` ) ;
101+ res . expectStatusCode ( 204 ) ;
102+ } ) ;
103+
104+ /**
105+ * 404 - NotFound Testing
106+ */
107+ test ( 'GET [/v1/user/:id] - should return with a 404, because we just deleted the user' , async ( ) => {
108+ const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` ) ;
109+ res . expectJson ( ) ;
110+ res . expectStatusCode ( 404 ) ;
111+ } ) ;
112+
113+ test ( 'DELETE [/v1/user/:id] - should return with a 404, because we just deleted the user' , async ( ) => {
114+ const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` ) ;
115+ res . expectJson ( ) ;
116+ res . expectStatusCode ( 404 ) ;
42117} ) ;
43118
119+ test ( 'PUT [/v1/user/:id] - should return with a 404, because we just deleted the user' , async ( ) => {
120+ const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` ) ;
121+ res . expectJson ( ) ;
122+ res . expectStatusCode ( 404 ) ;
123+ } ) ;
124+
125+
126+
127+
44128
0 commit comments