11import { api } from './lib/api' ;
22import { DatabaseResetCommand } from '../../src/console/DatabaseResetCommand' ;
3+ import { createAdminUser , getToken } from './lib/auth' ;
34
4- let createdId ;
5- const userKeys = [ 'id' , 'firstName' , 'lastName' , 'email' , 'updatedAt' , 'createdAt' ] ;
65
7- const testUser = {
8- firstName : 'Hans' ,
9- lastName : 'Muster' ,
10- email : 'hans@muster.ch'
11- } ;
6+ describe ( 'User' , ( ) => {
127
13- const testUserUpdated = {
14- firstName : 'Horst' ,
15- lastName : 'Maier' ,
16- email : 'horst@maier.ch'
17- } ;
8+ const userKeys = [ 'id' , 'firstName' , 'lastName' , 'email' , 'picture' , 'auth0UserId' , 'updatedAt' , 'createdAt' ] ;
189
10+ const testUser = {
11+ firstName : 'Hans' ,
12+ lastName : 'Muster' ,
13+ email : 'hans@muster.ch' ,
14+ auth0UserId : '1234'
15+ } ;
1916
20- describe ( 'User' , ( ) => {
17+ const testUserUpdated = {
18+ firstName : 'Horst' ,
19+ lastName : 'Maier' ,
20+ email : 'horst@maier.ch'
21+ } ;
2122
23+ let token ;
24+ let auth ;
25+ let createdId ;
2226 beforeAll ( async ( ) => {
2327 await DatabaseResetCommand . run ( ) ;
24- } ) ;
25-
26- test ( 'GET /v1/user Should return a empty list' , async ( ) => {
27- const res = await api ( 'GET' , '/api/v1/user' ) ;
28- res . expectJson ( ) ;
29- res . expectStatusCode ( 200 ) ;
30- const data = res . getData < any [ ] > ( ) ;
31- expect ( data . length ) . toBe ( 0 ) ;
28+ await createAdminUser ( ) ;
29+ token = getToken ( ) ;
30+ auth = {
31+ token : token
32+ } ;
3233 } ) ;
3334
3435 test ( 'POST /v1/user Should create a new user' , async ( ) => {
3536 const res = await api ( 'POST' , '/api/v1/user' , {
37+ token : token ,
3638 body : testUser
3739 } ) ;
3840 res . expectJson ( ) ;
@@ -43,28 +45,29 @@ describe('User', () => {
4345
4446 test ( 'POST /v1/user Should fail because we want to create a empty user' , async ( ) => {
4547 const res = await api ( 'POST' , '/api/v1/user' , {
48+ token : token ,
4649 body : { }
4750 } ) ;
4851 res . expectJson ( ) ;
4952 res . expectStatusCode ( 400 ) ;
5053 } ) ;
5154
5255 test ( 'GET /v1/user Should list of users with our new create one' , async ( ) => {
53- const res = await api ( 'GET' , '/api/v1/user' ) ;
56+ const res = await api ( 'GET' , '/api/v1/user' , auth ) ;
5457 res . expectJson ( ) ;
5558 res . expectStatusCode ( 200 ) ;
5659 res . expectData ( userKeys ) ;
5760 const data = res . getData < any [ ] > ( ) ;
58- expect ( data . length ) . toBe ( 1 ) ;
61+ expect ( data . length ) . toBe ( 2 ) ;
5962
60- const user = data [ 0 ] ;
63+ const user = data [ 1 ] ;
6164 expect ( user . firstName ) . toBe ( testUser . firstName ) ;
6265 expect ( user . lastName ) . toBe ( testUser . lastName ) ;
6366 expect ( user . email ) . toBe ( testUser . email ) ;
6467 } ) ;
6568
6669 test ( 'GET /v1/user/:id Should return one user' , async ( ) => {
67- const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` ) ;
70+ const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` , auth ) ;
6871 res . expectJson ( ) ;
6972 res . expectStatusCode ( 200 ) ;
7073 res . expectData ( userKeys ) ;
@@ -77,6 +80,7 @@ describe('User', () => {
7780
7881 test ( 'PUT /v1/user/:id Should update the user' , async ( ) => {
7982 const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` , {
83+ token : token ,
8084 body : testUserUpdated
8185 } ) ;
8286 res . expectJson ( ) ;
@@ -91,6 +95,7 @@ describe('User', () => {
9195
9296 test ( 'PUT /v1/user/:id Should fail because we want to update the user with a invalid email' , async ( ) => {
9397 const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` , {
98+ token : token ,
9499 body : {
95100 email : 'abc'
96101 }
@@ -100,27 +105,27 @@ describe('User', () => {
100105 } ) ;
101106
102107 test ( 'DELETE /v1/user/:id Should delete the user' , async ( ) => {
103- const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` ) ;
108+ const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` , auth ) ;
104109 res . expectStatusCode ( 204 ) ;
105110 } ) ;
106111
107112 /**
108113 * 404 - NotFound Testing
109114 */
110115 test ( 'GET /v1/user/:id Should return with a 404, because we just deleted the user' , async ( ) => {
111- const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` ) ;
116+ const res = await api ( 'GET' , `/api/v1/user/${ createdId } ` , auth ) ;
112117 res . expectJson ( ) ;
113118 res . expectStatusCode ( 404 ) ;
114119 } ) ;
115120
116121 test ( 'DELETE /v1/user/:id Should return with a 404, because we just deleted the user' , async ( ) => {
117- const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` ) ;
122+ const res = await api ( 'DELETE' , `/api/v1/user/${ createdId } ` , auth ) ;
118123 res . expectJson ( ) ;
119124 res . expectStatusCode ( 404 ) ;
120125 } ) ;
121126
122127 test ( 'PUT /v1/user/:id Should return with a 404, because we just deleted the user' , async ( ) => {
123- const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` ) ;
128+ const res = await api ( 'PUT' , `/api/v1/user/${ createdId } ` , auth ) ;
124129 res . expectJson ( ) ;
125130 res . expectStatusCode ( 404 ) ;
126131 } ) ;
0 commit comments