-
Notifications
You must be signed in to change notification settings - Fork 994
/
Copy pathTelemetryInjectionTest.swift
95 lines (83 loc) · 3.57 KB
/
TelemetryInjectionTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// TelemetryInjectionTest.swift
// StripeApplePayTests
//
// Created by David Estes on 2/9/22.
// Copyright © 2022 Stripe, Inc. All rights reserved.
//
import AVFoundation
import Foundation
import OHHTTPStubs
import OHHTTPStubsSwift
import StripeCoreTestUtils
import XCTest
// swift-format-ignore
@_spi(STP) @testable import StripeApplePay
// swift-format-ignore
@_spi(STP) @testable import StripeCore
class TelemetryInjectionTest: APIStubbedTestCase {
func testIntentConfirmAddsTelemetry() {
let apiClient = stubbedAPIClient()
let piTelemetryExpectation = self.expectation(description: "saw pi telemetry")
let siTelemetryExpectation = self.expectation(description: "saw si telemetry")
// As an implementation detail, OHHTTPStubs will run this block in `canInitWithRequest` in addition to
// `initWithRequest`. So it could be called more times than we expect.
// We don't have control over this behavior (CFNetwork drives it), so let's not worry
// about overfulfillment.
piTelemetryExpectation.assertForOverFulfill = false
siTelemetryExpectation.assertForOverFulfill = false
stub { urlRequest in
if urlRequest.url!.absoluteString.contains("_intent") {
let ua = urlRequest.queryItems!.first(where: {
$0.name == "payment_method_data[payment_user_agent]"
})!.value!
XCTAssertTrue(ua.hasPrefix("stripe-ios/"))
let muid = urlRequest.queryItems!.first(where: {
$0.name == "payment_method_data[muid]"
})!.value!
let guid = urlRequest.queryItems!.first(where: {
$0.name == "payment_method_data[guid]"
})!.value!
XCTAssertNotNil(muid)
XCTAssertNotNil(guid)
if urlRequest.url!.absoluteString.contains("payment_intent") {
piTelemetryExpectation.fulfill()
}
if urlRequest.url!.absoluteString.contains("setup_intent") {
siTelemetryExpectation.fulfill()
}
return true
}
return false
} response: { _ in
// We don't care about the response
return HTTPStubsResponse()
}
var params = StripeAPI.PaymentMethodParams(type: .card)
var card = StripeAPI.PaymentMethodParams.Card()
card.number = "4242424242424242"
card.expYear = 28
card.expMonth = 12
card.cvc = "100"
params.card = card
// Set up telemetry data
StripeAPI.advancedFraudSignalsEnabled = true
FraudDetectionData.shared.sid = "sid"
FraudDetectionData.shared.muid = "muid"
FraudDetectionData.shared.guid = "guid"
FraudDetectionData.shared.sidCreationDate = Date()
let piExpectation = self.expectation(description: "PI Confirmed")
var pip = StripeAPI.PaymentIntentParams(clientSecret: "pi_123_secret_abc")
pip.paymentMethodData = params
StripeAPI.PaymentIntent.confirm(apiClient: apiClient, params: pip) { _ in
piExpectation.fulfill()
}
let siExpectation = self.expectation(description: "SI Confirmed")
var sip = StripeAPI.SetupIntentConfirmParams(clientSecret: "seti_123_secret_abc")
sip.paymentMethodData = params
StripeAPI.SetupIntent.confirm(apiClient: apiClient, params: sip) { _ in
siExpectation.fulfill()
}
waitForExpectations(timeout: STPTestingNetworkRequestTimeout, handler: nil)
}
}