Skip to content

Commit

Permalink
test: added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Mar 6, 2020
1 parent 430688f commit e8bef12
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/authentication
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
Expand All @@ -9,7 +9,7 @@ import {anOpenApiSpec} from '@loopback/openapi-spec-builder';
import {api, get} from '@loopback/openapi-v3';
import {Request, RestServer} from '@loopback/rest';
import {SecurityBindings, securityId, UserProfile} from '@loopback/security';
import {Client, createClientForHandler} from '@loopback/testlab';
import {Client, createClientForHandler, expect} from '@loopback/testlab';
import {
authenticate,
AuthenticationBindings,
Expand Down Expand Up @@ -164,6 +164,7 @@ describe('Basic Authentication', () => {
},
});
});

it('returns error when undefined user profile returned from authentication strategy', async () => {
class BadBasicStrategy implements AuthenticationStrategy {
name = 'badbasic';
Expand Down Expand Up @@ -193,6 +194,22 @@ describe('Basic Authentication', () => {
},
});
});

it('adds security scheme component to apiSpec', async () => {
const EXPECTED_SPEC = {
components: {
securitySchemes: {
basic: {
type: 'http',
scheme: 'basic',
},
},
},
};
const spec = await server.getApiSpec();
expect(spec).to.containDeep(EXPECTED_SPEC);
});

async function givenAServer() {
app = getApp();
server = await app.getServer(RestServer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/authentication
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
Expand Down Expand Up @@ -454,6 +454,22 @@ describe('JWT Authentication', () => {
});
});

it('adds security scheme component to apiSpec', async () => {
const EXPECTED_SPEC = {
components: {
securitySchemes: {
jwt: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
};
const spec = await server.getApiSpec();
expect(spec).to.containDeep(EXPECTED_SPEC);
});

async function givenAServer() {
app = getApp();
server = await app.getServer(RestServer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/authentication
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/context';
import {HttpErrors, Request} from '@loopback/rest';
import {UserProfile} from '@loopback/security';
import {AuthenticationStrategy} from '../../../types';
import {AuthenticationStrategy, SecuritySchemeObjects} from '../../../types';
import {BasicAuthenticationStrategyBindings} from '../keys';
import {BasicAuthenticationUserService} from '../services/basic-auth-user-service';

Expand All @@ -15,8 +15,20 @@ export interface BasicAuthenticationStrategyCredentials {
password: string;
}

const STRATEGY_NAME = 'basic';

const SECURITY_SCHEME_SPEC = (): SecuritySchemeObjects => {
return {
[`${STRATEGY_NAME}`]: {
type: 'http',
scheme: 'basic',
},
};
};

export class BasicAuthenticationStrategy implements AuthenticationStrategy {
name = 'basic';
name = STRATEGY_NAME;
securitySpec = SECURITY_SCHEME_SPEC;

constructor(
@inject(BasicAuthenticationStrategyBindings.USER_SERVICE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/authentication
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/context';
import {HttpErrors, Request} from '@loopback/rest';
import {UserProfile} from '@loopback/security';
import {AuthenticationStrategy} from '../../../types';
import {AuthenticationStrategy, SecuritySchemeObjects} from '../../../types';
import {JWTAuthenticationStrategyBindings} from '../keys';
import {JWTService} from '../services/jwt-service';

const STRATEGY_NAME = 'jwt';

const SECURITY_SCHEME_SPEC = (): SecuritySchemeObjects => {
return {
[`${STRATEGY_NAME}`]: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
};
};

export class JWTAuthenticationStrategy implements AuthenticationStrategy {
name = 'jwt';
name = STRATEGY_NAME;
securitySpec = SECURITY_SCHEME_SPEC;

constructor(
@inject(JWTAuthenticationStrategyBindings.TOKEN_SERVICE)
Expand Down

0 comments on commit e8bef12

Please sign in to comment.