Search in sources :

Example 76 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class SocialProviderResponseHandlerTest method testSignInIdp_anonymousUserUpgradeEnabledAndExistingIdpUserWithDifferentIdp_expectMergeFailure.

@Test
public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingIdpUserWithDifferentIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResultObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    // Case 2 & 3: trying to link with an account that has 1 idp, which is different from the
    // one that we're trying to log in with
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(new FakeSignInMethodQueryResult(Collections.singletonList(FacebookAuthProvider.FACEBOOK_SIGN_IN_METHOD))));
    IdpResponse response = new IdpResponse.Builder(new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).setToken(TestConstants.TOKEN).build();
    mHandler.startSignIn(response);
    verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));
    InOrder inOrder = inOrder(mResultObserver);
    inOrder.verify(mResultObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture());
    // Make sure that we are trying to start the WelcomeBackIdpPrompt activity
    IntentRequiredException e = ((IntentRequiredException) resolveCaptor.getValue().getException());
    assertThat(e.getIntent().getComponent().getClassName()).isEqualTo(WelcomeBackIdpPrompt.class.toString().split(" ")[1]);
    assertThat(IdpResponse.fromResultIntent(e.getIntent())).isEqualTo(response);
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) FakeSignInMethodQueryResult(com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 77 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class SocialProviderResponseHandlerTest method testSignInIdp_anonymousUserUpgradeEnabledAndExistingPasswordUserWithDifferentIdp_expectMergeFailure.

@Test
public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingPasswordUserWithDifferentIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResultObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    // Case 2 & 3: trying to link with an account that has 1 password provider and logging in
    // with an idp that has the same email
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(new FakeSignInMethodQueryResult(Collections.singletonList(EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD))));
    IdpResponse response = new IdpResponse.Builder(new User.Builder(FacebookAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).setToken(TestConstants.TOKEN).setSecret(TestConstants.SECRET).build();
    mHandler.startSignIn(response);
    verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class));
    InOrder inOrder = inOrder(mResultObserver);
    inOrder.verify(mResultObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture());
    // Make sure that we are trying to start the WelcomeBackIdpPrompt activity
    IntentRequiredException e = ((IntentRequiredException) resolveCaptor.getValue().getException());
    assertThat(e.getIntent().getComponent().getClassName()).isEqualTo(WelcomeBackPasswordPrompt.class.toString().split(" ")[1]);
    assertThat(IdpResponse.fromResultIntent(e.getIntent())).isEqualTo(response);
}
Also used : FakeSignInMethodQueryResult(com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) AuthCredential(com.google.firebase.auth.AuthCredential) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.email.WelcomeBackPasswordPrompt) Test(org.junit.Test)

Example 78 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordHandlerTest method testSignIn_linksIdpCredential.

@Test
public void testSignIn_linksIdpCredential() {
    mHandler.getOperation().observeForever(mResponseObserver);
    // Fake social response from Facebook
    User user = new User.Builder(FacebookAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
    IdpResponse response = new IdpResponse.Builder(user).setToken(TestConstants.TOKEN).setSecret(TestConstants.SECRET).build();
    AuthCredential credential = FacebookAuthProvider.getCredential(TestConstants.TOKEN);
    // Mock sign in to always succeed
    when(mMockAuth.signInWithEmailAndPassword(TestConstants.EMAIL, TestConstants.PASSWORD)).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Mock linking to always succeed
    when(FakeAuthResult.INSTANCE.getUser().linkWithCredential(credential)).thenReturn(new AutoContinueTask<>(FakeAuthResult.INSTANCE, FakeAuthResult.INSTANCE, true, null));
    // Mock smartlock save to always succeed
    when(mMockCredentials.save(any(Credential.class))).thenReturn(AutoCompleteTask.forSuccess(null));
    // Kick off the sign in flow
    mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, credential);
    // Verify that we get a loading event
    verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    // Verify that sign in is called with the right arguments
    verify(mMockAuth).signInWithEmailAndPassword(TestConstants.EMAIL, TestConstants.PASSWORD);
    // Verify that account linking is attempted
    verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(credential);
    // Verify that we get a success event
    verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isSuccess()));
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 79 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class EmailLinkSendEmailHandlerTest method testSendSignInLinkToEmail_linkingFlow_expectSuccess.

@Test
public void testSendSignInLinkToEmail_linkingFlow_expectSuccess() {
    mHandler.getOperation().observeForever(mResponseObserver);
    boolean forceSameDevice = true;
    ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder().setUrl(URL).build();
    when(mMockAuth.sendSignInLinkToEmail(any(String.class), any(ActionCodeSettings.class))).thenReturn(AutoCompleteTask.forFailure(new Exception()));
    IdpResponse idpResponseForLinking = buildFacebookIdpResponseForLinking();
    mHandler.sendSignInLinkToEmail(TestConstants.EMAIL, actionCodeSettings, idpResponseForLinking, forceSameDevice);
    ArgumentCaptor<ActionCodeSettings> acsCaptor = ArgumentCaptor.forClass(ActionCodeSettings.class);
    verify(mMockAuth).sendSignInLinkToEmail(eq(TestConstants.EMAIL), acsCaptor.capture());
    validateSessionIdAddedToContinueUrl(acsCaptor.getValue(), idpResponseForLinking.getProviderType(), forceSameDevice);
    ArgumentCaptor<Resource<String>> captor = ArgumentCaptor.forClass(Resource.class);
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    inOrder.verify(mResponseObserver).onChanged(captor.capture());
    assertThat(captor.getValue().getState()).isEqualTo(State.FAILURE);
    assertThat(captor.getValue().getException()).isNotNull();
}
Also used : InOrder(org.mockito.InOrder) ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) Resource(com.firebase.ui.auth.data.model.Resource) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 80 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project quickstart-android by firebase.

the class MainActivity method onSignInResult.

private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
    if (result.getResultCode() == RESULT_OK) {
        showSnackbar("Signed in.");
    } else {
        showSnackbar("Error signing in.");
        IdpResponse response = result.getIdpResponse();
        Log.w(TAG, "signIn", response.getError());
    }
}
Also used : IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

IdpResponse (com.firebase.ui.auth.IdpResponse)81 Test (org.junit.Test)45 Resource (com.firebase.ui.auth.data.model.Resource)35 AuthCredential (com.google.firebase.auth.AuthCredential)31 InOrder (org.mockito.InOrder)31 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)22 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)19 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)19 User (com.firebase.ui.auth.data.model.User)18 FirebaseUser (com.google.firebase.auth.FirebaseUser)15 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)12 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)11 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)10 AuthResult (com.google.firebase.auth.AuthResult)10 Intent (android.content.Intent)9 Nullable (androidx.annotation.Nullable)9 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)9 Task (com.google.android.gms.tasks.Task)9 Application (android.app.Application)8