Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.apache.commons.codec.binary.ZBase32
import org.apache.commons.codec.digest.DigestUtils
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -38,10 +37,46 @@ import java.net.HttpURLConnection
class WkdClientTest {
private val context: Context = ApplicationProvider.getApplicationContext()

private val mockWebServerRule = FlowCryptMockWebServerRule(
TestConstants.MOCK_WEB_SERVER_PORT,
object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
val gson = ApiHelper.getInstance(ApplicationProvider.getApplicationContext()).gson

when (request.path) {
"/.well-known/openpgpkey/policy" -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
}

genLookupUrlPath(EXISTING_EMAIL) -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
PGPainless.generateKeyRing().simpleEcKeyRing(EXISTING_EMAIL).publicKey.armor()
)
}

genLookupUrlPath(NOT_EXISTING_EMAIL) -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
.setBody(
gson.toJson(
ApiError(
code = HttpURLConnection.HTTP_NOT_FOUND,
message = "Public key not found"
)
)
)
}
}

return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
})

@get:Rule
var ruleChain: TestRule = RuleChain
.outerRule(ClearAppSettingsRule())
.around(GrantPermissionRuleChooser.grant(android.Manifest.permission.POST_NOTIFICATIONS))
.around(mockWebServerRule)

@Test
fun existingEmailFlowCryptDomainTest() = runBlocking {
Expand Down Expand Up @@ -75,42 +110,6 @@ class WkdClientTest {
const val EXISTING_EMAIL = "existing@flowcrypt.test"
const val NOT_EXISTING_EMAIL = "not_existing@flowcrypt.test"

@get:ClassRule
@JvmStatic
val mockWebServerRule = FlowCryptMockWebServerRule(TestConstants.MOCK_WEB_SERVER_PORT,
object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
val gson = ApiHelper.getInstance(ApplicationProvider.getApplicationContext()).gson

when (request.path) {
"/.well-known/openpgpkey/policy" -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
}

genLookupUrlPath(EXISTING_EMAIL) -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
PGPainless.generateKeyRing().simpleEcKeyRing(EXISTING_EMAIL).publicKey.armor()
)
}

genLookupUrlPath(NOT_EXISTING_EMAIL) -> {
return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
.setBody(
gson.toJson(
ApiError(
code = HttpURLConnection.HTTP_NOT_FOUND,
message = "Public key not found"
)
)
)
}
}

return MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND)
}
})

private fun genLookupUrlPath(email: String): String {
val user = email.substringBefore("@")
val hu = ZBase32().encodeAsString(DigestUtils.sha1(user.toByteArray()))
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
id("com.android.application") version "8.9.1" apply false
id("com.android.application") version "8.9.2" apply false
id("org.jetbrains.kotlin.android") version "2.1.20" apply false
id("androidx.navigation.safeargs.kotlin") version "2.8.9" apply false
id("com.starter.easylauncher") version "6.4.0" apply false
Expand Down