Browse Source

Add test for Push parsers.

test/jme/compound-poc
Benoit Marty 1 year ago committed by Benoit Marty
parent
commit
3c2542847c
  1. 6
      libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/TestData.kt
  2. 10
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/unifiedpush/PushDataUnifiedPush.kt
  3. 4
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/unifiedpush/UnifiedPushParser.kt
  4. 100
      libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/firebase/FirebasePushParserTest.kt
  5. 95
      libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/unifiedpush/UnifiedPushParserTest.kt

6
libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/TestData.kt

@ -31,9 +31,9 @@ val A_USER_ID = UserId("@alice:server.org")
val A_USER_ID_2 = UserId("@bob:server.org") val A_USER_ID_2 = UserId("@bob:server.org")
val A_SESSION_ID = SessionId(A_USER_ID.value) val A_SESSION_ID = SessionId(A_USER_ID.value)
val A_SESSION_ID_2 = SessionId(A_USER_ID_2.value) val A_SESSION_ID_2 = SessionId(A_USER_ID_2.value)
val A_SPACE_ID = SpaceId("!aSpaceId") val A_SPACE_ID = SpaceId("!aSpaceId:domain")
val A_ROOM_ID = RoomId("!aRoomId") val A_ROOM_ID = RoomId("!aRoomId:domain")
val A_ROOM_ID_2 = RoomId("!aRoomId2") val A_ROOM_ID_2 = RoomId("!aRoomId2:domain")
val A_THREAD_ID = ThreadId("\$aThreadId") val A_THREAD_ID = ThreadId("\$aThreadId")
val AN_EVENT_ID = EventId("\$anEventId") val AN_EVENT_ID = EventId("\$anEventId")
val AN_EVENT_ID_2 = EventId("\$anEventId2") val AN_EVENT_ID_2 = EventId("\$anEventId2")

10
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/unifiedpush/PushDataUnifiedPush.kt

@ -41,19 +41,19 @@ import kotlinx.serialization.Serializable
*/ */
@Serializable @Serializable
data class PushDataUnifiedPush( data class PushDataUnifiedPush(
val notification: PushDataUnifiedPushNotification? val notification: PushDataUnifiedPushNotification? = null
) )
@Serializable @Serializable
data class PushDataUnifiedPushNotification( data class PushDataUnifiedPushNotification(
@SerialName("event_id") val eventId: String?, @SerialName("event_id") val eventId: String? = null,
@SerialName("room_id") val roomId: String?, @SerialName("room_id") val roomId: String? = null,
@SerialName("counts") var counts: PushDataUnifiedPushCounts?, @SerialName("counts") var counts: PushDataUnifiedPushCounts? = null,
) )
@Serializable @Serializable
data class PushDataUnifiedPushCounts( data class PushDataUnifiedPushCounts(
@SerialName("unread") val unread: Int? @SerialName("unread") val unread: Int? = null
) )
fun PushDataUnifiedPush.toPushData() = PushData( fun PushDataUnifiedPush.toPushData() = PushData(

4
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/unifiedpush/UnifiedPushParser.kt

@ -23,7 +23,9 @@ import kotlinx.serialization.json.Json
import javax.inject.Inject import javax.inject.Inject
class UnifiedPushParser @Inject constructor() { class UnifiedPushParser @Inject constructor() {
private val json by lazy { Json { ignoreUnknownKeys = true } }
fun parse(message: ByteArray): PushData? { fun parse(message: ByteArray): PushData? {
return tryOrNull { Json.decodeFromString<PushDataUnifiedPush>(String(message)) }?.toPushData() return tryOrNull { json.decodeFromString<PushDataUnifiedPush>(String(message)) }?.toPushData()
} }
} }

100
libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/firebase/FirebasePushParserTest.kt

@ -0,0 +1,100 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.firebase
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.push.impl.push.PushData
import org.junit.Test
class FirebasePushParserTest {
private val validData = PushData(
eventId = AN_EVENT_ID,
roomId = A_ROOM_ID,
unread = 1,
clientSecret = "a-secret"
)
private val emptyData = PushData(
eventId = null,
roomId = null,
unread = null,
clientSecret = null
)
@Test
fun `test edge cases Firebase`() {
val pushParser = FirebasePushParser()
// Empty Json
assertThat(pushParser.parse(emptyMap())).isEqualTo(emptyData)
// Bad Json
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("unread", "str"))).isEqualTo(validData.copy(unread = null))
// Extra data
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("extra", "5"))).isEqualTo(validData)
}
@Test
fun `test Firebase format`() {
val pushParser = FirebasePushParser()
assertThat(pushParser.parse(FIREBASE_PUSH_DATA)).isEqualTo(validData)
}
@Test
fun `test empty roomId`() {
val pushParser = FirebasePushParser()
val expected = validData.copy(roomId = null)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", null))).isEqualTo(expected)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", ""))).isEqualTo(expected)
}
@Test
fun `test invalid roomId`() {
val pushParser = FirebasePushParser()
val expected = validData.copy(roomId = null)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", "aRoomId:domain"))).isEqualTo(expected)
}
@Test
fun `test empty eventId`() {
val pushParser = FirebasePushParser()
val expected = validData.copy(eventId = null)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", null))).isEqualTo(expected)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", ""))).isEqualTo(expected)
}
@Test
fun `test invalid eventId`() {
val pushParser = FirebasePushParser()
val expected = validData.copy(eventId = null)
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", "anEventId"))).isEqualTo(expected)
}
companion object {
private val FIREBASE_PUSH_DATA = mapOf(
"event_id" to AN_EVENT_ID.value,
"room_id" to A_ROOM_ID.value,
"unread" to "1",
"prio" to "high",
"cs" to "a-secret",
)
}
}
private fun Map<String, String?>.mutate(key: String, value: String?): Map<String, String?> {
return toMutableMap().apply { put(key, value) }
}

