From ef45510abaca386369dba0e675e61f8928b88131 Mon Sep 17 00:00:00 2001 From: Victoria Park Date: Tue, 15 Oct 2024 11:16:46 -0700 Subject: [PATCH] Remove CardDelegate, functions and unit tests --- .../ViewModels/CardPaymentViewModel.swift | 48 +---- PayPal.xcodeproj/project.pbxproj | 8 - Sources/CardPayments/CardClient.swift | 85 -------- Sources/CardPayments/CardClientError.swift | 2 +- Sources/CardPayments/CardDelegate.swift | 35 ---- .../CardPaymentsTests/CardClient_Tests.swift | 189 +----------------- .../Mocks/MockCardDelegate.swift | 45 ----- 7 files changed, 10 insertions(+), 402 deletions(-) delete mode 100644 Sources/CardPayments/CardDelegate.swift delete mode 100644 UnitTests/CardPaymentsTests/Mocks/MockCardDelegate.swift diff --git a/Demo/Demo/ViewModels/CardPaymentViewModel.swift b/Demo/Demo/ViewModels/CardPaymentViewModel.swift index ea0f4af4a..4454f6e70 100644 --- a/Demo/Demo/ViewModels/CardPaymentViewModel.swift +++ b/Demo/Demo/ViewModels/CardPaymentViewModel.swift @@ -3,7 +3,7 @@ import CardPayments import CorePayments import FraudProtection -class CardPaymentViewModel: ObservableObject, CardDelegate { +class CardPaymentViewModel: ObservableObject { @Published var state = CardPaymentState() private var payPalDataCollector: PayPalDataCollector? @@ -117,7 +117,6 @@ class CardPaymentViewModel: ObservableObject, CardDelegate { let config = try await configManager.getCoreConfig() cardClient = CardClient(config: config) payPalDataCollector = PayPalDataCollector(config: config) - cardClient?.delegate = self let cardRequest = CardRequest(orderID: orderID, card: card, sca: sca) let result = try await cardClient?.approveOrderAsync(request: cardRequest) if let result { @@ -128,11 +127,15 @@ class CardPaymentViewModel: ObservableObject, CardDelegate { ) approveResultSuccessResult(approveResult: finalResult) } else { - self.state.approveResultResponse = .error(message: "cardResult was nil") + DispatchQueue.main.async { + self.state.approveResultResponse = .error(message: "cardResult was nil") + } } } catch { - self.state.approveResultResponse = .error(message: error.localizedDescription) - print("failed in checkout with card. \(error.localizedDescription)") + DispatchQueue.main.async { + self.state.approveResultResponse = .error(message: error.localizedDescription) + print("failed in checkout with card. \(error.localizedDescription)") + } } } @@ -149,39 +152,4 @@ class CardPaymentViewModel: ObservableObject, CardDelegate { self.state.approveResultResponse = .error(message: vaultError.localizedDescription) } } - - // MARK: - Card Delegate - - func card(_ cardClient: CardPayments.CardClient, didFinishWithResult result: CardPayments.CardResult) { - approveResultSuccessResult( - approveResult: CardPaymentState.CardResult( - id: result.orderID, - status: result.status, - didAttemptThreeDSecureAuthentication: result.didAttemptThreeDSecureAuthentication - ) - ) - } - - func card(_ cardClient: CardPayments.CardClient, didFinishWithError error: CorePayments.CoreSDKError) { - print("Error here") - DispatchQueue.main.async { - self.state.approveResultResponse = .error(message: error.localizedDescription) - } - } - - func cardDidCancel(_ cardClient: CardPayments.CardClient) { - print("Card Payment Canceled") - DispatchQueue.main.async { - self.state.approveResultResponse = .idle - self.state.approveResult = nil - } - } - - func cardThreeDSecureWillLaunch(_ cardClient: CardPayments.CardClient) { - print("About to launch 3DS") - } - - func cardThreeDSecureDidFinish(_ cardClient: CardPayments.CardClient) { - print("Finished 3DS") - } } diff --git a/PayPal.xcodeproj/project.pbxproj b/PayPal.xcodeproj/project.pbxproj index 5862f42c2..c22f19db6 100644 --- a/PayPal.xcodeproj/project.pbxproj +++ b/PayPal.xcodeproj/project.pbxproj @@ -119,7 +119,6 @@ BEA100F026EFA7C20036A6A5 /* NetworkingClientError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEA100EF26EFA7C20036A6A5 /* NetworkingClientError.swift */; }; BEA100F226EFA7DE0036A6A5 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEA100F126EFA7DE0036A6A5 /* Environment.swift */; }; CB16E6D8285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */; }; - CB16E6DA285B7B7300FD6F52 /* MockCardDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */; }; CB1A47F22820AFED00BD8184 /* PayPalPayLaterButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F12820AFED00BD8184 /* PayPalPayLaterButton.swift */; }; CB1A47F42820BA5D00BD8184 /* PaymentButtonEdges.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F32820BA5D00BD8184 /* PaymentButtonEdges.swift */; }; CB1A47F62820BAA600BD8184 /* PaymentButtonLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB1A47F52820BAA600BD8184 /* PaymentButtonLabel.swift */; }; @@ -130,7 +129,6 @@ CB22C018291049500097E592 /* PayPalPayLaterButton_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB22C017291049500097E592 /* PayPalPayLaterButton_Tests.swift */; }; CB4BE27D2847AF6F00EA2DD1 /* SCA.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE27C2847AF6F00EA2DD1 /* SCA.swift */; }; CB4BE27E2847EA7D00EA2DD1 /* WebAuthenticationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE4F784827EB629100FF4C0E /* WebAuthenticationSession.swift */; }; - CB4BE2802847F01000EA2DD1 /* CardDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */; }; CB4BE28A28512A9800EA2DD1 /* MockQuededURLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4BE28928512A9800EA2DD1 /* MockQuededURLSession.swift */; }; CB4BE28B2851423900EA2DD1 /* MockWebAuthenticationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE8A7F627EA54A000AC301B /* MockWebAuthenticationSession.swift */; }; CB4BE28C2851427600EA2DD1 /* MockViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE8A7F427EA544000AC301B /* MockViewController.swift */; }; @@ -294,7 +292,6 @@ BEDB7FE32788AB8E00CEA554 /* Coordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator.swift; sourceTree = ""; }; BEF3FF1627AC5DF3006B4B69 /* Coordinator_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator_Tests.swift; sourceTree = ""; }; CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeConfirmPaymentResponse.swift; sourceTree = ""; }; - CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCardDelegate.swift; sourceTree = ""; }; CB1A47F12820AFED00BD8184 /* PayPalPayLaterButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalPayLaterButton.swift; sourceTree = ""; }; CB1A47F32820BA5D00BD8184 /* PaymentButtonEdges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentButtonEdges.swift; sourceTree = ""; }; CB1A47F52820BAA600BD8184 /* PaymentButtonLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentButtonLabel.swift; sourceTree = ""; }; @@ -304,7 +301,6 @@ CB1A48042822BCED00BD8184 /* PaymentButton+ImageAsset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PaymentButton+ImageAsset.swift"; sourceTree = ""; }; CB22C017291049500097E592 /* PayPalPayLaterButton_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalPayLaterButton_Tests.swift; sourceTree = ""; }; CB4BE27C2847AF6F00EA2DD1 /* SCA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SCA.swift; sourceTree = ""; }; - CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardDelegate.swift; sourceTree = ""; }; CB4BE285284802C200EA2DD1 /* PaymentSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentSource.swift; sourceTree = ""; }; CB4BE28928512A9800EA2DD1 /* MockQuededURLSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockQuededURLSession.swift; sourceTree = ""; }; CB51FF7127FCB947001A97F5 /* PayPalWebCheckoutFundingSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PayPalWebCheckoutFundingSource.swift; sourceTree = ""; }; @@ -488,7 +484,6 @@ isa = PBXGroup; children = ( CB16E6D7285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift */, - CB16E6D9285B7B7300FD6F52 /* MockCardDelegate.swift */, 3B109B3A2A85B54800D8135F /* MockCardVaultDelegate.swift */, 808E3EDC2A981F240017FE46 /* MockCheckoutOrdersAPI.swift */, 808E3EDE2A981F390017FE46 /* MockVaultPaymentTokensAPI.swift */, @@ -554,7 +549,6 @@ children = ( 06CE009A26F3D5A40000CC46 /* CardClient.swift */, 80DCC59D2719DB6F00EC7C5A /* CardClientError.swift */, - CB4BE27F2847F01000EA2DD1 /* CardDelegate.swift */, 3B22E8B52A840ECF00962E34 /* CardVaultDelegate.swift */, 80DBC9D829C336D500462539 /* APIRequests */, 065A4DBD26FCDA270007014A /* Models */, @@ -1229,7 +1223,6 @@ 8048D28C270B9DE00072214A /* ConfirmPaymentSourceResponse.swift in Sources */, 3D1763A22720722A00652E1C /* CardResult.swift in Sources */, BC0A82A5270B9533006E9A21 /* ConfirmPaymentSourceRequest.swift in Sources */, - CB4BE2802847F01000EA2DD1 /* CardDelegate.swift in Sources */, 3BD82DBB2A835AF900CBE764 /* UpdateSetupTokenResponse.swift in Sources */, 80E8DAE126B8784600FAFC3F /* Card.swift in Sources */, 3B22E8B82A841AEA00962E34 /* CardVaultResult.swift in Sources */, @@ -1253,7 +1246,6 @@ 3B109B3C2A85CC6200D8135F /* MockCardVaultDelegate.swift in Sources */, 80B27AF12A9E9EE60008EA45 /* VaultPaymentTokensAPI_Tests.swift in Sources */, CB16E6D8285B7A2B00FD6F52 /* FakeConfirmPaymentResponse.swift in Sources */, - CB16E6DA285B7B7300FD6F52 /* MockCardDelegate.swift in Sources */, 3B783DC32B7A69C2004623DB /* FakeUpdateSetupTokenResponse.swift in Sources */, BC0A82A6270B9954006E9A21 /* ConfirmPaymentSourceRequest_Tests.swift in Sources */, 808E3EDD2A981F240017FE46 /* MockCheckoutOrdersAPI.swift in Sources */, diff --git a/Sources/CardPayments/CardClient.swift b/Sources/CardPayments/CardClient.swift index 1b3d27723..7e4acc79d 100644 --- a/Sources/CardPayments/CardClient.swift +++ b/Sources/CardPayments/CardClient.swift @@ -6,7 +6,6 @@ import CorePayments public class CardClient: NSObject { - public weak var delegate: CardDelegate? public weak var vaultDelegate: CardVaultDelegate? private let checkoutOrdersAPI: CheckoutOrdersAPI @@ -75,40 +74,6 @@ public class CardClient: NSObject { /// - Parameters: /// - orderId: Order id for approval /// - request: The request containing the card - public func approveOrder(request: CardRequest) { - analyticsService = AnalyticsService(coreConfig: config, orderID: request.orderID) - analyticsService?.sendEvent("card-payments:3ds:started") - Task { - do { - let result = try await checkoutOrdersAPI.confirmPaymentSource(cardRequest: request) - - if result.status == "PAYER_ACTION_REQUIRED", - let url = result.links?.first(where: { $0.rel == "payer-action" })?.href { - guard getQueryStringParameter(url: url, param: "flow") == "3ds", - url.contains("helios"), - let url = URL(string: url) else { - self.notifyFailure(with: CardClientError.threeDSecureURLError) - return - } - - analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:challenge-required") - startThreeDSecureChallenge(url: url, orderId: result.id) - } else { - analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:succeeded") - - let cardResult = CardResult(orderID: result.id, status: result.status, didAttemptThreeDSecureAuthentication: false) - notifySuccess(for: cardResult) - } - } catch let error as CoreSDKError { - analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed") - notifyFailure(with: error) - } catch { - analyticsService?.sendEvent("card-payments:3ds:confirm-payment-source:failed") - notifyFailure(with: CardClientError.unknownError) - } - } - } - public func approveOrderAsync(request: CardRequest) async throws -> CardResult { analyticsService = AnalyticsService(coreConfig: config, orderID: request.orderID) analyticsService?.sendEvent("card-payments:3ds:started") @@ -141,41 +106,6 @@ public class CardClient: NSObject { } } - private func startThreeDSecureChallenge( - url: URL, - orderId: String - ) { - delegate?.cardThreeDSecureWillLaunch(self) - - webAuthenticationSession.start( - url: url, - context: self, - sessionDidDisplay: { [weak self] didDisplay in - if didDisplay { - self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:succeeded") - } else { - self?.analyticsService?.sendEvent("card-payments:3ds:challenge-presentation:failed") - } - }, - sessionDidComplete: { _, error in - self.delegate?.cardThreeDSecureDidFinish(self) - if let error = error { - switch error { - case ASWebAuthenticationSessionError.canceledLogin: - self.notifyCancellation() - return - default: - self.notifyFailure(with: CardClientError.threeDSecureError(error)) - return - } - } - - let cardResult = CardResult(orderID: orderId, status: nil, didAttemptThreeDSecureAuthentication: true) - self.notifySuccess(for: cardResult) - } - ) - } - private func startAsyncThreeDSecureChallenge( url: URL, orderId: String @@ -250,16 +180,6 @@ public class CardClient: NSObject { } ) } - - private func notifySuccess(for result: CardResult) { - analyticsService?.sendEvent("card-payments:3ds:succeeded") - delegate?.card(self, didFinishWithResult: result) - } - - private func notifyFailure(with error: CoreSDKError) { - analyticsService?.sendEvent("card-payments:3ds:failed") - delegate?.card(self, didFinishWithError: error) - } private func notifyVaultSuccess(for vaultResult: CardVaultResult) { analyticsService?.sendEvent("card-payments:vault-wo-purchase:succeeded") @@ -271,11 +191,6 @@ public class CardClient: NSObject { vaultDelegate?.card(self, didFinishWithVaultError: vaultError) } - private func notifyCancellation() { - analyticsService?.sendEvent("card-payments:3ds:challenge:user-canceled") - delegate?.cardDidCancel(self) - } - private func notifyVaultCancellation() { analyticsService?.sendEvent("card-payments:vault-wo-purchase:challenge:canceled") vaultDelegate?.cardThreeDSecureDidCancel(self) diff --git a/Sources/CardPayments/CardClientError.swift b/Sources/CardPayments/CardClientError.swift index cd51f5abf..daa855bd7 100644 --- a/Sources/CardPayments/CardClientError.swift +++ b/Sources/CardPayments/CardClientError.swift @@ -69,7 +69,7 @@ enum CardClientError { ) static let threeDSecureCancellation = CoreSDKError( - code: Code.threeDSCancellation.rawValue, + code: Code.threeDSCancellation.rawValue, domain: domain, errorDescription: "3DS verification has been cancelled by the user" ) diff --git a/Sources/CardPayments/CardDelegate.swift b/Sources/CardPayments/CardDelegate.swift deleted file mode 100644 index 77c5cace6..000000000 --- a/Sources/CardPayments/CardDelegate.swift +++ /dev/null @@ -1,35 +0,0 @@ -import Foundation -#if canImport(CorePayments) -import CorePayments -#endif - -/// Card delegate to handle events from CardClient -public protocol CardDelegate: AnyObject { - - /// Notify that the Card flow finished with a successful result - /// - Parameters: - /// - client: the CardClient associated with delegate - /// - didFinishWithResult: the successful result from the flow - func card(_ cardClient: CardClient, didFinishWithResult result: CardResult) - - /// Notify that an error occurred in the Cardl flow - /// - Parameters: - /// - client: the CardClient associated with delegate - /// - didFinishWithError: the error returned by the Card flow - func card(_ cardClient: CardClient, didFinishWithError error: CoreSDKError) - - /// Notify that the Card flow has been cancelled - /// - Parameters: - /// - client: the CardClient associated with delegate - func cardDidCancel(_ cardClient: CardClient) - - /// Notify that the 3DS challenge will be launched - /// - Parameters: - /// - client: the CardClient associated with delegate - func cardThreeDSecureWillLaunch(_ cardClient: CardClient) - - /// Notify that the 3DS challenge has finished - /// - Parameters: - /// - client: the CardClient associated with delegate - func cardThreeDSecureDidFinish(_ cardClient: CardClient) -} diff --git a/UnitTests/CardPaymentsTests/CardClient_Tests.swift b/UnitTests/CardPaymentsTests/CardClient_Tests.swift index f5364284e..b234c5424 100644 --- a/UnitTests/CardPaymentsTests/CardClient_Tests.swift +++ b/UnitTests/CardPaymentsTests/CardClient_Tests.swift @@ -5,7 +5,7 @@ import AuthenticationServices @testable import CardPayments @testable import TestShared -// swiftlint:disable type_body_length file_length +// swiftlint:disable type_body_length class CardClient_Tests: XCTestCase { // MARK: - Helper Properties @@ -264,28 +264,6 @@ class CardClient_Tests: XCTestCase { // MARK: - approveOrder() tests - func testApproveOrder_withInvalid3DSURL_returnsError() { - mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withInvalid3DSURL - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate(success: {_, _ in - XCTFail("Invoked success() callback. Should invoke error().") - }, error: { _, err in - XCTAssertEqual(err.code, 3) - XCTAssertEqual(err.domain, "CardClientErrorDomain") - XCTAssertEqual(err.errorDescription, "An invalid 3DS URL was returned. Contact developer.paypal.com/support.") - expectation.fulfill() - }, threeDSWillLaunch: { _ in - XCTFail("Invoked willLaunch() callback. Should invoke error().") - }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_withInvalid3DSURL_returnsError() async throws { mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withInvalid3DSURL @@ -299,28 +277,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_withNoThreeDSecure_returnsOrderData() { - mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.without3DS - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate(success: {_, result in - XCTAssertEqual(result.orderID, "testOrderId") - XCTAssertEqual(result.status, "APPROVED") - XCTAssertFalse(result.didAttemptThreeDSecureAuthentication) - expectation.fulfill() - }, error: { _, _ in - XCTFail("Invoked error() callback. Should invoke success().") - }, threeDSWillLaunch: { _ in - XCTFail("Invoked willLaunch() callback. Should invoke success().") - }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_withNoThreeDSecure_returnsOrderData() async throws { mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.without3DS @@ -334,28 +290,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_whenConfirmPaymentSDKError_bubblesError() { - mockCheckoutOrdersAPI.stubError = CoreSDKError(code: 123, domain: "sdk-domain", errorDescription: "sdk-error-desc") - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate(success: {_, _ in - XCTFail("Invoked success() callback. Should invoke error().") - }, error: { _, error in - XCTAssertEqual(error.domain, "sdk-domain") - XCTAssertEqual(error.code, 123) - XCTAssertEqual(error.localizedDescription, "sdk-error-desc") - expectation.fulfill() - }, threeDSWillLaunch: { _ in - XCTFail("Invoked willLaunch() callback. Should invoke error().") - }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_whenConfirmPaymentSDKError_bubblesError() async throws { mockCheckoutOrdersAPI.stubError = CoreSDKError(code: 123, domain: "sdk-domain", errorDescription: "sdk-error-desc") @@ -369,32 +303,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_whenConfirmPaymentGeneralError_returnsUnknownError() { - mockCheckoutOrdersAPI.stubError = NSError( - domain: "ns-fake-domain", - code: 123, - userInfo: [NSLocalizedDescriptionKey: "ns-fake-error"] - ) - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate(success: {_, _ in - XCTFail("Invoked success() callback. Should invoke error().") - }, error: { _, error in - XCTAssertEqual(error.domain, CardClientError.domain) - XCTAssertEqual(error.code, CardClientError.Code.unknown.rawValue) - XCTAssertNotNil(error.localizedDescription) - expectation.fulfill() - }, threeDSWillLaunch: { _ in - XCTFail("Invoked willLaunch() callback. Should invoke error().") - }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_whenConfirmPaymentGeneralError_returnsUnknownError() async throws { mockCheckoutOrdersAPI.stubError = NSError( domain: "ns-fake-domain", @@ -412,34 +320,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_withThreeDSecure_browserSwitchLaunches_getOrderReturnsSuccess() { - mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL - - mockWebAuthSession.cannedResponseURL = .init(string: "sdk.ios.paypal://card/success?state=undefined&code=undefined&liability_shift=POSSIBLE") - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate( - success: {_, result in - XCTAssertEqual(result.orderID, "testOrderId") - XCTAssertNil(result.status) - XCTAssertTrue(result.didAttemptThreeDSecureAuthentication) - expectation.fulfill() - }, - error: { _, error in - XCTFail(error.localizedDescription) - expectation.fulfill() - }, - cancel: { _ in XCTFail("Invoked cancel() callback. Should invoke success().") }, - threeDSWillLaunch: { _ in XCTAssert(true) }, - threeDSLaunched: { _ in XCTAssert(true) }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_withThreeDSecure_browserSwitchLaunches_getOrderReturnsSuccess() async throws { mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL @@ -455,38 +335,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_withThreeDSecure_userCancelsBrowser() { - mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL - - mockWebAuthSession.cannedErrorResponse = ASWebAuthenticationSessionError( - .canceledLogin, - userInfo: ["Description": "Mock cancellation error description."] - ) - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate( - success: {_, _ in - XCTFail("Invoked success() callback. Should invoke cancel().") - expectation.fulfill() - }, - error: { _, error in - XCTFail(error.localizedDescription) - expectation.fulfill() - }, - cancel: { _ in - XCTAssert(true) - expectation.fulfill() - }, - threeDSWillLaunch: { _ in XCTAssert(true) }, - threeDSLaunched: { _ in XCTAssert(true) }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_withThreeDSecure_userCancelsBrowser() async throws { mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL @@ -505,41 +353,6 @@ class CardClient_Tests: XCTestCase { } } - func testApproveOrder_withThreeDSecure_browserReturnsError() { - mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL - - mockWebAuthSession.cannedErrorResponse = CoreSDKError( - code: CardClientError.Code.threeDSecureError.rawValue, - domain: CardClientError.domain, - errorDescription: "Mock web session error description." - ) - - let expectation = expectation(description: "approveOrder() completed") - - let mockCardDelegate = MockCardDelegate( - success: {_, _ in - XCTFail("Invoked success() callback. Should invoke error().") - expectation.fulfill() - }, - error: { _, error in - XCTAssertEqual(error.domain, CardClientError.domain) - XCTAssertEqual(error.code, CardClientError.Code.threeDSecureError.rawValue) - XCTAssertEqual(error.localizedDescription, "Mock web session error description.") - expectation.fulfill() - }, - cancel: { _ in - XCTFail("Invoked cancel() callback. Should invoke error().") - expectation.fulfill() - }, - threeDSWillLaunch: { _ in XCTAssert(true) }, - threeDSLaunched: { _ in XCTAssert(true) }) - - sut.delegate = mockCardDelegate - sut.approveOrder(request: cardRequest) - - waitForExpectations(timeout: 10) - } - func testAsyncApproveOrder_withThreeDSecure_browserReturnsError() async throws { mockCheckoutOrdersAPI.stubConfirmResponse = FakeConfirmPaymentResponse.withValid3DSURL diff --git a/UnitTests/CardPaymentsTests/Mocks/MockCardDelegate.swift b/UnitTests/CardPaymentsTests/Mocks/MockCardDelegate.swift deleted file mode 100644 index 2b9b5a71a..000000000 --- a/UnitTests/CardPaymentsTests/Mocks/MockCardDelegate.swift +++ /dev/null @@ -1,45 +0,0 @@ -@testable import CorePayments -@testable import CardPayments - -class MockCardDelegate: CardDelegate { - - private var success: ((CardClient, CardResult) -> Void)? - private var failure: ((CardClient, CoreSDKError) -> Void)? - private var cancel: ((CardClient) -> Void)? - private var threeDSWillLaunch: ((CardClient) -> Void)? - private var threeDSLaunched: ((CardClient) -> Void)? - - required init( - success: ((CardClient, CardResult) -> Void)? = nil, - error: ((CardClient, CoreSDKError) -> Void)? = nil, - cancel: ((CardClient) -> Void)? = nil, - threeDSWillLaunch: ((CardClient) -> Void)? = nil, - threeDSLaunched: ((CardClient) -> Void)? = nil - ) { - self.success = success - self.failure = error - self.cancel = cancel - self.threeDSWillLaunch = threeDSWillLaunch - self.threeDSLaunched = threeDSLaunched - } - - func card(_ cardClient: CardClient, didFinishWithResult result: CardResult) { - success?(cardClient, result) - } - - func card(_ cardClient: CardClient, didFinishWithError error: CoreSDKError) { - failure?(cardClient, error) - } - - func cardDidCancel(_ cardClient: CardClient) { - cancel?(cardClient) - } - - func cardThreeDSecureWillLaunch(_ cardClient: CardClient) { - threeDSWillLaunch?(cardClient) - } - - func cardThreeDSecureDidFinish(_ cardClient: CardClient) { - threeDSLaunched?(cardClient) - } -}