1
1
import "reflect-metadata" ;
2
2
import { Get } from "../../src/decorator/Get" ;
3
- import { createExpressServer , createKoaServer , getMetadataArgsStorage } from "../../src/index" ;
3
+ import { createExpressServer , createKoaServer , getMetadataArgsStorage , NotAcceptableError } from "../../src/index" ;
4
4
import { assertRequest } from "./test-utils" ;
5
5
import { JsonController } from "../../src/decorator/JsonController" ;
6
6
import { Authorized } from "../../src/decorator/Authorized" ;
@@ -9,7 +9,7 @@ import {RoutingControllersOptions} from "../../src/RoutingControllersOptions";
9
9
const chakram = require ( "chakram" ) ;
10
10
const expect = chakram . expect ;
11
11
12
- describe ( "Controller responds with value when Authorization succeeds" , function ( ) {
12
+ describe ( "Controller responds with value when Authorization succeeds (async) " , function ( ) {
13
13
14
14
before ( ( ) => {
15
15
@@ -70,6 +70,67 @@ describe("Controller responds with value when Authorization succeeds", function
70
70
71
71
} ) ;
72
72
73
+ describe ( "Controller responds with value when Authorization succeeds (sync)" , function ( ) {
74
+
75
+ before ( ( ) => {
76
+
77
+ // reset metadata args storage
78
+ getMetadataArgsStorage ( ) . reset ( ) ;
79
+
80
+ @JsonController ( )
81
+ class AuthController {
82
+
83
+ @Authorized ( )
84
+ @Get ( "/auth1" )
85
+ auth1 ( ) {
86
+ return { test : "auth1" } ;
87
+ }
88
+
89
+ @Authorized ( [ "role1" ] )
90
+ @Get ( "/auth2" )
91
+ auth2 ( ) {
92
+ return { test : "auth2" } ;
93
+ }
94
+
95
+ }
96
+ } ) ;
97
+
98
+ const serverOptions : RoutingControllersOptions = {
99
+ authorizationChecker : ( action : Action , roles ?: string [ ] ) => {
100
+ return true ;
101
+ }
102
+ } ;
103
+
104
+ let expressApp : any ;
105
+ before ( done => {
106
+ const server = createExpressServer ( serverOptions ) ;
107
+ expressApp = server . listen ( 3001 , done ) ;
108
+ } ) ;
109
+ after ( done => expressApp . close ( done ) ) ;
110
+
111
+ let koaApp : any ;
112
+ before ( done => {
113
+ const server = createKoaServer ( serverOptions ) ;
114
+ koaApp = server . listen ( 3002 , done ) ;
115
+ } ) ;
116
+ after ( done => koaApp . close ( done ) ) ;
117
+
118
+ describe ( "without roles" , ( ) => {
119
+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth1" , response => {
120
+ expect ( response ) . to . have . status ( 200 ) ;
121
+ expect ( response . body ) . to . eql ( { test : "auth1" } ) ;
122
+ } ) ;
123
+ } ) ;
124
+
125
+ describe ( "with roles" , ( ) => {
126
+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth2" , response => {
127
+ expect ( response ) . to . have . status ( 200 ) ;
128
+ expect ( response . body ) . to . eql ( { test : "auth2" } ) ;
129
+ } ) ;
130
+ } ) ;
131
+
132
+ } ) ;
133
+
73
134
describe ( "Authorized Decorators Http Status Code" , function ( ) {
74
135
75
136
before ( ( ) => {
@@ -129,48 +190,90 @@ describe("Authorized Decorators Http Status Code", function () {
129
190
130
191
} ) ;
131
192
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
- } ) ;
193
+ describe ( "Authorization checker allows to throw (async)" , function ( ) {
194
+ before ( ( ) => {
195
+ // reset metadata args storage
196
+ getMetadataArgsStorage ( ) . reset ( ) ;
197
+
198
+ @JsonController ( )
199
+ class AuthController {
200
+ @Authorized ( )
201
+ @Get ( "/auth1" )
202
+ auth1 ( ) {
203
+ return { test : "auth1" } ;
204
+ }
205
+ }
206
+ } ) ;
207
+
208
+ const serverOptions : RoutingControllersOptions = {
209
+ authorizationChecker : async ( action : Action , roles ?: string [ ] ) => {
210
+ throw new NotAcceptableError ( "Custom Error" ) ;
211
+ } ,
212
+ } ;
213
+
214
+ let expressApp : any ;
215
+ before ( done => {
216
+ const server = createExpressServer ( serverOptions ) ;
217
+ expressApp = server . listen ( 3001 , done ) ;
218
+ } ) ;
219
+ after ( done => expressApp . close ( done ) ) ;
220
+
221
+ let koaApp : any ;
222
+ before ( done => {
223
+ const server = createKoaServer ( serverOptions ) ;
224
+ koaApp = server . listen ( 3002 , done ) ;
225
+ } ) ;
226
+ after ( done => koaApp . close ( done ) ) ;
227
+
228
+ describe ( "custom errors" , ( ) => {
229
+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth1" , response => {
230
+ expect ( response ) . to . have . status ( 406 ) ;
231
+ expect ( response . body ) . to . have . property ( "name" , "NotAcceptableError" ) ;
232
+ expect ( response . body ) . to . have . property ( "message" , "Custom Error" ) ;
233
+ } ) ;
234
+ } ) ;
235
+ } ) ;
236
+
237
+ describe ( "Authorization checker allows to throw (sync)" , function ( ) {
238
+ before ( ( ) => {
239
+ // reset metadata args storage
240
+ getMetadataArgsStorage ( ) . reset ( ) ;
241
+
242
+ @JsonController ( )
243
+ class AuthController {
244
+ @Authorized ( )
245
+ @Get ( "/auth1" )
246
+ auth1 ( ) {
247
+ return { test : "auth1" } ;
248
+ }
249
+ }
250
+ } ) ;
251
+
252
+ const serverOptions : RoutingControllersOptions = {
253
+ authorizationChecker : ( action : Action , roles ?: string [ ] ) => {
254
+ throw new NotAcceptableError ( "Custom Error" ) ;
255
+ } ,
256
+ } ;
257
+
258
+ let expressApp : any ;
259
+ before ( done => {
260
+ const server = createExpressServer ( serverOptions ) ;
261
+ expressApp = server . listen ( 3001 , done ) ;
262
+ } ) ;
263
+ after ( done => expressApp . close ( done ) ) ;
264
+
265
+ let koaApp : any ;
266
+ before ( done => {
267
+ const server = createKoaServer ( serverOptions ) ;
268
+ koaApp = server . listen ( 3002 , done ) ;
269
+ } ) ;
270
+ after ( done => koaApp . close ( done ) ) ;
271
+
272
+ describe ( "custom errors" , ( ) => {
273
+ assertRequest ( [ 3001 , 3002 ] , "get" , "auth1" , response => {
274
+ expect ( response ) . to . have . status ( 406 ) ;
275
+ expect ( response . body ) . to . have . property ( "name" , "NotAcceptableError" ) ;
276
+ expect ( response . body ) . to . have . property ( "message" , "Custom Error" ) ;
277
+ } ) ;
278
+ } ) ;
176
279
} ) ;
0 commit comments