Fix test - no need to use Moshi to parse FCM Push data.

This commit is contained in:
Benoit Marty 2022-09-09 15:58:16 +02:00 committed by Benoit Marty
parent f88039b7fd
commit 7c4527fba6
3 changed files with 73 additions and 53 deletions

View File

@ -20,13 +20,11 @@ import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.pushers.model.PushDataFcm import im.vector.app.core.pushers.model.PushDataFcm
import im.vector.app.core.pushers.model.PushDataUnifiedPush import im.vector.app.core.pushers.model.PushDataUnifiedPush
import im.vector.app.core.pushers.model.toPushData import im.vector.app.core.pushers.model.toPushData
import org.json.JSONObject
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.util.MatrixJsonParser import org.matrix.android.sdk.api.util.MatrixJsonParser
import javax.inject.Inject import javax.inject.Inject
class PushParser @Inject constructor() { /**
/**
* Parse the received data from Push. Json format are different depending on the source. * Parse the received data from Push. Json format are different depending on the source.
* *
* Notifications received by FCM are formatted by the matrix gateway [1]. The data send to FCM is the content * Notifications received by FCM are formatted by the matrix gateway [1]. The data send to FCM is the content
@ -41,6 +39,7 @@ class PushParser @Inject constructor() {
* [3] https://spec.matrix.org/latest/push-gateway-api/ * [3] https://spec.matrix.org/latest/push-gateway-api/
* [4] https://github.com/p1gp1g/sygnal/blob/unifiedpush/sygnal/upfcmpushkin.py (Not tested for a while) * [4] https://github.com/p1gp1g/sygnal/blob/unifiedpush/sygnal/upfcmpushkin.py (Not tested for a while)
*/ */
class PushParser @Inject constructor() {
fun parsePushDataUnifiedPush(message: ByteArray): PushData? { fun parsePushDataUnifiedPush(message: ByteArray): PushData? {
return MatrixJsonParser.getMoshi().let { return MatrixJsonParser.getMoshi().let {
tryOrNull { it.adapter(PushDataUnifiedPush::class.java).fromJson(String(message)) }?.toPushData() tryOrNull { it.adapter(PushDataUnifiedPush::class.java).fromJson(String(message)) }?.toPushData()
@ -48,8 +47,16 @@ class PushParser @Inject constructor() {
} }
fun parsePushDataFcm(message: Map<*, *>): PushData? { fun parsePushDataFcm(message: Map<*, *>): PushData? {
return MatrixJsonParser.getMoshi().let { if ((message.containsKey("event_id") && message["event_id"] !is String) ||
tryOrNull { it.adapter(PushDataFcm::class.java).fromJson(JSONObject(message).toString()) }?.toPushData() message.containsKey("room_id") && message["room_id"] !is String ||
message.containsKey("unread") && message["unread"] !is Int) {
return null
} }
val pushDataFcm = PushDataFcm(
eventId = message["event_id"] as String?,
roomId = message["room_id"] as String?,
unread = message["unread"] as Int?,
)
return pushDataFcm.toPushData()
} }
} }

View File

@ -16,8 +16,6 @@
package im.vector.app.core.pushers.model package im.vector.app.core.pushers.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import org.matrix.android.sdk.api.MatrixPatterns import org.matrix.android.sdk.api.MatrixPatterns
/** /**
@ -32,11 +30,10 @@ import org.matrix.android.sdk.api.MatrixPatterns
* </pre> * </pre>
* . * .
*/ */
@JsonClass(generateAdapter = true)
data class PushDataFcm( data class PushDataFcm(
@Json(name = "event_id") val eventId: String?, val eventId: String?,
@Json(name = "room_id") val roomId: String?, val roomId: String?,
@Json(name = "unread") var unread: Int?, var unread: Int?,
) )
fun PushDataFcm.toPushData() = PushData( fun PushDataFcm.toPushData() = PushData(

View File

@ -35,73 +35,89 @@ class PushParserTest {
) )
@Test @Test
fun `test edge cases`() { fun `test edge cases Firebase`() {
doAllEdgeTests(true) val pushParser = PushParser()
doAllEdgeTests(false) // Empty Json
pushParser.parsePushDataFcm(emptyMap<String, Any>()) shouldBeEqualTo emptyData
// Bad Json
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", 5)) shouldBe null
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", 5)) shouldBe null
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("unread", "str")) shouldBe null
// Extra data
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("extra", 5)) shouldBeEqualTo validData
} }
private fun doAllEdgeTests(firebaseFormat: Boolean) { @Test
fun `test edge cases UnifiedPush`() {
val pushParser = PushParser() val pushParser = PushParser()
// Empty string // Empty string
pushParser.parseData("", firebaseFormat) shouldBe null pushParser.parsePushDataUnifiedPush("".toByteArray()) shouldBe null
// Empty Json // Empty Json
pushParser.parseData("{}", firebaseFormat) shouldBeEqualTo emptyData pushParser.parsePushDataUnifiedPush("{}".toByteArray()) shouldBeEqualTo emptyData
// Bad Json // Bad Json
pushParser.parseData("ABC", firebaseFormat) shouldBe null pushParser.parsePushDataUnifiedPush("ABC".toByteArray()) shouldBe null
} }
@Test @Test
fun `test unified push format`() { fun `test UnifiedPush format`() {
val pushParser = PushParser() val pushParser = PushParser()
pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.toByteArray()) shouldBeEqualTo validData
pushParser.parseData(UNIFIED_PUSH_DATA, false) shouldBeEqualTo validData
pushParser.parseData(UNIFIED_PUSH_DATA, true) shouldBeEqualTo emptyData
} }
@Test @Test
fun `test firebase push format`() { fun `test Firebase format`() {
val pushParser = PushParser() val pushParser = PushParser()
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA) shouldBeEqualTo validData
pushParser.parseData(FIREBASE_PUSH_DATA, true) shouldBeEqualTo validData
pushParser.parseData(FIREBASE_PUSH_DATA, false) shouldBeEqualTo emptyData
} }
@Test @Test
fun `test empty roomId`() { fun `test empty roomId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(roomId = null)
pushParser.parseData(FIREBASE_PUSH_DATA.replace("!aRoomId:domain", ""), true) shouldBeEqualTo validData.copy(roomId = null) pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", "")) shouldBeEqualTo expected
pushParser.parseData(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", ""), false) shouldBeEqualTo validData.copy(roomId = null) pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", "").toByteArray()) shouldBeEqualTo expected
} }
@Test @Test
fun `test invalid roomId`() { fun `test invalid roomId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(roomId = null)
pushParser.parseData(FIREBASE_PUSH_DATA.replace("!aRoomId:domain", "aRoomId:domain"), true) shouldBeEqualTo validData.copy(roomId = null) pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", "aRoomId:domain")) shouldBeEqualTo expected
pushParser.parseData(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", "aRoomId:domain"), false) shouldBeEqualTo validData.copy(roomId = null) pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("!aRoomId:domain", "aRoomId:domain")) shouldBeEqualTo expected
} }
@Test @Test
fun `test empty eventId`() { fun `test empty eventId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(eventId = null)
pushParser.parseData(FIREBASE_PUSH_DATA.replace("\$anEventId", ""), true) shouldBeEqualTo validData.copy(eventId = null) pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", "")) shouldBeEqualTo expected
pushParser.parseData(UNIFIED_PUSH_DATA.replace("\$anEventId", ""), false) shouldBeEqualTo validData.copy(eventId = null) pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("\$anEventId", "")) shouldBeEqualTo expected
} }
@Test @Test
fun `test invalid eventId`() { fun `test invalid eventId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(eventId = null)
pushParser.parseData(FIREBASE_PUSH_DATA.replace("\$anEventId", "anEventId"), true) shouldBeEqualTo validData.copy(eventId = null) pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", "anEventId")) shouldBeEqualTo expected
pushParser.parseData(UNIFIED_PUSH_DATA.replace("\$anEventId", "anEventId"), false) shouldBeEqualTo validData.copy(eventId = null) pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("\$anEventId", "anEventId")) shouldBeEqualTo expected
} }
companion object { companion object {
private const val UNIFIED_PUSH_DATA = private const val UNIFIED_PUSH_DATA =
"{\"notification\":{\"event_id\":\"\$anEventId\",\"room_id\":\"!aRoomId:domain\",\"counts\":{\"unread\":1},\"prio\":\"high\"}}" "{\"notification\":{\"event_id\":\"\$anEventId\",\"room_id\":\"!aRoomId:domain\",\"counts\":{\"unread\":1},\"prio\":\"high\"}}"
private const val FIREBASE_PUSH_DATA = private val FIREBASE_PUSH_DATA = mapOf(
"{\"event_id\":\"\$anEventId\",\"room_id\":\"!aRoomId:domain\",\"unread\":\"1\",\"prio\":\"high\"}" "event_id" to "\$anEventId",
"room_id" to "!aRoomId:domain",
"unread" to 1,
"prio" to "high",
)
} }
} }
private fun <K, V> Map<K, V?>.mutate(key: K, value: V?): Map<K, V?> {
return toMutableMap().apply { put(key, value) }
}
private fun String.mutate(oldValue: String, newValue: String): ByteArray {
return replace(oldValue, newValue).toByteArray()
}