Skip to content

Commit

Permalink
Mock Payments tests (#3815)
Browse files Browse the repository at this point in the history
## Summary
* Move most StripePayments and StripePaymentSheet tests to use network
mocking.
* Adjusted DownloadManagerTest timeouts
* Adjusted `maxRetries` in Payment Handler tests

Easier to review by filtering out the `.tail` files:
https://github.com/stripe/stripe-ios/pull/3815/files?file-filters%5B%5D=.m&file-filters%5B%5D=.pbxproj&file-filters%5B%5D=.swift&show-deleted-files=true&show-viewed-files=true

## Motivation
Reduce test time (~18 minutes to ~12 minutes) and flakiness, enforces
that we don't unintentionally change the request parameters.

## Testing
CI
  • Loading branch information
davidme-stripe authored Jul 24, 2024
1 parent d7f01a4 commit 8a462b5
Show file tree
Hide file tree
Showing 1,053 changed files with 67,724 additions and 414 deletions.
10 changes: 8 additions & 2 deletions Stripe/StripeiOSTests/APIRequestTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import XCTest
@testable@_spi(STP) import StripeCore
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class AnyAPIResponse: NSObject, STPAPIResponseDecodable {
Expand All @@ -32,9 +33,11 @@ class AnyAPIResponse: NSObject, STPAPIResponseDecodable {

}

class APIRequestTest: XCTestCase {
let apiClient = STPAPIClient()
class APIRequestTest: STPNetworkStubbingTestCase {
var apiClient: STPAPIClient!
override func setUp() {
super.setUp()
apiClient = STPAPIClient()
// HTTPBin clone
apiClient.apiURL = URL(string: "https://luxurious-alpine-devourer.glitch.me")
}
Expand Down Expand Up @@ -244,6 +247,8 @@ class APIRequestTest: XCTestCase {
}

func test429Backoff() {
let oldMaxRetries = StripeAPI.maxRetries
StripeAPI.maxRetries = 2
var inProgress = true

let e = expectation(description: "Request completed")
Expand All @@ -267,6 +272,7 @@ class APIRequestTest: XCTestCase {
}

wait(for: [e, checkedStillInProgress], timeout: 30)
StripeAPI.maxRetries = oldMaxRetries
}

func test429NoBackoff() {
Expand Down
13 changes: 8 additions & 5 deletions Stripe/StripeiOSTests/ConsumerSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import XCTest
import StripeCoreTestUtils
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class ConsumerSessionTests: XCTestCase {
class ConsumerSessionTests: STPNetworkStubbingTestCase {

let apiClient: STPAPIClient = {
let apiClient = STPAPIClient(publishableKey: STPTestingDefaultPublishableKey)
return apiClient
}()
var apiClient: STPAPIClient!

override func setUp() {
super.setUp()
apiClient = STPAPIClient(publishableKey: STPTestingDefaultPublishableKey)
}

func testLookupSession_noParams() {
let expectation = self.expectation(description: "Lookup ConsumerSession")
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/LinkSignupViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import XCTest
@testable@_spi(STP) import StripeCore
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class LinkInlineSignupViewModelTests: XCTestCase {
class LinkInlineSignupViewModelTests: STPNetworkStubbingTestCase {

// Should be ~4x the debounce time for best results.
let accountLookupTimeout: TimeInterval = 4
Expand Down
5 changes: 4 additions & 1 deletion Stripe/StripeiOSTests/STPApplePayContextFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import OHHTTPStubs
@testable import StripeCoreTestUtils
@testable import StripePayments
@testable import StripePaymentsObjcTestUtils
@testable import StripePaymentsTestUtils

class STPTestApplePayContextDelegate: NSObject, STPApplePayContextDelegate {
func applePayContext(_ context: StripeApplePay.STPApplePayContext, didCreatePaymentMethod paymentMethod: StripePayments.STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping StripeApplePay.STPIntentClientSecretCompletionBlock) {
Expand All @@ -27,12 +28,13 @@ class STPTestApplePayContextDelegate: NSObject, STPApplePayContextDelegate {
var didCreatePaymentMethodDelegateMethod: ((_ paymentMethod: STPPaymentMethod?, _ paymentInformation: PKPayment?, _ completion: @escaping STPIntentClientSecretCompletionBlock) -> Void)?
}

class STPApplePayContextFunctionalTest: XCTestCase {
class STPApplePayContextFunctionalTest: STPNetworkStubbingTestCase {
var apiClient: STPApplePayContextFunctionalTestAPIClient!
var delegate: STPTestApplePayContextDelegate!
var context: STPApplePayContext!

override func setUp() {
super.setUp()
delegate = STPTestApplePayContextDelegate()
let apiClient = STPApplePayContextFunctionalTestAPIClient(publishableKey: STPTestingDefaultPublishableKey)
apiClient.setupStubs()
Expand All @@ -47,6 +49,7 @@ class STPApplePayContextFunctionalTest: XCTestCase {

override func tearDown() {
HTTPStubs.removeAllStubs()
super.tearDown()
}

func testCompletesManualConfirmationPaymentIntent() {
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPBankAccountFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPBankAccountFunctionalTest: XCTestCase {
class STPBankAccountFunctionalTest: STPNetworkStubbingTestCase {
func testCreateAndRetreiveBankAccountToken() {
let bankAccount = STPBankAccountParams()
bankAccount.accountNumber = "000123456789"
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPCardFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPCardFunctionalTest: XCTestCase {
class STPCardFunctionalTest: STPNetworkStubbingTestCase {
func testCreateCardToken() {
let card = STPCardParams()

Expand Down
1 change: 1 addition & 0 deletions Stripe/StripeiOSTests/STPE2ETest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class STPE2ETest: XCTestCase {
// In this test, a PaymentIntent object is created from an example merchant backend,
// confirmed by the iOS SDK, and then retrieved to validate that the original amount,
// currency, and merchant are the same as the original inputs.
// (Don't network mock this one!)
func testE2E() throws {
continueAfterFailure = false
let backend = E2EBackend()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPFileFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPFileFunctionalTest: XCTestCase {
class STPFileFunctionalTest: STPNetworkStubbingTestCase {
func testImage() -> UIImage {
return UIImage(
named: "stp_test_upload_image.jpeg",
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPIIFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPPIIFunctionalTest: XCTestCase {
class STPPIIFunctionalTest: STPNetworkStubbingTestCase {
func testCreatePersonallyIdentifiableInformationToken() {
let client = STPAPIClient(publishableKey: STPTestingDefaultPublishableKey)

Expand Down
10 changes: 5 additions & 5 deletions Stripe/StripeiOSTests/STPPaymentHandlerFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import XCTest

// You can add tests in here for payment methods that don't require customer actions (i.e. don't open webviews for customer authentication).
// If they require customer action, use STPPaymentHandlerFunctionalTest.m instead
final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationContext {
final class STPPaymentHandlerFunctionalSwiftTest: STPNetworkStubbingTestCase, STPAuthenticationContext {
// MARK: - STPAuthenticationContext
func authenticationPresentingViewController() -> UIViewController {
return UIViewController()
Expand All @@ -29,7 +29,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTFail(String(describing: error))
return
}
STPTestingAPIClient().createPaymentIntent(withParams: [
STPTestingAPIClient.shared().createPaymentIntent(withParams: [
"confirm": "true",
"payment_method_types": ["card"],
"currency": "usd",
Expand Down Expand Up @@ -73,7 +73,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTFail(String(describing: error))
return
}
STPTestingAPIClient().createPaymentIntent(withParams: [
STPTestingAPIClient.shared().createPaymentIntent(withParams: [
"confirm": "true",
"payment_method_types": ["sepa_debit"],
"currency": "eur",
Expand Down Expand Up @@ -117,7 +117,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTFail(String(describing: error))
return
}
STPTestingAPIClient().createSetupIntent(withParams: [
STPTestingAPIClient.shared().createSetupIntent(withParams: [
"confirm": "true",
"payment_method_types": ["card"],
"payment_method": paymentMethod.stripeId,
Expand Down Expand Up @@ -160,7 +160,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTFail()
return
}
STPTestingAPIClient().createSetupIntent(withParams: [
STPTestingAPIClient.shared().createSetupIntent(withParams: [
"confirm": "true",
"payment_method_types": ["sepa_debit"],
"payment_method": paymentMethod.stripeId,
Expand Down
3 changes: 3 additions & 0 deletions Stripe/StripeiOSTests/STPPaymentHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class STPPaymentHandlerStubbedTests: STPNetworkStubbingTestCase {
class STPPaymentHandlerTests: APIStubbedTestCase {

func testPaymentHandlerRetriesWithBackoff() {
let oldMaxRetries = StripeAPI.maxRetries
StripeAPI.maxRetries = 1
STPPaymentHandler.sharedHandler.apiClient = stubbedAPIClient()

stub { urlRequest in
Expand Down Expand Up @@ -268,6 +270,7 @@ class STPPaymentHandlerTests: APIStubbedTestCase {

wait(for: [paymentHandlerExpectation, checkedStillInProgress, fetchedSetupIntentExpectation], timeout: 60)
STPPaymentHandler.sharedHandler.apiClient = STPAPIClient.shared
StripeAPI.maxRetries = oldMaxRetries
}
}

Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentIntentFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import StripeCoreTestUtils
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentIntentFunctionalTest: XCTestCase {
class STPPaymentIntentFunctionalTest: STPNetworkStubbingTestCase {
func testCreatePaymentIntentWithTestingServer() {
let expectation = self.expectation(description: "PaymentIntent create.")
STPTestingAPIClient.shared.createPaymentIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils

class STPPaymentMethodAUBECSDebitParamsTests: XCTestCase {
class STPPaymentMethodAUBECSDebitParamsTests: STPNetworkStubbingTestCase {
func testCreateAUBECSPaymentMethod() {
let client = STPAPIClient(publishableKey: STPTestingAUPublishableKey)
let becsParams = STPPaymentMethodAUBECSDebitParams()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodAUBECSDebitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils

@testable@_spi(STP) import Stripe
@testable@_spi(STP) import StripeCore
Expand All @@ -16,7 +17,7 @@ import StripeCoreTestUtils

private var kAUBECSDebitPaymentIntentClientSecret =
"pi_1GaRLjF7QokQdxByYgFPQEi0_secret_z76otRQH2jjOIEQYsA9vxhuKn"
class STPPaymentMethodAUBECSDebitTests: XCTestCase {
class STPPaymentMethodAUBECSDebitTests: STPNetworkStubbingTestCase {
private(set) var auBECSDebitJSON: [AnyHashable: Any]?

func _retrieveAUBECSDebitJSON(_ completion: @escaping ([AnyHashable: Any]?) -> Void) {
Expand Down
6 changes: 3 additions & 3 deletions Stripe/StripeiOSTests/STPPaymentMethodAffirmParamsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
//

import Foundation
import StripeCoreTestUtils

@testable@_spi(STP) import Stripe
@testable@_spi(STP) import StripeCore
import StripeCoreTestUtils
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentMethodAffirmParamsTests: XCTestCase {
class STPPaymentMethodAffirmParamsTests: STPNetworkStubbingTestCase {

func testCreateAffirmPaymentMethod() throws {
let affirmParams = STPPaymentMethodAffirmParams()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodAffirmTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

@testable@_spi(STP) import Stripe
Expand All @@ -14,7 +15,7 @@ import XCTest
@testable@_spi(STP) import StripePaymentSheet
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentMethodAffirmTests: XCTestCase {
class STPPaymentMethodAffirmTests: STPNetworkStubbingTestCase {

static let affirmPaymentIntentClientSecret =
"pi_3KUFbTFY0qyl6XeW1oDBbiQk_secret_8kdpLx37oa5WMrI2xoXThCK9s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils

class STPPaymentMethodAfterpayClearpayParamsTest: XCTestCase {
class STPPaymentMethodAfterpayClearpayParamsTest: STPNetworkStubbingTestCase {
func testCreateAfterpayClearpayPaymentMethod() {
let client = STPAPIClient(publishableKey: STPTestingDefaultPublishableKey)
let afterpayClearpayParams = STPPaymentMethodAfterpayClearpayParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import Stripe
import StripeCoreTestUtils
import StripePaymentsTestUtils

class STPPaymentMethodAfterpayClearpayTest: XCTestCase {
class STPPaymentMethodAfterpayClearpayTest: STPNetworkStubbingTestCase {
var afterpayJSON: [AnyHashable: Any]?

func _retrieveAfterpayJSON(_ completion: @escaping ([AnyHashable: Any]?) -> Void) {
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodAlmaParamsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import Foundation
import StripeCoreTestUtils
import StripePaymentsTestUtils

@testable@_spi(STP) import Stripe
@testable@_spi(STP) import StripeCore
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentMethodAlmaParamsTests: XCTestCase {
class STPPaymentMethodAlmaParamsTests: STPNetworkStubbingTestCase {

func testCreateAlmaPaymentMethod() throws {
let almaParams = STPPaymentMethodAlmaParams()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodAlmaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

@testable import Stripe
import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPPaymentMethodAlmaTests: XCTestCase {
class STPPaymentMethodAlmaTests: STPNetworkStubbingTestCase {

static let almaPaymentIntentClientSecret = "pi_3Oz1AfKG6vc7r7YC0VaP6KiE_secret_SxVptpJ5PaAceAYCGetQh8FVv"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import StripeCoreTestUtils
@testable@_spi(STP) import StripeCore
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentMethodAmazonPayParamsTests: XCTestCase {
class STPPaymentMethodAmazonPayParamsTests: STPNetworkStubbingTestCase {

func testCreateAmazonPayPaymentMethod() throws {
let amazonPayParams = STPPaymentMethodAmazonPayParams()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodAmazonPayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

@testable import Stripe
import StripeCoreTestUtils
import StripePaymentsTestUtils
import XCTest

class STPPaymentMethodAmazonPayTests: XCTestCase {
class STPPaymentMethodAmazonPayTests: STPNetworkStubbingTestCase {

static let amazonPayPaymentIntentClientSecret = "pi_3OmQQ0FY0qyl6XeW0H4X6eI0_secret_BerPIzUf8vFy1KXG53iYvX2Zb"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//

import StripeCoreTestUtils
import StripePaymentsTestUtils

class STPPaymentMethodBancontactParamsTests: XCTestCase {
class STPPaymentMethodBancontactParamsTests: STPNetworkStubbingTestCase {
func testCreateBancontactPaymentMethod() {
let client = STPAPIClient(publishableKey: STPTestingDefaultPublishableKey)
let bancontactParams = STPPaymentMethodBancontactParams()
Expand Down
3 changes: 2 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentMethodBancontactTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import StripeCoreTestUtils
@testable@_spi(STP) import StripeCore
@testable@_spi(STP) import StripePayments
@testable@_spi(STP) import StripePaymentSheet
import StripePaymentsTestUtils
@testable@_spi(STP) import StripePaymentsUI

class STPPaymentMethodBancontactTests: XCTestCase {
class STPPaymentMethodBancontactTests: STPNetworkStubbingTestCase {
private(set) var bancontactJSON: [AnyHashable: Any]?

func _retrieveBancontactJSON(_ completion: @escaping ([AnyHashable: Any]?) -> Void) {
Expand Down
Loading

0 comments on commit 8a462b5

Please sign in to comment.