diff options
| author | Albin <albin@mullvad.net> | 2023-03-22 09:07:53 +0100 |
|---|---|---|
| committer | Albin <albin@mullvad.net> | 2023-03-22 10:39:58 +0100 |
| commit | 68c356e5afa2165cadef86d43966e51d9203102c (patch) | |
| tree | 0a172dacad334066be40ea1239691ed963139dac /android/test/mockapi/src | |
| parent | e5d106c9260e0f282c75d105da7b0247535cdfdd (diff) | |
| download | mullvadvpn-68c356e5afa2165cadef86d43966e51d9203102c.tar.xz mullvadvpn-68c356e5afa2165cadef86d43966e51d9203102c.zip | |
Apply project-wide `kotlinLangStyle` formatting
Diffstat (limited to 'android/test/mockapi/src')
3 files changed, 79 insertions, 93 deletions
diff --git a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiDispatcher.kt b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiDispatcher.kt index e3708a47f1..06f7164034 100644 --- a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiDispatcher.kt +++ b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiDispatcher.kt @@ -29,8 +29,10 @@ class MockApiDispatcher : Dispatcher() { AUTH_TOKEN_URL_PATH -> handleLoginRequest(request.body) DEVICES_URL_PATH -> { when (request.method) { - "get", "GET" -> handleDeviceListRequest() - "post", "POST" -> handleDeviceCreationRequest(request.body) + "get", + "GET" -> handleDeviceListRequest() + "post", + "POST" -> handleDeviceCreationRequest(request.body) else -> MockResponse().setResponseCode(404) } } @@ -51,9 +53,10 @@ class MockApiDispatcher : Dispatcher() { .addJsonHeader() .setBody( accessTokenJsonResponse( - accessToken = DUMMY_ACCESS_TOKEN, - expiry = currentUtcTimeWithOffsetZero().plusDays(1) - ).toString() + accessToken = DUMMY_ACCESS_TOKEN, + expiry = currentUtcTimeWithOffsetZero().plusDays(1) + ) + .toString() ) } else { Log.e( @@ -69,13 +72,9 @@ class MockApiDispatcher : Dispatcher() { MockResponse() .setResponseCode(200) .addJsonHeader() - .setBody( - accountInfoJson( - id = DUMMY_ID, - expiry = expiry - ).toString() - ) - } ?: MockResponse().setResponseCode(400) + .setBody(accountInfoJson(id = DUMMY_ID, expiry = expiry).toString()) + } + ?: MockResponse().setResponseCode(400) } private fun handleDeviceInfoRequest(): MockResponse { @@ -85,33 +84,36 @@ class MockApiDispatcher : Dispatcher() { .addJsonHeader() .setBody( deviceJson( - id = DUMMY_ID, - name = DUMMY_DEVICE_NAME, - publicKey = cachedKey, - creationDate = currentUtcTimeWithOffsetZero().minusDays(1) - ).toString() + id = DUMMY_ID, + name = DUMMY_DEVICE_NAME, + publicKey = cachedKey, + creationDate = currentUtcTimeWithOffsetZero().minusDays(1) + ) + .toString() ) - } ?: MockResponse().setResponseCode(400) + } + ?: MockResponse().setResponseCode(400) } private fun handleDeviceCreationRequest(body: Buffer): MockResponse { - return body.getPubKey() - .also { newKey -> - cachedPubKeyFromAppUnderTest = newKey - } + return body + .getPubKey() + .also { newKey -> cachedPubKeyFromAppUnderTest = newKey } ?.let { newKey -> MockResponse() .setResponseCode(201) .addJsonHeader() .setBody( deviceJson( - id = DUMMY_ID, - name = DUMMY_DEVICE_NAME, - publicKey = newKey, - creationDate = currentUtcTimeWithOffsetZero().minusDays(1) - ).toString() + id = DUMMY_ID, + name = DUMMY_DEVICE_NAME, + publicKey = newKey, + creationDate = currentUtcTimeWithOffsetZero().minusDays(1) + ) + .toString() ) - } ?: MockResponse().setResponseCode(400) + } + ?: MockResponse().setResponseCode(400) } private fun handleDeviceListRequest(): MockResponse { @@ -120,15 +122,18 @@ class MockApiDispatcher : Dispatcher() { .setResponseCode(200) .addJsonHeader() .setBody( - JSONArray().put( - deviceJson( - id = DUMMY_ID, - name = DUMMY_DEVICE_NAME, - publicKey = cachedKey, - creationDate = currentUtcTimeWithOffsetZero().minusDays(1) + JSONArray() + .put( + deviceJson( + id = DUMMY_ID, + name = DUMMY_DEVICE_NAME, + publicKey = cachedKey, + creationDate = currentUtcTimeWithOffsetZero().minusDays(1) + ) ) - ).toString() + .toString() ) - } ?: MockResponse().setResponseCode(400) + } + ?: MockResponse().setResponseCode(400) } } diff --git a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiTest.kt b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiTest.kt index ba5ba01810..bb5c20eebb 100644 --- a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiTest.kt +++ b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/MockApiTest.kt @@ -24,21 +24,15 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) abstract class MockApiTest { - @Rule - @JvmField - val rule = CaptureScreenshotOnFailedTestRule(LOG_TAG) + @Rule @JvmField val rule = CaptureScreenshotOnFailedTestRule(LOG_TAG) @Rule @JvmField - val permissionRule: GrantPermissionRule = GrantPermissionRule.grant( - WRITE_EXTERNAL_STORAGE, - READ_EXTERNAL_STORAGE - ) + val permissionRule: GrantPermissionRule = + GrantPermissionRule.grant(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE) protected val apiDispatcher = MockApiDispatcher() - private val mockWebServer = MockWebServer().apply { - dispatcher = apiDispatcher - } + private val mockWebServer = MockWebServer().apply { dispatcher = apiDispatcher } lateinit var device: UiDevice lateinit var targetContext: Context @@ -50,10 +44,7 @@ abstract class MockApiTest { device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) targetContext = InstrumentationRegistry.getInstrumentation().targetContext - app = AppInteractor( - device, - targetContext - ) + app = AppInteractor(device, targetContext) mockWebServer.start() Log.d(LOG_TAG, "Mocked web server started using port: ${mockWebServer.port}") @@ -66,16 +57,14 @@ abstract class MockApiTest { } private fun createEndpoint(port: Int): CustomApiEndpointConfiguration { - val mockApiSocket = InetSocketAddress( - InetAddress.getLocalHost(), - port - ) - val api = ApiEndpoint( - address = mockApiSocket, - disableAddressCache = true, - disableTls = true, - forceDirectConnection = true - ) + val mockApiSocket = InetSocketAddress(InetAddress.getLocalHost(), port) + val api = + ApiEndpoint( + address = mockApiSocket, + disableAddressCache = true, + disableTls = true, + forceDirectConnection = true + ) return CustomApiEndpointConfiguration(api) } } diff --git a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/util/JsonUtils.kt b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/util/JsonUtils.kt index 145cbafbd2..b76c4d4278 100644 --- a/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/util/JsonUtils.kt +++ b/android/test/mockapi/src/main/kotlin/net/mullvad/mullvadvpn/test/mockapi/util/JsonUtils.kt @@ -5,38 +5,30 @@ import org.joda.time.DateTime import org.json.JSONArray import org.json.JSONObject -fun accountInfoJson( - id: String, - expiry: DateTime -) = JSONObject().apply { - put("id", id) - put("expiry", expiry.formatStrictlyAccordingToIso8601AndRfc3339()) - put("max_ports", 5) - put("can_add_ports", true) - put("max_devices", 5) - put("can_add_devices", true) -} +fun accountInfoJson(id: String, expiry: DateTime) = + JSONObject().apply { + put("id", id) + put("expiry", expiry.formatStrictlyAccordingToIso8601AndRfc3339()) + put("max_ports", 5) + put("can_add_ports", true) + put("max_devices", 5) + put("can_add_devices", true) + } -fun deviceJson( - id: String, - name: String, - publicKey: String, - creationDate: DateTime -) = JSONObject().apply { - put("id", id) - put("name", name) - put("pubkey", publicKey) - put("hijack_dns", true) - put("created", creationDate.formatStrictlyAccordingToIso8601AndRfc3339()) - put("ipv4_address", "127.0.0.1/32") - put("ipv6_address", "fc00::1/128") - put("ports", JSONArray()) -} +fun deviceJson(id: String, name: String, publicKey: String, creationDate: DateTime) = + JSONObject().apply { + put("id", id) + put("name", name) + put("pubkey", publicKey) + put("hijack_dns", true) + put("created", creationDate.formatStrictlyAccordingToIso8601AndRfc3339()) + put("ipv4_address", "127.0.0.1/32") + put("ipv6_address", "fc00::1/128") + put("ports", JSONArray()) + } -fun accessTokenJsonResponse( - accessToken: String, - expiry: DateTime -) = JSONObject().apply { - put("access_token", accessToken) - put("expiry", expiry.formatStrictlyAccordingToIso8601AndRfc3339()) -} +fun accessTokenJsonResponse(accessToken: String, expiry: DateTime) = + JSONObject().apply { + put("access_token", accessToken) + put("expiry", expiry.formatStrictlyAccordingToIso8601AndRfc3339()) + } |
