Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KunJeongPark committed Aug 26, 2024
1 parent dd6f788 commit c812cee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
13 changes: 5 additions & 8 deletions Sources/BraintreePayPal/BTPayPalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions UnitTests/BraintreePayPalTests/BTPayPalClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c812cee

Please sign in to comment.