Validate several ids in constructors (#336)

* Validate ids in constructors.

* Remove redundant `.value` usage in string interpolation.

* Make a distinction between `SessionId` and `UserId` in `TestData`.
This commit is contained in:
Jorge Martin Espinosa 2023-04-18 18:17:13 +02:00 committed by GitHub
parent f98fe8e52c
commit e704870e3f
36 changed files with 193 additions and 199 deletions

View file

@ -18,7 +18,7 @@ package io.element.android.libraries.push.providers.firebase
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import io.element.android.libraries.matrix.api.core.asSessionId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.push.providers.api.PusherSubscriber
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
import io.element.android.libraries.sessionstorage.api.SessionStore
@ -42,7 +42,7 @@ class FirebaseNewTokenHandler @Inject constructor(
firebaseStore.storeFcmToken(firebaseToken)
// Register the pusher for all the sessions
sessionStore.getAllSessions().toUserList()
.mapNotNull { it.asSessionId() }
.map { SessionId(it) }
.forEach { userId ->
val userDataStore = userPushStoreFactory.create(userId)
if (userDataStore.getPushProviderName() == FirebaseConfig.name) {

View file

@ -16,8 +16,8 @@
package io.element.android.libraries.push.providers.firebase
import io.element.android.libraries.matrix.api.core.asEventId
import io.element.android.libraries.matrix.api.core.asRoomId
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.push.providers.api.PushData
/**
@ -41,8 +41,8 @@ data class PushDataFirebase(
)
fun PushDataFirebase.toPushData(): PushData? {
val safeEventId = eventId?.asEventId() ?: return null
val safeRoomId = roomId?.asRoomId() ?: return null
val safeEventId = eventId?.let(::EventId) ?: return null
val safeRoomId = roomId?.let(::RoomId) ?: return null
return PushData(
eventId = safeEventId,
roomId = safeRoomId,

View file

@ -20,7 +20,7 @@ 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.providers.api.PushData
import io.element.android.tests.testutils.assertNullOrThrow
import io.element.android.tests.testutils.assertThrowsInDebug
import org.junit.Test
class FirebasePushParserTest {
@ -52,26 +52,26 @@ class FirebasePushParserTest {
fun `test empty roomId`() {
val pushParser = FirebasePushParser()
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", null))).isNull()
assertNullOrThrow { pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", "")) }
assertThrowsInDebug { pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", "")) }
}
@Test
fun `test invalid roomId`() {
val pushParser = FirebasePushParser()
assertNullOrThrow { pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", "aRoomId:domain")) }
assertThrowsInDebug { pushParser.parse(FIREBASE_PUSH_DATA.mutate("room_id", "aRoomId:domain")) }
}
@Test
fun `test empty eventId`() {
val pushParser = FirebasePushParser()
assertThat(pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", null))).isNull()
assertNullOrThrow { pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", "")) }
assertThrowsInDebug { pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", "")) }
}
@Test
fun `test invalid eventId`() {
val pushParser = FirebasePushParser()
assertNullOrThrow { pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", "anEventId")) }
assertThrowsInDebug { pushParser.parse(FIREBASE_PUSH_DATA.mutate("event_id", "anEventId")) }
}
companion object {

View file

@ -16,8 +16,8 @@
package io.element.android.libraries.push.providers.unifiedpush
import io.element.android.libraries.matrix.api.core.asEventId
import io.element.android.libraries.matrix.api.core.asRoomId
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.push.providers.api.PushData
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -56,8 +56,8 @@ data class PushDataUnifiedPushCounts(
)
fun PushDataUnifiedPush.toPushData(clientSecret: String): PushData? {
val safeEventId = notification?.eventId?.asEventId() ?: return null
val safeRoomId = notification.roomId?.asRoomId() ?: return null
val safeEventId = notification?.eventId?.let(::EventId) ?: return null
val safeRoomId = notification.roomId?.let(::RoomId) ?: return null
return PushData(
eventId = safeEventId,
roomId = safeRoomId,

View file

@ -20,7 +20,7 @@ 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.providers.api.PushData
import io.element.android.tests.testutils.assertNullOrThrow
import io.element.android.tests.testutils.assertThrowsInDebug
import org.junit.Test
class UnifiedPushParserTest {
@ -52,7 +52,7 @@ class UnifiedPushParserTest {
@Test
fun `test empty roomId`() {
val pushParser = UnifiedPushParser()
assertNullOrThrow {
assertThrowsInDebug {
pushParser.parse(UNIFIED_PUSH_DATA.replace(A_ROOM_ID.value, "").toByteArray(), aClientSecret)
}
}
@ -60,7 +60,7 @@ class UnifiedPushParserTest {
@Test
fun `test invalid roomId`() {
val pushParser = UnifiedPushParser()
assertNullOrThrow {
assertThrowsInDebug {
pushParser.parse(UNIFIED_PUSH_DATA.mutate(A_ROOM_ID.value, "aRoomId:domain"), aClientSecret)
}
}
@ -68,7 +68,7 @@ class UnifiedPushParserTest {
@Test
fun `test empty eventId`() {
val pushParser = UnifiedPushParser()
assertNullOrThrow {
assertThrowsInDebug {
pushParser.parse(UNIFIED_PUSH_DATA.mutate(AN_EVENT_ID.value, ""), aClientSecret)
}
}
@ -76,14 +76,14 @@ class UnifiedPushParserTest {
@Test
fun `test invalid eventId`() {
val pushParser = UnifiedPushParser()
assertNullOrThrow {
assertThrowsInDebug {
pushParser.parse(UNIFIED_PUSH_DATA.mutate(AN_EVENT_ID.value, "anEventId"), aClientSecret)
}
}
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\"}}"
"{\"notification\":{\"event_id\":\"$AN_EVENT_ID\",\"room_id\":\"$A_ROOM_ID\",\"counts\":{\"unread\":1},\"prio\":\"high\"}}"
// TODO Check client secret format?
}
}