Test fixes

This commit is contained in:
David Estes 2022-10-10 16:34:33 -07:00
parent ad9e9dcaaa
commit b9ea4c3fb9
18 changed files with 61 additions and 119 deletions

View File

@ -6,14 +6,6 @@
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
//
// STPImages.m
// Stripe
//
// Created by Jack Flintermann on 6/30/16.
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
import Foundation
import UIKit
@ -70,15 +62,6 @@ import UIKit
return self.safeImageNamed("stp_shipping_form", templateIfAvailable: true)
}
// TODO: This method can be removed when STPImageLibraryTest is converted to Swift
@objc(safeImageNamed:templateIfAvailable:)
class func _objc_safeImageNamed(
_ imageName: String,
templateIfAvailable: Bool
) -> UIImage {
safeImageNamed(imageName, templateIfAvailable: templateIfAvailable)
}
class func image(
withTintColor color: UIColor,
for image: UIImage
@ -92,7 +75,7 @@ import UIKit
x: 0, y: 0, width: templateImage.size.width, height: templateImage.size.height))
newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
return newImage ?? image
}
}

View File

@ -300,7 +300,7 @@ NS_ASSUME_NONNULL_BEGIN
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(queue, ^{
// SecTrustEvaluateAsync must be called from the same queue that is passed as an arg
// SecTrustEvaluateAsyncWithError must be called from the same queue that is passed as an arg
SecTrustEvaluateAsyncWithError(trust, queue, ^(SecTrustRef _Nonnull trustRef, bool result, CFErrorRef _Nullable error) {
if (result) {
verified = YES;

View File

@ -14,7 +14,7 @@ extension PKPayment {
}
/// Returns a fake transaction identifier with the expected ~-separated format.
@_spi(STP) public class func stp_testTransactionIdentifier() -> String? {
@_spi(STP) public class func stp_testTransactionIdentifier() -> String {
var uuid = UUID().uuidString
uuid = uuid.replacingOccurrences(of: "~", with: "")

View File

@ -48,7 +48,7 @@ extension StripeAPI.Token {
static let Resource = "tokens"
}
@_spi(STP) public extension PKPayment {
extension PKPayment {
func stp_tokenParameters(apiClient: STPAPIClient) -> [String: Any] {
let paymentString = String(data: self.token.paymentData, encoding: .utf8)
var payload: [String: Any] = [:]
@ -78,7 +78,7 @@ extension StripeAPI.Token {
var transactionIdentifier = self.token.transactionIdentifier
if transactionIdentifier != "" {
if self.stp_isSimulated() {
transactionIdentifier = PKPayment.stp_testTransactionIdentifier() ?? ""
transactionIdentifier = PKPayment.stp_testTransactionIdentifier()
}
payload["pk_token_transaction_id"] = transactionIdentifier
}

View File

@ -14,7 +14,7 @@ import UIKit
@_spi(STP) public let allResponseFields: [AnyHashable : Any]
@_spi(STP) public init(usBankAccount: USBankAccount?,
allResponseFields: [AnyHashable : Any]) {
allResponseFields: [AnyHashable : Any]) {
self.usBankAccount = usBankAccount
self.allResponseFields = allResponseFields
}

View File

@ -115,8 +115,7 @@ extension STPAPIClient {
}
}
@_spi(STP) public extension PKPayment {
extension PKPayment {
func stp_tokenParameters(apiClient: STPAPIClient) -> [String: Any] {
let paymentString = String(data: self.token.paymentData, encoding: .utf8)
var payload: [String: Any] = [:]
@ -146,7 +145,7 @@ extension STPAPIClient {
var transactionIdentifier = self.token.transactionIdentifier
if transactionIdentifier != "" {
if self.stp_isSimulated() {
transactionIdentifier = PKPayment.stp_testTransactionIdentifier() ?? ""
transactionIdentifier = PKPayment.stp_testTransactionIdentifier()
}
payload["pk_token_transaction_id"] = transactionIdentifier
}
@ -177,7 +176,7 @@ extension PKPayment {
}
/// Returns a fake transaction identifier with the expected ~-separated format.
@_spi(STP) public class func stp_testTransactionIdentifier() -> String? {
@_spi(STP) public class func stp_testTransactionIdentifier() -> String {
var uuid = UUID().uuidString
uuid = uuid.replacingOccurrences(of: "~", with: "")

View File

@ -11,7 +11,7 @@ import Foundation
/// An STPAPIResponseDecodable implementation to use for endpoints that don't
/// actually return objects, like /v1/3ds2/challenge_completed
@_spi(STP) public class STPEmptyStripeResponse: NSObject, STPAPIResponseDecodable {
@_spi(STP) public var allResponseFields: [AnyHashable: Any] = [:]
@_spi(STP) public private(set) var allResponseFields: [AnyHashable: Any] = [:]
required internal override init() {
super.init()

View File

@ -11,7 +11,7 @@ import Foundation
/// Deserializes the response returned from https://stripe.com/docs/api/payment_methods/list
@_spi(STP) public class STPPaymentMethodListDeserializer: NSObject, STPAPIResponseDecodable {
@_spi(STP) public var paymentMethods: [STPPaymentMethod]?
@_spi(STP) public var allResponseFields: [AnyHashable: Any] = [:]
@_spi(STP) public private(set) var allResponseFields: [AnyHashable: Any] = [:]
// MARK: STPAPIResponseDecodable
override required init() {

View File

@ -54,6 +54,5 @@ extension GenericPaymentAnalytic {
@_spi(STP) public protocol STPAnalyticsSerializer {
static func serializeConfiguration(_ configuration: NSObject) -> [String:
String]
static func serializeConfiguration(_ configuration: NSObject) -> [String: String]
}

View File

@ -6,14 +6,6 @@
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
//
// STPImages.m
// Stripe
//
// Created by Jack Flintermann on 6/30/16.
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
import Foundation
import UIKit
@ -148,23 +140,6 @@ public class STPImageLibrary: NSObject {
templateIfAvailable: shouldUseTemplate)
return image
}
class func image(
withTintColor color: UIColor,
for image: UIImage
) -> UIImage {
var newImage: UIImage?
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
color.set()
let templateImage = image.withRenderingMode(.alwaysTemplate)
templateImage.draw(
in: CGRect(
x: 0, y: 0, width: templateImage.size.width, height: templateImage.size.height))
newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
// MARK: - ImageMaker

View File

@ -370,8 +370,7 @@ public class STPCardFormView: STPFormView {
var scannerClassObject: AnyObject.Type?
if let scanner = NSClassFromString("STPCardScanner") {
scannerClassObject = scanner
}
if let scanner = NSClassFromString("STPCardScanner_legacy") {
} else if let scanner = NSClassFromString("STPCardScanner_legacy") {
scannerClassObject = scanner
}
let scannerClass = scannerClassObject as? STPCardScanningProtocol.Type

View File

@ -30,7 +30,7 @@ class PKPayment_StripeTest: XCTestCase {
}
func testTransactionIdentifier() {
let identifier = PKPayment.stp_testTransactionIdentifier()!
let identifier = PKPayment.stp_testTransactionIdentifier()
XCTAssertTrue(identifier.contains("ApplePayStubs~4242424242424242~0~USD~"))
}
}

View File

@ -11,6 +11,7 @@ import XCTest
@testable @_spi(STP) import StripeCore
@testable @_spi(STP) import StripeCore
@testable @_spi(STP) import StripePaymentSheet
@testable @_spi(STP) import StripePayments
@testable @_spi(STP) import StripeApplePay
import StripeCoreTestUtils

View File

@ -13,6 +13,7 @@ import XCTest
@testable @_spi(STP) import StripeCore
@testable @_spi(STP) import StripePayments
@testable @_spi(STP) import StripePaymentSheet
import StripeApplePay
class STPAnalyticsClientPaymentsTest: XCTestCase {
private var client: STPAnalyticsClient!
@ -37,52 +38,6 @@ class STPAnalyticsClientPaymentsTest: XCTestCase {
XCTAssertEqual(client.additionalInfo(), [])
}
func testProductUsageFull() {
client.addClass(toProductUsageIfNecessary: MockAnalyticsClass1.self)
client.addClass(toProductUsageIfNecessary: STPPaymentContext.self)
let usageLevel = STPAnalyticsClient.uiUsageLevelString(from: client.productUsage)
XCTAssertEqual(usageLevel, "full")
XCTAssertEqual(client.productUsage, Set([
MockAnalyticsClass1.stp_analyticsIdentifier,
STPPaymentContext.stp_analyticsIdentifier,
]))
}
func testProductUsageCardTextField() {
client.addClass(toProductUsageIfNecessary: STPPaymentCardTextField.self)
let usageLevel = STPAnalyticsClient.uiUsageLevelString(from: client.productUsage)
XCTAssertEqual(usageLevel, "card_text_field")
XCTAssertEqual(client.productUsage, Set([
STPPaymentCardTextField.stp_analyticsIdentifier,
]))
}
func testProductUsagePartial() {
client.addClass(toProductUsageIfNecessary: STPPaymentCardTextField.self)
client.addClass(toProductUsageIfNecessary: MockAnalyticsClass1.self)
client.addClass(toProductUsageIfNecessary: MockAnalyticsClass2.self)
let usageLevel = STPAnalyticsClient.uiUsageLevelString(from: client.productUsage)
XCTAssertEqual(usageLevel, "partial")
XCTAssertEqual(client.productUsage, Set([
MockAnalyticsClass1.stp_analyticsIdentifier,
MockAnalyticsClass2.stp_analyticsIdentifier,
STPPaymentCardTextField.stp_analyticsIdentifier,
]))
}
func testProductUsageNone() {
let usageLevel = STPAnalyticsClient.uiUsageLevelString(from: client.productUsage)
XCTAssertEqual(usageLevel, "none")
XCTAssert(client.productUsage.isEmpty)
}
func testPayloadFromAnalytic() throws {
client.addAdditionalInfo("test_additional_info")

View File

@ -10,7 +10,7 @@
@testable @_spi(STP) import StripeCore
@testable @_spi(STP) import StripePaymentSheet
@testable @_spi(STP) import StripePaymentsUI
@testable @_spi(STP) import StripePayments
@_spi(STP) import StripePayments
@testable @_spi(STP) import StripeApplePay
class STPApplePayTestDelegateiOS11: NSObject, STPApplePayContextDelegate {
@ -122,4 +122,13 @@ class STPApplePayContextTest: XCTestCase {
XCTAssertEqual(shippingParams?.address.country, "US")
XCTAssertEqual(shippingParams?.address.postalCode, "94105")
}
// Tests stp_tokenParameters in StripeApplePay, not StripePayments
func testStpTokenParameters() {
let applePay = STPFixtures.applePayPayment()
let applePayDict = applePay.stp_tokenParameters(apiClient: .shared)
XCTAssertNotNil(applePayDict["pk_token"])
XCTAssertEqual((applePayDict["card"] as! NSDictionary)["name"] as! String, "Test Testerson")
XCTAssertEqual(applePayDict["pk_token_instrument_name"] as! String, "Master Charge")
}
}

View File

@ -21,4 +21,13 @@ class STPApplePaySwiftTest : XCTestCase {
XCTAssertTrue(StripeAPI.supportedPKPaymentNetworks().contains(.JCB))
StripeAPI.additionalEnabledApplePayNetworks = []
}
// Tests stp_tokenParameters in StripePayments, not StripeApplePay
func testStpTokenParameters() {
let applePay = STPFixtures.applePayPayment()
let applePayDict = applePay.stp_tokenParameters(apiClient: .shared)
XCTAssertNotNil(applePayDict["pk_token"])
XCTAssertEqual((applePayDict["card"] as! NSDictionary)["name"] as! String, "Test Testerson")
XCTAssertEqual(applePayDict["pk_token_instrument_name"] as! String, "Master Charge")
}
}

View File

@ -319,11 +319,24 @@ NSString *const STPTestJSONSourceWeChatPay = @"WeChatPaySource";
@"\"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEhKpIc6wTNQGy39bHM0a0qziDb20jMBFZT9XKSdjGULpDGRdyil6MLwMyIf3lQxaV/"
@"P7CQztw28IvYozvKvjBPQ==\",\"publicKeyHash\":\"yRcyn7njT6JL3AY9nmg0KD/xm/ch7gW1sGl2OuEucZY=\"}}";
NSData *data = [tokenDataString dataUsingEncoding:NSUTF8StringEncoding];
NSPersonNameComponents *nameComponents = [[NSPersonNameComponents alloc] init];
[nameComponents setGivenName:@"Test"];
[nameComponents setFamilyName:@"Testerson"];
PKContact *contact = [[PKContact alloc] init];
contact.name = nameComponents;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
// Add a fake display name
PKPaymentMethod *paymentMethod = [[PKPaymentMethod alloc] init];
[paymentMethod performSelector:@selector(setDisplayName:) withObject:@"Master Charge"];
[paymentToken performSelector:@selector(setPaymentMethod:) withObject:paymentMethod];
[paymentToken performSelector:@selector(setPaymentData:) withObject:data];
[payment performSelector:@selector(setToken:) withObject:paymentToken];
[payment performSelector:@selector(setBillingContact:) withObject:contact];
#pragma clang diagnostic pop
return payment;
}

View File

@ -17,7 +17,7 @@ class STPStringUtilsSwiftTest: XCTestCase {
STPStringUtils.parseRange(from: "Test <b>string</b>",
withTag: "b") { string, range in
XCTAssertTrue(NSEqualRanges(range, NSRange(location: 5, length: 6)))
XCTAssertTrue(string == "Test string")
XCTAssertEqual(string, "Test string")
exp.fulfill()
}
waitForExpectations(timeout: 1)
@ -28,7 +28,7 @@ class STPStringUtilsSwiftTest: XCTestCase {
STPStringUtils.parseRange(from: "<a>Test <b>str</a>ing</b>",
withTag: "b") { string, range in
XCTAssertTrue(NSEqualRanges(range, NSRange(location: 8, length: 10)))
XCTAssertTrue(string == "<a>Test str</a>ing")
XCTAssertEqual(string, "<a>Test str</a>ing")
exp.fulfill()
}
waitForExpectations(timeout: 1)
@ -38,8 +38,8 @@ class STPStringUtilsSwiftTest: XCTestCase {
let exp = self.expectation(description: "Parsed")
STPStringUtils.parseRange(from: "Test <b>string</b>",
withTag: "a") { string, range in
XCTAssertTrue(range.location == NSNotFound)
XCTAssertTrue(string == "Test <b>string</b>")
XCTAssertEqual(range.location, NSNotFound)
XCTAssertEqual(string, "Test <b>string</b>")
exp.fulfill()
}
waitForExpectations(timeout: 1)
@ -49,8 +49,8 @@ class STPStringUtilsSwiftTest: XCTestCase {
let exp = self.expectation(description: "Parsed")
STPStringUtils.parseRange(from: "Test <b>string",
withTag: "b") { string, range in
XCTAssertTrue(range.location == NSNotFound)
XCTAssertTrue(string == "Test <b>string")
XCTAssertEqual(range.location, NSNotFound)
XCTAssertEqual(string, "Test <b>string")
exp.fulfill()
}
waitForExpectations(timeout: 1)
@ -61,8 +61,8 @@ class STPStringUtilsSwiftTest: XCTestCase {
STPStringUtils.parseRanges(from: "<a>Test</a> <b>string</b>", withTags: Set(["a", "b", "c"])) { string, tagMap in
XCTAssertTrue(NSEqualRanges(tagMap["a"]!.rangeValue, NSRange(location: 0, length: 4)))
XCTAssertTrue(NSEqualRanges(tagMap["b"]!.rangeValue, NSRange(location: 5, length: 6)))
XCTAssertTrue(tagMap["c"]!.rangeValue.location == NSNotFound)
XCTAssertTrue(string == "Test string")
XCTAssertEqual(tagMap["c"]!.rangeValue.location, NSNotFound)
XCTAssertEqual(string, "Test string")
exp.fulfill()
}
waitForExpectations(timeout: 1)
@ -71,10 +71,10 @@ class STPStringUtilsSwiftTest: XCTestCase {
func testParseRangeMultiTag2() {
let exp = self.expectation(description: "Parsed")
STPStringUtils.parseRanges(from: "Test string", withTags: Set(["a", "b", "c"])) { string, tagMap in
XCTAssertTrue(tagMap["a"]!.rangeValue.location == NSNotFound)
XCTAssertTrue(tagMap["b"]!.rangeValue.location == NSNotFound)
XCTAssertTrue(tagMap["c"]!.rangeValue.location == NSNotFound)
XCTAssertTrue(string == "Test string")
XCTAssertEqual(tagMap["a"]!.rangeValue.location, NSNotFound)
XCTAssertEqual(tagMap["b"]!.rangeValue.location, NSNotFound)
XCTAssertEqual(tagMap["c"]!.rangeValue.location, NSNotFound)
XCTAssertEqual(string, "Test string")
exp.fulfill()
}
waitForExpectations(timeout: 1)