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 ;
@@ -132,11 +134,20 @@ describe('RouterExecutionContext', () => {
132134 done ( ) ;
133135 } ) ;
134136 } ) ;
135- it ( 'should throw exception when "tryActivate" returns false' , ( ) => {
137+ it ( 'should throw exception when "tryActivate" returns false' , async ( ) => {
136138 tryActivateStub . callsFake ( async ( ) => false ) ;
137- proxyContext ( request , response , next ) . catch (
138- error => expect ( error ) . to . not . be . undefined ,
139- ) ;
139+ let error : Error ;
140+ try {
141+ await proxyContext ( request , response , next ) ;
142+ } catch ( e ) {
143+ error = e ;
144+ }
145+ expect ( error ) . to . be . instanceOf ( ForbiddenException ) ;
146+ expect ( error . message ) . to . be . eql ( {
147+ statusCode : HttpStatus . FORBIDDEN ,
148+ error : 'Forbidden' ,
149+ message : FORBIDDEN_MESSAGE ,
150+ } ) ;
140151 } ) ;
141152 it ( 'should apply expected context when "canActivateFn" apply' , ( ) => {
142153 proxyContext ( request , response , next ) . then ( ( ) => {
@@ -282,10 +293,21 @@ describe('RouterExecutionContext', () => {
282293 } ) ;
283294 } ) ;
284295 describe ( 'createGuardsFn' , ( ) => {
285- it ( 'should throw exception when "tryActivate" returns false' , ( ) => {
296+ it ( 'should throw ForbiddenException when "tryActivate" returns false' , async ( ) => {
286297 const guardsFn = contextCreator . createGuardsFn ( [ null ] , null , null ) ;
287298 sinon . stub ( guardsConsumer , 'tryActivate' ) . callsFake ( async ( ) => false ) ;
288- guardsFn ( [ ] ) . catch ( err => expect ( err ) . to . not . be . undefined ) ;
299+ let error : ForbiddenException ;
300+ try {
301+ await guardsFn ( [ ] ) ;
302+ } catch ( e ) {
303+ error = e ;
304+ }
305+ expect ( error ) . to . be . instanceOf ( ForbiddenException ) ;
306+ expect ( error . message ) . to . be . eql ( {
307+ statusCode : HttpStatus . FORBIDDEN ,
308+ error : 'Forbidden' ,
309+ message : FORBIDDEN_MESSAGE ,
310+ } ) ;
289311 } ) ;
290312 } ) ;
291313 describe ( 'createHandleResponseFn' , ( ) => {
0 commit comments