Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSG-4614 #73

Merged
merged 11 commits into from
Aug 30, 2024
Merged
27 changes: 11 additions & 16 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
name: Integration tests

on:
pull_request:
push:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
api-level: [33] # TODO: add more api levels to test
api-level: [33] # TODO: add more API levels to test

env:
MAILOSAUR_API_KEY: ${{ secrets.MAILOSAUR_API_KEY }}
OTP_TEST_USER_AUTH_TOKEN: ${{ secrets.OTP_TEST_USER_AUTH_TOKEN }}
APP_API_KEY: ${{ secrets.APP_API_KEY }}

steps:
- name: checkout
- name: Checkout
uses: actions/checkout@v4

- name: Setup JDK 17
Expand All @@ -41,23 +45,14 @@ jobs:
~/.android/adb*
key: avd-${{ matrix.api-level }}

- name: create AVD and generate snapshot for caching
if: steps.avd-cache.outputs.cache-hit != 'true'
- name: Create AVD and run tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
force-avd-creation: false
force-avd-creation: true
wipe-data: true
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: false
arch: x86_64
script: echo "Generated AVD snapshot for caching."

- name: run tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
force-avd-creation: false
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
arch: x86_64
script: ./gradlew connectedDebugAndroidTest
script: |
./gradlew clean connectedDebugAndroidTest
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class PassageHostedTests {
val expectedState = "state="
val expectedCodeChallenge = "code_challenge="

passage.hosted.hostedAuthStart()
passage.hosted.start()

intended(
allOf(
Expand All @@ -86,7 +86,7 @@ internal class PassageHostedTests {
runTest {
try {
val invalidAuthCode = "INVALID_AUTH_CODE"
passage.hosted.hostedAuthFinish(invalidAuthCode, "")
passage.hosted.finish(invalidAuthCode, "")
fail("Test should throw FinishOIDCAuthenticationInvalidRequestException")
} catch (e: Exception) {
assertThat(e is HostedAuthorizationError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class PassageSocialTests {
val expectedState = "state="
val expectedCodeChallenge = "code_challenge="

passage.social.authorizeWith(SocialConnection.github)
passage.social.authorize(SocialConnection.github)

intended(
allOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import id.passage.android.IntegrationTestConfig.Companion.API_BASE_URL
import id.passage.android.IntegrationTestConfig.Companion.APP_ID_OTP
import id.passage.android.IntegrationTestConfig.Companion.EXISTING_USER_EMAIL_OTP
import id.passage.android.IntegrationTestConfig.Companion.WAIT_TIME_MILLISECONDS
import id.passage.android.exceptions.PassageTokenException
import id.passage.android.exceptions.PassageUserUnauthorizedException
import id.passage.android.exceptions.UserInfoUnauthorizedException
import id.passage.android.model.AuthResult
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.fail
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
Expand All @@ -24,6 +27,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
internal class TokenStoreTests {
private lateinit var passage: Passage
private var refreshToken = ""

@Before
fun setup(): Unit =
Expand All @@ -38,7 +42,8 @@ internal class TokenStoreTests {
val otpId = passage.oneTimePasscode.login(EXISTING_USER_EMAIL_OTP).otpId
delay(WAIT_TIME_MILLISECONDS)
val otp = MailosaurAPIClient.getMostRecentOneTimePasscode()
passage.oneTimePasscode.activate(otp, otpId)
val authResult = passage.oneTimePasscode.activate(otp, otpId)
refreshToken = authResult.authToken
}

@After
Expand Down Expand Up @@ -111,4 +116,57 @@ internal class TokenStoreTests {
assertThat(e is PassageUserUnauthorizedException)
}
}

@Test
fun getValidAuthTokenWithValidToken() =
runTest {
val result = passage.tokenStore.getValidAuthToken()
assertNotNull(result)
}

@Test
fun getValidAuthTokenWithInvalidToken() =
runTest {
try {
passage.tokenStore.setTokens(AuthResult("invalid", ""))
passage.tokenStore.getValidAuthToken()
fail("Test should throw PassageTokenException")
} catch (e: Exception) {
assertThat(e is PassageTokenException)
}
}

@Test
fun isAuthTokenValidWithValidToken() {
val validToken = IntegrationTestConfig.AUTH_TOEKN
val result = passage.tokenStore.isAuthTokenValid(validToken)
assertThat(result).isTrue()
}

@Test
fun isAuthTokenValidWithInvalidToken() {
val invalidToken = "invalidAuthToken"
val result = passage.tokenStore.isAuthTokenValid(invalidToken)
assertThat(result).isFalse()
}

@Test
fun revokedWithValidToken() =
runTest {
val validRefreshToken = refreshToken
passage.tokenStore.revokeRefreshToken(validRefreshToken)
// should not throw any error
}

@Test
fun refreshWithInvalidToken(): Unit =
runBlocking {
try {
val invalidRefreshToken = "invalid"
val authResult = passage.tokenStore.refreshAuthToken(invalidRefreshToken)
fail("Test should throw PassageTokenException")
} catch (e: Exception) {
assertThat(e is PassageTokenException)
}
}
}
4 changes: 2 additions & 2 deletions passage/src/main/java/id/passage/android/PassageHosted.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PassageHosted(
* If your Passage app is Hosted, use this method to register and log in your user.
* This method will open up a Passage login experience on a Chrome tab.
*/
fun hostedAuthStart() {
fun start() {
HostedUtils.openChromeTab(
activity,
)
Expand All @@ -31,7 +31,7 @@ class PassageHosted(
* @throws HostedAuthorizationError
*/

suspend fun hostedAuthFinish(
suspend fun finish(
code: String,
state: String,
): Pair<AuthResult, String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PassageSocial(
* Authorizes user via a supported third-party social provider.
* @param connection The Social connection to use for authorization
*/
fun authorizeWith(connection: SocialConnection) {
fun authorize(connection: SocialConnection) {
SocialUtils.openChromeTab(
connection,
Passage.authOrigin,
Expand Down
77 changes: 0 additions & 77 deletions passage/src/main/java/id/passage/android/PassageToken.kt

This file was deleted.

Loading
Loading