Skip to content

Commit 726b913

Browse files
Merge pull request #247 from pleerock/fix/authorization
Fix/authorization
2 parents faf0f15 + 1777c8e commit 726b913

File tree

6 files changed

+56
-15
lines changed

6 files changed

+56
-15
lines changed

src/driver/koa/KoaDriver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ export class KoaDriver extends BaseDriver implements Driver {
103103
};
104104

105105
if (isPromiseLike(checkResult)) {
106-
return checkResult.then(result => handleError(result));
106+
return checkResult
107+
.then(result => handleError(result))
108+
.catch(error => this.handleError(error, actionMetadata, action));
107109
} else {
108110
handleError(checkResult);
109111
}
@@ -289,12 +291,10 @@ export class KoaDriver extends BaseDriver implements Driver {
289291
handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
290292
if (this.isDefaultErrorHandlingEnabled) {
291293
const response: any = options.response;
292-
console.log("ERROR: ", error);
293294

294295
// set http status
295296
// note that we can't use error instanceof HttpError properly anymore because of new typescript emit process
296297
if (error.httpCode) {
297-
console.log("setting status code: ", error.httpCode);
298298
options.context.status = error.httpCode;
299299
response.status = error.httpCode;
300300
} else {

test/functional/action-params.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("action parameters", () => {
3939
let uploadedFilesFirstName: string;
4040
let uploadedFilesSecondName: string;
4141
let requestReq: any, requestRes: any;
42-
42+
4343
beforeEach(() => {
4444
paramUserId = undefined;
4545
paramFirstId = undefined;
@@ -375,7 +375,6 @@ describe("action parameters", () => {
375375

376376
describe("@Session(param) should allow to inject empty property", () => {
377377
assertRequest([3001, 3002], "get", "session-param-empty", response => {
378-
console.log(response.body);
379378
expect(response).to.be.status(200);
380379
expect(response).to.have.header("content-type", "text/html; charset=utf-8");
381380
expect(response.body).to.be.equal("<html><body>true</body></html>");
@@ -829,7 +828,7 @@ describe("action parameters", () => {
829828
assertRequest([3001, 3002], "post", "photos-with-required", undefined, {}, response => {
830829
expect(response).to.be.status(400);
831830
});
832-
831+
833832
});
834833

835834
});

test/functional/auth-decorator.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,50 @@ describe("Authorized Decorators Http Status Code", function () {
127127
});
128128
});
129129

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+
});
130176
});

test/functional/express-error-handling.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe("express error handling", () => {
1818
errorHandlerCalled = undefined;
1919
errorHandledSpecifically = undefined;
2020
});
21-
21+
2222
before(() => {
2323

2424
// reset metadata args storage
@@ -29,7 +29,7 @@ describe("express error handling", () => {
2929

3030
error(error: any, request: any, response: any, next?: Function): any {
3131
errorHandlerCalled = true;
32-
console.log("ERROR HANDLED GLOBALLY: ", error);
32+
// ERROR HANDLED GLOBALLY
3333
next(error);
3434
}
3535

@@ -39,7 +39,7 @@ describe("express error handling", () => {
3939

4040
error(error: any, request: any, response: any, next?: Function): any {
4141
errorHandledSpecifically = true;
42-
console.log("ERROR HANDLED SPECIFICALLY: ", error);
42+
// ERROR HANDLED SPECIFICALLY
4343
next(error);
4444
}
4545

@@ -48,7 +48,7 @@ describe("express error handling", () => {
4848
class SoftErrorHandler implements ExpressErrorMiddlewareInterface {
4949

5050
error(error: any, request: any, response: any, next?: Function): any {
51-
console.log("ERROR WAS IGNORED: ", error);
51+
// ERROR WAS IGNORED
5252
next();
5353
}
5454

@@ -96,7 +96,7 @@ describe("express error handling", () => {
9696
photos() {
9797
return "1234";
9898
}
99-
99+
100100
}
101101
});
102102

test/functional/express-global-before-error-handling.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ describe("custom express global before middleware error handling", () => {
3131
@Middleware({ type: "before" })
3232
class GlobalBeforeMiddleware implements ExpressMiddlewareInterface {
3333
use(request: any, response: any, next?: Function): any {
34-
console.log("GLOBAL BEFORE MIDDLEWARE CALLED");
3534
throw new CustomError();
3635
}
3736
}

test/functional/redirect-decorator.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,17 @@ describe("dynamic redirect", function () {
3434
@Get("/template")
3535
@Redirect("/users/:owner")
3636
template() {
37-
// console.log("/template");
3837
return {owner: "pleerock", repo: "routing-controllers"};
3938
}
4039

4140
@Get("/original")
4241
@Redirect("/users/pleerock")
4342
original() {
44-
// console.log("/original");
4543
}
4644

4745
@Get("/override")
4846
@Redirect("https://api.github.com")
4947
override() {
50-
// console.log("/override");
5148
return "/users/pleerock";
5249
}
5350

0 commit comments

Comments
 (0)