diff --git a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt index 6abbc6094f..0d03cce7cf 100755 --- a/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt +++ b/features/rageshake/impl/src/test/kotlin/io/element/android/features/rageshake/impl/reporter/DefaultBugReporterTest.kt @@ -36,6 +36,7 @@ import okhttp3.MultipartReader import okhttp3.OkHttpClient import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest import okio.buffer import okio.source import org.junit.Test @@ -102,21 +103,7 @@ class DefaultBugReporterTest { server.start() val mockSessionStore = InMemorySessionStore().apply { - storeData( - SessionData( - userId = "@foo:eample.com", - deviceId = "ABCDEFGH", - homeserverUrl = "example.com", - accessToken = "AA", - isTokenValid = true, - loginType = LoginType.DIRECT, - loginTimestamp = null, - oidcData = null, - refreshToken = null, - slidingSyncProxy = null, - passphrase = null - ) - ) + storeData(mockSessionData("@foo:eample.com", "ABCDEFGH")) } val buildMeta = aBuildMeta() @@ -159,24 +146,7 @@ class DefaultBugReporterTest { ) val request = server.takeRequest() - val boundary = request.headers["Content-Type"]!!.split("=").last() - val foundValues = HashMap() - request.body.inputStream().source().buffer().use { - val multipartReader = MultipartReader(it, boundary) - // Just use simple parsing to detect basic properties - val regex = "form-data; name=\"(\\w*)\".*".toRegex() - multipartReader.use { - var part = multipartReader.nextPart() - while (part != null) { - part.headers["Content-Disposition"]?.let { contentDisposition -> - regex.find(contentDisposition)?.groupValues?.get(1)?.let { name -> - foundValues.put(name, part!!.body.readUtf8()) - } - } - part = multipartReader.nextPart() - } - } - } + val foundValues = collectValuesFromFormData(request) assertThat(foundValues["app"]).isEqualTo("element-x-android") assertThat(foundValues["can_contact"]).isEqualTo("true") @@ -192,6 +162,88 @@ class DefaultBugReporterTest { server.shutdown() } + @Test + fun `test sendBugReport should not report device_keys if not known`() = runTest { + val server = MockWebServer() + server.enqueue( + MockResponse() + .setResponseCode(200) + ) + server.start() + + val mockSessionStore = InMemorySessionStore().apply { + storeData(mockSessionData("@foo:eample.com", null)) + } + + val buildMeta = aBuildMeta() + val fakeEncryptionService = FakeEncryptionService() + val matrixClient = FakeMatrixClient(encryptionService = fakeEncryptionService) + + fakeEncryptionService.givenDeviceKeys(null, null) + val sut = DefaultBugReporter( + context = RuntimeEnvironment.getApplication(), + screenshotHolder = FakeScreenshotHolder(), + crashDataStore = FakeCrashDataStore(), + coroutineDispatchers = testCoroutineDispatchers(), + okHttpClient = { OkHttpClient.Builder().build() }, + userAgentProvider = DefaultUserAgentProvider(buildMeta, FakeSdkMetadata("123456789")), + sessionStore = mockSessionStore, + buildMeta = buildMeta, + bugReporterUrlProvider = { server.url("/") }, + sdkMetadata = FakeSdkMetadata("123456789"), + matrixClientProvider = FakeMatrixClientProvider(getClient = { Result.success(matrixClient) }) + ) + + sut.sendBugReport( + withDevicesLogs = true, + withCrashLogs = true, + withScreenshot = true, + theBugDescription = "a bug occurred", + canContact = true, + listener = null + ) + val request = server.takeRequest() + + val foundValues = collectValuesFromFormData(request) + assertThat(foundValues["device_keys"]).isNull() + server.shutdown() + } + + private fun collectValuesFromFormData(request: RecordedRequest): HashMap { + val boundary = request.headers["Content-Type"]!!.split("=").last() + val foundValues = HashMap() + request.body.inputStream().source().buffer().use { + val multipartReader = MultipartReader(it, boundary) + // Just use simple parsing to detect basic properties + val regex = "form-data; name=\"(\\w*)\".*".toRegex() + multipartReader.use { + var part = multipartReader.nextPart() + while (part != null) { + part.headers["Content-Disposition"]?.let { contentDisposition -> + regex.find(contentDisposition)?.groupValues?.get(1)?.let { name -> + foundValues.put(name, part!!.body.readUtf8()) + } + } + part = multipartReader.nextPart() + } + } + } + return foundValues + } + + private fun mockSessionData(userId: String, deviceId: String) = SessionData( + userId = userId, + deviceId = deviceId, + homeserverUrl = "example.com", + accessToken = "AA", + isTokenValid = true, + loginType = LoginType.DIRECT, + loginTimestamp = null, + oidcData = null, + refreshToken = null, + slidingSyncProxy = null, + passphrase = null + ) @Test fun `test sendBugReport error`() = runTest { val server = MockWebServer()