Adding unit tests for EncryptedEventRelationsAggregationProcessor
This commit is contained in:
parent
195b2e74b1
commit
74c4ca6d7f
@ -21,7 +21,13 @@ import org.matrix.android.sdk.api.session.events.model.Event
|
|||||||
|
|
||||||
internal interface UnableToDecryptEventLiveProcessor {
|
internal interface UnableToDecryptEventLiveProcessor {
|
||||||
|
|
||||||
fun process(realm: Realm, event: Event)
|
/**
|
||||||
|
* Process the given event.
|
||||||
|
* @param realm a realm instance
|
||||||
|
* @param event the event to be processed
|
||||||
|
* @return true if it has been processed, false if it was ignored.
|
||||||
|
*/
|
||||||
|
fun process(realm: Realm, event: Event): Boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called after transaction.
|
* Called after transaction.
|
||||||
|
@ -32,28 +32,27 @@ internal class EncryptedEventRelationsAggregationProcessor @Inject constructor(
|
|||||||
private val encryptedReferenceAggregationProcessor: EncryptedReferenceAggregationProcessor,
|
private val encryptedReferenceAggregationProcessor: EncryptedReferenceAggregationProcessor,
|
||||||
) : UnableToDecryptEventLiveProcessor {
|
) : UnableToDecryptEventLiveProcessor {
|
||||||
|
|
||||||
// TODO add unit tests
|
override fun process(realm: Realm, event: Event): Boolean {
|
||||||
override fun process(realm: Realm, event: Event) {
|
|
||||||
val roomId = event.roomId
|
val roomId = event.roomId
|
||||||
if (roomId == null) {
|
return if (roomId == null) {
|
||||||
Timber.w("Event has no room id ${event.eventId}")
|
Timber.w("Event has no room id ${event.eventId}")
|
||||||
return
|
false
|
||||||
}
|
} else {
|
||||||
|
val isLocalEcho = LocalEcho.isLocalEchoId(event.eventId ?: "")
|
||||||
|
|
||||||
val isLocalEcho = LocalEcho.isLocalEchoId(event.eventId ?: "")
|
return when (event.getClearType()) {
|
||||||
|
EventType.ENCRYPTED -> {
|
||||||
when (event.getClearType()) {
|
val encryptedEventContent = event.content.toModel<EncryptedEventContent>()
|
||||||
EventType.ENCRYPTED -> {
|
processEncryptedContent(
|
||||||
val encryptedEventContent = event.content.toModel<EncryptedEventContent>()
|
encryptedEventContent = encryptedEventContent,
|
||||||
processEncryptedContent(
|
realm = realm,
|
||||||
encryptedEventContent = encryptedEventContent,
|
event = event,
|
||||||
realm = realm,
|
roomId = roomId,
|
||||||
event = event,
|
isLocalEcho = isLocalEcho,
|
||||||
roomId = roomId,
|
)
|
||||||
isLocalEcho = isLocalEcho,
|
}
|
||||||
)
|
else -> false
|
||||||
}
|
}
|
||||||
else -> Unit
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,30 +62,34 @@ internal class EncryptedEventRelationsAggregationProcessor @Inject constructor(
|
|||||||
event: Event,
|
event: Event,
|
||||||
roomId: String,
|
roomId: String,
|
||||||
isLocalEcho: Boolean,
|
isLocalEcho: Boolean,
|
||||||
) {
|
): Boolean {
|
||||||
when (encryptedEventContent?.relatesTo?.type) {
|
return when (encryptedEventContent?.relatesTo?.type) {
|
||||||
RelationType.REPLACE -> {
|
RelationType.REPLACE -> {
|
||||||
Timber.w("## UTD replace in room $roomId for event ${event.eventId}")
|
Timber.w("## UTD replace in room $roomId for event ${event.eventId}")
|
||||||
|
false
|
||||||
}
|
}
|
||||||
RelationType.RESPONSE -> {
|
RelationType.RESPONSE -> {
|
||||||
// can we / should we do we something for UTD response??
|
// can we / should we do we something for UTD response??
|
||||||
Timber.w("## UTD response in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
Timber.w("## UTD response in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
||||||
|
false
|
||||||
}
|
}
|
||||||
RelationType.REFERENCE -> {
|
RelationType.REFERENCE -> {
|
||||||
// can we / should we do we something for UTD reference??
|
// can we / should we do we something for UTD reference??
|
||||||
Timber.w("## UTD reference in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
Timber.w("## UTD reference in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
||||||
encryptedReferenceAggregationProcessor.handle(
|
val result = encryptedReferenceAggregationProcessor.handle(
|
||||||
realm = realm,
|
realm = realm,
|
||||||
event = event,
|
event = event,
|
||||||
isLocalEcho = isLocalEcho,
|
isLocalEcho = isLocalEcho,
|
||||||
relatedEventId = encryptedEventContent.relatesTo.eventId
|
relatedEventId = encryptedEventContent.relatesTo.eventId,
|
||||||
)
|
)
|
||||||
|
result
|
||||||
}
|
}
|
||||||
RelationType.ANNOTATION -> {
|
RelationType.ANNOTATION -> {
|
||||||
// can we / should we do we something for UTD annotation??
|
// can we / should we do we something for UTD annotation??
|
||||||
Timber.w("## UTD annotation in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
Timber.w("## UTD annotation in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
|
||||||
|
false
|
||||||
}
|
}
|
||||||
else -> Unit
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,209 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
*
|
||||||
|
* 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 org.matrix.android.sdk.internal.session.room
|
||||||
|
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import org.amshove.kluent.shouldBeEqualTo
|
||||||
|
import org.amshove.kluent.shouldBeFalse
|
||||||
|
import org.junit.Test
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.RelationType
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
|
||||||
|
import org.matrix.android.sdk.test.fakes.FakeRealm
|
||||||
|
import org.matrix.android.sdk.test.fakes.internal.session.room.aggregation.utd.FakeEncryptedReferenceAggregationProcessor
|
||||||
|
|
||||||
|
class EncryptedEventRelationsAggregationProcessorTest {
|
||||||
|
|
||||||
|
private val fakeEncryptedReferenceAggregationProcessor = FakeEncryptedReferenceAggregationProcessor()
|
||||||
|
private val fakeRealm = FakeRealm()
|
||||||
|
|
||||||
|
private val encryptedEventRelationsAggregationProcessor = EncryptedEventRelationsAggregationProcessor(
|
||||||
|
encryptedReferenceAggregationProcessor = fakeEncryptedReferenceAggregationProcessor.instance,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given no room Id when process then result is false`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = null,
|
||||||
|
eventType = EventType.ENCRYPTED,
|
||||||
|
)
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.shouldBeFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an encrypted reference event when process then reference is processed`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = "room-id",
|
||||||
|
eventType = EventType.ENCRYPTED,
|
||||||
|
)
|
||||||
|
val relatedEventId = "related-event-id"
|
||||||
|
val encryptedEventContent = givenEncryptedEventContent(
|
||||||
|
relationType = RelationType.REFERENCE,
|
||||||
|
relatedEventId = relatedEventId,
|
||||||
|
)
|
||||||
|
every { anEvent.content } returns encryptedEventContent.toContent()
|
||||||
|
val resultOfReferenceProcess = false
|
||||||
|
fakeEncryptedReferenceAggregationProcessor.givenHandleReturns(resultOfReferenceProcess)
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result shouldBeEqualTo resultOfReferenceProcess
|
||||||
|
fakeEncryptedReferenceAggregationProcessor.verifyHandle(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
isLocalEcho = false,
|
||||||
|
relatedEventId = relatedEventId,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an encrypted replace event when process then result is false`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = "room-id",
|
||||||
|
eventType = EventType.ENCRYPTED,
|
||||||
|
)
|
||||||
|
val relatedEventId = "related-event-id"
|
||||||
|
val encryptedEventContent = givenEncryptedEventContent(
|
||||||
|
relationType = RelationType.REPLACE,
|
||||||
|
relatedEventId = relatedEventId,
|
||||||
|
)
|
||||||
|
every { anEvent.content } returns encryptedEventContent.toContent()
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.shouldBeFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an encrypted response event when process then result is false`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = "room-id",
|
||||||
|
eventType = EventType.ENCRYPTED,
|
||||||
|
)
|
||||||
|
val relatedEventId = "related-event-id"
|
||||||
|
val encryptedEventContent = givenEncryptedEventContent(
|
||||||
|
relationType = RelationType.RESPONSE,
|
||||||
|
relatedEventId = relatedEventId,
|
||||||
|
)
|
||||||
|
every { anEvent.content } returns encryptedEventContent.toContent()
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.shouldBeFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given an encrypted annotation event when process then result is false`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = "room-id",
|
||||||
|
eventType = EventType.ENCRYPTED,
|
||||||
|
)
|
||||||
|
val relatedEventId = "related-event-id"
|
||||||
|
val encryptedEventContent = givenEncryptedEventContent(
|
||||||
|
relationType = RelationType.ANNOTATION,
|
||||||
|
relatedEventId = relatedEventId,
|
||||||
|
)
|
||||||
|
every { anEvent.content } returns encryptedEventContent.toContent()
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.shouldBeFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given a non encrypted event when process then result is false`() {
|
||||||
|
// Given
|
||||||
|
val anEvent = givenAnEvent(
|
||||||
|
eventId = "event-id",
|
||||||
|
roomId = "room-id",
|
||||||
|
eventType = EventType.MESSAGE,
|
||||||
|
)
|
||||||
|
|
||||||
|
// When
|
||||||
|
val result = encryptedEventRelationsAggregationProcessor.process(
|
||||||
|
realm = fakeRealm.instance,
|
||||||
|
event = anEvent,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.shouldBeFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenAnEvent(
|
||||||
|
eventId: String,
|
||||||
|
roomId: String?,
|
||||||
|
eventType: String,
|
||||||
|
): Event {
|
||||||
|
return mockk<Event>().also {
|
||||||
|
every { it.eventId } returns eventId
|
||||||
|
every { it.roomId } returns roomId
|
||||||
|
every { it.getClearType() } returns eventType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenEncryptedEventContent(relationType: String, relatedEventId: String): EncryptedEventContent {
|
||||||
|
val relationContent = RelationDefaultContent(
|
||||||
|
eventId = relatedEventId,
|
||||||
|
type = relationType,
|
||||||
|
)
|
||||||
|
return EncryptedEventContent(
|
||||||
|
relatesTo = relationContent,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
*
|
||||||
|
* 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 org.matrix.android.sdk.test.fakes.internal.session.room.aggregation.utd
|
||||||
|
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import io.realm.Realm
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.Event
|
||||||
|
import org.matrix.android.sdk.internal.session.room.aggregation.utd.EncryptedReferenceAggregationProcessor
|
||||||
|
|
||||||
|
internal class FakeEncryptedReferenceAggregationProcessor {
|
||||||
|
|
||||||
|
val instance: EncryptedReferenceAggregationProcessor = mockk()
|
||||||
|
|
||||||
|
fun givenHandleReturns(result: Boolean) {
|
||||||
|
every { instance.handle(any(), any(), any(), any()) } returns result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun verifyHandle(
|
||||||
|
realm: Realm,
|
||||||
|
event: Event,
|
||||||
|
isLocalEcho: Boolean,
|
||||||
|
relatedEventId: String?,
|
||||||
|
) {
|
||||||
|
verify { instance.handle(realm, event, isLocalEcho, relatedEventId) }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user