@@ -25,23 +25,25 @@ import com.google.firebase.FirebaseOptions
25
25
import com.google.firebase.auth.FirebaseAuth
26
26
import com.google.firebase.auth.FirebaseAuthRecentLoginRequiredException
27
27
import com.google.firebase.auth.FirebaseUser
28
- import com.google.android.gms.tasks.Task
29
28
import com.google.android.gms.tasks.TaskCompletionSource
29
+ import com.google.firebase.auth.AuthCredential
30
30
import com.google.firebase.auth.AuthResult
31
- import com.google.firebase.auth.actionCodeSettings
31
+ import com.google.firebase.auth.EmailAuthProvider
32
32
import kotlinx.coroutines.CancellationException
33
33
import kotlinx.coroutines.flow.first
34
34
import kotlinx.coroutines.test.runTest
35
35
import org.junit.After
36
36
import org.junit.Before
37
37
import org.junit.Test
38
38
import org.junit.runner.RunWith
39
+ import org.mockito.ArgumentMatchers
39
40
import org.mockito.Mock
40
41
import org.mockito.Mockito.`when`
41
42
import org.mockito.Mockito.mock
42
43
import org.mockito.Mockito.verify
43
44
import org.mockito.Mockito.doNothing
44
45
import org.mockito.Mockito.doThrow
46
+ import org.mockito.Mockito.mockStatic
45
47
import org.mockito.MockitoAnnotations
46
48
import org.robolectric.RobolectricTestRunner
47
49
import org.robolectric.annotation.Config
@@ -70,7 +72,7 @@ class FirebaseAuthUITest {
70
72
FirebaseAuthUI .clearInstanceCache()
71
73
72
74
// Clear any existing Firebase apps
73
- val context = ApplicationProvider .getApplicationContext< android.content. Context > ()
75
+ val context = ApplicationProvider .getApplicationContext<Context >()
74
76
FirebaseApp .getApps(context).forEach { app ->
75
77
app.delete()
76
78
}
@@ -534,7 +536,7 @@ class FirebaseAuthUITest {
534
536
// =============================================================================================
535
537
536
538
@Test
537
- fun `Create or link user with email and password without anonymous upgrade should succeed` () =
539
+ fun `Create user with email and password without anonymous upgrade should succeed` () =
538
540
runTest {
539
541
val applicationContext = ApplicationProvider .getApplicationContext<Context >()
540
542
val mockUser = mock(FirebaseUser ::class .java)
@@ -573,4 +575,57 @@ class FirebaseAuthUITest {
573
575
val successState = authState as AuthState .Success
574
576
assertThat(successState.user.email).isEqualTo(
" [email protected] " )
575
577
}
578
+
579
+ @Test
580
+ fun `Link user with email and password with anonymous upgrade should succeed` () = runTest {
581
+ mockStatic(EmailAuthProvider ::class .java).use { mockedProvider ->
582
+ val applicationContext = ApplicationProvider .getApplicationContext<Context >()
583
+ val mockCredential = mock(AuthCredential ::class .java)
584
+ mockedProvider.`when `<AuthCredential > {
585
+ EmailAuthProvider .getCredential(
" [email protected] " ,
" Pass@123" )
586
+ }.thenReturn(mockCredential)
587
+ val mockAnonymousUser = mock(FirebaseUser ::class .java)
588
+ `
when `(mockAnonymousUser.email).thenReturn(
" [email protected] " )
589
+ `when `(mockAnonymousUser.isAnonymous).thenReturn(true )
590
+ `when `(mockFirebaseAuth.currentUser).thenReturn(mockAnonymousUser)
591
+ val taskCompletionSource = TaskCompletionSource <AuthResult >()
592
+ taskCompletionSource.setResult(null )
593
+ `when `(
594
+ mockFirebaseAuth.currentUser?.linkWithCredential(
595
+ ArgumentMatchers .any(AuthCredential ::class .java)
596
+ )
597
+ ).thenReturn(taskCompletionSource.task)
598
+ val instance = FirebaseAuthUI .create(defaultApp, mockFirebaseAuth)
599
+ val emailProvider = AuthProvider .Email (
600
+ actionCodeSettings = null ,
601
+ passwordValidationRules = emptyList()
602
+ )
603
+ val config = authUIConfiguration {
604
+ context = applicationContext
605
+ providers {
606
+ provider(emailProvider)
607
+ }
608
+ isAnonymousUpgradeEnabled = true
609
+ }
610
+
611
+ instance.createOrLinkUserWithEmailAndPassword(
612
+ config = config,
613
+ provider = emailProvider,
614
+
615
+ password = " Pass@123"
616
+ )
617
+
618
+ mockedProvider.verify {
619
+ EmailAuthProvider .getCredential(
" [email protected] " ,
" Pass@123" )
620
+ }
621
+ // Verify linkWithCredential was called with the mock credential
622
+ verify(mockAnonymousUser).linkWithCredential(mockCredential)
623
+
624
+ val authState = instance.authStateFlow().first()
625
+ assertThat(authState)
626
+ .isEqualTo(AuthState .Success (result = null , user = mockAnonymousUser))
627
+ val successState = authState as AuthState .Success
628
+ assertThat(successState.user.email).isEqualTo(
" [email protected] " )
629
+ }
630
+ }
576
631
}
0 commit comments