import { Request } from '@loopback/rest' ; import { UserProfile } from '@loopback/security' ; import { AuthenticateOptions , Strategy } from 'passport' ; /**
 * Test fixture for a mock asynchronous passport-strategy
 */ export class MockPassportStrategy extends Strategy { private mockUser : UserProfile ; public name = 'mock-strategy' ; setMockUser ( userObj ) { this . mockUser = userObj ; } param req async authenticate ( req , options ? ) { await this . verify ( req ) ; } param req async verify ( request ) { if ( request . headers ?. testState === 'fail' ) { this . returnUnauthorized ( 'authorization failed' ) ; return ; } else if ( request . headers ?. testState === 'error' ) { this . returnError ( 'unexpected error' ) ; return ; } process . nextTick ( this . returnMockUser . bind ( this ) ) ; } returnMockUser ( ) { this . success ( this . mockUser ) ; } returnUnauthorized ( challenge ? , status ? ) { this . fail ( challenge , status ) ; } returnError ( err ) { this . error ( err ) ; } }