import { Request } from '@loopback/rest' ; import { securityId , UserProfile } from '@loopback/security' ; import { AuthenticationStrategy } from '../../../types' ; class AuthenticationError extends Error { statusCode ? : number ; } /**
 * Test fixture for a mock asynchronous authentication strategy
 */ export class MockStrategy implements AuthenticationStrategy { name = 'MockStrategy' ; private mockUser : UserProfile ; setMockUser ( userObj ) { this . mockUser = userObj ; } returnMockUser ( ) { return this . mockUser ; } async authenticate ( req ) { return this . verify ( req ) ; } param req async verify ( request ) { if ( request . headers ?. testState === 'fail' ) { const err = new AuthenticationError ( 'authorization failed' ) ; err . statusCode = 401 ; throw err ; } else if ( request . headers ?. testState === 'empty' ) { return ; } else if ( request . headers ?. testState === 'error' ) { throw new Error ( 'unexpected error' ) ; } return this . returnMockUser ( ) ; } } export class MockStrategy2 implements AuthenticationStrategy { name = 'MockStrategy2' ; async authenticate ( request ) { if ( request . headers ?. testState2 === 'fail' ) { throw new AuthenticationError ( ) ; } return { [ securityId ] : 'mock-id' } ; } }