com.google.firebase.auth.FirebaseAuthUserCollisionException Java Examples
The following examples show how to use
com.google.firebase.auth.FirebaseAuthUserCollisionException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultErrorResolver.java From demo-firebase-android with The Unlicense | 5 votes |
@Override @Nullable protected String baseResolve(Throwable t) { if (t instanceof HttpException) { HttpException exception = (HttpException) t; switch (exception.code()) { case Const.Http.BAD_REQUEST: return "Bad Request"; case Const.Http.UNAUTHORIZED: return "Unauthorized"; case Const.Http.FORBIDDEN: return "Forbidden"; case Const.Http.NOT_ACCEPTABLE: return "Not acceptable"; case Const.Http.UNPROCESSABLE_ENTITY: return "Unprocessable entity"; case Const.Http.SERVER_ERROR: return "Server error"; default: return null; } } else if (t instanceof FirebaseAuthInvalidUserException) { return "This Id is not registered"; } else if (t instanceof FirebaseAuthInvalidCredentialsException) { return "Password is wrong"; } else if (t instanceof FirebaseAuthWeakPasswordException) { return "Password is not strong enough"; } else if (t instanceof FirebaseAuthInvalidCredentialsException) { return "Id is malformed"; } else if (t instanceof FirebaseAuthUserCollisionException) { return "User with current Id already exists"; } else { return null; } }
Example #2
Source File: PhoneProviderResponseHandler.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
public void startSignIn(@NonNull PhoneAuthCredential credential, @NonNull final IdpResponse response) { if (!response.isSuccessful()) { setResult(Resource.<IdpResponse>forFailure(response.getError())); return; } if (!response.getProviderType().equals(PhoneAuthProvider.PROVIDER_ID)) { throw new IllegalStateException( "This handler cannot be used without a phone response."); } setResult(Resource.<IdpResponse>forLoading()); AuthOperationManager.getInstance() .signInAndLinkWithCredential(getAuth(), getArguments(), credential) .addOnSuccessListener(new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult result) { handleSuccess(response, result); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { if (e instanceof FirebaseAuthUserCollisionException) { // With phone auth, this only happens if we are trying to upgrade // an anonymous account using a phone number that is already registered // on another account handleMergeFailure(((FirebaseAuthUserCollisionException) e).getUpdatedCredential()); } else { setResult(Resource.<IdpResponse>forFailure(e)); } } }); }
Example #3
Source File: SocialProviderResponseHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@Test public void testSignInIdp_resolution() { mHandler.getOperation().observeForever(mResultObserver); when(mMockAuth.signInWithCredential(any(AuthCredential.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure( new FirebaseAuthUserCollisionException("foo", "bar"))); when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))) .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess( new FakeSignInMethodQueryResult(Collections.singletonList( 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).signInWithCredential(any(AuthCredential.class)); verify(mMockAuth).fetchSignInMethodsForEmail(any(String.class)); InOrder inOrder = inOrder(mResultObserver); inOrder.verify(mResultObserver) .onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading())); ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class); inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture()); // Call activity result IntentRequiredException e = ((IntentRequiredException) resolveCaptor.getValue().getException()); mHandler.onActivityResult(e.getRequestCode(), Activity.RESULT_OK, response.toIntent()); // Make sure we get success inOrder.verify(mResultObserver) .onChanged(argThat(ResourceMatchers.<IdpResponse>isSuccess())); }
Example #4
Source File: SocialProviderResponseHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@Test public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingUserWithSameIdp_expectMergeFailure() { mHandler.getOperation().observeForever(mResultObserver); setupAnonymousUpgrade(); when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))) .thenReturn(AutoCompleteTask.<AuthResult>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.<SignInMethodQueryResult>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.<IdpResponse>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(); }
Example #5
Source File: PhoneProviderResponseHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@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.<AuthResult>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.<IdpResponse>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(); }
Example #6
Source File: GenericIdpSignInHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@Test public void testStartSignIn_normalSignInFlowWithRecoverableError_expectFailure() { AuthCredential credential = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN); FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar"); collisionException.zza(EMAIL).zza(credential); when(mMockAuth.startActivityForSignInWithProvider(any(Activity.class), any(OAuthProvider.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException)); mHandler.startSignIn(mMockAuth, mMockActivity, MICROSOFT_PROVIDER); ArgumentCaptor<OAuthProvider> providerCaptor = ArgumentCaptor.forClass(OAuthProvider.class); verify(mMockAuth).startActivityForSignInWithProvider(eq(mMockActivity), providerCaptor.capture()); assertThat(providerCaptor.getValue().getProviderId()).isEqualTo(MICROSOFT_PROVIDER); InOrder inOrder = inOrder(mResponseObserver); inOrder.verify(mResponseObserver) .onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading())); ArgumentCaptor<Resource<IdpResponse>> resourceCaptor = ArgumentCaptor.forClass(Resource.class); inOrder.verify(mResponseObserver) .onChanged(resourceCaptor.capture()); FirebaseUiUserCollisionException e = (FirebaseUiUserCollisionException) resourceCaptor.getValue().getException(); assertThat(e.getCredential()).isNotNull(); assertThat(e.getEmail()).isEqualTo(EMAIL); }
Example #7
Source File: GenericIdpSignInHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@Test public void testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableError() { setupAnonymousUpgrade(); AuthCredential credential = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN); FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar"); collisionException.zza(EMAIL).zza(credential); when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider( any(Activity.class), any(OAuthProvider.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException)); // Case 1: Anon user signing in with an existing account when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))) .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess( new FakeSignInMethodQueryResult(Arrays.asList( 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.<IdpResponse>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(); }
Example #8
Source File: EmailLinkSignInHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@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(); }
Example #9
Source File: EmailLinkSignInHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("all") public void testStartSignIn_linkingFlow_expectUserCollisionException() { 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(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(); FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) captor.getValue().getException(); assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())) .isNull(); }
Example #10
Source File: EmailLinkSignInHandler.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
private void handleNormalFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final String email, final String link) { final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(email, link); // Bug in core SDK - credential is mutated and won't be usable for sign in, so create // a new one to pass back. b/117425827 final AuthCredential emailLinkCredentialForLinking = EmailAuthProvider .getCredentialWithLink(email, link); // Either regular sign in or anonymous user upgrade authOperationManager.signInAndLinkWithCredential(getAuth(), getArguments(), emailLinkCredential) .addOnSuccessListener(new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { persistenceManager.clearAllData(getApplication()); FirebaseUser user = authResult.getUser(); IdpResponse response = new IdpResponse.Builder( new User.Builder(EMAIL_LINK_PROVIDER, user.getEmail()) .setName(user.getDisplayName()) .setPhotoUri(user.getPhotoUrl()) .build()) .build(); handleSuccess(response, authResult); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { persistenceManager.clearAllData(getApplication()); if (e instanceof FirebaseAuthUserCollisionException) { handleMergeFailure(emailLinkCredentialForLinking); } else { setResult(Resource.<IdpResponse>forFailure(e)); } } }); }
Example #11
Source File: GenericIdpSignInHandler.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
protected void handleNormalSignInFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider) { auth.startActivityForSignInWithProvider(activity, provider) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(@NonNull AuthResult authResult) { handleSuccess(provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential()); } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { if (e instanceof FirebaseAuthException) { FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) e); if (e instanceof FirebaseAuthUserCollisionException) { FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e; setResult(Resource.<IdpResponse>forFailure( new FirebaseUiUserCollisionException( ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), collisionException.getEmail(), collisionException.getUpdatedCredential()))); } else if (error == FirebaseAuthError.ERROR_WEB_CONTEXT_CANCELED) { setResult(Resource.<IdpResponse>forFailure( new UserCancellationException())); } else { setResult(Resource.<IdpResponse>forFailure(e)); } } else { setResult(Resource.<IdpResponse>forFailure(e)); } } }); }
Example #12
Source File: GenericIdpSignInHandler.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
private void handleAnonymousUpgradeFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider, final FlowParameters flowParameters) { auth.getCurrentUser() .startActivityForLinkWithProvider(activity, provider) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(@NonNull AuthResult authResult) { handleSuccess(provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential()); } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { if (!(e instanceof FirebaseAuthUserCollisionException)) { setResult(Resource.<IdpResponse>forFailure(e)); return; } FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e; final AuthCredential credential = collisionException.getUpdatedCredential(); final String email = collisionException.getEmail(); // Case 1: Anonymous user trying to link with an existing user // Case 2: Anonymous user trying to link with a provider keyed // by an email that already belongs to an existing account // (linking flow) ProviderUtils.fetchSortedProviders(auth, flowParameters, email) .addOnSuccessListener(new OnSuccessListener<List<String>>() { @Override public void onSuccess(List<String> providers) { if (providers.isEmpty()) { String errorMessage = "Unable to complete the linkingflow -" + " the user is using " + "unsupported providers."; setResult(Resource.<IdpResponse>forFailure( new FirebaseUiException( ErrorCodes.DEVELOPER_ERROR, errorMessage))); return; } if (providers.contains(provider.getProviderId())) { // Case 1 handleMergeFailure(credential); } else { // Case 2 - linking flow to be handled by // SocialProviderResponseHandler setResult(Resource.<IdpResponse>forFailure( new FirebaseUiUserCollisionException( ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), email, credential))); } } }); } }); }
Example #13
Source File: SocialProviderResponseHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
@Test public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingIdpUserWithDifferentIdp_expectMergeFailure() { mHandler.getOperation().observeForever(mResultObserver); setupAnonymousUpgrade(); when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure( new FirebaseAuthUserCollisionException("foo", "bar"))); // Case 2 & 3: trying to link with an account that has 1 idp, which is different from the // one that we're trying to log in with when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))) .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess( new FakeSignInMethodQueryResult(Collections.singletonList( 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.<IdpResponse>isLoading())); ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class); inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture()); // Make sure that we are trying to start the WelcomeBackIdpPrompt activity IntentRequiredException e = ((IntentRequiredException) resolveCaptor.getValue().getException()); assertThat(e.getIntent().getComponent().getClassName()) .isEqualTo(WelcomeBackIdpPrompt.class.toString().split(" ")[1]); assertThat(IdpResponse.fromResultIntent(e.getIntent())).isEqualTo(response); }
Example #14
Source File: SocialProviderResponseHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
@Test public void testSignInIdp_anonymousUserUpgradeEnabledAndExistingPasswordUserWithDifferentIdp_expectMergeFailure() { mHandler.getOperation().observeForever(mResultObserver); setupAnonymousUpgrade(); when(mMockAuth.getCurrentUser().linkWithCredential(any(AuthCredential.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure( new FirebaseAuthUserCollisionException("foo", "bar"))); // Case 2 & 3: trying to link with an account that has 1 password provider and logging in // with an idp that has the same email when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))) .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess( new FakeSignInMethodQueryResult(Collections.singletonList( EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)))); IdpResponse response = new IdpResponse.Builder(new User.Builder( FacebookAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build()) .setToken(TestConstants.TOKEN) .setSecret(TestConstants.SECRET) .build(); mHandler.startSignIn(response); verify(mMockAuth.getCurrentUser()).linkWithCredential(any(AuthCredential.class)); InOrder inOrder = inOrder(mResultObserver); inOrder.verify(mResultObserver) .onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading())); ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class); inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture()); // Make sure that we are trying to start the WelcomeBackIdpPrompt activity IntentRequiredException e = ((IntentRequiredException) resolveCaptor.getValue().getException()); assertThat(e.getIntent().getComponent().getClassName()) .isEqualTo(WelcomeBackPasswordPrompt.class.toString().split(" ")[1]); assertThat(IdpResponse.fromResultIntent(e.getIntent())).isEqualTo(response); }
Example #15
Source File: GenericIdpSignInHandlerTest.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
@Test public void testStartSignIn_anonymousUpgradeFlowWithConflict_expectRecoverableLinkingError() { setupAnonymousUpgrade(); AuthCredential credential = OAuthProvider.getCredential(MICROSOFT_PROVIDER, ID_TOKEN, ACCESS_TOKEN); FirebaseAuthUserCollisionException collisionException = new FirebaseAuthUserCollisionException("foo", "bar"); collisionException.zza(EMAIL).zza(credential); when(mMockAuth.getCurrentUser().startActivityForLinkWithProvider( any(Activity.class), any(OAuthProvider.class))) .thenReturn(AutoCompleteTask.<AuthResult>forFailure(collisionException)); // Case 2: Anonymous user trying to link with a provider keyed by an email that already // belongs to an existing account when(mMockAuth.fetchSignInMethodsForEmail(any(String.class))) .thenReturn(AutoCompleteTask.<SignInMethodQueryResult>forSuccess( new FakeSignInMethodQueryResult(Arrays.asList( GoogleAuthProvider.PROVIDER_ID)))); 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.<IdpResponse>isLoading())); ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class); inOrder.verify(mResponseObserver).onChanged(resolveCaptor.capture()); FirebaseUiUserCollisionException e = (FirebaseUiUserCollisionException) resolveCaptor.getValue().getException(); assertThat(e.getCredential()).isNotNull(); assertThat(e.getEmail()).isEqualTo(EMAIL); }