11import { expect } from 'chai' ;
22import * as sinon from 'sinon' ;
3- import { RouteParamMetadata } from '../../../common' ;
3+ import { RouteParamMetadata , HttpStatus } from '../../../common' ;
44import { CUSTOM_ROUTE_AGRS_METADATA } from '../../../common/constants' ;
55import { RouteParamtypes } from '../../../common/enums/route-paramtypes.enum' ;
66import { AbstractHttpAdapter } from '../../adapters' ;
@@ -15,6 +15,8 @@ import { PipesContextCreator } from '../../pipes/pipes-context-creator';
1515import { RouteParamsFactory } from '../../router/route-params-factory' ;
1616import { RouterExecutionContext } from '../../router/router-execution-context' ;
1717import { NoopHttpAdapter } from '../utils/noop-adapter.spec' ;
18+ import { FORBIDDEN_MESSAGE } from '../../guards/constants' ;
19+ import { ForbiddenException } from '@nestjs/common/exceptions/forbidden.exception' ;
1820
1921describe ( 'RouterExecutionContext' , ( ) => {
2022 let contextCreator : RouterExecutionContext ;
@@ -91,6 +93,7 @@ describe('RouterExecutionContext', () => {
9193 let tryActivateStub ;
9294 beforeEach ( ( ) => {
9395 instance = { foo : 'bar' } ;
96+
9497 const canActivateFn = contextCreator . createGuardsFn ( [ 1 ] , null , null ) ;
9598 sinon . stub ( contextCreator , 'createGuardsFn' ) . returns ( canActivateFn ) ;
9699 tryActivateStub = sinon
@@ -132,11 +135,20 @@ describe('RouterExecutionContext', () => {
132135 done ( ) ;
133136 } ) ;
134137 } ) ;
135- it ( 'should throw exception when "tryActivate" returns false' , ( ) => {
138+ it ( 'should throw exception when "tryActivate" returns false' , async ( ) => {
136139 tryActivateStub . callsFake ( async ( ) => false ) ;
137- proxyContext ( request , response , next ) . catch (
138- error => expect ( error ) . to . not . be . undefined ,
139- ) ;
140+ let error : Error ;
141+ try {
142+ await proxyContext ( request , response , next ) ;
143+ } catch ( e ) {
144+ error = e ;
145+ }
146+ expect ( error ) . to . be . instanceOf ( ForbiddenException ) ;
147+ expect ( error . message ) . to . be . eql ( {
148+ statusCode : HttpStatus . FORBIDDEN ,
149+ error : 'Forbidden' ,
150+ message : FORBIDDEN_MESSAGE ,
151+ } ) ;
140152 } ) ;
141153 it ( 'should apply expected context when "canActivateFn" apply' , ( ) => {
142154 proxyContext ( request , response , next ) . then ( ( ) => {
@@ -282,10 +294,21 @@ describe('RouterExecutionContext', () => {
282294 } ) ;
283295 } ) ;
284296 describe ( 'createGuardsFn' , ( ) => {
285- it ( 'should throw exception when "tryActivate" returns false' , ( ) => {
297+ it ( 'should throw ForbiddenException when "tryActivate" returns false' , async ( ) => {
286298 const guardsFn = contextCreator . createGuardsFn ( [ null ] , null , null ) ;
287299 sinon . stub ( guardsConsumer , 'tryActivate' ) . callsFake ( async ( ) => false ) ;
288- guardsFn ( [ ] ) . catch ( err => expect ( err ) . to . not . be . undefined ) ;
300+ let error : ForbiddenException ;
301+ try {
302+ await guardsFn ( [ ] ) ;
303+ } catch ( e ) {
304+ error = e ;
305+ }
306+ expect ( error ) . to . be . instanceOf ( ForbiddenException ) ;
307+ expect ( error . message ) . to . be . eql ( {
308+ statusCode : HttpStatus . FORBIDDEN ,
309+ error : 'Forbidden' ,
310+ message : FORBIDDEN_MESSAGE ,
311+ } ) ;
289312 } ) ;
290313 } ) ;
291314 describe ( 'createHandleResponseFn' , ( ) => {
0 commit comments