Search in sources :

Example 11 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedLinkPropagated.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedLinkPropagated() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    // Mock linking with Facebook to always work
    when(mMockAuthResult.getUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoContinueTask.<AuthResult>forFailure(new Exception("FAILED")));
    mHandler.startSignIn();
    // Verify sign in was called
    verify(mScratchMockAuth).signInWithCredential(any(AuthCredential.class));
    // Validate linking was called
    verify(mMockAuthResult.getUser()).linkWithCredential(any(FacebookAuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate failure
    ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
    inOrder.verify(mResponseObserver).onChanged(captor.capture());
    assertThat(captor.getValue().getException()).isNotNull();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 12 with AuthOperationManager

use of com.firebase.ui.auth.util.data.AuthOperationManager in project FirebaseUI-Android by firebase.

the class GenericIdpAnonymousUpgradeLinkingHandlerTest method testStartSignIn_anonymousUpgradeLinkingFlow_expectIdpResponseWithCredential.

@Test
public void testStartSignIn_anonymousUpgradeLinkingFlow_expectIdpResponseWithCredential() {
    setupAnonymousUpgrade();
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.startActivityForSignInWithProvider(any(HelperActivityBase.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    AuthCredential credential = OAuthProvider.newCredentialBuilder(MICROSOFT_PROVIDER).setIdToken(ID_TOKEN).setAccessToken(ACCESS_TOKEN).build();
    when(mMockAuthResult.getCredential()).thenReturn(credential);
    when(mMockAuthResult.getUser()).thenReturn(mMockUser);
    when(mMockUser.getDisplayName()).thenReturn(DISPLAY_NAME);
    when(mMockUser.getPhotoUrl()).thenReturn(new Uri.Builder().build());
    mockOAuthProvider(MICROSOFT_PROVIDER);
    mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER);
    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mScratchMockAuth).startActivityForSignInWithProvider(eq(mMockActivity), providerCaptor.capture());
    assertThat(providerCaptor.getValue().getProviderId()).isEqualTo(MICROSOFT_PROVIDER);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture());
    IdpResponse idpResponse = resolveCaptor.getValue().getValue();
    assertThat(idpResponse.getCredentialForLinking()).isNotNull();
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) InOrder(org.mockito.InOrder) HelperActivityBase(com.firebase.ui.auth.ui.HelperActivityBase) Resource(com.firebase.ui.auth.data.model.Resource) OAuthProvider(com.google.firebase.auth.OAuthProvider) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Aggregations

AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 IdpResponse (com.firebase.ui.auth.IdpResponse)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 Resource (com.firebase.ui.auth.data.model.Resource)9 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)7 User (com.firebase.ui.auth.data.model.User)6 Test (org.junit.Test)6 Application (android.app.Application)5 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 SignInViewModelBase (com.firebase.ui.auth.viewmodel.SignInViewModelBase)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)5 AuthResult (com.google.firebase.auth.AuthResult)5 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)5 EmailAuthProvider (com.google.firebase.auth.EmailAuthProvider)5 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)5 ErrorCodes (com.firebase.ui.auth.ErrorCodes)4 ProfileMerger (com.firebase.ui.auth.data.remote.ProfileMerger)4 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)4