Skip to content

Commit f3b5b01

Browse files
committed
Playing with firestore testing
1 parent 2a8cd73 commit f3b5b01

File tree

11 files changed

+289
-0
lines changed

11 files changed

+289
-0
lines changed

Diff for: .firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "clean-notes-11971"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codingwithmitch.cleannotes.framework.datasource.network
2+
3+
import com.google.firebase.firestore.FirebaseFirestore
4+
import com.google.firebase.firestore.FirebaseFirestoreSettings
5+
6+
/**
7+
* Disable the use of production data.
8+
* If you don't use this, the tests will use production data and access the real firestore!
9+
*/
10+
class EmulatorSuite {
11+
12+
val firestoreSettings = FirebaseFirestoreSettings.Builder()
13+
.setHost("10.0.2.2:8080")
14+
.setSslEnabled(false)
15+
.setPersistenceEnabled(false)
16+
.build()
17+
18+
init {
19+
FirebaseFirestore.getInstance().firestoreSettings = firestoreSettings
20+
}
21+
22+
companion object{
23+
const val CLEAR_EMULATOR_DB = "http://localhost:8080/emulator/v1/projects/clean-notes-11971/databases/(default)/documents"
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.codingwithmitch.cleannotes.framework.datasource.network
2+
3+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
4+
import com.codingwithmitch.cleannotes.business.domain.model.NoteFactory
5+
import com.codingwithmitch.cleannotes.business.util.DateUtil
6+
import com.codingwithmitch.cleannotes.framework.datasource.network.implementation.NoteFirestoreServiceImpl.Companion.NOTES_COLLECTION
7+
import com.codingwithmitch.cleannotes.framework.datasource.network.implementation.NoteFirestoreServiceImpl.Companion.USER_ID
8+
import com.codingwithmitch.cleannotes.framework.datasource.network.mappers.NetworkMapper
9+
import com.codingwithmitch.cleannotes.framework.datasource.network.model.NoteNetworkEntity
10+
import com.google.android.gms.tasks.Task
11+
import com.google.firebase.Timestamp
12+
import com.google.firebase.firestore.FirebaseFirestore
13+
import com.google.firebase.firestore.FirebaseFirestoreSettings
14+
import com.google.firebase.firestore.QuerySnapshot
15+
import kotlinx.coroutines.runBlocking
16+
import kotlinx.coroutines.tasks.await
17+
import org.junit.Test
18+
import org.junit.runner.RunWith
19+
import java.text.SimpleDateFormat
20+
import java.util.*
21+
import kotlin.test.assertEquals
22+
23+
@RunWith(AndroidJUnit4ClassRunner::class)
24+
class FirestoreTest {
25+
26+
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
27+
private val dateUtil: DateUtil = DateUtil(dateFormat)
28+
private val noteFactory = NoteFactory(dateUtil)
29+
private val networkMapper = NetworkMapper(dateUtil)
30+
31+
val firestoreSettings = FirebaseFirestoreSettings.Builder()
32+
.setHost("10.0.2.2:8080")
33+
.setSslEnabled(false)
34+
.setPersistenceEnabled(false)
35+
.build()
36+
37+
val firestore = FirebaseFirestore.getInstance()
38+
39+
init {
40+
FirebaseFirestore.getInstance().firestoreSettings = firestoreSettings
41+
}
42+
43+
@Test
44+
fun firstTest() = runBlocking{
45+
46+
val note = noteFactory.createSingleNote(
47+
UUID.randomUUID().toString(),
48+
UUID.randomUUID().toString(),
49+
UUID.randomUUID().toString()
50+
)
51+
val entity = networkMapper.mapToEntity(note)
52+
53+
firestore
54+
.collection(NOTES_COLLECTION)
55+
.document(USER_ID)
56+
.collection(NOTES_COLLECTION)
57+
.document(entity.id)
58+
.set(entity)
59+
.addOnCompleteListener {
60+
println("test: inserted new note: ${entity.title}")
61+
}
62+
.addOnFailureListener {
63+
println("test: failure: ${it}")
64+
assertEquals(0, 1)
65+
}
66+
.await()
67+
68+
69+
val noteList = firestore
70+
.collection(NOTES_COLLECTION)
71+
.document(USER_ID)
72+
.collection(NOTES_COLLECTION)
73+
.get()
74+
.await()
75+
.toObjects(NoteNetworkEntity::class.java)
76+
77+
println("test: size: ${noteList.size}")
78+
for(noteEntity in noteList){
79+
println("test: ${noteEntity.title}")
80+
}
81+
}
82+
83+
}
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+

Diff for: app/src/debug/res/xml/network_security_config.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<network-security-config>
2+
<domain-config cleartextTrafficPermitted="true">
3+
<!-- cloud functions emulator -->
4+
<domain includeSubdomains="true">http://10.0.2.2:5001</domain>
5+
</domain-config>
6+
</network-security-config>

Diff for: app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
android:roundIcon="@mipmap/ic_launcher_round"
1313
android:supportsRtl="true"
1414
android:theme="@style/AppTheme"
15+
android:networkSecurityConfig="@xml/network_security_config"
1516
>
1617

1718
<activity

Diff for: app/src/release/xml/network_security_config.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
4+
</network-security-config>

Diff for: firebase-debug.log

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[debug] [2020-04-28T00:43:22.285Z] ----------------------------------------------------------------------
2+
[debug] [2020-04-28T00:43:22.287Z] Command: C:\Program Files\nodejs\node.exe C:\Users\mitch\AppData\Roaming\npm\node_modules\firebase-tools\lib\bin\firebase.js emulators:start --only firestore,functions
3+
[debug] [2020-04-28T00:43:22.287Z] CLI Version: 8.2.0
4+
[debug] [2020-04-28T00:43:22.288Z] Platform: win32
5+
[debug] [2020-04-28T00:43:22.288Z] Node Version: v12.16.2
6+
[debug] [2020-04-28T00:43:22.288Z] Time: Mon Apr 27 2020 17:43:22 GMT-0700 (Pacific Daylight Time)
7+
[debug] [2020-04-28T00:43:22.288Z] ----------------------------------------------------------------------
8+
[debug] [2020-04-28T00:43:22.289Z]
9+
[debug] [2020-04-28T00:43:22.294Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
10+
[debug] [2020-04-28T00:43:22.295Z] > authorizing via signed-in user
11+
[info] i emulators: Starting emulators: firestore
12+
[warn] ! functions: Not starting the functions emulator, make sure you have run firebase init.
13+
[debug] [2020-04-28T00:43:24.338Z] [hub] writing locator at C:\Users\mitch\AppData\Local\Temp\hub-clean-notes-11971.json
14+
[info] + hub: emulator hub started at http://localhost:4400
15+
[debug] [2020-04-28T00:43:26.348Z] Ignoring unsupported arg: projectId
16+
[debug] [2020-04-28T00:43:26.348Z] Ignoring unsupported arg: auto_download
17+
[debug] [2020-04-28T00:43:26.348Z] Starting firestore emulator with command {"binary":"java","args":["-Duser.language=en","-jar","C:\\Users\\mitch\\.cache\\firebase\\emulators\\cloud-firestore-emulator-v1.11.3.jar","--host","localhost","--port",8080,"--rules","D:\\Android Studio Projects\\CleanNotes\\firestore.rules"],"optionalArgs":["port","webchannel_port","host","rules","functions_emulator","seed_from_export"],"joinArgs":false}
18+
[info] i firestore: firestore emulator logging to firestore-debug.log
19+
[debug] [2020-04-28T00:43:27.647Z] API endpoint: http://localhost:8080
20+
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
21+
[debug] [2020-04-28T00:43:27.648Z]
22+
23+
export FIRESTORE_EMULATOR_HOST=localhost:8080
24+
25+
Dev App Server is now running.
26+
27+
28+
[info] + firestore: firestore emulator started at http://localhost:8080
29+
[info] i firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
30+
[info] + emulators: All emulators started, it is now safe to connect.
31+
[debug] [2020-04-28T00:43:35.087Z] Apr 27, 2020 5:43:35 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
32+
INFO: Detected HTTP/2 connection.
33+
34+
[debug] [2020-04-28T00:43:35.528Z] Apr 27, 2020 5:43:35 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
35+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
36+
37+
[debug] [2020-04-28T00:43:35.756Z] Apr 27, 2020 5:43:35 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
38+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
39+
40+
[debug] [2020-04-28T00:43:50.099Z] Apr 27, 2020 5:43:50 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
41+
INFO: Detected HTTP/2 connection.
42+
43+
[debug] [2020-04-28T00:43:50.121Z] Apr 27, 2020 5:43:50 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
44+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
45+
46+
[debug] [2020-04-28T00:43:50.179Z] Apr 27, 2020 5:43:50 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
47+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
48+
49+
[debug] [2020-04-28T00:44:09.418Z] Apr 27, 2020 5:44:09 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
50+
INFO: Detected HTTP/2 connection.
51+
52+
[debug] [2020-04-28T00:44:09.442Z] Apr 27, 2020 5:44:09 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
53+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
54+
55+
[debug] [2020-04-28T00:44:09.501Z] Apr 27, 2020 5:44:09 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
56+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
57+
58+
[debug] [2020-04-28T00:45:08.187Z] Apr 27, 2020 5:45:08 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
59+
INFO: Detected HTTP/2 connection.
60+
61+
[debug] [2020-04-28T00:45:08.210Z] Apr 27, 2020 5:45:08 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
62+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
63+
64+
[debug] [2020-04-28T00:45:08.273Z] Apr 27, 2020 5:45:08 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
65+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
66+
67+
[debug] [2020-04-28T00:45:48.166Z] Apr 27, 2020 5:45:48 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
68+
INFO: Detected HTTP/2 connection.
69+
70+
[debug] [2020-04-28T00:45:48.190Z] Apr 27, 2020 5:45:48 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
71+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
72+
73+
[debug] [2020-04-28T00:45:48.249Z] Apr 27, 2020 5:45:48 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
74+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
75+
76+
[debug] [2020-04-28T00:47:20.711Z] Apr 27, 2020 5:47:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
77+
INFO: Detected HTTP/2 connection.
78+
79+
[debug] [2020-04-28T00:47:20.733Z] Apr 27, 2020 5:47:20 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
80+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
81+
82+
[debug] [2020-04-28T00:47:20.791Z] Apr 27, 2020 5:47:20 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
83+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
84+

Diff for: firebase.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"firestore": {
3+
"rules": "firestore.rules",
4+
"indexes": "firestore.indexes.json"
5+
}
6+
}

Diff for: firestore-debug.log

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
API endpoint: http://localhost:8080
2+
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
3+
4+
export FIRESTORE_EMULATOR_HOST=localhost:8080
5+
6+
Dev App Server is now running.
7+
8+
Apr 27, 2020 5:43:35 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
9+
INFO: Detected HTTP/2 connection.
10+
Apr 27, 2020 5:43:35 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
11+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
12+
Apr 27, 2020 5:43:35 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
13+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
14+
Apr 27, 2020 5:43:50 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
15+
INFO: Detected HTTP/2 connection.
16+
Apr 27, 2020 5:43:50 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
17+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
18+
Apr 27, 2020 5:43:50 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
19+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
20+
Apr 27, 2020 5:44:09 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
21+
INFO: Detected HTTP/2 connection.
22+
Apr 27, 2020 5:44:09 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
23+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
24+
Apr 27, 2020 5:44:09 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
25+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
26+
Apr 27, 2020 5:45:08 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
27+
INFO: Detected HTTP/2 connection.
28+
Apr 27, 2020 5:45:08 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
29+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
30+
Apr 27, 2020 5:45:08 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
31+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
32+
Apr 27, 2020 5:45:48 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
33+
INFO: Detected HTTP/2 connection.
34+
Apr 27, 2020 5:45:48 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
35+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
36+
Apr 27, 2020 5:45:48 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
37+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
38+
Apr 27, 2020 5:47:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
39+
INFO: Detected HTTP/2 connection.
40+
Apr 27, 2020 5:47:20 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
41+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE
42+
Apr 27, 2020 5:47:20 PM com.google.cloud.datastore.emulator.impl.context.EmulatorAuthorization warnAboutSecuredJwt
43+
WARNING: expected an unsecured JWT, the emulator does not validate JWTs and IS NOT SECURE

Diff for: firestore.indexes.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"indexes": [],
3+
"fieldOverrides": []
4+
}

Diff for: firestore.rules

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rules_version = '2';
2+
service cloud.firestore {
3+
match /databases/{database}/documents {
4+
match /{document=**} {
5+
allow read, write: if request.auth.uid == "9E7fDYAUTNUPFirw4R28NhBZE1u1";
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)