95
libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/unifiedpush/UnifiedPushParserTest.kt

@ -0,0 +1,95 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.unifiedpush
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.push.impl.push.PushData
import org.junit.Test
class UnifiedPushParserTest {
private val validData = PushData(
eventId = AN_EVENT_ID,
roomId = A_ROOM_ID,
unread = 1,
// TODO handle client secret here.
clientSecret = null
)
private val emptyData = PushData(
eventId = null,
roomId = null,
unread = null,
clientSecret = null
)
@Test
fun `test edge cases UnifiedPush`() {
val pushParser = UnifiedPushParser()
// Empty string
assertThat(pushParser.parse("".toByteArray())).isNull()
// Empty Json
assertThat(pushParser.parse("{}".toByteArray())).isEqualTo(emptyData)
// Bad Json
assertThat(pushParser.parse("ABC".toByteArray())).isNull()
}
@Test
fun `test UnifiedPush format`() {
val pushParser = UnifiedPushParser()
assertThat(pushParser.parse(UNIFIED_PUSH_DATA.toByteArray())).isEqualTo(validData)
}
@Test
fun `test empty roomId`() {
val pushParser = UnifiedPushParser()
val expected = validData.copy(roomId = null)
assertThat(pushParser.parse(UNIFIED_PUSH_DATA.replace(A_ROOM_ID.value, "").toByteArray())).isEqualTo(expected)
}
@Test
fun `test invalid roomId`() {
val pushParser = UnifiedPushParser()
val expected = validData.copy(roomId = null)
assertThat(pushParser.parse(UNIFIED_PUSH_DATA.mutate(A_ROOM_ID.value, "aRoomId:domain"))).isEqualTo(expected)
}
@Test
fun `test empty eventId`() {
val pushParser = UnifiedPushParser()
val expected = validData.copy(eventId = null)
assertThat(pushParser.parse(UNIFIED_PUSH_DATA.mutate(AN_EVENT_ID.value, ""))).isEqualTo(expected)
}
@Test
fun `test invalid eventId`() {
val pushParser = UnifiedPushParser()
val expected = validData.copy(eventId = null)
assertThat(pushParser.parse(UNIFIED_PUSH_DATA.mutate(AN_EVENT_ID.value, "anEventId"))).isEqualTo(expected)
}
companion object {
private val UNIFIED_PUSH_DATA =
"{\"notification\":{\"event_id\":\"${AN_EVENT_ID.value}\",\"room_id\":\"${A_ROOM_ID.value}\",\"counts\":{\"unread\":1},\"prio\":\"high\"}}"
// TODO Check client secret format?
}
}
private fun String.mutate(oldValue: String, newValue: String): ByteArray {
return replace(oldValue, newValue).toByteArray()
}
Loading…
Cancel
Save