From c812ceedd26d799461b24c9d42f28bdf3a573314 Mon Sep 17 00:00:00 2001 From: Victoria Park Date: Mon, 26 Aug 2024 06:55:16 -0700 Subject: [PATCH] Add more unit tests --- Sources/BraintreePayPal/BTPayPalClient.swift | 13 ++-- .../BTPayPalClient_Tests.swift | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/Sources/BraintreePayPal/BTPayPalClient.swift b/Sources/BraintreePayPal/BTPayPalClient.swift index 8c00aea03f..29a00c03ce 100644 --- a/Sources/BraintreePayPal/BTPayPalClient.swift +++ b/Sources/BraintreePayPal/BTPayPalClient.swift @@ -24,13 +24,9 @@ import BraintreeDataCollector var approvalURL: URL? = nil /// Exposed for testing the clientMetadataID associated with this request. - /// Used in POST body for FPTI analytics & `/paypal_account` fetch. + /// Used in POST body for FPTI analytics & `/paypal_account` fetch & editFI POST body. var clientMetadataID: String? = nil - // should we use the same clientMetadataID variable? - /// Exposed for testing the editFI request construction - var correlationID: String? = nil - /// Exposed for testing the intent associated with this request var payPalRequest: BTPayPalRequest? = nil @@ -224,8 +220,8 @@ import BraintreeDataCollector let dataCollector = BTDataCollector(apiClient: self.apiClient) // TODO: set correlation_id to riskCorrelationId if passed in by merchant // note: we don't have payPalContextID from approvalURL yet - self.correlationID = dataCollector.clientMetadataID(nil) - request.correlationID = self.correlationID + self.clientMetadataID = dataCollector.clientMetadataID(nil) + request.correlationID = self.clientMetadataID self.editFIRequest = request self.apiClient.post(request.hermesPath, parameters: request.parameters()) { body, response, error in if let error = error { @@ -249,7 +245,8 @@ import BraintreeDataCollector self.notifyEditFIFailure(with: BTPayPalError.invalidURL("Returned app switch URL when web browser switch was expected"), completion: completion) case .webBrowser(let url): print("🌸 url \(url.absoluteString)") - self.handlePayPalEditFIRequest(with: url, paymentType: .vault, completion: completion) + // TODO: handle response here per updated reqs + // self.handlePayPalEditFIRequest(with: url, paymentType: .vault, completion: completion) } } } diff --git a/UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift b/UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift index 19d0a07dd3..3a470b1677 100644 --- a/UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift +++ b/UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift @@ -150,6 +150,44 @@ class BTPayPalClient_Tests: XCTestCase { } } + func testTokenizeEdit_whenRemoteConfigurationFetchFails_callsBackWithConfigurationError() { + mockAPIClient.cannedConfigurationResponseBody = nil + + let editRequest = BTPayPalVaultEditRequest(editPayPalVaultID: "test-ID") + let expectation = expectation(description: "Edit Vault fails with error") + + payPalClient.edit(editRequest) { editResult, error in + guard let error = error as NSError? else { XCTFail(); return } + XCTAssertNil(editResult) + XCTAssertEqual(error.domain, BTPayPalError.errorDomain) + XCTAssertEqual(error.code, BTPayPalError.fetchConfigurationFailed.errorCode) + XCTAssertEqual(error.localizedDescription, BTPayPalError.fetchConfigurationFailed.errorDescription) + expectation.fulfill() + } + + self.waitForExpectations(timeout: 1) + } + + func testTokenizeEdit_whenPayPalNotEnabledInConfiguration_callsBackWithError() { + mockAPIClient.cannedConfigurationResponseBody = BTJSON(value: [ + "paypalEnabled": false + ]) + + let editRequest = BTPayPalVaultEditRequest(editPayPalVaultID: "test-ID") + let expectation = expectation(description: "Edit Vault fails with error") + + payPalClient.edit(editRequest) { result, error in + guard let error = error as NSError? else { XCTFail(); return } + XCTAssertNil(result) + XCTAssertEqual(error.domain, BTPayPalError.errorDomain) + XCTAssertEqual(error.code, BTPayPalError.disabled.errorCode) + XCTAssertEqual(error.localizedDescription, "PayPal is not enabled for this merchant. Enable PayPal for this merchant in the Braintree Control Panel.") + expectation.fulfill() + } + + self.waitForExpectations(timeout: 1) + } + func testEditFI_whenRemoteConfigurationFetchSucceeds_postsToCorrectEndpoint() { let editRequest = BTPayPalVaultEditRequest(editPayPalVaultID: "test-ID") @@ -343,6 +381,31 @@ class BTPayPalClient_Tests: XCTestCase { XCTAssertTrue(mockAPIClient.postedAnalyticsEvents.contains("paypal:tokenize:handle-return:started")) } + func testTEditVault_whenAllApprovalURLsInvalid_returnsError() { + mockAPIClient.cannedResponseBody = BTJSON(value: [ + "agreementSetup": [ + "approvalUrl": "", + "paypalAppApprovalUrl": "" + ] + ]) + + let request = BTPayPalVaultEditRequest(editPayPalVaultID: "test-id") + let expectation = expectation(description: "Returns error") + + payPalClient.edit(request) { editResult, error in + guard let error = error as NSError? else { XCTFail(); return } + XCTAssertNil(editResult) + XCTAssertEqual(error.domain, BTPayPalError.errorDomain) + XCTAssertEqual(error.code, BTPayPalError.invalidURL("").errorCode) + XCTAssertEqual(error.localizedDescription, "An error occurred with retrieving a PayPal URL: Missing approval URL in gateway response.") + expectation.fulfill() + } + + waitForExpectations(timeout: 1.0) + } + + // TODO: test correct parsing of url's, BA token + // MARK: - Browser switch func testTokenizePayPalAccount_whenPayPalPayLaterOffered_performsSwitchCorrectly() {