Skip to content

Commit

Permalink
Link Gate to determine link behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Jan 22, 2025
1 parent 4ac8f33 commit 39b6a68
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.stripe.android.paymentsheet.example.playground.settings

import com.stripe.android.core.utils.FeatureFlags

internal object LinkTypeSettingsDefinition : PlaygroundSettingDefinition<LinkType>,
PlaygroundSettingDefinition.Saveable<LinkType> by EnumSaveable(
key = "LinkType",
values = LinkType.entries.toTypedArray(),
defaultValue = LinkType.Native
),
PlaygroundSettingDefinition.Displayable<LinkType> {
override val displayName: String = "LinkType"

override fun createOptions(configurationData: PlaygroundConfigurationData): List<PlaygroundSettingDefinition.Displayable.Option<LinkType>> {
return listOf(
option("Native", LinkType.Native),
option("Native + Attest", LinkType.NativeAttest),
option("Web", LinkType.Web),
)
}

override fun setValue(value: LinkType) {
when (value) {
LinkType.Native -> {
FeatureFlags.nativeLinkEnabled.setEnabled(true)
FeatureFlags.nativeLinkAttestationEnabled.setEnabled(false)
}
LinkType.NativeAttest -> {
FeatureFlags.nativeLinkEnabled.setEnabled(true)
FeatureFlags.nativeLinkAttestationEnabled.setEnabled(true)
}
LinkType.Web -> {
FeatureFlags.nativeLinkEnabled.setEnabled(false)
FeatureFlags.nativeLinkAttestationEnabled.setEnabled(false)
}
}
}
}

enum class LinkType(override val value: String) : ValueEnum {
Native("Native"),
NativeAttest("Native + Attest"),
Web("Web"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ internal class PlaygroundSettings private constructor(
CustomerSettingsDefinition,
CheckoutModeSettingsDefinition,
LinkSettingsDefinition,
FeatureFlagSettingsDefinition(FeatureFlags.nativeLinkEnabled),
LinkTypeSettingsDefinition,
CountrySettingsDefinition,
CurrencySettingsDefinition,
GooglePaySettingsDefinition,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.stripe.android.link.gate

import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.link.LinkConfiguration

internal class DefaultLinkGate(
private val configuration: LinkConfiguration
) : LinkGate {
override val useNativeLink: Boolean
get() {
if (FeatureFlags.nativeLinkEnabled.isEnabled) return true
return useAttestationEndpoints
}

override val useAttestationEndpoints: Boolean
get() {
if (FeatureFlags.nativeLinkAttestationEnabled.isEnabled) return true
return configuration.useAttestationEndpointsForLink
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.stripe.android.link.gate

internal interface LinkGate {
val useNativeLink: Boolean
val useAttestationEndpoints: Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.stripe.android.link.gate

import com.google.common.truth.Truth.assertThat
import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.link.TestFactory
import com.stripe.android.testing.FeatureFlagTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class DefaultLinkGateTest {

@get:Rule
val nativeLinkFeatureFlagTestRule = FeatureFlagTestRule(
featureFlag = FeatureFlags.nativeLinkEnabled,
isEnabled = false
)

@get:Rule
val attestationFeatureFlagTestRule = FeatureFlagTestRule(
featureFlag = FeatureFlags.nativeLinkAttestationEnabled,
isEnabled = false
)

@Test
fun `useNativeLink is true when nativeLinkFeatureFlag is enabled`() {
nativeLinkFeatureFlagTestRule.setEnabled(true)
val gate = gate()

assertThat(gate.useNativeLink).isTrue()
}

@Test
fun `useNativeLink is true when nativeLinkFeatureFlag is enabled regardless of other settings`() {
nativeLinkFeatureFlagTestRule.setEnabled(true)
attestationFeatureFlagTestRule.setEnabled(false)

val gate = gate(useAttestationEndpoints = false)

assertThat(gate.useNativeLink).isTrue()
}

@Test
fun `useNativeLink reflects useAttestationEndpoints when nativeLinkFeatureFlag is disabled`() {
nativeLinkFeatureFlagTestRule.setEnabled(false)
attestationFeatureFlagTestRule.setEnabled(true)

val gate = gate()

assertThat(gate.useNativeLink).isEqualTo(gate.useAttestationEndpoints)
}

@Test
fun `useAttestationEndpoints is true when attestationFeatureFlag is enabled`() {
attestationFeatureFlagTestRule.setEnabled(true)

val gate = gate()

assertThat(gate.useAttestationEndpoints).isTrue()
}

@Test
fun `useAttestationEndpoints is true when attestationFeatureFlag is enabled regardless of configuration`() {
attestationFeatureFlagTestRule.setEnabled(true)

val gate = gate(useAttestationEndpoints = false)

assertThat(gate.useAttestationEndpoints).isTrue()
}

@Test
fun `useAttestationEndpoints reflects configuration when attestationFeatureFlag is disabled`() {
attestationFeatureFlagTestRule.setEnabled(false)

val gateTrue = gate(useAttestationEndpoints = true)
val gateFalse = gate(useAttestationEndpoints = false)

assertThat(gateTrue.useAttestationEndpoints).isTrue()
assertThat(gateFalse.useAttestationEndpoints).isFalse()
}

private fun gate(useAttestationEndpoints: Boolean = true): DefaultLinkGate {
return DefaultLinkGate(
configuration = TestFactory.LINK_CONFIGURATION.copy(
useAttestationEndpointsForLink = useAttestationEndpoints
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.stripe.android.link.gate

internal class FakeLinkGate : LinkGate {
private var _useNativeLink = true
override val useNativeLink: Boolean
get() = _useNativeLink

private var _useAttestationEndpoints = true
override val useAttestationEndpoints: Boolean
get() = _useAttestationEndpoints

fun setUseNativeLink(value: Boolean) {
_useNativeLink = value
}

fun setUseAttestationEndpoints(value: Boolean) {
_useAttestationEndpoints = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.stripe.android.core.BuildConfig
object FeatureFlags {
// Add any feature flags here
val nativeLinkEnabled = FeatureFlag("Native Link")
val nativeLinkAttestationEnabled = FeatureFlag("Native Link Attestation")
val instantDebitsIncentives = FeatureFlag("Instant Bank Payments Incentives")
val enableDefaultPaymentMethods = FeatureFlag("Enable Default Payment Methods")
}
Expand Down

0 comments on commit 39b6a68

Please sign in to comment.