Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright IBM Corp. 2019. 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 {Request} from 'express';
import {AuthenticateOptions, Strategy} from 'passport';
import {UserProfile} from '../../../types';

/**
* Test fixture for a mock asynchronous passport-strategy
*/
export class MockPassportStrategy extends Strategy {
// user to return for successful authentication
private mockUser: UserProfile;

setMockUser(userObj: UserProfile) {
this.mockUser = userObj;
}

/**
* authenticate() function similar to passport-strategy packages
* @param req
*/
async authenticate(req: Request, options?: AuthenticateOptions) {
await this.verify(req);
}
/**
* @param req
* mock verification function; usually passed in as constructor argument for
* passport-strategy
*
* For the purpose of mock tests we have this here
* pass req.query.testState = 'fail' to mock failed authorization
* pass req.query.testState = 'error' to mock unexpected error
*/
async verify(request: Request) {
if (
request.headers &&
request.headers.testState &&
request.headers.testState === 'fail'
) {
this.returnUnauthorized('authorization failed');
return;
} else if (
request.headers &&
request.headers.testState &&
request.headers.testState === 'error'
) {
this.returnError('unexpected error');
return;
}
process.nextTick(this.returnMockUser.bind(this));
}

returnMockUser() {
this.success(this.mockUser);
}

returnUnauthorized(challenge?: string | number, status?: number) {
this.fail(challenge, status);
}

returnError(err: string) {
this.error(err);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Strategy, AuthenticateOptions} from 'passport';
import {Request} from 'express';
import {Request} from '@loopback/rest';
import {AuthenticationStrategy, UserProfile} from '../../../types';

class AuthenticationError extends Error {
statusCode?: number;
}

/**
* Test fixture for a mock asynchronous passport-strategy
*/
export class MockStrategy extends Strategy {
export class MockStrategy implements AuthenticationStrategy {
name: 'MockStrategy';
// user to return for successful authentication
private mockUser: Object;
private mockUser: UserProfile;

setMockUser(userObj: Object) {
setMockUser(userObj: UserProfile) {
this.mockUser = userObj;
}

returnMockUser(): UserProfile {
return this.mockUser;
}

/**
* authenticate() function similar to passport-strategy packages
* @param req
*/
async authenticate(req: Request, options?: AuthenticateOptions) {
await this.verify(req);
async authenticate(req: Request): Promise<UserProfile> {
return await this.verify(req);
}
/**
* @param req
Expand All @@ -39,28 +48,16 @@ export class MockStrategy extends Strategy {
request.headers.testState &&
request.headers.testState === 'fail'
) {
this.returnUnauthorized('authorization failed');
return;
const err = new AuthenticationError('authorization failed');
err.statusCode = 401;
throw err;
} else if (
request.headers &&
request.headers.testState &&
request.headers.testState === 'error'
) {
this.returnError('unexpected error');
return;
throw new Error('unexpected error');
}
process.nextTick(this.returnMockUser.bind(this));
}

returnMockUser() {
this.success(this.mockUser);
}

returnUnauthorized(challenge?: string | number, status?: number) {
this.fail(challenge, status);
}

returnError(err: string) {
this.error(err);
return this.returnMockUser();
}
}
Loading