From c9fb1b102e5cde5369aba764d4f8610b2cd79dca Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 17 Oct 2022 21:15:21 +0100 Subject: [PATCH] updating tests --- .../app/dapk/st/matrix/sync/RoomState.kt | 3 +- .../sync/internal/sync/RoomEventCreator.kt | 1 - .../internal/room/RoomEventsDecrypterTest.kt | 36 ++++++++++++------- .../internal/sync/RoomEventCreatorTest.kt | 5 ++- .../kotlin/fixture/RoomEventFixture.kt | 14 ++++---- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/RoomState.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/RoomState.kt index 0dd9bbb..837066d 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/RoomState.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/RoomState.kt @@ -23,12 +23,11 @@ sealed class RoomEvent { data class Encrypted( @SerialName("event_id") override val eventId: EventId, @SerialName("timestamp") override val utcTimestamp: Long, - @SerialName("content") val content: String, @SerialName("author") override val author: RoomMember, @SerialName("meta") override val meta: MessageMeta, - @SerialName("encrypted_content") val encryptedContent: MegOlmV1, @SerialName("edited") val edited: Boolean = false, @SerialName("redacted") override val redacted: Boolean = false, + @SerialName("encrypted_content") val encryptedContent: MegOlmV1, ) : RoomEvent() { @Serializable diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt index ea39048..2bb35af 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt @@ -29,7 +29,6 @@ internal class RoomEventCreator( author = roomMembersService.find(roomId, this.senderId)!!, utcTimestamp = this.utcTimestamp, meta = MessageMeta.FromServer, - content = "Encrypted message", encryptedContent = RoomEvent.Encrypted.MegOlmV1( this.encryptedContent.cipherText, this.encryptedContent.deviceId, diff --git a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt index 23a5623..4283146 100644 --- a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt +++ b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt @@ -15,12 +15,12 @@ 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 = aMatrixRoomMessageEvent(encryptedContent = AN_ENCRYPTED_ROOM_CONTENT) +private val AN_ENCRYPTED_ROOM_MESSAGE = anEncryptedRoomMessageEvent(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)) +private val A_DECRYPTED_TEXT_CONTENT = DecryptedContent.TimelineText(aTimelineTextEventContent(body = A_DECRYPTED_MESSAGE_CONTENT)) private val A_USER_CREDENTIALS = aUserCredentials() private val json = Json { encodeDefaults = true } @@ -37,7 +37,7 @@ class RoomEventsDecrypterTest { @Test fun `given clear message event, when decrypting, then does nothing`() = runTest { - val aClearMessageEvent = aMatrixRoomMessageEvent(encryptedContent = null) + val aClearMessageEvent = aMatrixRoomMessageEvent() val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(aClearMessageEvent)) result shouldBeEqualTo listOf(aClearMessageEvent) @@ -45,42 +45,52 @@ class RoomEventsDecrypterTest { @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) + givenEncryptedMessage(AN_ENCRYPTED_ROOM_MESSAGE, decryptsTo = A_DECRYPTED_TEXT_CONTENT) val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(AN_ENCRYPTED_ROOM_MESSAGE)) - result shouldBeEqualTo listOf(AN_ENCRYPTED_ROOM_MESSAGE.copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null)) + result shouldBeEqualTo listOf(AN_ENCRYPTED_ROOM_MESSAGE.toText(A_DECRYPTED_MESSAGE_CONTENT)) } @Test fun `given encrypted reply event, when decrypting, then decrypts message and replyTo`() = runTest { - givenEncryptedReply(AN_ENCRYPTED_ROOM_REPLY, decryptsTo = A_DECRYPTED_CONTENT) + givenEncryptedReply(AN_ENCRYPTED_ROOM_REPLY, decryptsTo = A_DECRYPTED_TEXT_CONTENT) val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(AN_ENCRYPTED_ROOM_REPLY)) result shouldBeEqualTo listOf( AN_ENCRYPTED_ROOM_REPLY.copy( - message = (AN_ENCRYPTED_ROOM_REPLY.message as RoomEvent.Message).copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null), - replyingTo = (AN_ENCRYPTED_ROOM_REPLY.replyingTo as RoomEvent.Message).copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null), + message = (AN_ENCRYPTED_ROOM_REPLY.message as RoomEvent.Encrypted).toText(A_DECRYPTED_MESSAGE_CONTENT), + replyingTo = (AN_ENCRYPTED_ROOM_REPLY.replyingTo as RoomEvent.Encrypted).toText(A_DECRYPTED_MESSAGE_CONTENT), ) ) } - private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) { - val model = roomMessage.encryptedContent!!.toModel() + private fun givenEncryptedMessage(roomMessage: RoomEvent.Encrypted, 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 as RoomEvent.Message, decryptsTo) - givenEncryptedMessage(roomReply.replyingTo as RoomEvent.Message, decryptsTo) + givenEncryptedMessage(roomReply.message as RoomEvent.Encrypted, decryptsTo) + givenEncryptedMessage(roomReply.replyingTo as RoomEvent.Encrypted, decryptsTo) } } -private fun RoomEvent.Message.MegOlmV1.toModel() = EncryptedMessageContent.MegOlmV1( +private fun RoomEvent.Encrypted.MegOlmV1.toModel() = EncryptedMessageContent.MegOlmV1( this.cipherText, this.deviceId, this.senderKey, this.sessionId, ) + +private fun RoomEvent.Encrypted.toText(text: String) = RoomEvent.Message( + this.eventId, + this.utcTimestamp, + content = text, + this.author, + this.meta, + this.edited, + this.redacted, +) \ No newline at end of file diff --git a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt index b265549..38f1ed1 100644 --- a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt +++ b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt @@ -40,10 +40,9 @@ internal class RoomEventCreatorTest { val result = with(roomEventCreator) { megolmEvent.toRoomEvent(A_ROOM_ID) } - result shouldBeEqualTo aMatrixRoomMessageEvent( + result shouldBeEqualTo anEncryptedRoomMessageEvent( eventId = megolmEvent.eventId, utcTimestamp = megolmEvent.utcTimestamp, - content = "Encrypted message", author = A_SENDER, encryptedContent = megolmEvent.encryptedContent.toMegolm(), ) @@ -320,7 +319,7 @@ private fun RoomEvent.Message.toReplyEvent(messageContent: String) = anApiTimeli ) ) -private fun ApiEncryptedContent.toMegolm(): RoomEvent.Message.MegOlmV1 { +private fun ApiEncryptedContent.toMegolm(): RoomEvent.Encrypted.MegOlmV1 { require(this is ApiEncryptedContent.MegOlmV1) return aMegolmV1(this.cipherText, this.deviceId, this.senderKey, this.sessionId) } diff --git a/matrix/services/sync/src/testFixtures/kotlin/fixture/RoomEventFixture.kt b/matrix/services/sync/src/testFixtures/kotlin/fixture/RoomEventFixture.kt index 9b4d641..36bdf0b 100644 --- a/matrix/services/sync/src/testFixtures/kotlin/fixture/RoomEventFixture.kt +++ b/matrix/services/sync/src/testFixtures/kotlin/fixture/RoomEventFixture.kt @@ -10,9 +10,8 @@ fun aMatrixRoomMessageEvent( content: String = "message-content", author: RoomMember = aRoomMember(), meta: MessageMeta = MessageMeta.FromServer, - encryptedContent: RoomEvent.Message.MegOlmV1? = null, edited: Boolean = false, -) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, encryptedContent, edited) +) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, edited) fun aRoomImageMessageEvent( eventId: EventId = anEventId(), @@ -20,9 +19,8 @@ fun aRoomImageMessageEvent( content: RoomEvent.Image.ImageMeta = anImageMeta(), author: RoomMember = aRoomMember(), meta: MessageMeta = MessageMeta.FromServer, - encryptedContent: RoomEvent.Message.MegOlmV1? = null, edited: Boolean = false, -) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, encryptedContent, edited) +) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, edited) fun aRoomReplyMessageEvent( message: RoomEvent = aMatrixRoomMessageEvent(), @@ -32,19 +30,19 @@ fun aRoomReplyMessageEvent( fun anEncryptedRoomMessageEvent( eventId: EventId = anEventId(), utcTimestamp: Long = 0L, - content: String = "encrypted-content", author: RoomMember = aRoomMember(), meta: MessageMeta = MessageMeta.FromServer, - encryptedContent: RoomEvent.Message.MegOlmV1? = aMegolmV1(), + encryptedContent: RoomEvent.Encrypted.MegOlmV1 = aMegolmV1(), edited: Boolean = false, -) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, encryptedContent, edited) + redacted: Boolean = false, +) = RoomEvent.Encrypted(eventId, utcTimestamp, author, meta, edited, redacted, encryptedContent) fun aMegolmV1( cipherText: CipherText = CipherText("a-cipher"), deviceId: DeviceId = aDeviceId(), senderKey: String = "a-sender-key", sessionId: SessionId = aSessionId(), -) = RoomEvent.Message.MegOlmV1(cipherText, deviceId, senderKey, sessionId) +) = RoomEvent.Encrypted.MegOlmV1(cipherText, deviceId, senderKey, sessionId) fun anImageMeta( width: Int? = 100,