testing the room events decrypter

This commit is contained in:
Adam Brown 2022-03-06 12:46:18 +00:00
parent 1f96bfbc0f
commit 4dc89091ba
4 changed files with 96 additions and 3 deletions

View File

@ -45,9 +45,6 @@ jobs:
- name: Run all unit tests
run: ./gradlew clean allCodeCoverageReport --no-daemon
- name: Print coverage size
run: ls -l ./build/reports/jacoco/allCodeCoverageReport/allCodeCoverageReport.xml
- uses: codecov/codecov-action@v2
with:
files: ./build/reports/jacoco/allCodeCoverageReport/allCodeCoverageReport.xml

View File

@ -9,5 +9,6 @@ dependencies {
testImplementation(testFixtures(project(":matrix:common")))
testImplementation(testFixtures(project(":matrix:matrix-http")))
testImplementation(testFixtures(project(":core")))
testFixturesImplementation(testFixtures(project(":core")))
testFixturesImplementation(testFixtures(project(":matrix:common")))
}

View File

@ -0,0 +1,83 @@
package app.dapk.st.matrix.sync.internal.room
import app.dapk.st.matrix.common.EncryptedMessageContent
import app.dapk.st.matrix.common.JsonString
import app.dapk.st.matrix.sync.RoomEvent
import app.dapk.st.matrix.sync.internal.request.DecryptedContent
import fake.FakeMatrixLogger
import fake.FakeMessageDecrypter
import fixture.*
import internalfixture.aTimelineTextEventContent
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
private const val A_DECRYPTED_MESSAGE_CONTENT = "decrypted - content"
private val AN_ENCRYPTED_ROOM_CONTENT = aMegolmV1()
private val AN_ENCRYPTED_ROOM_MESSAGE = aRoomMessageEvent(encryptedContent = AN_ENCRYPTED_ROOM_CONTENT)
private val AN_ENCRYPTED_ROOM_REPLY = aRoomReplyMessageEvent(
message = AN_ENCRYPTED_ROOM_MESSAGE,
replyingTo = AN_ENCRYPTED_ROOM_MESSAGE.copy(eventId = anEventId("other-event"))
)
private val A_DECRYPTED_CONTENT = DecryptedContent.TimelineText(aTimelineTextEventContent(body = A_DECRYPTED_MESSAGE_CONTENT))
class RoomEventsDecrypterTest {
private val fakeMessageDecrypter = FakeMessageDecrypter()
private val roomEventsDecrypter = RoomEventsDecrypter(
fakeMessageDecrypter,
Json,
FakeMatrixLogger(),
)
@Test
fun `given clear message event, when decrypting, then does nothing`() = runTest {
val aClearMessageEvent = aRoomMessageEvent(encryptedContent = null)
val result = roomEventsDecrypter.decryptRoomEvents(listOf(aClearMessageEvent))
result shouldBeEqualTo listOf(aClearMessageEvent)
}
@Test
fun `given encrypted message event, when decrypting, then applies decrypted body and removes encrypted content`() = runTest {
givenEncryptedMessage(AN_ENCRYPTED_ROOM_MESSAGE, decryptsTo = A_DECRYPTED_CONTENT)
val result = roomEventsDecrypter.decryptRoomEvents(listOf(AN_ENCRYPTED_ROOM_MESSAGE))
result shouldBeEqualTo listOf(AN_ENCRYPTED_ROOM_MESSAGE.copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null))
}
@Test
fun `given encrypted reply event, when decrypting, then decrypts message and replyTo`() = runTest {
givenEncryptedReply(AN_ENCRYPTED_ROOM_REPLY, decryptsTo = A_DECRYPTED_CONTENT)
val result = roomEventsDecrypter.decryptRoomEvents(listOf(AN_ENCRYPTED_ROOM_REPLY))
result shouldBeEqualTo listOf(
AN_ENCRYPTED_ROOM_REPLY.copy(
message = AN_ENCRYPTED_ROOM_REPLY.message.copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null),
replyingTo = AN_ENCRYPTED_ROOM_REPLY.replyingTo.copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null),
)
)
}
private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) {
val model = roomMessage.encryptedContent!!.toModel()
fakeMessageDecrypter.givenDecrypt(model)
.returns(aDecryptionSuccessResult(payload = JsonString(Json.encodeToString(DecryptedContent.serializer(), decryptsTo))))
}
private fun givenEncryptedReply(roomReply: RoomEvent.Reply, decryptsTo: DecryptedContent) {
givenEncryptedMessage(roomReply.message, decryptsTo)
givenEncryptedMessage(roomReply.replyingTo, decryptsTo)
}
}
private fun RoomEvent.Message.MegOlmV1.toModel() = EncryptedMessageContent.MegOlmV1(
this.cipherText,
this.deviceId,
this.senderKey,
this.sessionId,
)

View File

@ -0,0 +1,12 @@
package fake
import app.dapk.st.matrix.common.EncryptedMessageContent
import app.dapk.st.matrix.sync.internal.room.MessageDecrypter
import io.mockk.coEvery
import io.mockk.mockk
import test.delegateReturn
class FakeMessageDecrypter : MessageDecrypter by mockk() {
fun givenDecrypt(content: EncryptedMessageContent) = coEvery { decrypt(content) }.delegateReturn()
}