Adding unit tests for EncryptedEventRelationsAggregationProcessor

This commit is contained in:
Maxime NATUREL 2022-12-27 11:57:50 +01:00 committed by Maxime NATUREL
parent 1bd11775e9
commit a04c60a85b
4 changed files with 284 additions and 24 deletions

View file

@ -21,7 +21,13 @@ import org.matrix.android.sdk.api.session.events.model.Event
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.

View file

@ -32,28 +32,27 @@ internal class EncryptedEventRelationsAggregationProcessor @Inject constructor(
private val encryptedReferenceAggregationProcessor: EncryptedReferenceAggregationProcessor,
) : UnableToDecryptEventLiveProcessor {
// TODO add unit tests
override fun process(realm: Realm, event: Event) {
override fun process(realm: Realm, event: Event): Boolean {
val roomId = event.roomId
if (roomId == null) {
return if (roomId == null) {
Timber.w("Event has no room id ${event.eventId}")
return
}
false
} else {
val isLocalEcho = LocalEcho.isLocalEchoId(event.eventId ?: "")
val isLocalEcho = LocalEcho.isLocalEchoId(event.eventId ?: "")
when (event.getClearType()) {
EventType.ENCRYPTED -> {
val encryptedEventContent = event.content.toModel<EncryptedEventContent>()
processEncryptedContent(
encryptedEventContent = encryptedEventContent,
realm = realm,
event = event,
roomId = roomId,
isLocalEcho = isLocalEcho,
)
return when (event.getClearType()) {
EventType.ENCRYPTED -> {
val encryptedEventContent = event.content.toModel<EncryptedEventContent>()
processEncryptedContent(
encryptedEventContent = encryptedEventContent,
realm = realm,
event = event,
roomId = roomId,
isLocalEcho = isLocalEcho,
)
}
else -> false
}
else -> Unit
}
}
@ -63,30 +62,34 @@ internal class EncryptedEventRelationsAggregationProcessor @Inject constructor(
event: Event,
roomId: String,
isLocalEcho: Boolean,
) {
when (encryptedEventContent?.relatesTo?.type) {
): Boolean {
return when (encryptedEventContent?.relatesTo?.type) {
RelationType.REPLACE -> {
Timber.w("## UTD replace in room $roomId for event ${event.eventId}")
false
}
RelationType.RESPONSE -> {
// can we / should we do we something for UTD response??
Timber.w("## UTD response in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
false
}
RelationType.REFERENCE -> {
// can we / should we do we something for UTD reference??
Timber.w("## UTD reference in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
encryptedReferenceAggregationProcessor.handle(
val result = encryptedReferenceAggregationProcessor.handle(
realm = realm,
event = event,
isLocalEcho = isLocalEcho,
relatedEventId = encryptedEventContent.relatesTo.eventId
relatedEventId = encryptedEventContent.relatesTo.eventId,
)
result
}
RelationType.ANNOTATION -> {
// can we / should we do we something for UTD annotation??
Timber.w("## UTD annotation in room $roomId related to ${encryptedEventContent.relatesTo.eventId}")
false
}
else -> Unit
else -> false
}
}
}

View file

@ -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,
)
}
}

View file

@ -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) }
}
}