-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: DPS-292 fixes externalPayerInfo on Paypal (#849)
* Mirror externalPayerId to external_payer_id in PayPal instrument * Also mirror firsName and lastName * Add constructor for tests * Added unit tests for mirrored encode/decode
- Loading branch information
Showing
2 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// PayPalResposeTests.swift | ||
// Debug App SPM | ||
// | ||
// Created by Niall Quinn on 04/05/24. | ||
// Copyright © 2024 Primer API Ltd. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import PrimerSDK | ||
|
||
final class PayPalResposeTests: XCTestCase { | ||
|
||
private typealias PayPalResponse = Response.Body.Tokenization.PayPal | ||
|
||
func test_externalPayerInfo_snakecase() async { | ||
let JSON_snake_case = """ | ||
{ | ||
"external_payer_id": "external-id", | ||
"first_name": "John", | ||
"last_name": "Doe", | ||
"email": "[email protected]" | ||
} | ||
""" | ||
|
||
guard let data = JSON_snake_case.data(using: .utf8) else { | ||
XCTFail() | ||
return | ||
} | ||
do { | ||
let decoder = JSONDecoder() | ||
let decoded: PayPalResponse.ExternalPayerInfo = try decoder.decode(PayPalResponse.ExternalPayerInfo.self, from: data) | ||
|
||
XCTAssertEqual(decoded.externalPayerId, "external-id") | ||
XCTAssertEqual(decoded.externalPayerId, decoded.externalPayerIdSnakeCase) | ||
|
||
XCTAssertEqual(decoded.firstName, "John") | ||
XCTAssertEqual(decoded.firstName, decoded.firstNameSnakeCase) | ||
|
||
XCTAssertEqual(decoded.lastName, "Doe") | ||
XCTAssertEqual(decoded.lastName, decoded.lastNameSnakeCase) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
|
||
func test_externalPayerInfo_camelcase() async { | ||
let JSON_camelCase = """ | ||
{ | ||
"externalPayerId": "external-id", | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"email": "[email protected]" | ||
} | ||
""" | ||
|
||
guard let data = JSON_camelCase.data(using: .utf8) else { | ||
XCTFail() | ||
return | ||
} | ||
do { | ||
let decoder = JSONDecoder() | ||
let decoded: PayPalResponse.ExternalPayerInfo = try decoder.decode(PayPalResponse.ExternalPayerInfo.self, from: data) | ||
|
||
XCTAssertEqual(decoded.externalPayerId, "external-id") | ||
XCTAssertEqual(decoded.externalPayerId, decoded.externalPayerIdSnakeCase) | ||
|
||
XCTAssertEqual(decoded.firstName, "John") | ||
XCTAssertEqual(decoded.firstName, decoded.firstNameSnakeCase) | ||
|
||
XCTAssertEqual(decoded.lastName, "Doe") | ||
XCTAssertEqual(decoded.lastName, decoded.lastNameSnakeCase) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
|
||
func test_jsonEncoding() async { | ||
let payerInfo = PayPalResponse.ExternalPayerInfo(externalPayerId: "external-id", | ||
email: "[email protected]", | ||
firstName: "John", | ||
lastName: "Doe") | ||
|
||
do { | ||
let encoder = JSONEncoder() | ||
let encoded = try encoder.encode(payerInfo) | ||
guard let string = String(data: encoded, encoding: .utf8) else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
XCTAssertTrue(string.contains("\"externalPayerId\":\"external-id\"")) | ||
XCTAssertTrue(string.contains("\"external_payer_id\":\"external-id\"")) | ||
|
||
XCTAssertTrue(string.contains("\"firstName\":\"John\"")) | ||
XCTAssertTrue(string.contains("\"first_name\":\"John\"")) | ||
|
||
XCTAssertTrue(string.contains("\"lastName\":\"Doe\"")) | ||
XCTAssertTrue(string.contains("\"last_name\":\"Doe\"")) | ||
|
||
XCTAssertTrue(string.contains("\"email\":\"[email protected]\"")) | ||
} catch { | ||
XCTFail() | ||
} | ||
} | ||
} |