forked from GitLiveApp/firebase-kotlin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.kt
147 lines (122 loc) · 5.28 KB
/
auth.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.gitlive.firebase.auth
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseOptions
import dev.gitlive.firebase.apps
import dev.gitlive.firebase.initialize
import dev.gitlive.firebase.runBlockingTest
import dev.gitlive.firebase.runTest
import kotlin.random.Random
import kotlin.test.*
expect val emulatorHost: String
expect val context: Any
expect annotation class IgnoreForAndroidUnitTest()
@IgnoreForAndroidUnitTest
class FirebaseAuthTest {
lateinit var auth: FirebaseAuth
@BeforeTest
fun initializeFirebase() {
val app = Firebase.apps(context).firstOrNull() ?: Firebase.initialize(
context,
FirebaseOptions(
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
storageBucket = "fir-kotlin-sdk.appspot.com",
projectId = "fir-kotlin-sdk",
gcmSenderId = "846484016111"
)
)
auth = Firebase.auth(app).apply {
useEmulator(emulatorHost, 9099)
}
}
@AfterTest
fun deinitializeFirebase() = runBlockingTest {
Firebase.apps(context).forEach {
it.delete()
}
}
@Test
fun testSignInWithUsernameAndPassword() = runTest {
val uid = getTestUid("[email protected]", "test123")
val result = auth.signInWithEmailAndPassword("[email protected]", "test123")
assertEquals(uid, result.user!!.uid)
}
@Test
fun testCreateUserWithEmailAndPassword() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)
assertEquals(null, createResult.user?.displayName)
assertEquals(null, createResult.user?.phoneNumber)
assertEquals(email, createResult.user?.email)
val signInResult = auth.signInWithEmailAndPassword(email, "test123")
assertEquals(createResult.user?.uid, signInResult.user?.uid)
signInResult.user!!.delete()
}
@Test
fun testFetchSignInMethods() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
var signInMethodResult = auth.fetchSignInMethodsForEmail(email)
assertEquals(emptyList(), signInMethodResult)
auth.createUserWithEmailAndPassword(email, "test123")
signInMethodResult = auth.fetchSignInMethodsForEmail(email)
assertEquals(listOf("password"), signInMethodResult)
auth.signInWithEmailAndPassword(email, "test123").user!!.delete()
}
@Test
fun testSendEmailVerification() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)
createResult.user!!.sendEmailVerification()
createResult.user!!.delete()
}
@Test
fun sendPasswordResetEmail() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)
auth.sendPasswordResetEmail(email)
createResult.user!!.delete()
}
@Test
fun testSignInWithCredential() = runTest {
val uid = getTestUid("[email protected]", "test123")
val credential = EmailAuthProvider.credential("[email protected]", "test123")
val result = auth.signInWithCredential(credential)
assertEquals(uid, result.user!!.uid)
}
@Test
fun testIsSignInWithEmailLink() {
val validLink = "http://localhost:9099/emulator/action?mode=signIn&lang=en&oobCode=_vr0QcFcxcVeLZbrcU-GpTaZiuxlHquqdC8MSy0YM_vzWCTAQgV9Jq&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
val invalidLink = "http://localhost:9099/emulator/action?mode=signIn&lang=en&&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
assertTrue(auth.isSignInWithEmailLink(validLink))
assertFalse(auth.isSignInWithEmailLink(invalidLink))
}
@Test
fun testCredentialWithLink() {
val link = "http://localhost:9099/emulator/action?mode=signIn&lang=en&oobCode=_vr0QcFcxcVeLZbrcU-GpTaZiuxlHquqdC8MSy0YM_vzWCTAQgV9Jq&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
val email = "[email protected]"
val credential = EmailAuthProvider.credentialWithLink(email, link)
assertEquals("password", credential.providerId)
}
private suspend fun getTestUid(email: String, password: String): String {
val uid = auth.let {
val user = try {
it.createUserWithEmailAndPassword(email, password).user
} catch (e: FirebaseAuthUserCollisionException) {
// the user already exists, just sign in for getting user's ID
it.signInWithEmailAndPassword(email, password).user
}
user!!.uid
}
check(auth.currentUser != null)
auth.signOut()
check(auth.currentUser == null)
return uid
}
}