Search in sources :

Example 16 with Resource

use of com.firebase.ui.auth.data.model.Resource in project FirebaseUI-Android by firebase.

the class EmailLinkSendEmailHandlerTest method testSendSignInLinkToEmail_normalFlow_expectFailure.

@Test
@SuppressWarnings("unchecked")
public void testSendSignInLinkToEmail_normalFlow_expectFailure() {
    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()));
    mHandler.sendSignInLinkToEmail(TestConstants.EMAIL, actionCodeSettings, null, forceSameDevice);
    ArgumentCaptor<ActionCodeSettings> acsCaptor = ArgumentCaptor.forClass(ActionCodeSettings.class);
    verify(mMockAuth).sendSignInLinkToEmail(eq(TestConstants.EMAIL), acsCaptor.capture());
    assertThat(acsCaptor.getValue()).isNotEqualTo(actionCodeSettings);
    validateSessionIdAddedToContinueUrl(acsCaptor.getValue(), null, 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) Test(org.junit.Test)

Example 17 with Resource

use of com.firebase.ui.auth.data.model.Resource in project FirebaseUI-Android by firebase.

the class EmailLinkSendEmailHandlerTest method testSendSignInLinkToEmail_normalFlow_expectSuccess.

@Test
@SuppressWarnings("unchecked")
public void testSendSignInLinkToEmail_normalFlow_expectSuccess() {
    mHandler.getOperation().observeForever(mResponseObserver);
    boolean forceSameDevice = false;
    ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder().setUrl(URL).build();
    when(mMockAuth.sendSignInLinkToEmail(any(String.class), any(ActionCodeSettings.class))).thenReturn(AutoCompleteTask.forSuccess(null));
    mHandler.sendSignInLinkToEmail(TestConstants.EMAIL, actionCodeSettings, null, forceSameDevice);
    ArgumentCaptor<ActionCodeSettings> acsCaptor = ArgumentCaptor.forClass(ActionCodeSettings.class);
    verify(mMockAuth).sendSignInLinkToEmail(eq(TestConstants.EMAIL), acsCaptor.capture());
    assertThat(acsCaptor.getValue()).isNotEqualTo(actionCodeSettings);
    validateSessionIdAddedToContinueUrl(acsCaptor.getValue(), null, forceSameDevice);
    String email = mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext()).getEmail();
    assertThat(email).isNotNull();
    assertThat(email).isEqualTo(TestConstants.EMAIL);
    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.SUCCESS);
    assertThat(captor.getValue().getValue()).isEqualTo(TestConstants.EMAIL);
}
Also used : InOrder(org.mockito.InOrder) ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) Resource(com.firebase.ui.auth.data.model.Resource) Test(org.junit.Test)

Example 18 with Resource

use of com.firebase.ui.auth.data.model.Resource 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 19 with Resource

use of com.firebase.ui.auth.data.model.Resource 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 20 with Resource

use of com.firebase.ui.auth.data.model.Resource 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

Resource (com.firebase.ui.auth.data.model.Resource)36 InOrder (org.mockito.InOrder)35 Test (org.junit.Test)34 IdpResponse (com.firebase.ui.auth.IdpResponse)28 AuthCredential (com.google.firebase.auth.AuthCredential)20 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)13 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)11 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)9 EmailAuthCredential (com.google.firebase.auth.EmailAuthCredential)9 FacebookAuthCredential (com.google.firebase.auth.FacebookAuthCredential)8 OAuthProvider (com.google.firebase.auth.OAuthProvider)7 Activity (android.app.Activity)6 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)6 AuthOperationManager (com.firebase.ui.auth.util.data.AuthOperationManager)5 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)3 ActionCodeSettings (com.google.firebase.auth.ActionCodeSettings)3 GoogleAuthCredential (com.google.firebase.auth.GoogleAuthCredential)3 FirebaseUiUserCollisionException (com.firebase.ui.auth.FirebaseUiUserCollisionException)2 User (com.firebase.ui.auth.data.model.User)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2