1
+ import android.app.Application
2
+ import com.google.firebase.Firebase
3
+ import com.google.firebase.FirebaseOptions
4
+ import com.google.firebase.FirebasePlatform
5
+ import com.google.firebase.auth.FirebaseAuth
6
+ import com.google.firebase.firestore.firestore
7
+ import com.google.firebase.initialize
8
+ import kotlinx.coroutines.tasks.await
9
+ import kotlinx.coroutines.test.runTest
10
+ import org.junit.Assert.assertArrayEquals
11
+ import org.junit.Assert.assertEquals
12
+ import org.junit.Assert.assertNotEquals
13
+ import org.junit.Before
14
+ import org.junit.Test
15
+ import java.io.File
16
+ import kotlin.random.Random
17
+
18
+ internal class FirebaseAuthTest : FirebaseTest () {
19
+
20
+ private lateinit var auth: FirebaseAuth
21
+
22
+ @Before
23
+ fun initialize () {
24
+ FirebasePlatform .initializeFirebasePlatform(object : FirebasePlatform () {
25
+ val storage = mutableMapOf<String , String >()
26
+ override fun store (key : String , value : String ) = storage.set(key, value)
27
+ override fun retrieve (key : String ) = storage[key]
28
+ override fun clear (key : String ) { storage.remove(key) }
29
+ override fun log (msg : String ) = println (msg)
30
+ override fun getDatabasePath (name : String ) = File (" ./build/$name " )
31
+ })
32
+ val options = FirebaseOptions .Builder ()
33
+ .setProjectId(" my-firebase-project" )
34
+ .setApplicationId(" 1:27992087142:android:ce3b6448250083d1" )
35
+ .setApiKey(" AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw" )
36
+ // setDatabaseURL(...)
37
+ // setStorageBucket(...)
38
+ .build()
39
+ val firebaseApp = Firebase .initialize(Application (), options)
40
+ auth = FirebaseAuth .getInstance(app = firebaseApp)
41
+ }
42
+
43
+ @Test
44
+ fun testCreateUserWithEmailAndPassword () = runTest {
45
+ val email = " test+${Random .nextInt(100000 )} @test.com"
46
+ val createResult = auth.createUserWithEmailAndPassword(
47
+ email,
48
+ " test123"
49
+ ).await()
50
+ assertNotEquals(null , createResult.user?.uid)
51
+ // assertEquals(null, createResult.user?.displayName)
52
+ // assertEquals(null, createResult.user?.phoneNumber)
53
+ assertEquals(false , createResult.user?.isAnonymous)
54
+ assertEquals(email, createResult.user?.email)
55
+
56
+ val signInResult = auth.signInWithEmailAndPassword(email, " test123" ).await()
57
+ assertEquals(createResult.user?.uid, signInResult.user?.uid)
58
+
59
+ signInResult.user!! .delete()
60
+ }
61
+
62
+ @Test
63
+ fun testSignInAnonymously () = runTest {
64
+ val signInResult = auth.signInAnonymously().await()
65
+ assertEquals(true , signInResult.user?.isAnonymous)
66
+ signInResult.user!! .delete()
67
+ }
68
+ }
0 commit comments