1
1
// TODO replace the test below with your tests
2
- import { request } from 'express '
2
+ import request from 'supertest '
3
3
import app from '../../app'
4
+ import users from '../../db/models/users'
4
5
import router from '../../routes/authroutes'
5
6
import { add , multiply } from '../../totest'
6
7
import supertest from 'supertest'
@@ -13,66 +14,101 @@ describe('Math functions', () => {
13
14
const result = multiply ( 5 , 3 )
14
15
expect ( result ) . toEqual ( 15 )
15
16
} )
17
+ describe ( 'disabling account' , ( ) => {
18
+ jest . setTimeout ( 20000 )
16
19
17
- it ( 'should add 5 by 3' , ( ) => {
18
- const result = add ( 5 , 3 )
19
- expect ( result ) . toEqual ( 8 )
20
+ test ( '200 status for disabling account' , async ( ) => {
21
+ const signUp = await request ( app ) . post ( '/signup' ) . send ( {
22
+ firstName : 'Festo' ,
23
+ lastName : 'kabab' ,
24
+
25
+ password : 'admin1' ,
26
+ } )
27
+ console . log ( signUp . body . token )
28
+
29
+ const login = await request ( app ) . post ( '/login' ) . send ( {
30
+
31
+ password : 'admin1' ,
32
+ } )
33
+ const users = await request ( app ) . get ( '/users' )
34
+ console . log ( users )
35
+ const user = users . body . users [ 0 ]
36
+
37
+ const disable = await request ( app )
38
+ . post ( `/users/${ user . id } /disable-account` )
39
+ . set ( 'Authorization' , `Bearer ${ login . body . token } ` )
40
+ . send ( )
41
+ expect ( disable . statusCode ) . toBe ( 200 )
42
+ } )
43
+ test ( '404 status for unexisting user' , async ( ) => {
44
+ const login = await request ( app ) . post ( '/login' ) . send ( {
45
+
46
+ password : 'admin1' ,
47
+ } )
48
+ console . log ( login . body )
49
+ const unExist = await request ( app )
50
+ . post ( `/users/234567/disable-account` )
51
+ . set ( 'Authorization' , `Bearer ${ login . body . token } ` )
52
+ . send ( )
53
+ console . log ( unExist . body )
54
+ expect ( unExist . statusCode ) . toBe ( 404 )
55
+ } )
20
56
} )
21
- } )
22
- // reset password coontroller tests
23
- describe ( 'reset password' , ( ) => {
24
- describe ( 'send link to email' , ( ) => {
25
- test ( 'incase of unregistered email' , async ( ) => {
57
+ // reset password coontroller tests
58
+ describe ( 'reset password' , ( ) => {
59
+ describe ( 'send link to email' , ( ) => {
60
+ test ( 'incase of unregistered email' , async ( ) => {
61
+ const response = await supertest ( app )
62
+ . post ( '/resetpassword/link' )
63
+ . send ( { email :
'[email protected] ' } )
64
+ expect ( response . status ) . toBe ( 400 )
65
+ } , 10000 ) // timeout 10 seconds
66
+ } )
67
+ test ( 'incase of a registered email' , async ( ) => {
26
68
const response = await supertest ( app )
27
69
. post ( '/resetpassword/link' )
28
- . send ( { email :
'[email protected] ' } )
29
- expect ( response . status ) . toBe ( 400 )
30
- } , 10000 ) // timeout 10 seconds
31
- } )
32
- test ( 'incase of a registered email' , async ( ) => {
33
- const response = await supertest ( app )
34
- . post ( '/resetpassword/link' )
35
- . send ( { email :
'[email protected] ' } )
36
- expect ( response . status ) . toBe ( 200 )
37
- } , 20000 )
38
- test ( 'incase invalid email input' , async ( ) => {
39
- const response = await supertest ( app )
40
- . post ( '/resetpassword/link' )
41
- . send ( { email : 'rukundjoseph' } )
42
- expect ( response . status ) . toBe ( 400 )
43
- } , 20000 )
44
- describe ( 'add token and change password' , ( ) => {
45
- test ( 'incase incorrect token' , async ( ) => {
46
- const response = await supertest ( app )
47
- . patch ( '/changepassword/[email protected] /65328dba23' )
48
- . send ( { newpassword : 'newpassword' , confirmpass : 'newpassword' } )
49
- expect ( response . status ) . toBe ( 400 )
70
+ . send ( { email :
'[email protected] ' } )
71
+ expect ( response . status ) . toBe ( 200 )
50
72
} , 20000 )
51
- test ( 'incase incorrect token ' , async ( ) => {
73
+ test ( 'incase invalid email input ' , async ( ) => {
52
74
const response = await supertest ( app )
53
- . patch ( '/changepassword/[email protected] /65328dba23 ' )
54
- . send ( { newpassword : 'newpassword' , confirmpass : 'newpassword '} )
75
+ . post ( '/resetpassword/link ' )
76
+ . send ( { email : 'rukundjoseph ' } )
55
77
expect ( response . status ) . toBe ( 400 )
56
78
} , 20000 )
57
- test ( 'incase of a unmatching passwords' , async ( ) => {
58
- const user : any = await USER . findOne ( {
59
- where :
{ email :
'[email protected] ' } ,
79
+ describe ( 'add token and change password' , ( ) => {
80
+ test ( 'incase incorrect token' , async ( ) => {
81
+ const response = await supertest ( app )
82
+ . patch ( '/changepassword/[email protected] /65328dba23' )
83
+ . send ( { newpassword : 'newpassword' , confirmpass : 'newpassword' } )
84
+ expect ( response . status ) . toBe ( 400 )
85
+ } , 20000 )
86
+ test ( 'incase incorrect token' , async ( ) => {
87
+ const response = await supertest ( app )
88
+ . patch ( '/changepassword/[email protected] /65328dba23' )
89
+ . send ( { newpassword : 'newpassword' , confirmpass : 'newpassword' } )
90
+ expect ( response . status ) . toBe ( 400 )
91
+ } , 20000 )
92
+ test ( 'incase of a unmatching passwords' , async ( ) => {
93
+ const user : any = await USER . findOne ( {
94
+ where :
{ email :
'[email protected] ' } ,
95
+ } )
96
+ const token : any = await Tokens . findOne ( { where : { userId : `${ user . id } ` } } )
97
+ const response = await supertest ( app )
98
+ . patch ( `/changepassword/[email protected] /${ token . token } ` )
99
+ . send ( { newpassword : 'newpas' , confirmpass : 'newpaa' } )
100
+ expect ( response . status ) . toBe ( 400 )
60
101
} )
61
- const token : any = await Tokens . findOne ( { where : { userId : ` ${ user . id } ` } } )
62
- const response = await supertest ( app )
63
- . patch ( `/changepassword/ [email protected] / ${ token . token } ` )
64
- . send ( { newpassword : 'newpas' , confirmpass : 'newpaa' } )
65
- expect ( response . status ) . toBe ( 400 )
66
- } )
67
- test ( 'incase of a valid token and email' , async ( ) => {
68
- const user : any = await USER . findOne ( {
69
- where : { email : '[email protected] ' } ,
102
+ test ( 'incase of a valid token and email' , async ( ) => {
103
+ const user : any = await USER . findOne ( {
104
+ where : { email : ' [email protected] ' } ,
105
+ } )
106
+ const token : any = await Tokens . findOne ( { where : { userId : ` ${ user . id } ` } } )
107
+ const response = await supertest ( app )
108
+ . patch ( `/changepassword/[email protected] / ${ token . token } ` )
109
+ . send ( { newpassword : 'newpas' , confirmpass : 'newpas' } )
110
+ expect ( response . status ) . toBe ( 200 )
70
111
} )
71
- const token : any = await Tokens . findOne ( { where : { userId : `${ user . id } ` } } )
72
- const response = await supertest ( app )
73
- . patch ( `/changepassword/[email protected] /${ token . token } ` )
74
- . send ( { newpassword : 'newpas' , confirmpass : 'newpas' } )
75
- expect ( response . status ) . toBe ( 200 )
76
112
} )
77
113
} )
78
114
} )
0 commit comments