-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
CustomerState
logic to factory methods. (#8769)
* Move `CustomerState` logic to factory methods. * Use `ElementsSession.Customer` in factory method and handle `null` customer in `PaymentSheetLoader`
- Loading branch information
1 parent
fa8a3a9
commit b7cbd4d
Showing
3 changed files
with
241 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/CustomerStateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.stripe.android.paymentsheet.state | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.model.ElementsSession | ||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.paymentsheet.PaymentSheet | ||
import com.stripe.android.testing.PaymentMethodFactory | ||
import org.junit.Test | ||
|
||
class CustomerStateTest { | ||
@Test | ||
fun `Should create 'CustomerState' for customer session properly with permissions disabled`() { | ||
val paymentMethods = PaymentMethodFactory.cards(4) | ||
val customer = createElementsSessionCustomer( | ||
customerId = "cus_1", | ||
ephemeralKeySecret = "ek_1", | ||
paymentMethods = paymentMethods, | ||
paymentSheetComponent = ElementsSession.Customer.Components.PaymentSheet.Disabled | ||
) | ||
|
||
val customerState = CustomerState.createForCustomerSession(customer) | ||
|
||
assertThat(customerState).isEqualTo( | ||
CustomerState( | ||
id = "cus_1", | ||
ephemeralKeySecret = "ek_1", | ||
paymentMethods = paymentMethods, | ||
permissions = CustomerState.Permissions( | ||
canRemovePaymentMethods = false, | ||
// Always true for `customer_session` | ||
canRemoveDuplicates = true, | ||
), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `Should create 'CustomerState' for customer session properly with remove permissions enabled`() { | ||
val paymentMethods = PaymentMethodFactory.cards(4) | ||
val customer = createElementsSessionCustomer( | ||
customerId = "cus_1", | ||
ephemeralKeySecret = "ek_1", | ||
paymentMethods = paymentMethods, | ||
paymentSheetComponent = ElementsSession.Customer.Components.PaymentSheet.Enabled( | ||
isPaymentMethodSaveEnabled = false, | ||
isPaymentMethodRemoveEnabled = true, | ||
), | ||
) | ||
|
||
val customerState = CustomerState.createForCustomerSession(customer) | ||
|
||
assertThat(customerState).isEqualTo( | ||
CustomerState( | ||
id = "cus_1", | ||
ephemeralKeySecret = "ek_1", | ||
paymentMethods = paymentMethods, | ||
permissions = CustomerState.Permissions( | ||
canRemovePaymentMethods = true, | ||
// Always true for `customer_session` | ||
canRemoveDuplicates = true, | ||
), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `Should create 'CustomerState' for customer session properly with remove permissions disabled`() { | ||
val paymentMethods = PaymentMethodFactory.cards(3) | ||
val customer = createElementsSessionCustomer( | ||
customerId = "cus_3", | ||
ephemeralKeySecret = "ek_3", | ||
paymentMethods = paymentMethods, | ||
paymentSheetComponent = ElementsSession.Customer.Components.PaymentSheet.Enabled( | ||
isPaymentMethodSaveEnabled = false, | ||
isPaymentMethodRemoveEnabled = false, | ||
), | ||
) | ||
|
||
val customerState = CustomerState.createForCustomerSession(customer) | ||
|
||
assertThat(customerState).isEqualTo( | ||
CustomerState( | ||
id = "cus_3", | ||
ephemeralKeySecret = "ek_3", | ||
paymentMethods = paymentMethods, | ||
permissions = CustomerState.Permissions( | ||
canRemovePaymentMethods = false, | ||
// Always true for `customer_session` | ||
canRemoveDuplicates = true, | ||
), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `Should create 'CustomerState' for legacy ephemeral keys properly`() { | ||
val paymentMethods = PaymentMethodFactory.cards(7) | ||
val customerState = CustomerState.createForLegacyEphemeralKey( | ||
customerId = "cus_1", | ||
accessType = PaymentSheet.CustomerAccessType.LegacyCustomerEphemeralKey( | ||
ephemeralKeySecret = "ek_1", | ||
), | ||
paymentMethods = paymentMethods, | ||
) | ||
|
||
assertThat(customerState).isEqualTo( | ||
CustomerState( | ||
id = "cus_1", | ||
ephemeralKeySecret = "ek_1", | ||
paymentMethods = paymentMethods, | ||
permissions = CustomerState.Permissions( | ||
// Always true for legacy ephemeral keys since un-scoped | ||
canRemovePaymentMethods = true, | ||
// Always 'false' for legacy ephemeral keys | ||
canRemoveDuplicates = false, | ||
), | ||
) | ||
) | ||
} | ||
|
||
private fun createElementsSessionCustomer( | ||
customerId: String, | ||
ephemeralKeySecret: String, | ||
paymentMethods: List<PaymentMethod>, | ||
paymentSheetComponent: ElementsSession.Customer.Components.PaymentSheet | ||
): ElementsSession.Customer { | ||
return ElementsSession.Customer( | ||
paymentMethods = paymentMethods, | ||
defaultPaymentMethod = null, | ||
session = ElementsSession.Customer.Session( | ||
id = "cuss_1", | ||
customerId = customerId, | ||
apiKey = ephemeralKeySecret, | ||
apiKeyExpiry = 999999999, | ||
liveMode = false, | ||
components = ElementsSession.Customer.Components( | ||
customerSheet = ElementsSession.Customer.Components.CustomerSheet.Disabled, | ||
paymentSheet = paymentSheetComponent | ||
) | ||
), | ||
) | ||
} | ||
} |