File tree Expand file tree Collapse file tree 2 files changed +194
-85
lines changed Expand file tree Collapse file tree 2 files changed +194
-85
lines changed Original file line number Diff line number Diff line change
1
+ import request from 'supertest-as-promised' ;
2
+ import httpStatus from 'http-status' ;
3
+ import chai , { expect } from 'chai' ;
4
+ import app from '../../index' ;
5
+
6
+ chai . config . includeStack = true ;
7
+
8
+ describe ( '## AUTH APIs' , ( ) => {
9
+ const user = {
10
+ username : 'react' ,
11
+ password : 'express'
12
+ } ;
13
+ let jwtToken ;
14
+
15
+ describe ( '# GET /api/auth/login (login to get JWT token)' , ( ) => {
16
+ it ( 'should get JWT token' , ( done ) => {
17
+ request ( app )
18
+ . post ( '/api/auth/login' )
19
+ . send ( user )
20
+ . expect ( httpStatus . OK )
21
+ . then ( ( res ) => {
22
+ expect ( res . body . username ) . to . equal ( user . username ) ;
23
+ jwtToken = `Bearer ${ res . body . token } ` ;
24
+ done ( ) ;
25
+ } )
26
+ . catch ( done ) ;
27
+ } ) ;
28
+ } ) ;
29
+
30
+ describe ( '# GET /api/auth/random-number' , ( ) => {
31
+ it ( 'should fail to get random number because of missing Authorization' , ( done ) => {
32
+ request ( app )
33
+ . get ( '/api/auth/random-number' )
34
+ . expect ( httpStatus . UNAUTHORIZED )
35
+ . then ( ( res ) => {
36
+ expect ( res . body . message ) . to . equal ( 'Unauthorized' ) ;
37
+ done ( ) ;
38
+ } )
39
+ . catch ( done ) ;
40
+ } ) ;
41
+
42
+ it ( 'should get a random number' , ( done ) => {
43
+ request ( app )
44
+ . get ( '/api/auth/random-number' )
45
+ . set ( 'Authorization' , jwtToken )
46
+ . expect ( httpStatus . OK )
47
+ . then ( ( res ) => {
48
+ expect ( res . body . num ) . to . be . a ( 'number' ) ;
49
+ done ( ) ;
50
+ } )
51
+ . catch ( done ) ;
52
+ } ) ;
53
+ } ) ;
54
+ } ) ;
You can’t perform that action at this time.
0 commit comments