Search in sources :

Example 26 with IdpResponse

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

the class EmailLinkSignInHandlerTest method testStartSignIn_normalFlowWithAnonymousUpgrade_expectMergeFailure.

@Test
@SuppressWarnings("all")
public void testStartSignIn_normalFlowWithAnonymousUpgrade_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);
    when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.<AuthResult>forFailure(new FirebaseAuthUserCollisionException("foo", "bar")));
    mHandler.startSignIn();
    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();
    FirebaseAuthAnonymousUpgradeException mergeException = (FirebaseAuthAnonymousUpgradeException) captor.getValue().getException();
    assertThat(mergeException.getResponse().getCredentialForLinking()).isNotNull();
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) 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)

Example 27 with IdpResponse

use of com.firebase.ui.auth.IdpResponse 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 28 with IdpResponse

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

the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure.

@Test
public void testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure() {
    setupAnonymousUpgrade();
    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);
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Mock linking with Facebook to always work
    when(FakeAuthResult.INSTANCE.getUser().linkWithCredential(credential)).thenReturn(new AutoContinueTask<>(FakeAuthResult.INSTANCE, FakeAuthResult.INSTANCE, true, null));
    // Kick off the sign in flow
    mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, credential);
    // Verify that signInWithCredential was called
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    EmailAuthCredential capturedCredential = credentialCaptor.getValue();
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
    // assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
    // Verify that account linking is attempted
    verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(credential);
    verifyMergeFailure();
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 29 with IdpResponse

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

the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure.

@Test
public void testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure() {
    setupAnonymousUpgrade();
    mHandler.getOperation().observeForever(mResponseObserver);
    // Fake user with password provider
    User user = new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
    IdpResponse response = new IdpResponse.Builder(user).build();
    // Need to control FirebaseAuth's return values
    AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
    authOperationManager.mScratchAuth = mScratchMockAuth;
    when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    // Kick off the sign in flow
    mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, null);
    // Verify that signInWithCredential was called
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
    EmailAuthCredential capturedCredential = credentialCaptor.getValue();
    // TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
    // assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
    // assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
    verifyMergeFailure();
}
Also used : EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) User(com.firebase.ui.auth.data.model.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 30 with IdpResponse

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

the class EmailActivityTest method testOnCreate_emailLinkLinkingFlow_expectSendEmailLinkFlowStarted.

@Test
public void testOnCreate_emailLinkLinkingFlow_expectSendEmailLinkFlowStarted() {
    // This is normally done by EmailLinkSendEmailHandler, saving the IdpResponse is done
    // in EmailActivity but it will not be saved if we haven't previously set the email
    EmailLinkPersistenceManager.getInstance().saveEmail(ApplicationProvider.getApplicationContext(), EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    EmailActivity emailActivity = createActivity(AuthUI.EMAIL_LINK_PROVIDER, true, false);
    EmailLinkFragment fragment = (EmailLinkFragment) emailActivity.getSupportFragmentManager().findFragmentByTag(EmailLinkFragment.TAG);
    assertThat(fragment).isNotNull();
    EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
    IdpResponse response = persistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext()).getIdpResponseForLinking();
    assertThat(response.getProviderType()).isEqualTo(GoogleAuthProvider.PROVIDER_ID);
    assertThat(response.getEmail()).isEqualTo(EMAIL);
    assertThat(response.getIdpToken()).isEqualTo(ID_TOKEN);
    assertThat(response.getIdpSecret()).isEqualTo(SECRET);
}
Also used : EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

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