@@ -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 ( ( ) => {
@@ -66,4 +127,50 @@ describe("Authorized Decorators Http Status Code", function () {
66127 } ) ;
67128 } ) ;
68129
130+ } ) ;
131+
132+ describe ( "Authorization checker allows to throw" , function ( ) {
133+ before ( ( ) => {
134+ // reset metadata args storage
135+ getMetadataArgsStorage ( ) . reset ( ) ;
136+
137+ @JsonController ( )
138+ class AuthController {
139+ @Authorized ( )
140+ @Get ( "/auth1" )
141+ auth1 ( ) {
142+ return { test : "auth1" } ;
143+ }
144+
145+ }
146+ } ) ;
147+
148+ const serverOptions : RoutingControllersOptions = {
149+ authorizationChecker : async ( action : Action , roles ?: string [ ] ) => {
150+ throw new Error ( 'Custom Error' ) ;
151+ }
152+ } ;
153+
154+ let expressApp : any ;
155+ before ( done => {
156+ const server = createExpressServer ( serverOptions ) ;
157+ expressApp = server . listen ( 3001 , done ) ;
158+ } ) ;
159+ after ( done => expressApp . close ( done ) ) ;
160+
161+ let koaApp : any ;
162+ before ( done => {
163+ const server = createKoaServer ( serverOptions ) ;
164+ koaApp = server . listen ( 3002 , done ) ;
165+ } ) ;
166+ after ( done => koaApp . close ( done ) ) ;
167+
168+ describe ( "custom errors" , ( ) => {
169+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth1" , response => {
170+ expect ( response ) . to . have . status ( 500 ) ;
171+ expect ( response . body ) . to . have . property ( "name" , "Error" ) ;
172+ expect ( response . body ) . to . have . property ( "message" , "Custom Error" ) ;
173+
174+ } ) ;
175+ } ) ;
69176} ) ;
0 commit comments