Search in sources :

Example 6 with FirebaseAuthAnonymousUpgradeException

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

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_expectMergeFailure.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_expectMergeFailure() {
    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(new AutoContinueTask<>(mMockAuthResult, mMockAuthResult, true, null));
    mHandler.startSignIn();
    // Validate regular sign in
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(credentialCaptor.getValue().getEmail()).isEqualTo(TestConstants.EMAIL);
    assertThat(credentialCaptor.getValue().getSignInMethod()).isEqualTo(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD);
    // Validate linking was called
    verify(mMockAuthResult.getUser()).linkWithCredential(any(FacebookAuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate IdpResponse
    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());
    FirebaseAuthAnonymousUpgradeException mergeException = ((FirebaseAuthAnonymousUpgradeException) captor.getValue().getException());
    IdpResponse response = mergeException.getResponse();
    assertThat(response.getCredentialForLinking()).isNotNull();
    assertThat(response.getCredentialForLinking().getProvider()).isEqualTo(FacebookAuthProvider.PROVIDER_ID);
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) 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) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 7 with FirebaseAuthAnonymousUpgradeException

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

the class WelcomeBackPasswordHandlerTest method verifyMergeFailure.

private void verifyMergeFailure() {
    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());
    FirebaseAuthAnonymousUpgradeException e = ((FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException());
    EmailAuthCredential responseCredential = (EmailAuthCredential) e.getResponse().getCredentialForLinking();
// TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
// assertThat(responseCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
// assertThat(responseCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)

Example 8 with FirebaseAuthAnonymousUpgradeException

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

the class LinkingSocialProviderResponseHandlerTest method testSignIn_anonymousUpgradeEnabledWithSameIdp_expectMergeFailure.

@Test
public void testSignIn_anonymousUpgradeEnabledWithSameIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    // Fake social response from Google
    IdpResponse response = new IdpResponse.Builder(new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).setToken(TestConstants.TOKEN).build();
    mHandler.startSignIn(response);
    // Since we are signing in with the same IDP and anonymous upgrade is enabled, a merge
    // failure should occur without any RPC calls
    AuthCredential credential = GoogleAuthProvider.getCredential(TestConstants.TOKEN, null);
    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());
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    GoogleAuthCredential responseCredential = (GoogleAuthCredential) e.getResponse().getCredentialForLinking();
    assertThat(responseCredential.getProvider()).isEqualTo(credential.getProvider());
    assertThat(responseCredential.getSignInMethod()).isEqualTo(credential.getSignInMethod());
}
Also used : GoogleAuthCredential(com.google.firebase.auth.GoogleAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) GoogleAuthCredential(com.google.firebase.auth.GoogleAuthCredential) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 9 with FirebaseAuthAnonymousUpgradeException

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

the class LinkingSocialProviderResponseHandlerTest method testSignIn_anonymousUpgradeEnabledWithDifferentIdp_expectMergeFailure.

@Test
public void testSignIn_anonymousUpgradeEnabledWithDifferentIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    // We're going to fake a sign in with facebook, where the email belongs
    // to an existing account with a Google provider.
    // We need to link Facebook to this account, and then a merge failure should occur
    // so that the developer can handle it.
    // Before we can link, they need to sign in with Google to prove they own the account.
    // Fake social response from Google
    IdpResponse response = new IdpResponse.Builder(new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).setToken(TestConstants.TOKEN).build();
    // Set facebook credential
    AuthCredential facebookAuthCredential = FacebookAuthProvider.getCredential(TestConstants.TOKEN);
    mHandler.setRequestedSignInCredentialForEmail(facebookAuthCredential, TestConstants.EMAIL);
    when(mScratchMockAuth.signInWithCredential(any(GoogleAuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Mock linking with Facebook to always work
    when(FakeAuthResult.INSTANCE.getUser().linkWithCredential(facebookAuthCredential)).thenReturn(new AutoContinueTask<>(FakeAuthResult.INSTANCE, FakeAuthResult.INSTANCE, true, null));
    mHandler.startSignIn(response);
    verify(mScratchMockAuth).signInWithCredential(any(GoogleAuthCredential.class));
    verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(facebookAuthCredential);
    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());
    // Merge failure should occur after successful linking
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    AuthCredential credential = ProviderUtils.getAuthCredential(response);
    GoogleAuthCredential responseCredential = (GoogleAuthCredential) e.getResponse().getCredentialForLinking();
    assertThat(responseCredential.getProvider()).isEqualTo(credential.getProvider());
    assertThat(responseCredential.getSignInMethod()).isEqualTo(credential.getSignInMethod());
}
Also used : GoogleAuthCredential(com.google.firebase.auth.GoogleAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) GoogleAuthCredential(com.google.firebase.auth.GoogleAuthCredential) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 10 with FirebaseAuthAnonymousUpgradeException

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

the class SocialProviderResponseHandlerTest method testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_expectMergeFailure.

@Test
public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResultObserver);
    setupAnonymousUpgrade();
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    // Case 1: Anon user signing in with a Google credential that belongs to an existing user.
    when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(new FakeSignInMethodQueryResult(Arrays.asList(GoogleAuthProvider.GOOGLE_SIGN_IN_METHOD, 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());
    FirebaseAuthAnonymousUpgradeException e = (FirebaseAuthAnonymousUpgradeException) resolveCaptor.getValue().getException();
    assertThat(e.getResponse().getCredentialForLinking()).isNotNull();
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) FakeSignInMethodQueryResult(com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Aggregations

FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)16 IdpResponse (com.firebase.ui.auth.IdpResponse)14 Resource (com.firebase.ui.auth.data.model.Resource)8 InOrder (org.mockito.InOrder)8 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)7 Test (org.junit.Test)7 ViewModelProvider (androidx.lifecycle.ViewModelProvider)6 AuthCredential (com.google.firebase.auth.AuthCredential)6 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)4 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 AuthUI (com.firebase.ui.auth.AuthUI)3 User (com.firebase.ui.auth.data.model.User)3 UserCancellationException (com.firebase.ui.auth.data.model.UserCancellationException)3 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)3 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)2 FirebaseAuthError (com.firebase.ui.auth.util.FirebaseAuthError)2 SocialProviderResponseHandler (com.firebase.ui.auth.viewmodel.idp.SocialProviderResponseHandler)2 FacebookAuthCredential (com.google.firebase.auth.FacebookAuthCredential)2 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)2