updating tests

This commit is contained in:
Adam Brown 2022-10-17 21:15:21 +01:00
parent a3daea199d
commit c9fb1b102e
5 changed files with 32 additions and 27 deletions

View File

@ -23,12 +23,11 @@ sealed class RoomEvent {
data class Encrypted( data class Encrypted(
@SerialName("event_id") override val eventId: EventId, @SerialName("event_id") override val eventId: EventId,
@SerialName("timestamp") override val utcTimestamp: Long, @SerialName("timestamp") override val utcTimestamp: Long,
@SerialName("content") val content: String,
@SerialName("author") override val author: RoomMember, @SerialName("author") override val author: RoomMember,
@SerialName("meta") override val meta: MessageMeta, @SerialName("meta") override val meta: MessageMeta,
@SerialName("encrypted_content") val encryptedContent: MegOlmV1,
@SerialName("edited") val edited: Boolean = false, @SerialName("edited") val edited: Boolean = false,
@SerialName("redacted") override val redacted: Boolean = false, @SerialName("redacted") override val redacted: Boolean = false,
@SerialName("encrypted_content") val encryptedContent: MegOlmV1,
) : RoomEvent() { ) : RoomEvent() {
@Serializable @Serializable

View File

@ -29,7 +29,6 @@ internal class RoomEventCreator(
author = roomMembersService.find(roomId, this.senderId)!!, author = roomMembersService.find(roomId, this.senderId)!!,
utcTimestamp = this.utcTimestamp, utcTimestamp = this.utcTimestamp,
meta = MessageMeta.FromServer, meta = MessageMeta.FromServer,
content = "Encrypted message",
encryptedContent = RoomEvent.Encrypted.MegOlmV1( encryptedContent = RoomEvent.Encrypted.MegOlmV1(
this.encryptedContent.cipherText, this.encryptedContent.cipherText,
this.encryptedContent.deviceId, this.encryptedContent.deviceId,

View File

@ -15,12 +15,12 @@ import org.junit.Test
private const val A_DECRYPTED_MESSAGE_CONTENT = "decrypted - content" private const val A_DECRYPTED_MESSAGE_CONTENT = "decrypted - content"
private val AN_ENCRYPTED_ROOM_CONTENT = aMegolmV1() 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( private val AN_ENCRYPTED_ROOM_REPLY = aRoomReplyMessageEvent(
message = AN_ENCRYPTED_ROOM_MESSAGE, message = AN_ENCRYPTED_ROOM_MESSAGE,
replyingTo = AN_ENCRYPTED_ROOM_MESSAGE.copy(eventId = anEventId("other-event")) 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 A_USER_CREDENTIALS = aUserCredentials()
private val json = Json { encodeDefaults = true } private val json = Json { encodeDefaults = true }
@ -37,7 +37,7 @@ class RoomEventsDecrypterTest {
@Test @Test
fun `given clear message event, when decrypting, then does nothing`() = runTest { 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)) val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(aClearMessageEvent))
result shouldBeEqualTo listOf(aClearMessageEvent) result shouldBeEqualTo listOf(aClearMessageEvent)
@ -45,42 +45,52 @@ class RoomEventsDecrypterTest {
@Test @Test
fun `given encrypted message event, when decrypting, then applies decrypted body and removes encrypted content`() = runTest { 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)) 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 @Test
fun `given encrypted reply event, when decrypting, then decrypts message and replyTo`() = runTest { 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)) val result = roomEventsDecrypter.decryptRoomEvents(A_USER_CREDENTIALS, listOf(AN_ENCRYPTED_ROOM_REPLY))
result shouldBeEqualTo listOf( result shouldBeEqualTo listOf(
AN_ENCRYPTED_ROOM_REPLY.copy( AN_ENCRYPTED_ROOM_REPLY.copy(
message = (AN_ENCRYPTED_ROOM_REPLY.message 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.Message).copy(content = A_DECRYPTED_MESSAGE_CONTENT, encryptedContent = null), replyingTo = (AN_ENCRYPTED_ROOM_REPLY.replyingTo as RoomEvent.Encrypted).toText(A_DECRYPTED_MESSAGE_CONTENT),
) )
) )
} }
private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) { private fun givenEncryptedMessage(roomMessage: RoomEvent.Encrypted, decryptsTo: DecryptedContent) {
val model = roomMessage.encryptedContent!!.toModel() val model = roomMessage.encryptedContent.toModel()
fakeMessageDecrypter.givenDecrypt(model) fakeMessageDecrypter.givenDecrypt(model)
.returns(aDecryptionSuccessResult(payload = JsonString(json.encodeToString(DecryptedContent.serializer(), decryptsTo)))) .returns(aDecryptionSuccessResult(payload = JsonString(json.encodeToString(DecryptedContent.serializer(), decryptsTo))))
} }
private fun givenEncryptedReply(roomReply: RoomEvent.Reply, decryptsTo: DecryptedContent) { private fun givenEncryptedReply(roomReply: RoomEvent.Reply, decryptsTo: DecryptedContent) {
givenEncryptedMessage(roomReply.message as RoomEvent.Message, decryptsTo) givenEncryptedMessage(roomReply.message as RoomEvent.Encrypted, decryptsTo)
givenEncryptedMessage(roomReply.replyingTo as RoomEvent.Message, 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.cipherText,
this.deviceId, this.deviceId,
this.senderKey, this.senderKey,
this.sessionId, 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,
)

View File

@ -40,10 +40,9 @@ internal class RoomEventCreatorTest {
val result = with(roomEventCreator) { megolmEvent.toRoomEvent(A_ROOM_ID) } val result = with(roomEventCreator) { megolmEvent.toRoomEvent(A_ROOM_ID) }
result shouldBeEqualTo aMatrixRoomMessageEvent( result shouldBeEqualTo anEncryptedRoomMessageEvent(
eventId = megolmEvent.eventId, eventId = megolmEvent.eventId,
utcTimestamp = megolmEvent.utcTimestamp, utcTimestamp = megolmEvent.utcTimestamp,
content = "Encrypted message",
author = A_SENDER, author = A_SENDER,
encryptedContent = megolmEvent.encryptedContent.toMegolm(), 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) require(this is ApiEncryptedContent.MegOlmV1)
return aMegolmV1(this.cipherText, this.deviceId, this.senderKey, this.sessionId) return aMegolmV1(this.cipherText, this.deviceId, this.senderKey, this.sessionId)
} }

View File

@ -10,9 +10,8 @@ fun aMatrixRoomMessageEvent(
content: String = "message-content", content: String = "message-content",
author: RoomMember = aRoomMember(), author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer, meta: MessageMeta = MessageMeta.FromServer,
encryptedContent: RoomEvent.Message.MegOlmV1? = null,
edited: Boolean = false, edited: Boolean = false,
) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, encryptedContent, edited) ) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, edited)
fun aRoomImageMessageEvent( fun aRoomImageMessageEvent(
eventId: EventId = anEventId(), eventId: EventId = anEventId(),
@ -20,9 +19,8 @@ fun aRoomImageMessageEvent(
content: RoomEvent.Image.ImageMeta = anImageMeta(), content: RoomEvent.Image.ImageMeta = anImageMeta(),
author: RoomMember = aRoomMember(), author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer, meta: MessageMeta = MessageMeta.FromServer,
encryptedContent: RoomEvent.Message.MegOlmV1? = null,
edited: Boolean = false, edited: Boolean = false,
) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, encryptedContent, edited) ) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, edited)
fun aRoomReplyMessageEvent( fun aRoomReplyMessageEvent(
message: RoomEvent = aMatrixRoomMessageEvent(), message: RoomEvent = aMatrixRoomMessageEvent(),
@ -32,19 +30,19 @@ fun aRoomReplyMessageEvent(
fun anEncryptedRoomMessageEvent( fun anEncryptedRoomMessageEvent(
eventId: EventId = anEventId(), eventId: EventId = anEventId(),
utcTimestamp: Long = 0L, utcTimestamp: Long = 0L,
content: String = "encrypted-content",
author: RoomMember = aRoomMember(), author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer, meta: MessageMeta = MessageMeta.FromServer,
encryptedContent: RoomEvent.Message.MegOlmV1? = aMegolmV1(), encryptedContent: RoomEvent.Encrypted.MegOlmV1 = aMegolmV1(),
edited: Boolean = false, 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( fun aMegolmV1(
cipherText: CipherText = CipherText("a-cipher"), cipherText: CipherText = CipherText("a-cipher"),
deviceId: DeviceId = aDeviceId(), deviceId: DeviceId = aDeviceId(),
senderKey: String = "a-sender-key", senderKey: String = "a-sender-key",
sessionId: SessionId = aSessionId(), sessionId: SessionId = aSessionId(),
) = RoomEvent.Message.MegOlmV1(cipherText, deviceId, senderKey, sessionId) ) = RoomEvent.Encrypted.MegOlmV1(cipherText, deviceId, senderKey, sessionId)
fun anImageMeta( fun anImageMeta(
width: Int? = 100, width: Int? = 100,