|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.firefox.vpn |
| 6 | + |
| 7 | +import android.content.Intent |
| 8 | +import android.net.Uri |
| 9 | +import androidx.test.espresso.Espresso.onView |
| 10 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 11 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 12 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 13 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 14 | +import androidx.test.filters.FlakyTest |
| 15 | +import androidx.test.filters.LargeTest |
| 16 | +import androidx.test.rule.ActivityTestRule |
| 17 | +import kotlinx.coroutines.CompletableDeferred |
| 18 | +import kotlinx.coroutines.delay |
| 19 | +import kotlinx.coroutines.runBlocking |
| 20 | +import kotlinx.coroutines.withTimeout |
| 21 | +import org.junit.Assert.assertNotNull |
| 22 | +import org.junit.Rule |
| 23 | +import org.junit.Test |
| 24 | +import org.junit.runner.RunWith |
| 25 | +import org.mozilla.firefox.vpn.splash.SplashActivity |
| 26 | + |
| 27 | +@LargeTest |
| 28 | +@RunWith(AndroidJUnit4::class) |
| 29 | +class PkceRegressionTest { |
| 30 | + |
| 31 | + @Rule |
| 32 | + @JvmField |
| 33 | + val splashActivityTestRule = ActivityTestRule(SplashActivity::class.java) |
| 34 | + |
| 35 | + @Rule |
| 36 | + @JvmField |
| 37 | + val intentReceiverActivityTestRule = ActivityTestRule(IntentReceiverActivity::class.java) |
| 38 | + |
| 39 | + @Test |
| 40 | + @FlakyTest |
| 41 | + /** |
| 42 | + * Flaky for two reasons. 1) this needs to touch the network, 2) Espresso tests are just flaky. |
| 43 | + */ |
| 44 | + fun pkce_regression_test() { |
| 45 | + // Simulate response from user logging in at: |
| 46 | + // "https://stage-vpn.guardian.nonprod.cloudops.mozgcp.net/api/v2/vpn/login/android?code_challenge=fx-O4_N_sfGrXxLgDkByfVNgZUPCI1s5PqWp8k1fG8M=&code_challenge_method=S256" |
| 47 | + val authCode = "d60b4de6f4a8a6e2228e82b328729d9cc1666b96a1f7a5202fdc563c925bb7a3ea3f4efa1ef3c37d" |
| 48 | + val intentUri = Uri.parse( |
| 49 | + "https://stage-vpn.guardian.nonprod.cloudops.mozgcp.net" + |
| 50 | + "/vpn/client/login/success?" + |
| 51 | + "code=$authCode" + |
| 52 | + "#Intent;category=android.intent.category.BROWSABLE;" + |
| 53 | + "launchFlags=0x14000000;" + |
| 54 | + "component=org.mozilla.firefox.vpn.debug/org.mozilla.firefox.vpn.IntentReceiverActivity;" + |
| 55 | + "i.org.chromium.chrome.browser.referrer_id=18;" + |
| 56 | + "S.com.android.browser.application_id=com.android.chrome;end" |
| 57 | + ) |
| 58 | + val intent = Intent("android.intent.action.VIEW", intentUri) |
| 59 | + |
| 60 | + val receivedCode = CompletableDeferred<AuthCode>() |
| 61 | + IntentReceiverActivity.setAuthCodeReceivedDeferred(receivedCode) |
| 62 | + |
| 63 | + // IntentReceiverActivity launched with the above auth code |
| 64 | + intentReceiverActivityTestRule.launchActivity(intent) |
| 65 | + |
| 66 | + // assert an auth code was received |
| 67 | + runBlocking { |
| 68 | + withTimeout(5_000) { |
| 69 | + assertNotNull(receivedCode.await()) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + runBlocking { delay(1_000) } |
| 74 | + |
| 75 | + // assert onboarding screen still shown, login did not proceed |
| 76 | + onView(withId(R.id.auth_btn)).check(matches(isDisplayed())) |
| 77 | + } |
| 78 | +} |
0 commit comments