mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-22 09:25:49 +03:00
Adding unit tests for EncryptedEventRelationsAggregationProcessor
This commit is contained in:
parent
1bd11775e9
commit
a04c60a85b
4 changed files with 284 additions and 24 deletions
|
@ -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.
|
||||
|
|
|
@ -32,17 +32,15 @@ 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 ?: "")
|
||||
|
||||
when (event.getClearType()) {
|
||||
return when (event.getClearType()) {
|
||||
EventType.ENCRYPTED -> {
|
||||
val encryptedEventContent = event.content.toModel<EncryptedEventContent>()
|
||||
processEncryptedContent(
|
||||
|
@ -53,7 +51,8 @@ internal class EncryptedEventRelationsAggregationProcessor @Inject constructor(
|
|||
isLocalEcho = isLocalEcho,
|
||||
)
|
||||
}
|
||||
else -> Unit
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 a new issue