@@ -12,6 +12,9 @@ const userData = require("../fixtures/user/user")();
12
12
13
13
const config = require ( "config" ) ;
14
14
const cookieName = config . get ( "userToken.cookieName" ) ;
15
+ const Sinon = require ( "sinon" ) ;
16
+ const { INTERNAL_SERVER_ERROR } = require ( "../../constants/errorMessages" ) ;
17
+ const dataAccess = require ( "../.././services/dataAccessLayer" ) ;
15
18
16
19
chai . use ( chaiHttp ) ;
17
20
@@ -258,11 +261,34 @@ describe("Members", function () {
258
261
} ) ;
259
262
260
263
describe ( "PATCH /members/archiveMembers/:username" , function ( ) {
264
+ let dataAccessStub ;
261
265
beforeEach ( async function ( ) {
262
266
const superUserId = await addUser ( superUser ) ;
263
267
jwt = authService . generateAuthToken ( { userId : superUserId } ) ;
264
268
} ) ;
265
-
269
+ afterEach ( async function ( ) {
270
+ Sinon . restore ( ) ;
271
+ await cleanDb ( ) ;
272
+ } ) ;
273
+ it ( "Should return an object with status 500 and an error message" , function ( done ) {
274
+ dataAccessStub = Sinon . stub ( dataAccess , "retrieveUsers" ) ;
275
+ dataAccessStub . throws ( new Error ( INTERNAL_SERVER_ERROR ) ) ;
276
+ addUser ( userToBeArchived ) . then ( ( ) => {
277
+ chai
278
+ . request ( app )
279
+ . patch ( `/members/archiveMembers/${ userToBeArchived . username } ` )
280
+ . set ( "Cookie" , `${ cookieName } =${ jwt } ` )
281
+ . send ( { reason : "some reason" } )
282
+ . end ( ( err , res ) => {
283
+ if ( err ) {
284
+ return done ( err ) ;
285
+ }
286
+ expect ( res ) . to . have . status ( 500 ) ;
287
+ expect ( res . body . message ) . to . be . equal ( INTERNAL_SERVER_ERROR ) ;
288
+ return done ( ) ;
289
+ } ) ;
290
+ } ) ;
291
+ } ) ;
266
292
it ( "Should return 404 if user doesn't exist" , function ( done ) {
267
293
chai
268
294
. request ( app )
@@ -279,7 +305,24 @@ describe("Members", function () {
279
305
return done ( ) ;
280
306
} ) ;
281
307
} ) ;
308
+ it ( "Should return 400 if body is empty" , function ( done ) {
309
+ chai
310
+ . request ( app )
311
+ . patch ( `/members/archiveMembers/${ userToBeArchived . username } ` )
312
+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
313
+ . send ( { } )
314
+ . end ( ( err , res ) => {
315
+ if ( err ) {
316
+ return done ( err ) ;
317
+ }
318
+
319
+ expect ( res ) . to . have . status ( 400 ) ;
320
+ expect ( res . body ) . to . be . a ( "object" ) ;
321
+ expect ( res . body . message ) . to . equal ( "Reason is required" ) ;
282
322
323
+ return done ( ) ;
324
+ } ) ;
325
+ } ) ;
283
326
it ( "Should archive the user" , function ( done ) {
284
327
addUser ( userToBeArchived ) . then ( ( ) => {
285
328
chai
@@ -321,26 +364,5 @@ describe("Members", function () {
321
364
} ) ;
322
365
} ) ;
323
366
} ) ;
324
-
325
- it ( "Should return 401 if user is not a super user" , function ( done ) {
326
- addUser ( nonSuperUser ) . then ( ( nonSuperUserId ) => {
327
- const nonSuperUserJwt = authService . generateAuthToken ( { userId : nonSuperUserId } ) ;
328
- chai
329
- . request ( app )
330
- . patch ( `/members/moveToMembers/${ nonSuperUser . username } ` )
331
- . set ( "cookie" , `${ cookieName } =${ nonSuperUserJwt } ` )
332
- . end ( ( err , res ) => {
333
- if ( err ) {
334
- return done ( err ) ;
335
- }
336
-
337
- expect ( res ) . to . have . status ( 401 ) ;
338
- expect ( res . body ) . to . be . a ( "object" ) ;
339
- expect ( res . body . message ) . to . equal ( "You are not authorized for this action." ) ;
340
-
341
- return done ( ) ;
342
- } ) ;
343
- } ) ;
344
- } ) ;
345
367
} ) ;
346
368
} ) ;
0 commit comments