@@ -50,7 +50,7 @@ describe("Discord actions", function () {
50
50
let jwt ;
51
51
beforeEach ( async function ( ) {
52
52
fetchStub = sinon . stub ( global , "fetch" ) ;
53
- userId = await addUser ( ) ;
53
+ userId = await addUser ( userData [ 0 ] ) ;
54
54
superUserId = await addUser ( superUser ) ;
55
55
superUserAuthToken = authService . generateAuthToken ( { userId : superUserId } ) ;
56
56
jwt = authService . generateAuthToken ( { userId } ) ;
@@ -190,12 +190,70 @@ describe("Discord actions", function () {
190
190
} ) ;
191
191
} ) ;
192
192
193
+ describe ( "POST /discord-actions/roles" , function ( ) {
194
+ let roleid ;
195
+ beforeEach ( async function ( ) {
196
+ const discordRoleModelPromise = [ discordRoleModel . add ( groupData [ 0 ] ) , discordRoleModel . add ( groupData [ 1 ] ) ] ;
197
+ roleid = groupData [ 0 ] . roleid ;
198
+ await Promise . all ( discordRoleModelPromise ) ;
199
+ } ) ;
200
+
201
+ afterEach ( async function ( ) {
202
+ sinon . restore ( ) ;
203
+ await cleanDb ( ) ;
204
+ } ) ;
205
+
206
+ it ( "should allow role to be added" , async function ( ) {
207
+ fetchStub . returns (
208
+ Promise . resolve ( {
209
+ status : 200 ,
210
+ json : ( ) => Promise . resolve ( { } ) ,
211
+ } )
212
+ ) ;
213
+ const res = await chai
214
+ . request ( app )
215
+ . post ( "/discord-actions/roles" )
216
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
217
+ . send ( { roleid, userid : userData [ 0 ] . discordId } ) ;
218
+
219
+ expect ( res ) . to . have . status ( 201 ) ;
220
+ expect ( res . body ) . to . be . an ( "object" ) ;
221
+ expect ( res . body . message ) . to . equal ( "Role added successfully!" ) ;
222
+ } ) ;
223
+ it ( "should not allow unknown role to be added to user" , async function ( ) {
224
+ const res = await chai
225
+ . request ( app )
226
+ . post ( "/discord-actions/roles" )
227
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
228
+ . send ( { roleid : "randomId" , userid : "abc" } ) ;
229
+
230
+ expect ( res ) . to . have . status ( 403 ) ;
231
+ expect ( res . body ) . to . be . an ( "object" ) ;
232
+ expect ( res . body . message ) . to . equal ( "Permission denied. Cannot add the role." ) ;
233
+ } ) ;
234
+ it ( "should not allow role to be added when userid does not belong to authenticated user" , async function ( ) {
235
+ const res = await chai
236
+ . request ( app )
237
+ . post ( "/discord-actions/roles" )
238
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
239
+ . send ( { roleid, userid : "asdf" } ) ;
240
+
241
+ expect ( res ) . to . have . status ( 403 ) ;
242
+ expect ( res . body ) . to . be . an ( "object" ) ;
243
+ expect ( res . body . message ) . to . equal ( "Permission denied. Cannot add the role." ) ;
244
+ } ) ;
245
+ } ) ;
193
246
describe ( "DELETE /discord-actions/roles" , function ( ) {
247
+ let roleid ;
248
+
194
249
beforeEach ( async function ( ) {
195
250
const addRolePromises = memberGroupData . map ( async ( data ) => {
196
251
await memberRoleModel . add ( data ) ;
197
252
} ) ;
198
-
253
+ const discordRoleModelPromise = [ discordRoleModel . add ( groupData [ 0 ] ) , discordRoleModel . add ( groupData [ 1 ] ) ] ;
254
+ await Promise . all ( discordRoleModelPromise ) ;
255
+ roleid = groupData [ 0 ] . roleid ;
256
+ await memberRoleModel . add ( { roleid, userid : userData [ 0 ] . discordId } ) ;
199
257
await Promise . all ( addRolePromises ) ;
200
258
} ) ;
201
259
@@ -215,7 +273,7 @@ describe("Discord actions", function () {
215
273
. request ( app )
216
274
. delete ( "/discord-actions/roles" )
217
275
. set ( "cookie" , `${ cookieName } =${ jwt } ` )
218
- . send ( memberGroupData [ 0 ] )
276
+ . send ( { roleid , userid : userData [ 0 ] . discordId } )
219
277
. end ( ( err , res ) => {
220
278
if ( err ) {
221
279
return done ( err ) ;
@@ -229,16 +287,34 @@ describe("Discord actions", function () {
229
287
} ) ;
230
288
} ) ;
231
289
290
+ it ( "should not allow unknown role to be deleted from user" , async function ( ) {
291
+ const res = await chai
292
+ . request ( app )
293
+ . delete ( "/discord-actions/roles" )
294
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
295
+ . send ( { roleid : "randomId" , userid : "abc" } ) ;
296
+
297
+ expect ( res ) . to . have . status ( 403 ) ;
298
+ expect ( res . body ) . to . be . an ( "object" ) ;
299
+ expect ( res . body . message ) . to . equal ( "Permission denied. Cannot delete the role." ) ;
300
+ } ) ;
301
+ it ( "should not allow role to be deleted when userid does not belong to authenticated user" , async function ( ) {
302
+ const res = await chai
303
+ . request ( app )
304
+ . delete ( "/discord-actions/roles" )
305
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
306
+ . send ( { roleid, userid : "asdf" } ) ;
307
+
308
+ expect ( res ) . to . have . status ( 403 ) ;
309
+ expect ( res . body ) . to . be . an ( "object" ) ;
310
+ expect ( res . body . message ) . to . equal ( "Permission denied. Cannot delete the role." ) ;
311
+ } ) ;
232
312
it ( "should handle internal server error" , function ( done ) {
233
- const mockdata = {
234
- roleid : "mockroleid" ,
235
- userid : "mockUserId" ,
236
- } ;
237
313
chai
238
314
. request ( app )
239
315
. delete ( "/discord-actions/roles" )
240
316
. set ( "cookie" , `${ cookieName } =${ jwt } ` )
241
- . send ( mockdata )
317
+ . send ( { roleid , userid : userData [ 0 ] . discordId } )
242
318
. end ( ( err , res ) => {
243
319
if ( err ) {
244
320
return done ( err ) ;
0 commit comments