Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joyceqin-stripe committed Nov 1, 2024
1 parent 8216e2f commit 0cc420d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,86 @@ final class PaymentSheetDeferredValidatorTests: XCTestCase {
XCTAssertNotNil(analyticEvent?["error_code"] as? String)
}

func testPaymentIntentMatchedCardFingerprint() throws {
let testCard = STPPaymentMethod._testCard()
var paymentMethodJson = STPPaymentMethod.paymentMethodJson
paymentMethodJson["id"] = "pm_mismatch_id"
paymentMethodJson["card"] = testCard.card
let testCardPi = STPFixtures.makePaymentIntent(paymentMethodJson: paymentMethodJson)
XCTAssertNoThrow(try PaymentSheetDeferredValidator.validatePaymentMethod(intentPaymentMethod: testCardPi.paymentMethod,
paymentMethod: testCard))
}

func testPaymentIntentMismatchedCardFingerprint() throws {
let testCard = STPPaymentMethod._testCard()
var paymentMethodJson = STPPaymentMethod.paymentMethodJson
paymentMethodJson["id"] = "pm_mismatch_id"
paymentMethodJson["card"] = ["fingerprint": "mismatch_fingerprint"]
let testCardPi = STPFixtures.makePaymentIntent(paymentMethodJson: paymentMethodJson)
guard let intentPaymentMethod = testCardPi.paymentMethod else {
return
}
guard let intentPaymentMethodFingerprint = intentPaymentMethod.card?.fingerprint else {
return
}
guard let testCardFingerprint = testCard.card?.fingerprint else {
return
}
XCTAssertThrowsError(try PaymentSheetDeferredValidator.validatePaymentMethod(intentPaymentMethod: testCardPi.paymentMethod,
paymentMethod: testCard)) { error in
XCTAssertEqual("\(error)", """
An error occurred in PaymentSheet. \nThere is a mismatch between the fingerprint of the payment method on your Intent: \(intentPaymentMethodFingerprint) and the fingerprint of the payment method passed into the `confirmHandler`: \(testCardFingerprint).
To resolve this issue, you can:
1. Create a new Intent each time before you call the `confirmHandler`, or
2. Update the existing Intent with the desired `paymentMethod` before calling the `confirmHandler`.
""")
}
let analyticEvent = STPAnalyticsClient.sharedClient._testLogHistory.last
XCTAssertEqual(analyticEvent?["event"] as? String, STPAnalyticEvent.paymentSheetDeferredIntentPaymentMethodMismatch.rawValue)
XCTAssertNotNil(analyticEvent?["error_code"] as? String)
}

func testPaymentIntentMatchedUSBankAccountFingerprint() throws {
let testUSBankAccount = STPPaymentMethod._testUSBankAccount()
var paymentMethodJson = STPPaymentMethod.usBankAccountJson
paymentMethodJson["id"] = "pm_mismatch_id"
paymentMethodJson["us_bank_account"] = testUSBankAccount.usBankAccount
let testUSBankAccountPi = STPFixtures.makePaymentIntent(paymentMethodJson: paymentMethodJson)
XCTAssertNoThrow(try PaymentSheetDeferredValidator.validatePaymentMethod(intentPaymentMethod: testUSBankAccountPi.paymentMethod,
paymentMethod: testUSBankAccount))
}

func testPaymentIntentMismatchedUSBankAccountFingerprint() throws {
let testUSBankAccount = STPPaymentMethod._testUSBankAccount()
var paymentMethodJson = STPPaymentMethod.usBankAccountJson
paymentMethodJson["id"] = "pm_mismatch_id"
paymentMethodJson["us_bank_account"] = ["fingerprint": "mismatch_fingerprint"]
let testUSBankAccountPi = STPFixtures.makePaymentIntent(paymentMethodJson: paymentMethodJson)
guard let intentPaymentMethod = testUSBankAccountPi.paymentMethod else {
return
}
guard let intentPaymentMethodFingerprint = intentPaymentMethod.card?.fingerprint else {
return
}
guard let testUSBankAccountFingerprint = testUSBankAccount.usBankAccount?.fingerprint else {
return
}
XCTAssertThrowsError(try PaymentSheetDeferredValidator.validatePaymentMethod(intentPaymentMethod: testUSBankAccountPi.paymentMethod,
paymentMethod: testUSBankAccount)) { error in
XCTAssertEqual("\(error)", """
An error occurred in PaymentSheet. \nThere is a mismatch between the fingerprint of the payment method on your Intent: \(intentPaymentMethodFingerprint) and the fingerprint of the payment method passed into the `confirmHandler`: \(testUSBankAccountFingerprint).
To resolve this issue, you can:
1. Create a new Intent each time before you call the `confirmHandler`, or
2. Update the existing Intent with the desired `paymentMethod` before calling the `confirmHandler`.
""")
}
let analyticEvent = STPAnalyticsClient.sharedClient._testLogHistory.last
XCTAssertEqual(analyticEvent?["event"] as? String, STPAnalyticEvent.paymentSheetDeferredIntentPaymentMethodMismatch.rawValue)
XCTAssertNotNil(analyticEvent?["error_code"] as? String)
}

func testPaymentIntentNilPaymentMethod() throws {
let testCard = STPPaymentMethod._testCard()
let nilPaymentMethodPi = STPFixtures.makePaymentIntent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ extension STPPaymentMethod {
"card": [
"last4": "4242",
"brand": "visa",
"fingerprint": "B8XXs2y2JsVBtB9f",
],
])!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,31 @@ extension STPPaymentMethod {
"card": [
"last4": "4242",
"brand": "visa",
"fingerprint": "B8XXs2y2JsVBtB9f",
],
]
}

static var usBankAccountJson: [String: Any] {
return [
"id": "pm_123",
"type": "us_bank_account",
"us_bank_account": [
"account_holder_type": "individual",
"account_type": "checking",
"bank_name": "STRIPE TEST BANK",
"fingerprint": "ickfX9sbxIyAlbuh",
"last4": "6789",
"networks": [
"preferred": "ach",
"supported": [
"ach",
],
] as [String: Any],
"routing_number": "110000000",
] as [String: Any],
]
}

static var paymentMethodsJson: [String: Any] = [
"data": [
Expand Down

0 comments on commit 0cc420d

Please sign in to comment.