import { AUTHENTICATION_METHODS , OpenYoloCredential , OpenYoloCredentialRequestOptions } from '../protocol/data' ; import { credentialResultMessage , retrieveMessage , showProviderMessage } from '../protocol/rpc_messages' ; import { SecureChannel } from '../protocol/secure_channel' ; import { FakeProviderConnection } from '../test_utils/channels' ; import { createSpyFrame } from '../test_utils/frames' ; import { CredentialRequest } from './credential_request' ; import { ProviderFrameElement } from './provider_frame_elem' ; describe ( 'CredentialRequest' , ( ) => { let request ; let connection ; let clientChannel ; let providerChannel ; let frame ; const options = { supportedAuthMethods : [ ] } ; beforeEach ( ( ) => { connection = new FakeProviderConnection ( ) ; frame = createSpyFrame ( 'spyframe' ) ; clientChannel = connection . clientChannel ; providerChannel = connection . providerChannel ; request = new CredentialRequest ( frame , clientChannel ) ; spyOn ( request , 'dispose' ) . and . callThrough ( ) ; } ) ; afterEach ( ( ) => { request . dispose ( ) ; } ) ; describe ( 'dispatch' , ( ) => { it ( 'should send a RPC message through the channel' , ( ) => { spyOn ( clientChannel , 'send' ) . and . callThrough ( ) ; let options = { supportedAuthMethods : [ 'openyolo://id-and-password' ] } ; request . dispatch ( options ) ; expect ( clientChannel . send ) . toHaveBeenCalledWith ( retrieveMessage ( request . id , options ) ) ; } ) ; } ) ; describe ( 'response handling' , ( ) => { it ( 'should display the frame if there are credentials' , ( ) => { request . dispatch ( options ) ; providerChannel . send ( showProviderMessage ( request . id , { height : 200 } ) ) ; expect ( frame . display ) . toHaveBeenCalled ( ) ; } ) ; it ( 'should resolve with credential on success' , async function ( done ) { let credential = { id : 'alice@gmail.com' , authMethod : AUTHENTICATION_METHODS . ID_AND_PASSWORD , displayName : 'Google' } ; let requestPromise = request . dispatch ( options ) ; providerChannel . send ( credentialResultMessage ( request . id , credential ) ) ; try { let result = await requestPromise ; expect ( result ) . toEqual ( credential ) ; expect ( request . dispose ) . toHaveBeenCalled ( ) ; done ( ) ; } catch ( err ) { fail ( 'Result promise was rejected' ) ; } } ) ; it ( 'should ignore when different id' , ( ) => { request . dispatch ( options ) ; let credential = { id : 'alice@gmail.com' , authMethod : AUTHENTICATION_METHODS . ID_AND_PASSWORD , name : 'Alice' , password : 'qwertyui' } ; providerChannel . send ( credentialResultMessage ( 'otherId' , credential ) ) ; expect ( request . dispose ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;