diff --git a/src/resources/card.rs b/src/resources/card.rs index f784a69..fab2c60 100644 --- a/src/resources/card.rs +++ b/src/resources/card.rs @@ -30,7 +30,8 @@ pub struct Card { pub address_line1: String, pub address_line2: Option, pub address_city: String, - pub address_postcode: String, + pub address_postcode: Option, + pub address_state: Option, pub address_country: String, pub network_type: Option, pub network_format: Option, diff --git a/tests/card.rs b/tests/card.rs new file mode 100644 index 0000000..e647fbf --- /dev/null +++ b/tests/card.rs @@ -0,0 +1,73 @@ +use pinpayments::{Client, CardParams, Card}; +use std::fs::File; +use httptest::{ServerPool, Expectation, matchers::*, responders::*}; +use surf::http::auth::BasicAuth; + +static SERVER_POOL: ServerPool = ServerPool::new(2); + +fn get_fixture(path: &str) -> serde_json::Value { + let file = File::open(path) + .expect("file should open read only"); + serde_json::from_reader(file).expect("file should be JSON") +} + +#[tokio::test] +async fn card_create_test() { + let json = get_fixture("tests/fixtures/create-card.json"); + + let auth = BasicAuth::new("sk_test_12345", ""); + + let server = SERVER_POOL.get_server(); + + server.expect( + Expectation::matching( + all_of![ + request::method_path("POST", "/1/cards"), + request::headers( + contains((String::from(auth.name().as_str()), String::from(auth.value().as_str()))) + ), + ]). + respond_with( + status_code(201) + .append_header("Content-Type", "application/json") + .body(serde_json::to_string(&json).expect("failed to serialize body"))), + ); + + let client = Client::from_url(server.url_str("/1/").as_str(), "sk_test_12345"); + + let card = Card::create( + &client, + CardParams { + number: "5520000000000000", + expiry_month: "05", + expiry_year: "2024", + cvc: "123", + name: "Roland Roboat", + address_line1: "42 Sevenoaks St", + address_city: "Lathlain", + address_postcode: "6454", + address_state: "WA", + address_country: "Australia", + } + ) + .await + .unwrap(); + + assert_eq!(card.token, "card_pIQJKMs93GsCc9vLSLevbw"); + assert_eq!(card.scheme, "master"); + assert_eq!(card.display_number, "XXXX-XXXX-XXXX-0000"); + assert_eq!(card.issuing_country, "US"); + assert_eq!(card.expiry_month, 5); + assert_eq!(card.expiry_year, 2024); + assert_eq!(card.name, "Roland Robot"); + assert_eq!(card.address_line1, "42 Sevenoaks St"); + assert_eq!(card.address_line2.unwrap(), ""); + assert_eq!(card.address_city, "Lathlain"); + assert_eq!(card.address_postcode.unwrap(), "6454"); + assert_eq!(card.address_state.unwrap(), "WA"); + assert_eq!(card.address_country, "Australia"); + assert_eq!(card.network_type, None); + assert_eq!(card.network_format, None); + assert_eq!(card.customer_token, None); + assert_eq!(card.primary, None); +} diff --git a/tests/fixtures/create-card.json b/tests/fixtures/create-card.json new file mode 100644 index 0000000..1e16c72 --- /dev/null +++ b/tests/fixtures/create-card.json @@ -0,0 +1,22 @@ +{ + "response": { + "token": "card_pIQJKMs93GsCc9vLSLevbw", + "scheme": "master", + "display_number": "XXXX-XXXX-XXXX-0000", + "issuing_country": "US", + "expiry_month": 5, + "expiry_year": 2024, + "name": "Roland Robot", + "address_line1": "42 Sevenoaks St", + "address_line2": "", + "address_city": "Lathlain", + "address_postcode": "6454", + "address_state": "WA", + "address_country": "Australia", + "network_type": null, + "network_format": null, + "customer_token": null, + "primary": null + }, + "ip_address": "192.0.2.42" +}