1-
21import android.net.Uri
3- import com.google.firebase.auth.FirebaseUser
2+ import com.google.firebase.auth.FirebaseAuth
3+ import com.google.firebase.auth.FirebaseAuthInvalidUserException
4+ import kotlinx.coroutines.runBlocking
45import kotlinx.coroutines.tasks.await
56import kotlinx.coroutines.test.runTest
67import org.junit.Assert.assertEquals
78import org.junit.Assert.assertNotEquals
9+ import org.junit.Assert.assertThrows
10+ import org.junit.Before
811import org.junit.Test
912import java.util.UUID
1013
11- internal class FirebaseAuthTest : FirebaseTest () {
14+ class FirebaseAuthTest : FirebaseTest () {
15+ private val email = " email${UUID .randomUUID()} @example.com"
16+ private val auth by lazy { FirebaseAuth .getInstance(app) }
17+
18+ @Before
19+ fun initialize () {
20+ auth.apply {
21+ useEmulator(" localhost" , 9099 )
22+ }
23+ }
24+
1225 @Test
13- fun testCreateUserWithEmailAndPassword () =
26+ fun `should authenticate via anonymous auth` () =
27+ runTest {
28+ auth.signInAnonymously().await()
29+ assertEquals(null , auth.currentUser!! .email)
30+ assertEquals(true , auth.currentUser!! .isAnonymous)
31+ }
32+
33+ @Test
34+ fun `should create user via email and password` () =
1435 runTest {
15- val email = " test+${UUID .randomUUID()} @test.com"
1636 val createResult = auth.createUserWithEmailAndPassword(email, " test123" ).await()
1737 assertNotEquals(null , createResult.user?.uid)
18- // assertEquals(null, createResult.user?.displayName)
38+ assertEquals(null , createResult.user?.displayName)
1939 // assertEquals(null, createResult.user?.phoneNumber)
2040 assertEquals(false , createResult.user?.isAnonymous)
2141 assertEquals(email, createResult.user?.email)
@@ -26,10 +46,38 @@ internal class FirebaseAuthTest : FirebaseTest() {
2646 }
2747
2848 @Test
29- fun testUpdateProfile () =
49+ fun `should authenticate via email and password` () =
50+ runTest {
51+ auth.createUserWithEmailAndPassword(email, " test123" ).await()
52+
53+ auth.signInWithEmailAndPassword(email, " test123" ).await()
54+
55+ assertEquals(false , auth.currentUser?.isAnonymous)
56+ }
57+
58+ /* @Test
59+ fun `should authenticate via custom token`() =
60+ runTest {
61+ val user = auth.createUserWithEmailAndPassword(email, "test123").await()
62+ auth
63+ .signInWithCustomToken(
64+ user.user
65+ .getIdToken(false)
66+ .await()
67+ .token ?: "",
68+ ).await()
69+
70+ assertEquals(false, auth.currentUser?.isAnonymous)
71+ }*/
72+
73+ @Test
74+ fun `should update displayName and photoUrl` () =
3075 runTest {
31- val user = createUser()
32- user
76+ auth
77+ .createUserWithEmailAndPassword(email, " test123" )
78+ .await()
79+ .user
80+ auth.currentUser
3381 ?.updateProfile(
3482 com.google.firebase.auth.UserProfileChangeRequest
3583 .Builder ()
@@ -42,16 +90,25 @@ internal class FirebaseAuthTest : FirebaseTest() {
4290 }
4391
4492 @Test
45- fun testSignInAnonymously () =
93+ fun `should sign in anonymously` () =
4694 runTest {
4795 val signInResult = auth.signInAnonymously().await()
4896 assertNotEquals(" " , signInResult.user!! .email)
4997 assertEquals(true , signInResult.user?.isAnonymous)
5098 }
5199
52- private suspend fun createUser (email : String = "test+${UUID .randomUUID()}@test.com"): FirebaseUser ? =
53- auth
54- .createUserWithEmailAndPassword(email, " test123" )
55- .await()
56- .user
100+ @Test
101+ fun `should throw exception on invalid password` () =
102+ runTest {
103+ auth.createUserWithEmailAndPassword(email, " test123" ).await()
104+
105+ val exception =
106+ assertThrows(FirebaseAuthInvalidUserException ::class .java) {
107+ runBlocking {
108+ auth.signInWithEmailAndPassword(email, " wrongpassword" ).await()
109+ }
110+ }
111+
112+ assertEquals(" INVALID_PASSWORD" , exception.errorCode)
113+ }
57114}
0 commit comments