@@ -2,17 +2,17 @@ import chai from "chai";
22import chaiHttp from "chai-http" ;
33const { expect } = chai ;
44import config from "config" ;
5+ import sinon from "sinon" ;
56const app = require ( "../../server" ) ;
67const addUser = require ( "../utils/addUser" ) ;
78const cleanDb = require ( "../utils/cleanDb" ) ;
89const authService = require ( "../../services/authService" ) ;
910const userData = require ( "../fixtures/user/user" ) ( ) ;
1011const applicationModel = require ( "../../models/applications" ) ;
11- const { requestRoleData } = require ( "../fixtures/discordactions/discordactions" ) ;
1212
1313const applicationsData = require ( "../fixtures/applications/applications" ) ( ) ;
1414const cookieName = config . get ( "userToken.cookieName" ) ;
15- const { getUserApplicationObject } = require ( "../../utils /application" ) ;
15+ const { APPLICATION_ERROR_MESSAGES , API_RESPONSE_MESSAGES } = require ( "../../constants /application" ) ;
1616
1717const appOwner = userData [ 3 ] ;
1818const superUser = userData [ 4 ] ;
@@ -65,6 +65,7 @@ describe("Application", function () {
6565
6666 after ( async function ( ) {
6767 await cleanDb ( ) ;
68+ sinon . restore ( ) ;
6869 } ) ;
6970
7071 describe ( "GET /applications" , function ( ) {
@@ -488,4 +489,150 @@ describe("Application", function () {
488489 } ) ;
489490 } ) ;
490491 } ) ;
492+
493+ describe ( "PATCH /applications/:applicationId/nudge" , function ( ) {
494+ let nudgeApplicationId : string ;
495+
496+ beforeEach ( async function ( ) {
497+ const applicationData = { ...applicationsData [ 0 ] , userId } ;
498+ nudgeApplicationId = await applicationModel . addApplication ( applicationData ) ;
499+ } ) ;
500+
501+ afterEach ( async function ( ) {
502+ sinon . restore ( ) ;
503+ } ) ;
504+
505+ it ( "should successfully nudge a pending application when user owns it and no previous nudge exists" , function ( done ) {
506+ chai
507+ . request ( app )
508+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
509+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
510+ . end ( function ( err , res ) {
511+ if ( err ) return done ( err ) ;
512+
513+ expect ( res ) . to . have . status ( 200 ) ;
514+ expect ( res . body . message ) . to . be . equal ( API_RESPONSE_MESSAGES . NUDGE_SUCCESS ) ;
515+ expect ( res . body . nudgeCount ) . to . be . equal ( 1 ) ;
516+ expect ( res . body . lastNudgeAt ) . to . be . a ( "string" ) ;
517+ done ( ) ;
518+ } ) ;
519+ } ) ;
520+
521+ it ( "should successfully nudge an application when 24 hours have passed since last nudge" , function ( done ) {
522+ chai
523+ . request ( app )
524+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
525+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
526+ . end ( function ( err , res ) {
527+ if ( err ) return done ( err ) ;
528+
529+ expect ( res ) . to . have . status ( 200 ) ;
530+ expect ( res . body . nudgeCount ) . to . be . equal ( 1 ) ;
531+
532+ const twentyFiveHoursAgo = new Date ( Date . now ( ) - 25 * 60 * 60 * 1000 ) . toISOString ( ) ;
533+ applicationModel . updateApplication ( { lastNudgeAt : twentyFiveHoursAgo } , nudgeApplicationId ) . then ( ( ) => {
534+ chai
535+ . request ( app )
536+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
537+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
538+ . end ( function ( err , res ) {
539+ if ( err ) return done ( err ) ;
540+
541+ expect ( res ) . to . have . status ( 200 ) ;
542+ expect ( res . body . message ) . to . be . equal ( API_RESPONSE_MESSAGES . NUDGE_SUCCESS ) ;
543+ expect ( res . body . nudgeCount ) . to . be . equal ( 2 ) ;
544+ expect ( res . body . lastNudgeAt ) . to . be . a ( "string" ) ;
545+ done ( ) ;
546+ } ) ;
547+ } ) ;
548+ } ) ;
549+ } ) ;
550+
551+ it ( "should return 404 if the application doesn't exist" , function ( done ) {
552+ chai
553+ . request ( app )
554+ . patch ( `/applications/non-existent-id/nudge` )
555+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
556+ . end ( function ( err , res ) {
557+ if ( err ) return done ( err ) ;
558+
559+ expect ( res ) . to . have . status ( 404 ) ;
560+ expect ( res . body . error ) . to . be . equal ( "Not Found" ) ;
561+ expect ( res . body . message ) . to . be . equal ( "Application not found" ) ;
562+ done ( ) ;
563+ } ) ;
564+ } ) ;
565+
566+ it ( "should return 401 if user is not authenticated" , function ( done ) {
567+ chai
568+ . request ( app )
569+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
570+ . end ( function ( err , res ) {
571+ if ( err ) return done ( err ) ;
572+
573+ expect ( res ) . to . have . status ( 401 ) ;
574+ expect ( res . body . error ) . to . be . equal ( "Unauthorized" ) ;
575+ expect ( res . body . message ) . to . be . equal ( "Unauthenticated User" ) ;
576+ done ( ) ;
577+ } ) ;
578+ } ) ;
579+
580+ it ( "should return 401 if user does not own the application" , function ( done ) {
581+ chai
582+ . request ( app )
583+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
584+ . set ( "cookie" , `${ cookieName } =${ secondUserJwt } ` )
585+ . end ( function ( err , res ) {
586+ if ( err ) return done ( err ) ;
587+
588+ expect ( res ) . to . have . status ( 401 ) ;
589+ expect ( res . body . error ) . to . be . equal ( "Unauthorized" ) ;
590+ expect ( res . body . message ) . to . be . equal ( "You are not authorized to nudge this application" ) ;
591+ done ( ) ;
592+ } ) ;
593+ } ) ;
594+
595+ it ( "should return 429 when trying to nudge within 24 hours" , function ( done ) {
596+ chai
597+ . request ( app )
598+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
599+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
600+ . end ( function ( err , res ) {
601+ if ( err ) return done ( err ) ;
602+
603+ expect ( res ) . to . have . status ( 200 ) ;
604+
605+ chai
606+ . request ( app )
607+ . patch ( `/applications/${ nudgeApplicationId } /nudge` )
608+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
609+ . end ( function ( err , res ) {
610+ if ( err ) return done ( err ) ;
611+
612+ expect ( res ) . to . have . status ( 429 ) ;
613+ expect ( res . body . error ) . to . be . equal ( "Too Many Requests" ) ;
614+ expect ( res . body . message ) . to . be . equal ( APPLICATION_ERROR_MESSAGES . NUDGE_TOO_SOON ) ;
615+ done ( ) ;
616+ } ) ;
617+ } ) ;
618+ } ) ;
619+
620+ it ( "should return 400 when trying to nudge an application that is not in pending status" , function ( done ) {
621+ const nonPendingApplicationData = { ...applicationsData [ 1 ] , userId } ;
622+ applicationModel . addApplication ( nonPendingApplicationData ) . then ( ( nonPendingApplicationId : string ) => {
623+ chai
624+ . request ( app )
625+ . patch ( `/applications/${ nonPendingApplicationId } /nudge` )
626+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
627+ . end ( function ( err , res ) {
628+ if ( err ) return done ( err ) ;
629+
630+ expect ( res ) . to . have . status ( 400 ) ;
631+ expect ( res . body . error ) . to . be . equal ( "Bad Request" ) ;
632+ expect ( res . body . message ) . to . be . equal ( APPLICATION_ERROR_MESSAGES . NUDGE_ONLY_PENDING_ALLOWED ) ;
633+ done ( ) ;
634+ } ) ;
635+ } ) ;
636+ } ) ;
637+ } ) ;
491638} ) ;
0 commit comments