@@ -3,14 +3,20 @@ const chai = require('chai');
3
3
const { expect } = chai ;
4
4
const sinonChai = require ( 'sinon-chai' ) ;
5
5
const { mockReq, mockRes } = require ( 'sinon-express-mock' ) ;
6
+ const { User } = require ( '../../models' ) ;
6
7
const usersController = require ( '../../controllers/users' ) ;
7
8
const { deleteTestUser } = require ( '../utils' ) ;
8
9
9
10
chai . use ( sinonChai ) ;
10
11
11
12
describe ( 'user.controller' , ( ) => {
13
+ before ( async ( ) => {
14
+ user = await User . create ( { username : 'testuserchange@test.com' , password : '1234567' } ) ;
15
+ } ) ;
16
+
12
17
after ( async ( ) => {
13
18
await deleteTestUser ( 'testuser3@test.com' ) ;
19
+ await deleteTestUser ( 'testuserchange@test.com' ) ;
14
20
} ) ;
15
21
16
22
describe ( 'signup' , ( ) => {
@@ -39,6 +45,7 @@ describe('user.controller', () => {
39
45
expect ( res . status ) . to . have . been . calledWith ( 400 ) ;
40
46
} ) ;
41
47
} ) ;
48
+
42
49
describe ( 'login' , ( ) => {
43
50
it ( 'should return authentication failed when a username is not found' , async ( ) => {
44
51
const request = {
@@ -53,4 +60,55 @@ describe('user.controller', () => {
53
60
expect ( res . status ) . to . have . been . calledWith ( 401 ) ;
54
61
} ) ;
55
62
} ) ;
63
+
64
+ describe ( 'update password' , ( ) => {
65
+ it ( 'should return a 404 when a user is not found' , async ( ) => {
66
+ const request = {
67
+ body : {
68
+ password : '12345678' ,
69
+ } ,
70
+ params : {
71
+ userId : 10000 ,
72
+ } ,
73
+ } ;
74
+ const req = mockReq ( request ) ;
75
+ const res = mockRes ( ) ;
76
+ await usersController . updatePassword ( req , res ) ;
77
+ expect ( res . status ) . to . have . been . calledWith ( 404 ) ;
78
+ } ) ;
79
+ it ( 'should return a 403 forbidden when a user tries to update the password of another user' , async ( ) => {
80
+ const request = {
81
+ body : {
82
+ password : '12345678' ,
83
+ } ,
84
+ params : {
85
+ userId : user . id ,
86
+ } ,
87
+ user : {
88
+ id : 44
89
+ }
90
+ } ;
91
+ const req = mockReq ( request ) ;
92
+ const res = mockRes ( ) ;
93
+ await usersController . updatePassword ( req , res ) ;
94
+ expect ( res . status ) . to . have . been . calledWith ( 403 ) ;
95
+ } ) ;
96
+ it ( 'should return a 200 on successful update of the password of a user' , async ( ) => {
97
+ const request = {
98
+ body : {
99
+ password : '12345678' ,
100
+ } ,
101
+ params : {
102
+ userId : user . id ,
103
+ } ,
104
+ user : {
105
+ id : user . id
106
+ }
107
+ } ;
108
+ const req = mockReq ( request ) ;
109
+ const res = mockRes ( ) ;
110
+ await usersController . updatePassword ( req , res ) ;
111
+ expect ( res . status ) . to . have . been . calledWith ( 200 ) ;
112
+ } ) ;
113
+ } ) ;
56
114
} ) ;
0 commit comments