From debae23bc89d05b3cfbd18b84a8f3662833a1f86 Mon Sep 17 00:00:00 2001 From: Seth Friedman Date: Thu, 27 Apr 2023 00:06:58 -0400 Subject: [PATCH] Add validation to catch blank ephemeral keys in CustomerConfiguration We currently don't validate that an ephemeral key is non-empty when a user includes it in the `CustomerConfiguration`. This ends up giving users a pretty obscure error in the API response, and is difficult to debug today. Including this validation will catch this issue early and provide users a better debugging experience. --- StripePaymentSheet/Project.swift | 2 +- .../PaymentSheetConfiguration.swift | 4 +++- .../PaymentSheetConfigurationTests.swift | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 StripePaymentSheet/StripePaymentSheetTests/PaymentSheetConfigurationTests.swift diff --git a/StripePaymentSheet/Project.swift b/StripePaymentSheet/Project.swift index ce7432d8e45..e2f308f6e65 100644 --- a/StripePaymentSheet/Project.swift +++ b/StripePaymentSheet/Project.swift @@ -12,5 +12,5 @@ let project = Project.stripeFramework( .project(target: "StripePayments", path: "//StripePayments"), .project(target: "StripePaymentsUI", path: "//StripePaymentsUI"), ], - unitTestOptions: .testOptions() + unitTestOptions: .testOptions(usesPreconditionTesting: true) ) diff --git a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentSheetConfiguration.swift b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentSheetConfiguration.swift index c37163592c0..6c7630cbe14 100644 --- a/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentSheetConfiguration.swift +++ b/StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/PaymentSheetConfiguration.swift @@ -172,11 +172,13 @@ extension PaymentSheet { /// See https://stripe.com/docs/api/customers/object#customer_object-id public let id: String - /// A short-lived token that allows the SDK to access a Customer's payment methods + /// A short-lived token that allows the SDK to access a Customer's payment methods. Cannot be empty public let ephemeralKeySecret: String /// Initializes a CustomerConfiguration public init(id: String, ephemeralKeySecret: String) { + assert(!ephemeralKeySecret.isEmpty, "Ephemeral key secret cannot be empty") + self.id = id self.ephemeralKeySecret = ephemeralKeySecret } diff --git a/StripePaymentSheet/StripePaymentSheetTests/PaymentSheetConfigurationTests.swift b/StripePaymentSheet/StripePaymentSheetTests/PaymentSheetConfigurationTests.swift new file mode 100644 index 00000000000..dc8e9241899 --- /dev/null +++ b/StripePaymentSheet/StripePaymentSheetTests/PaymentSheetConfigurationTests.swift @@ -0,0 +1,21 @@ +// +// PaymentSheetConfigurationTests.swift +// StripePaymentSheetTests +// +// Copyright © 2023 Stripe, Inc. All rights reserved. +// + +import CwlPreconditionTesting +import StripePaymentSheet +import XCTest + +final class PaymentSheetConfigurationTests: XCTestCase { + + func test_customerConfigurationInit_assertsWhenEphemeralKeyIsBlank() throws { + let exception = catchBadInstruction { + _ = PaymentSheet.CustomerConfiguration(id: "foo", ephemeralKeySecret: "") + } + + XCTAssertNotNil(exception) + } +}