Skip to content

Commit cbe4e36

Browse files
committed
fix and standardize tests
1 parent 32381e2 commit cbe4e36

File tree

6 files changed

+85
-157
lines changed

6 files changed

+85
-157
lines changed

src/test/kotlin/AuthTest.kt

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import com.google.firebase.FirebasePlatform
55
import com.google.firebase.initialize
66
import org.junit.Test
77

8-
class AppTest : FirebaseTest() {
8+
class FirebaseAppTest : FirebaseTest() {
99
@Test
10-
fun testInitialize() {
10+
fun `initialize firebase`() {
1111
FirebasePlatform.initializeFirebasePlatform(
1212
object : FirebasePlatform() {
1313
val storage = mutableMapOf<String, String>()
@@ -36,6 +36,6 @@ class AppTest : FirebaseTest() {
3636
.setStorageBucket("fir-java-sdk.appspot.com")
3737
.setGcmSenderId("341458593155")
3838
.build()
39-
val app = Firebase.initialize(Application(), options)
39+
Firebase.initialize(Application(), options)
4040
}
4141
}
Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
21
import 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
45
import kotlinx.coroutines.tasks.await
56
import kotlinx.coroutines.test.runTest
67
import org.junit.Assert.assertEquals
78
import org.junit.Assert.assertNotEquals
9+
import org.junit.Assert.assertThrows
10+
import org.junit.Before
811
import org.junit.Test
912
import 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
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import kotlinx.coroutines.test.runTest
55
import org.junit.Assert.assertEquals
66
import org.junit.Test
77

8-
class FirestoreTest : FirebaseTest() {
8+
class FirebaseFirestoreTest : FirebaseTest() {
99

1010
@Test
11-
fun testFirestore(): Unit = runTest {
11+
fun `set and get a document`(): Unit = runTest {
1212
val firestore = Firebase.firestore(app)
1313
firestore.disableNetwork().await()
1414

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import com.google.firebase.storage.storage
55
import org.junit.Assert
66
import org.junit.Test
77

8-
class FirestoreStorageTest : FirebaseTest() {
8+
class FirebaseStorageTest : FirebaseTest() {
99

1010
@Test
11-
fun test_parsing_storage_uri() {
11+
fun `parsing storage uri`() {
1212
val input = "gs://edifikana-stage.appspot.com"
1313

1414
val normalized = Slashes.normalizeSlashes(input.substring(5))
@@ -19,19 +19,19 @@ class FirestoreStorageTest : FirebaseTest() {
1919
}
2020

2121
@Test
22-
fun test_loading_default_storage_client() {
22+
fun `loading default storage client`() {
2323
Firebase.storage(app)
2424
}
2525

2626
@Test
27-
fun test_getting_root_reference() {
27+
fun `getting root reference`() {
2828
val storage = Firebase.storage(app)
2929
val reference = storage.reference
3030
Assert.assertNotNull(reference)
3131
}
3232

3333
@Test
34-
fun test_getting_child_reference() {
34+
fun `getting child reference`() {
3535
val storage = Firebase.storage(app)
3636
val reference = storage.reference
3737
val downloadRef = reference.child("mountains.jpg")

src/test/kotlin/FirebaseTest.kt

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import com.google.firebase.Firebase
33
import com.google.firebase.FirebaseApp
44
import com.google.firebase.FirebaseOptions
55
import com.google.firebase.FirebasePlatform
6-
import com.google.firebase.auth.FirebaseAuth
76
import com.google.firebase.initialize
87
import org.junit.After
98
import org.junit.Before
109
import java.io.File
1110

1211
abstract class FirebaseTest {
13-
protected lateinit var auth: FirebaseAuth
1412

15-
protected val app: FirebaseApp get() {
13+
protected val app: FirebaseApp by lazy {
1614
val options =
1715
FirebaseOptions
1816
.Builder()
@@ -24,7 +22,7 @@ abstract class FirebaseTest {
2422
.setGcmSenderId("341458593155")
2523
.build()
2624

27-
return Firebase.initialize(Application(), options)
25+
Firebase.initialize(Application(), options)
2826
}
2927

3028
@Before
@@ -49,25 +47,10 @@ abstract class FirebaseTest {
4947
override fun getDatabasePath(name: String) = File("./build/$name")
5048
}
5149
)
52-
val options =
53-
FirebaseOptions
54-
.Builder()
55-
.setProjectId("fir-java-sdk")
56-
.setApplicationId("1:341458593155:web:bf8e1aa37efe01f32d42b6")
57-
.setApiKey("AIzaSyCvVHjTJHyeStnzIE7J9LLtHqWk6reGM08")
58-
.setDatabaseUrl("https://fir-java-sdk-default-rtdb.firebaseio.com")
59-
.setStorageBucket("fir-java-sdk.appspot.com")
60-
.setGcmSenderId("341458593155")
61-
.build()
62-
63-
val firebaseApp = Firebase.initialize(Application(), options)
64-
auth = FirebaseAuth.getInstance(app = firebaseApp)
65-
66-
FirebaseApp.clearInstancesForTest()
6750
}
6851

6952
@After
7053
fun clear() {
71-
auth.currentUser?.delete()
54+
FirebaseApp.clearInstancesForTest()
7255
}
7356
}

0 commit comments

Comments
 (0)