1+ const request = require ( 'supertest' )
2+ const app = require ( '../src/app' )
3+
4+ describe ( 'GET /users/me' , ( ) => {
5+ let name = "jane doe" ;
6+ let email = Math . random ( ) . toString ( 36 ) . substring ( 7 ) + "@mail.com" ;
7+ let pass = "pass123" ;
8+ let token = "" ;
9+
10+ beforeEach ( async ( ) => {
11+ const res = await request ( app )
12+ . post ( '/users' )
13+ . send ( {
14+ name :name ,
15+ email : email ,
16+ password : pass ,
17+ } )
18+ // problem with this check
19+ // expect(res.statusCode).toEqual(201)
20+ return true ;
21+ } ) ;
22+
23+ afterAll ( async ( ) => {
24+ // still error : https://github.com/visionmedia/supertest/issues/520
25+ await app . close ( )
26+ } ) ;
27+
28+ it ( 'should successfuly login' , async ( ) => {
29+ const res = await request ( app )
30+ . post ( '/users/login' )
31+ . send ( {
32+ email : email ,
33+ password : pass ,
34+ } )
35+ token = res . body . token ; // save the token!
36+ expect ( res . statusCode ) . toEqual ( 200 )
37+ expect ( res . body ) . toHaveProperty ( 'token' )
38+ } )
39+
40+ it ( 'should successfuly get profile' , async ( ) => {
41+ const res = await request ( app )
42+ . get ( '/users/me' )
43+ . set ( 'Authorization' , `Bearer ${ token } ` )
44+ . send ( )
45+ expect ( res . statusCode ) . toEqual ( 200 )
46+ expect ( res . body ) . toHaveProperty ( 'name' )
47+ } )
48+
49+ it ( 'should successfuly logout' , async ( ) => {
50+ const res = await request ( app )
51+ . post ( '/users/me/logout' )
52+ . set ( 'Authorization' , `Bearer ${ token } ` )
53+ . send ( )
54+ expect ( res . statusCode ) . toEqual ( 200 )
55+ } )
56+ } )
0 commit comments