adding test for clear messaging (in prep for images), which uncovered bad test state

This commit is contained in:
Adam Brown 2022-06-11 12:28:18 +01:00
parent 716790712b
commit fe28496cba
4 changed files with 70 additions and 28 deletions

View File

@ -65,31 +65,14 @@ class SmokeTest {
@Test
@Order(4)
fun `can send and receive encrypted messages`() = testAfterInitialSync { alice, bob ->
val message = "from alice to bob : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
alice.sendEncryptedMessage(SharedState.sharedRoom, message.content)
bob.expectMessage(SharedState.sharedRoom, message)
val message2 = "from bob to alice : ${System.currentTimeMillis()}".from(SharedState.bob.roomMember)
bob.sendEncryptedMessage(SharedState.sharedRoom, message2.content)
alice.expectMessage(SharedState.sharedRoom, message2)
val aliceSecondDevice = TestMatrix(SharedState.alice).also { it.newlogin() }
aliceSecondDevice.client.syncService().startSyncing().collectAsync {
val message3 = "from alice to bob and alice's second device : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
alice.sendEncryptedMessage(SharedState.sharedRoom, message3.content)
aliceSecondDevice.expectMessage(SharedState.sharedRoom, message3)
bob.expectMessage(SharedState.sharedRoom, message3)
val message4 = "from alice's second device to bob and alice's first device : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
aliceSecondDevice.sendEncryptedMessage(SharedState.sharedRoom, message4.content)
alice.expectMessage(SharedState.sharedRoom, message4)
bob.expectMessage(SharedState.sharedRoom, message4)
}
}
fun `can send and receive clear text messages`() = testTextMessaging(isEncrypted = false)
@Test
@Order(5)
fun `can send and receive encrypted text messages`() = testTextMessaging(isEncrypted = true)
@Test
@Order(6)
fun `can request and verify devices`() = testAfterInitialSync { alice, bob ->
alice.client.cryptoService().verificationAction(Verification.Action.Request(bob.userId(), bob.deviceId()))
alice.client.cryptoService().verificationState().automaticVerification(alice).expectAsync { it == Verification.State.Done }
@ -110,6 +93,30 @@ class SmokeTest {
result shouldBeEqualTo listOf(RoomId(value = "!qOSENTtFUuCEKJSVzl:matrix.org"))
}
private fun testTextMessaging(isEncrypted: Boolean) = testAfterInitialSync { alice, bob ->
val message = "from alice to bob : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
alice.sendTextMessage(SharedState.sharedRoom, message.content, isEncrypted)
bob.expectTextMessage(SharedState.sharedRoom, message)
val message2 = "from bob to alice : ${System.currentTimeMillis()}".from(SharedState.bob.roomMember)
bob.sendTextMessage(SharedState.sharedRoom, message2.content, isEncrypted)
alice.expectTextMessage(SharedState.sharedRoom, message2)
val aliceSecondDevice = testMatrix(SharedState.alice).also { it.newlogin() }
aliceSecondDevice.client.syncService().startSyncing().collectAsync {
val message3 = "from alice to bob and alice's second device : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
alice.sendTextMessage(SharedState.sharedRoom, message3.content, isEncrypted)
aliceSecondDevice.expectTextMessage(SharedState.sharedRoom, message3)
bob.expectTextMessage(SharedState.sharedRoom, message3)
val message4 = "from alice's second device to bob and alice's first device : ${System.currentTimeMillis()}".from(SharedState.alice.roomMember)
aliceSecondDevice.sendTextMessage(SharedState.sharedRoom, message4.content, isEncrypted)
alice.expectTextMessage(SharedState.sharedRoom, message4)
bob.expectTextMessage(SharedState.sharedRoom, message4)
}
}
}
private suspend fun createAndRegisterAccount(): TestUser {
@ -135,7 +142,6 @@ private suspend fun login(user: TestUser) {
.authService()
.login(AuthService.LoginRequest(userName = user.roomMember.id.value, password = user.password, serverUrl = null))
result shouldBeInstanceOf AuthService.LoginResult.Success::class.java
(result as AuthService.LoginResult.Success).userCredentials.let { credentials ->
credentials.accessToken shouldNotBeEqualTo null

View File

@ -3,6 +3,7 @@
package test
import TestMessage
import TestUser
import app.dapk.st.core.extensions.ifNull
import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.message.MessageService
@ -31,6 +32,8 @@ fun restoreLoginAndInitialSync(m1: TestMatrix, m2: TestMatrix, testBody: suspend
println("restore login 2")
m2.restoreLogin()
val testHelper = MatrixTestScope(this)
testHelper.testMatrix(m1)
testHelper.testMatrix(m2)
with(testHelper) {
combine(m1.client.syncService().startSyncing(), m2.client.syncService().startSyncing()) { _, _ -> }.collectAsync {
m1.client.syncService().overview().first()
@ -38,6 +41,7 @@ fun restoreLoginAndInitialSync(m1: TestMatrix, m2: TestMatrix, testBody: suspend
testBody(testHelper, m1, m2)
}
}
testHelper.release()
}
}
@ -54,6 +58,7 @@ suspend fun <T> Flow<T>.collectAsync(scope: CoroutineScope, block: suspend () ->
class MatrixTestScope(private val testScope: TestScope) {
private val inProgressExpects = mutableListOf<Deferred<*>>()
private val inProgressInstances = mutableListOf<TestMatrix>()
suspend fun <T> Flow<T>.collectAsync(block: suspend () -> Unit) {
collectAsync(testScope, block)
@ -118,18 +123,18 @@ class MatrixTestScope(private val testScope: TestScope) {
.expect { it.any { it.roomId == roomId } }
}
suspend fun TestMatrix.expectMessage(roomId: RoomId, message: TestMessage) {
suspend fun TestMatrix.expectTextMessage(roomId: RoomId, message: TestMessage) {
this.client.syncService().room(roomId)
.map { it.events.filterIsInstance<RoomEvent.Message>().map { TestMessage(it.content, it.author) }.firstOrNull() }
.assert(message)
}
suspend fun TestMatrix.sendEncryptedMessage(roomId: RoomId, content: String) {
suspend fun TestMatrix.sendTextMessage(roomId: RoomId, content: String, isEncrypted: Boolean) {
this.client.messageService().scheduleMessage(
MessageService.Message.TextMessage(
content = MessageService.Message.Content.TextContent(body = content),
roomId = roomId,
sendEncrypted = true,
sendEncrypted = isEncrypted,
localId = "local.${UUID.randomUUID()}",
timestampUtc = System.currentTimeMillis(),
)
@ -143,4 +148,14 @@ class MatrixTestScope(private val testScope: TestScope) {
}
}
fun testMatrix(user: TestUser) = TestMatrix(user).also {
inProgressInstances.add(it)
}
fun testMatrix(testMatrix: TestMatrix) = inProgressInstances.add(testMatrix)
suspend fun release() {
inProgressInstances.forEach { it.release() }
}
}

View File

@ -33,8 +33,7 @@ import app.dapk.st.matrix.sync.internal.room.MessageDecrypter
import app.dapk.st.olm.DeviceKeyFactory
import app.dapk.st.olm.OlmPersistenceWrapper
import app.dapk.st.olm.OlmWrapper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import test.impl.InMemoryDatabase
@ -277,6 +276,24 @@ class TestMatrix(
suspend fun deviceId() = storeModule.credentialsStore().credentials()!!.deviceId
suspend fun userId() = storeModule.credentialsStore().credentials()!!.userId
suspend fun release() {
coroutineDispatchers.global.waitForCancel()
coroutineDispatchers.io.waitForCancel()
coroutineDispatchers.main.waitForCancel()
}
}
private suspend fun CoroutineDispatcher.waitForCancel() {
if (this.isActive) {
this.job.cancelAndJoin()
}
}
private suspend fun CoroutineScope.waitForCancel() {
if (this.isActive) {
this.coroutineContext.job.cancelAndJoin()
}
}
class JavaBase64 : Base64 {

View File

@ -19,4 +19,8 @@ object InMemoryDatabase {
})
}
fun temp(): DapkDb {
return DapkDb(JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY))
}
}