Skip to content

Commit e289d11

Browse files
authored
Merge branch 'master' into 404-return-type-fix
2 parents 9b70134 + 726b913 commit e289d11

File tree

8 files changed

+126
-15
lines changed

8 files changed

+126
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog and release notes
22

3+
**0.7.2**
4+
- FIXED: Using `@Authorization` decorator with Koa caused 404 responses (ref #240)
5+
6+
**0.7.1**
7+
38
**0.7.0** *[BREAKING CHANGES]*
49

510
* some routing-controllers options has been changed and renamed

src/driver/express/ExpressDriver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ export class ExpressDriver extends BaseDriver implements Driver {
127127
};
128128

129129
if (isPromiseLike(checkResult)) {
130-
checkResult.then(result => handleError(result));
130+
checkResult
131+
.then(result => handleError(result))
132+
.catch(error => this.handleError(error, actionMetadata, action));
131133
} else {
132134
handleError(checkResult);
133135
}

src/driver/koa/KoaDriver.ts

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

106106
if (isPromiseLike(checkResult)) {
107-
checkResult.then(result => handleError(result));
107+
return checkResult
108+
.then(result => handleError(result))
109+
.catch(error => this.handleError(error, actionMetadata, action));
108110
} else {
109111
handleError(checkResult);
110112
}
@@ -285,7 +287,7 @@ export class KoaDriver extends BaseDriver implements Driver {
285287
/**
286288
* Handles result of failed executed controller action.
287289
*/
288-
handleError(error: any, action: ActionMetadata | undefined, options: Action): any {
290+
handleError(error: any, action: ActionMetadata | undefined, options: Action) {
289291
return new Promise((resolve, reject) => {
290292
if (this.isDefaultErrorHandlingEnabled) {
291293
// set http status

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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,67 @@ import {RoutingControllersOptions} from "../../src/RoutingControllersOptions";
99
const chakram = require("chakram");
1010
const 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+
1273
describe("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
});

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)