-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAuthTest.kt
112 lines (96 loc) · 3.73 KB
/
AuthTest.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
import android.net.Uri
import com.google.firebase.auth.FirebaseAuthInvalidUserException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Test
import java.util.UUID
class AuthTest : FirebaseTest() {
private val email = "email${UUID.randomUUID()}@example.com"
@Before
fun initialize() {
auth.apply {
useEmulator("localhost", 9099)
}
}
@Test
fun `should authenticate via anonymous auth`() =
runTest {
auth.signInAnonymously().await()
assertEquals(true, auth.currentUser?.isAnonymous)
}
@Test
fun `should create user via email and password`() =
runTest {
val createResult = auth.createUserWithEmailAndPassword(email, "test123").await()
assertNotEquals(null, createResult.user?.uid)
assertEquals(null, createResult.user?.displayName)
// assertEquals(null, createResult.user?.phoneNumber)
assertEquals(false, createResult.user?.isAnonymous)
assertEquals(email, createResult.user?.email)
assertNotEquals("", createResult.user!!.email)
val signInResult = auth.signInWithEmailAndPassword(email, "test123").await()
assertEquals(createResult.user?.uid, signInResult.user?.uid)
}
@Test
fun `should authenticate via email and password`() =
runTest {
auth.createUserWithEmailAndPassword(email, "test123").await()
auth.signInWithEmailAndPassword(email, "test123").await()
assertEquals(false, auth.currentUser?.isAnonymous)
}
/*@Test
fun `should authenticate via custom token`() =
runTest {
val user = auth.createUserWithEmailAndPassword(email, "test123").await()
auth
.signInWithCustomToken(
user.user
.getIdToken(false)
.await()
.token ?: "",
).await()
assertEquals(false, auth.currentUser?.isAnonymous)
}*/
@Test
fun `should update displayName and photoUrl`() =
runTest {
auth
.createUserWithEmailAndPassword(email, "test123")
.await()
.user
auth.currentUser
?.updateProfile(
com.google.firebase.auth.UserProfileChangeRequest
.Builder()
.setDisplayName("testDisplayName")
.setPhotoUri(Uri.parse("https://picsum.photos/100"))
.build(),
)?.await()
assertEquals("testDisplayName", auth.currentUser?.displayName)
assertEquals("https://picsum.photos/100", auth.currentUser?.photoUrl)
}
@Test
fun `should sign in anonymously`() =
runTest {
val signInResult = auth.signInAnonymously().await()
assertNotEquals("", signInResult.user!!.email)
assertEquals(true, signInResult.user?.isAnonymous)
}
@Test
fun `should throw exception on invalid password`() =
runTest {
auth.createUserWithEmailAndPassword(email, "test123").await()
val exception =
assertThrows(FirebaseAuthInvalidUserException::class.java) {
runBlocking {
auth.signInWithEmailAndPassword(email, "wrongpassword").await()
}
}
assertEquals("INVALID_LOGIN_CREDENTIALS", exception.errorCode)
}
}