@@ -24,7 +24,6 @@ import org.apache.commons.codec.binary.ZBase32
2424import org.apache.commons.codec.digest.DigestUtils
2525import org.junit.Assert.assertNull
2626import org.junit.Assert.assertTrue
27- import org.junit.ClassRule
2827import org.junit.Rule
2928import org.junit.Test
3029import org.junit.rules.RuleChain
@@ -38,10 +37,46 @@ import java.net.HttpURLConnection
3837class WkdClientTest {
3938 private val context: Context = ApplicationProvider .getApplicationContext()
4039
40+ private val mockWebServerRule = FlowCryptMockWebServerRule (
41+ TestConstants .MOCK_WEB_SERVER_PORT ,
42+ object : Dispatcher () {
43+ override fun dispatch (request : RecordedRequest ): MockResponse {
44+ val gson = ApiHelper .getInstance(ApplicationProvider .getApplicationContext()).gson
45+
46+ when (request.path) {
47+ " /.well-known/openpgpkey/policy" -> {
48+ return MockResponse ().setResponseCode(HttpURLConnection .HTTP_OK )
49+ }
50+
51+ genLookupUrlPath(EXISTING_EMAIL ) -> {
52+ return MockResponse ().setResponseCode(HttpURLConnection .HTTP_OK )
53+ .setBody(
54+ PGPainless .generateKeyRing().simpleEcKeyRing(EXISTING_EMAIL ).publicKey.armor()
55+ )
56+ }
57+
58+ genLookupUrlPath(NOT_EXISTING_EMAIL ) -> {
59+ return MockResponse ().setResponseCode(HttpURLConnection .HTTP_NOT_FOUND )
60+ .setBody(
61+ gson.toJson(
62+ ApiError (
63+ code = HttpURLConnection .HTTP_NOT_FOUND ,
64+ message = " Public key not found"
65+ )
66+ )
67+ )
68+ }
69+ }
70+
71+ return MockResponse ().setResponseCode(HttpURLConnection .HTTP_NOT_FOUND )
72+ }
73+ })
74+
4175 @get:Rule
4276 var ruleChain: TestRule = RuleChain
4377 .outerRule(ClearAppSettingsRule ())
4478 .around(GrantPermissionRuleChooser .grant(android.Manifest .permission.POST_NOTIFICATIONS ))
79+ .around(mockWebServerRule)
4580
4681 @Test
4782 fun existingEmailFlowCryptDomainTest () = runBlocking {
@@ -75,42 +110,6 @@ class WkdClientTest {
75110 const val EXISTING_EMAIL = " existing@flowcrypt.test"
76111 const val NOT_EXISTING_EMAIL = " not_existing@flowcrypt.test"
77112
78- @get:ClassRule
79- @JvmStatic
80- val mockWebServerRule = FlowCryptMockWebServerRule (TestConstants .MOCK_WEB_SERVER_PORT ,
81- object : Dispatcher () {
82- override fun dispatch (request : RecordedRequest ): MockResponse {
83- val gson = ApiHelper .getInstance(ApplicationProvider .getApplicationContext()).gson
84-
85- when (request.path) {
86- " /.well-known/openpgpkey/policy" -> {
87- return MockResponse ().setResponseCode(HttpURLConnection .HTTP_OK )
88- }
89-
90- genLookupUrlPath(EXISTING_EMAIL ) -> {
91- return MockResponse ().setResponseCode(HttpURLConnection .HTTP_OK )
92- .setBody(
93- PGPainless .generateKeyRing().simpleEcKeyRing(EXISTING_EMAIL ).publicKey.armor()
94- )
95- }
96-
97- genLookupUrlPath(NOT_EXISTING_EMAIL ) -> {
98- return MockResponse ().setResponseCode(HttpURLConnection .HTTP_NOT_FOUND )
99- .setBody(
100- gson.toJson(
101- ApiError (
102- code = HttpURLConnection .HTTP_NOT_FOUND ,
103- message = " Public key not found"
104- )
105- )
106- )
107- }
108- }
109-
110- return MockResponse ().setResponseCode(HttpURLConnection .HTTP_NOT_FOUND )
111- }
112- })
113-
114113 private fun genLookupUrlPath (email : String ): String {
115114 val user = email.substringBefore(" @" )
116115 val hu = ZBase32 ().encodeAsString(DigestUtils .sha1(user.toByteArray()))
0 commit comments