Skip to content

Commit

Permalink
test: add register auth strategy
Browse files Browse the repository at this point in the history
Advice needed:  Binding.key check worthwhile?

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Mar 9, 2020
1 parent 69ae770 commit 59782c3
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context} from '@loopback/context';
import {bind, Context, createBindingFromClass} from '@loopback/context';
import {
asSpecEnhancer,
OASEnhancer,
OAS_ENHANCER_EXTENSION_POINT_NAME,
OpenApiSpec,
} from '@loopback/openapi-v3';
import {Request} from '@loopback/rest';
import {securityId, UserProfile} from '@loopback/security';
import {expect} from '@loopback/testlab';
import {
asAuthStrategy,
AuthenticationBindings,
AuthenticationStrategy,
registerAuthenticationStrategy,
Expand All @@ -24,21 +31,45 @@ describe('registerAuthenticationStrategy', () => {
MyAuthenticationStrategy,
);
expect(binding.tagMap).to.containEql({
extensionFor:
extensionFor: [
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
OAS_ENHANCER_EXTENSION_POINT_NAME,
],
});
expect(binding.key).to.eql(
`${AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME}.MyAuthenticationStrategy`,
);
});

class MyAuthenticationStrategy implements AuthenticationStrategy {
it('adds a binding for the strategy and security spec', () => {
const binding = createBindingFromClass(MyAuthenticationStrategy);
expect(binding.tagMap).to.containEql({
extensionFor: [
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
OAS_ENHANCER_EXTENSION_POINT_NAME,
],
});
expect(binding.key).to.eql(
`${OAS_ENHANCER_EXTENSION_POINT_NAME}.MyAuthenticationStrategy`,
);
});

@bind(asAuthStrategy, asSpecEnhancer)
class MyAuthenticationStrategy
implements AuthenticationStrategy, OASEnhancer {
name: 'my-auth';
async authenticate(request: Request): Promise<UserProfile | undefined> {
return {
[securityId]: 'somebody',
};
}
modifySpec(spec: OpenApiSpec): OpenApiSpec {
return {
openapi: '3.0.0',
info: {title: 'Test', version: '1.0.0'},
paths: {},
};
}
}

function givenContext() {
Expand Down

0 comments on commit 59782c3

Please sign in to comment.