Skip to content

Commit 352c1f5

Browse files
test(Authorization): add test for custom errors in authorization checker
1 parent 1d13fcd commit 352c1f5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

0 commit comments

Comments
 (0)