@@ -9,6 +9,67 @@ import {RoutingControllersOptions} from "../../src/RoutingControllersOptions";
99const chakram = require ( "chakram" ) ;
1010const expect = chakram . expect ;
1111
12+ describe ( "Controller responds with value when Authorization succeeds" , function ( ) {
13+
14+ before ( ( ) => {
15+
16+ // reset metadata args storage
17+ getMetadataArgsStorage ( ) . reset ( ) ;
18+
19+ @JsonController ( )
20+ class AuthController {
21+
22+ @Authorized ( )
23+ @Get ( "/auth1" )
24+ auth1 ( ) {
25+ return { test : "auth1" } ;
26+ }
27+
28+ @Authorized ( [ "role1" ] )
29+ @Get ( "/auth2" )
30+ auth2 ( ) {
31+ return { test : "auth2" } ;
32+ }
33+
34+ }
35+ } ) ;
36+
37+ const serverOptions : RoutingControllersOptions = {
38+ authorizationChecker : async ( action : Action , roles ?: string [ ] ) => {
39+ return true ;
40+ }
41+ } ;
42+
43+ let expressApp : any ;
44+ before ( done => {
45+ const server = createExpressServer ( serverOptions ) ;
46+ expressApp = server . listen ( 3001 , done ) ;
47+ } ) ;
48+ after ( done => expressApp . close ( done ) ) ;
49+
50+ let koaApp : any ;
51+ before ( done => {
52+ const server = createKoaServer ( serverOptions ) ;
53+ koaApp = server . listen ( 3002 , done ) ;
54+ } ) ;
55+ after ( done => koaApp . close ( done ) ) ;
56+
57+ describe ( "without roles" , ( ) => {
58+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth1" , response => {
59+ expect ( response ) . to . have . status ( 200 ) ;
60+ expect ( response . body ) . to . eql ( { test : "auth1" } ) ;
61+ } ) ;
62+ } ) ;
63+
64+ describe ( "with roles" , ( ) => {
65+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth2" , response => {
66+ expect ( response ) . to . have . status ( 200 ) ;
67+ expect ( response . body ) . to . eql ( { test : "auth2" } ) ;
68+ } ) ;
69+ } ) ;
70+
71+ } ) ;
72+
1273describe ( "Authorized Decorators Http Status Code" , function ( ) {
1374
1475 before ( ( ) => {
0 commit comments