Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('returns undefined if no auth metadata is defined', async () => {
const context: Context = new Context();
context
.bind(CoreBindings.CONTROLLER_CLASS)
.to(ControllerWithNoMetadata);
context.bind(CoreBindings.CONTROLLER_METHOD_NAME).to('whoAmI');
context
.bind(CoreBindings.CONTROLLER_METHOD_META)
.toProvider(AuthMetadataProvider);
const authMetadata = await context.get(
CoreBindings.CONTROLLER_METHOD_META,
);
expect(authMetadata).to.be.undefined();
});
it('adds bindings to the request context', async () => {
expect(requestCtx.contains(CoreBindings.CONTROLLER_CURRENT));
expect(
requestCtx.getBinding(CoreBindings.CONTROLLER_CURRENT).scope,
).to.equal(BindingScope.SINGLETON);
expect(await requestCtx.get(CoreBindings.CONTROLLER_CLASS)).to.equal(
MyController,
);
expect(
await requestCtx.get(CoreBindings.CONTROLLER_METHOD_NAME),
).to.equal('greet');
expect(await requestCtx.get(RestBindings.OPERATION_SPEC_CURRENT)).to.eql({
'x-controller-name': 'MyController',
'x-operation-name': 'greet',
tags: ['MyController'],
responses: {'200': {description: 'An undocumented response body.'}},
});
});
it('returns the auth metadata of a controller method', async () => {
const context: Context = new Context();
context.bind(CoreBindings.CONTROLLER_CLASS).to(TestController);
context.bind(CoreBindings.CONTROLLER_METHOD_NAME).to('whoAmI');
context
.bind(CoreBindings.CONTROLLER_METHOD_META)
.toProvider(AuthMetadataProvider);
const authMetadata = await context.get(
CoreBindings.CONTROLLER_METHOD_META,
);
expect(authMetadata).to.be.eql({
strategy: 'my-strategy',
options: {option1: 'value1', option2: 'value2'},
});
});
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true}) protected _controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true}) protected _methodName: string,
) {
super(_controllerClass, _methodName);
}
* Within the same request, we always get the same instance of the
* current controller when `requestContext.get(CoreBindings.CONTROLLER_CURRENT)`
* is invoked.
*
* Please note the controller class itself can be bound to other scopes,
* such as SINGLETON or TRANSIENT (default) in the application or server
* context.
*
* - SINGLETON: all requests share the same instance of a given controller
* - TRANSIENT: each request has its own instance of a given controller
*/
requestContext
.bind(CoreBindings.CONTROLLER_CURRENT)
.toDynamicValue(() => this._controllerFactory(requestContext))
.inScope(BindingScope.SINGLETON);
requestContext.bind(CoreBindings.CONTROLLER_CLASS).to(this._controllerCtor);
requestContext
.bind(CoreBindings.CONTROLLER_METHOD_NAME)
.to(this._methodName);
requestContext.bind(RestBindings.OPERATION_SPEC_CURRENT).to(this.spec);
}
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true})
private readonly controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true})
private readonly methodName: string,
@config({fromBinding: AuthenticationBindings.COMPONENT})
private readonly options: AuthenticationOptions = {},
) {}
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true})
private readonly controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true})
private readonly methodName: string,
) {}