Skip to content

Commit

Permalink
Modify tests to use new BrowserSwitch methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
sshropshire committed Oct 16, 2024
1 parent 14825c1 commit 1ec012d
Showing 1 changed file with 62 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.paypal.android.cardpayments

import android.content.Intent
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import com.braintreepayments.api.BrowserSwitchClient
import com.braintreepayments.api.BrowserSwitchException
import com.braintreepayments.api.BrowserSwitchFinalResult
import com.braintreepayments.api.BrowserSwitchOptions
import com.braintreepayments.api.BrowserSwitchResult
import com.braintreepayments.api.BrowserSwitchStatus
import com.braintreepayments.api.BrowserSwitchStartResult
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.slot
import org.json.JSONObject
import org.junit.Assert.assertEquals
Expand All @@ -30,6 +28,7 @@ class CardAuthLauncherUnitTest {
// TODO: consider using androidx.test activity instead of mockk
// Ref: https://robolectric.org/androidx_test/#activities
private val activity: FragmentActivity = mockk(relaxed = true)
private val intent: Intent = Intent()

private val url = Uri.parse("https://fake.com/destination")
private val card = Card("4111111111111111", "01", "25", "123")
Expand All @@ -49,24 +48,25 @@ class CardAuthLauncherUnitTest {

@Test
fun `presentAuthChallenge() returns an error for approve order when it cannot browser switch`() {
val browserSwitchException = mockk<BrowserSwitchException>()
every { browserSwitchException.message } returns "error message from browser switch"

every { browserSwitchClient.start(any(), any()) } throws browserSwitchException
val browserSwitchResult =
BrowserSwitchStartResult.Failure(Exception("error message from browser switch"))
every { browserSwitchClient.start(any(), any()) } returns browserSwitchResult

val returnUrl = "merchant.app://return.com/deep-link"
val cardRequest = CardRequest("fake-order-id", card, returnUrl)
val cardAuthChallenge = CardAuthChallenge.ApproveOrder(url, cardRequest)

sut = CardAuthLauncher(browserSwitchClient)
val error = sut.presentAuthChallenge(activity, authChallenge = cardAuthChallenge)
assertEquals("error message from browser switch", error?.errorDescription)
val result = sut.presentAuthChallenge(activity, authChallenge = cardAuthChallenge)
as CardPresentAuthChallengeResult.Failure
assertEquals("error message from browser switch", result.error.errorDescription)
}

@Test
fun `presentAuthChallenge() browser switches to approve order auth challenge url`() {
val slot = slot<BrowserSwitchOptions>()
every { browserSwitchClient.start(activity, capture(slot)) } just runs
val browserSwitchResult = BrowserSwitchStartResult.Started("pending request")
every { browserSwitchClient.start(activity, capture(slot)) } returns browserSwitchResult

val returnUrl = "merchant.app://return.com/deep-link"
val cardRequest = CardRequest("fake-order-id", card, returnUrl)
Expand All @@ -87,7 +87,8 @@ class CardAuthLauncherUnitTest {
@Test
fun `presentAuthChallenge() browser switches to vault auth challenge url`() {
val slot = slot<BrowserSwitchOptions>()
every { browserSwitchClient.start(activity, capture(slot)) } just runs
val browserSwitchResult = BrowserSwitchStartResult.Started("pending request")
every { browserSwitchClient.start(activity, capture(slot)) } returns browserSwitchResult

val returnUrl = "merchant.app://return.com/deep-link"
val vaultRequest = CardVaultRequest("fake-setup-token-id", card, returnUrl)
Expand All @@ -114,14 +115,15 @@ class CardAuthLauncherUnitTest {
val successDeepLink =
"$scheme://$domain/return_url?state=undefined&code=undefined&liability_shift=NO"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
approveOrderMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(approveOrderMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.completeRequest(intent, "pending request")
as CardStatus.ApproveOrderSuccess

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderSuccess
val cardResult = status.result
assertEquals("fake-order-id", cardResult.orderId)
assertEquals("NO", cardResult.liabilityShift)
Expand All @@ -137,31 +139,13 @@ class CardAuthLauncherUnitTest {
val domain = "example.com"
val successDeepLink = "$scheme://$domain/return_url?error=error"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
approveOrderMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderError
val error = status.error
assertEquals(0, error.code)
assertEquals("3DS Verification is returning an error.", error.errorDescription)
}

@Test
fun `deliverBrowserSwitchResult() returns approve order error when deep link is null`() {
sut = CardAuthLauncher(browserSwitchClient)

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
approveOrderMetadata,
deepLinkUrl = null
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(approveOrderMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderError
val status = sut.completeRequest(intent, "pending request") as CardStatus.ApproveOrderError
val error = status.error
assertEquals(0, error.code)
assertEquals("3DS Verification is returning an error.", error.errorDescription)
Expand All @@ -175,14 +159,13 @@ class CardAuthLauncherUnitTest {
val domain = "example.com"
val successDeepLink = "$scheme://$domain/return_url?state=undefined&liability_shift=NO"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
approveOrderMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(approveOrderMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderError
val status = sut.completeRequest(intent, "pending request") as CardStatus.ApproveOrderError
val error = status.error
assertEquals(1, error.code)
assertEquals("Malformed deeplink URL.", error.errorDescription)
Expand All @@ -196,31 +179,18 @@ class CardAuthLauncherUnitTest {
val domain = "example.com"
val successDeepLink = "$scheme://$domain/return_url?code=undefined&liability_shift=NO"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
approveOrderMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(approveOrderMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderError
val status = sut.completeRequest(intent, "pending request") as CardStatus.ApproveOrderError
val error = status.error
assertEquals(1, error.code)
assertEquals("Malformed deeplink URL.", error.errorDescription)
}

@Test
fun `deliverBrowserSwitchResult() returns approve order canceled when browser switch was canceled`() {
sut = CardAuthLauncher(browserSwitchClient)

val browserSwitchResult =
createBrowserSwitchResult(BrowserSwitchStatus.CANCELED, approveOrderMetadata)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.ApproveOrderCanceled
assertEquals("fake-order-id", status.orderId)
}

@Test
fun `deliverBrowserSwitchResult() returns vault success when deep link url contains the word success`() {
sut = CardAuthLauncher(browserSwitchClient)
Expand All @@ -229,14 +199,13 @@ class CardAuthLauncherUnitTest {
val domain = "example.com"
val successDeepLink = "$scheme://$domain/success"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
vaultMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(vaultMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.VaultSuccess
val status = sut.completeRequest(intent, "pending request") as CardStatus.VaultSuccess
val vaultResult = status.result
assertEquals("fake-setup-token-id", vaultResult.setupTokenId)
assertEquals("SCA_COMPLETE", vaultResult.status)
Expand All @@ -250,28 +219,25 @@ class CardAuthLauncherUnitTest {
val domain = "example.com"
val successDeepLink = "$scheme://$domain/canceled"

val browserSwitchResult = createBrowserSwitchResult(
BrowserSwitchStatus.SUCCESS,
vaultMetadata,
Uri.parse(successDeepLink)
)
every { browserSwitchClient.deliverResult(activity) } returns browserSwitchResult
val finalResult =
createBrowserSwitchSuccessFinalResult(vaultMetadata, Uri.parse(successDeepLink))
every {
browserSwitchClient.completeRequest(intent, "pending request")
} returns finalResult

val status = sut.deliverBrowserSwitchResult(activity) as CardStatus.VaultCanceled
val status = sut.completeRequest(intent, "pending request") as CardStatus.VaultCanceled
assertEquals("fake-setup-token-id", status.setupTokenId)
}

private fun createBrowserSwitchResult(
@BrowserSwitchStatus status: Int,
metadata: JSONObject? = null,
deepLinkUrl: Uri? = null
): BrowserSwitchResult {

val browserSwitchResult = mockk<BrowserSwitchResult>(relaxed = true)
every { browserSwitchResult.status } returns status
every { browserSwitchResult.deepLinkUrl } returns deepLinkUrl

every { browserSwitchResult.requestMetadata } returns metadata
return browserSwitchResult
private fun createBrowserSwitchSuccessFinalResult(
metadata: JSONObject,
deepLinkUrl: Uri
): BrowserSwitchFinalResult.Success {
val finalResult = mockk<BrowserSwitchFinalResult.Success>(relaxed = true)
every { finalResult.returnUrl } returns deepLinkUrl
every { finalResult.requestMetadata } returns metadata
every { finalResult.requestCode } returns 123
every { finalResult.requestUrl } returns Uri.parse("https://example.com/url")
return finalResult
}
}

0 comments on commit 1ec012d

Please sign in to comment.