Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Now add ChineseGreeter
ctx
.bind('greeters.ChineseGreeter')
.toClass(ChineseGreeter)
.tag('greeter');
greeting = await invokeMethod(greetingService, 'greet', ctx);
console.log(greeting);
// Change the current language to `en`
ctx.bind('currentLanguage').to('en');
// Change the current user to `Jane`
ctx.bind('currentUser').to('Jane');
greeting = await invokeMethod(greetingService, 'greet', ctx);
console.log(greeting);
}
async invokeHandler(
requestContext: Context,
args: OperationArgs,
): Promise {
const controller = await requestContext.get(
'controller.current',
);
if (typeof controller[this._methodName] !== 'function') {
throw new HttpErrors.NotFound(
`Controller method not found: ${this.describe()}`,
);
}
// Invoke the method with dependency injection
return invokeMethod(controller, this._methodName, requestContext, args, {
source: new RouteSource(this),
});
}
}
it('denies cancelOrder for bob', async () => {
givenRequestContext({[securityId]: 'bob', name: 'bob'});
const orderId = await controller.placeOrder({
customerId: 'bob',
productId: 'product-a',
quantity: 10,
price: 320,
});
const result = invokeMethod(controller, 'cancelOrder', reqCtx, [orderId]);
await expect(result).to.be.rejectedWith('Access denied');
expect(events).to.containEql('OrderController.prototype.cancelOrder');
});
it('allows cancelOrder for customer_service', async () => {
givenRequestContext({
[securityId]: 'customer_service',
name: 'customer_service',
});
const orderId = await controller.placeOrder({
customerId: 'bob',
productId: 'product-a',
quantity: 10,
price: 320,
});
const result = await invokeMethod(controller, 'cancelOrder', reqCtx, [
orderId,
]);
expect(result).to.be.true();
expect(events).to.containEql('OrderController.prototype.cancelOrder');
});
it('denies cancelOrder for regular user', async () => {
const result = invokeMethod(controller, 'cancelOrder', reqCtx, [
'order-01',
]);
await expect(result).to.be.rejectedWith('Access denied');
expect(events).to.containEql('OrderController.prototype.cancelOrder');
});
it('allows placeOrder for everyone', async () => {
const orderId = await invokeMethod(controller, 'placeOrder', reqCtx, [
{
customerId: 'customer-01',
productId: 'product-a',
quantity: 10,
price: 320,
},
]);
expect(orderId).to.eql('order-1');
expect(events).to.containEql('OrderController.prototype.placeOrder');
});
async function placeOrder() {
let finalDecision = Deny;
try {
await invokeMethod(controller, 'placeOrder', reqCtx, ['prod-01', 10]);
finalDecision = Allow;
} catch (err) {
finalDecision = Deny;
}
return finalDecision;
}
async function cancelOrder() {
let finalDecision = Deny;
try {
await invokeMethod(controller, 'cancelOrder', reqCtx, ['order-01']);
finalDecision = Allow;
} catch (err) {
finalDecision = Deny;
}
return finalDecision;
}
regexpEventHandlers.set(regexps, async (...args: unknown[]) => {
await invokeMethod(this.controller, m, this.ctx, args);
});
}