Search in sources :

Example 51 with IdpResponse

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

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlow_expectSuccessfulLink.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlow_expectSuccessfulLink() {
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
    mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
    when(mMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(mMockAuthResult));
    // Mock linking with Facebook to always work
    when(mMockAuthResult.getUser().linkWithCredential(any(FacebookAuthCredential.class))).thenReturn(new AutoContinueTask<>(mMockAuthResult, mMockAuthResult, true, null));
    mHandler.startSignIn();
    // Validate regular sign in
    ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
    verify(mMockAuth).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());
    IdpResponse response = captor.getValue().getValue();
    assertThat(captor.getValue().getState()).isEqualTo(State.SUCCESS);
    assertThat(response.getUser().getProviderId()).isEqualTo(AuthUI.EMAIL_LINK_PROVIDER);
    assertThat(response.getUser().getEmail()).isEqualTo(mMockAuthResult.getUser().getEmail());
    assertThat(response.getUser().getName()).isEqualTo(mMockAuthResult.getUser().getDisplayName());
    assertThat(response.getUser().getPhotoUri()).isEqualTo(mMockAuthResult.getUser().getPhotoUrl());
}
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) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 52 with IdpResponse

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

the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithForceSameDeviceTrue_expectWrongDeviceError.

@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithForceSameDeviceTrue_expectWrongDeviceError() {
    String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
    initializeHandlerWithSessionInfo(differentSessionId, null, null, true);
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    mHandler.startSignIn();
    verify(mMockAuth).isSignInWithEmailLink(any(String.class));
    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());
    FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
    assertThat(exception).isNotNull();
    assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 53 with IdpResponse

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

the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectPromptForEmailError.

@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectPromptForEmailError() {
    String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
    initializeHandlerWithSessionInfo(differentSessionId, null, null, false);
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
    when(mMockAuth.checkActionCode(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(mMockActionCodeResult));
    mHandler.startSignIn();
    verify(mMockAuth).isSignInWithEmailLink(any(String.class));
    verify(mMockAuth).checkActionCode(any(String.class));
    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());
    FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
    assertThat(exception).isNotNull();
    assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.EMAIL_LINK_PROMPT_FOR_EMAIL_ERROR);
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 54 with IdpResponse

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

the class GenericIdpSignInHandlerTest method testStartSignIn_anonymousUpgradeFlow_expectSuccess.

@Test
public void testStartSignIn_anonymousUpgradeFlow_expectSuccess() {
    setupAnonymousUpgrade();
    when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider(any(Activity.class), any(OAuthProvider.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    mockOAuthProvider(MICROSOFT_PROVIDER);
    mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER);
    ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class);
    verify(mMockAuth.getCurrentUser()).startActivityForLinkWithProvider(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>> resourceCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(resourceCaptor.capture());
    IdpResponse returnedIdpResponse = resourceCaptor.getValue().getValue();
    assertThat(returnedIdpResponse.isSuccessful()).isTrue();
    assertThat(returnedIdpResponse.getUser()).isNotNull();
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) Activity(android.app.Activity) OAuthProvider(com.google.firebase.auth.OAuthProvider) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 55 with IdpResponse

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

the class PhoneProviderResponseHandlerTest method testSignIn_autoUpgradeAnonymousEnabledWithExistingUser_expectMergeFailure.

@Test
public void testSignIn_autoUpgradeAnonymousEnabledWithExistingUser_expectMergeFailure() {
    mHandler.getOperation().observeForever(mResponseObserver);
    setupAnonymousUpgrade();
    FirebaseAuthUserCollisionException ex = new FirebaseAuthUserCollisionException("foo", "bar");
    TestHelper.setPrivateField(ex, FirebaseAuthUserCollisionException.class, AuthCredential.class, mCredential);
    when(mMockAuth.getCurrentUser().linkWithCredential(mCredential)).thenReturn(AutoCompleteTask.forFailure(ex));
    IdpResponse response = new IdpResponse.Builder(new User.Builder(PhoneAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()).build();
    mHandler.startSignIn(mCredential, response);
    verify(mMockAuth.getCurrentUser()).linkWithCredential(mCredential);
    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();
    assertThat(e.getResponse().getCredentialForLinking()).isNotNull();
}
Also used : 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) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) 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