Improve coverage
This commit is contained in:
parent
6ec2b74982
commit
15cd05ebff
1 changed files with 85 additions and 33 deletions
|
|
@ -36,6 +36,7 @@ import okhttp3.MultipartReader
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.mockwebserver.MockResponse
|
import okhttp3.mockwebserver.MockResponse
|
||||||
import okhttp3.mockwebserver.MockWebServer
|
import okhttp3.mockwebserver.MockWebServer
|
||||||
|
import okhttp3.mockwebserver.RecordedRequest
|
||||||
import okio.buffer
|
import okio.buffer
|
||||||
import okio.source
|
import okio.source
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
@ -102,21 +103,7 @@ class DefaultBugReporterTest {
|
||||||
server.start()
|
server.start()
|
||||||
|
|
||||||
val mockSessionStore = InMemorySessionStore().apply {
|
val mockSessionStore = InMemorySessionStore().apply {
|
||||||
storeData(
|
storeData(mockSessionData("@foo:eample.com", "ABCDEFGH"))
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val buildMeta = aBuildMeta()
|
val buildMeta = aBuildMeta()
|
||||||
|
|
@ -159,24 +146,7 @@ class DefaultBugReporterTest {
|
||||||
)
|
)
|
||||||
val request = server.takeRequest()
|
val request = server.takeRequest()
|
||||||
|
|
||||||
val boundary = request.headers["Content-Type"]!!.split("=").last()
|
val foundValues = collectValuesFromFormData(request)
|
||||||
val foundValues = HashMap<String, String>()
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(foundValues["app"]).isEqualTo("element-x-android")
|
assertThat(foundValues["app"]).isEqualTo("element-x-android")
|
||||||
assertThat(foundValues["can_contact"]).isEqualTo("true")
|
assertThat(foundValues["can_contact"]).isEqualTo("true")
|
||||||
|
|
@ -192,6 +162,88 @@ class DefaultBugReporterTest {
|
||||||
server.shutdown()
|
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<String, String> {
|
||||||
|
val boundary = request.headers["Content-Type"]!!.split("=").last()
|
||||||
|
val foundValues = HashMap<String, String>()
|
||||||
|
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
|
@Test
|
||||||
fun `test sendBugReport error`() = runTest {
|
fun `test sendBugReport error`() = runTest {
|
||||||
val server = MockWebServer()
|
val server = MockWebServer()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue