Search in sources :

Example 21 with Resource

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

the class AnonymousSignInHandlerTest method testStartSignIn_expectSuccess.

@Test
public void testStartSignIn_expectSuccess() {
    mHandler.getOperation().observeForever(mResponseObserver);
    when(mMockAuth.signInAnonymously()).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
    mHandler.startSignIn(mMockActivity);
    verify(mMockAuth).signInAnonymously();
    InOrder inOrder = inOrder(mResponseObserver);
    inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResponseObserver).onChanged(captor.capture());
    assertThat(captor.getValue().getState()).isEqualTo(State.SUCCESS);
    IdpResponse response = captor.getValue().getValue();
    assertThat(response.isNewUser()).isFalse();
}
Also used : InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 22 with Resource

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

the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedLinkPropagated.

@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedLinkPropagated() {
    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(AutoContinueTask.<AuthResult>forFailure(new Exception("FAILED")));
    mHandler.startSignIn();
    // Verify sign in was called
    verify(mScratchMockAuth).signInWithCredential(any(AuthCredential.class));
    // Validate linking was called
    verify(mMockAuthResult.getUser()).linkWithCredential(any(FacebookAuthCredential.class));
    // Validate that the data was cleared
    assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
    // Validate failure
    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();
}
Also used : FacebookAuthCredential(com.google.firebase.auth.FacebookAuthCredential) EmailAuthCredential(com.google.firebase.auth.EmailAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) 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) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 23 with Resource

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

the class EmailLinkSignInHandlerTest method testStartSignIn_normalFlowWithAnonymousUpgrade_expectSuccessfulMerge.

@Test
@SuppressWarnings("all")
public void testStartSignIn_normalFlowWithAnonymousUpgrade_expectSuccessfulMerge() {
    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(new AutoContinueTask<>(mMockAuthResult, mMockAuthResult, true, null));
    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());
    IdpResponse response = captor.getValue().getValue();
    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());
    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) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 24 with Resource

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

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

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