/
mock-strategy.ts
63 lines (57 loc) · 1.71 KB
/
mock-strategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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 '@loopback/rest';
import {AuthenticationStrategy, UserProfile} from '../../../types';
class AuthenticationError extends Error {
statusCode?: number;
}
/**
* Test fixture for a mock asynchronous passport-strategy
*/
export class MockStrategy implements AuthenticationStrategy {
name: 'MockStrategy';
// user to return for successful authentication
private mockUser: UserProfile;
setMockUser(userObj: UserProfile) {
this.mockUser = userObj;
}
returnMockUser(): UserProfile {
return this.mockUser;
}
/**
* authenticate() function similar to passport-strategy packages
* @param req
*/
async authenticate(req: Request): Promise<UserProfile> {
return 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'
) {
const err = new AuthenticationError('authorization failed');
err.statusCode = 401;
throw err;
} else if (
request.headers &&
request.headers.testState &&
request.headers.testState === 'error'
) {
throw new Error('unexpected error');
}
return this.returnMockUser();
}
}