Skip to content

Commit

Permalink
add card create (#3)
Browse files Browse the repository at this point in the history
* add card create

* add card create tests
  • Loading branch information
montdidier authored Dec 20, 2023
1 parent 33e0a17 commit 079bda0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/resources/card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::client::{Client, Response};
use crate::ids::{CardId};
use crate::params::{unpack_contained};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -28,10 +30,17 @@ pub struct Card {
pub address_line1: String,
pub address_line2: Option<String>,
pub address_city: String,
pub address_postcode: String,
pub address_postcode: Option<String>,
pub address_state: Option<String>,
pub address_country: String,
pub network_type: Option<String>,
pub network_format: Option<String>,
pub customer_token: Option<String>,
pub primary: Option<bool>,
}

impl Card {
pub fn create(client: &Client, params: CardParams<'_>) -> Response<Card> {
unpack_contained(client.post_form(&format!("/cards"), &params))
}
}
73 changes: 73 additions & 0 deletions tests/card.rs
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions tests/fixtures/create-card.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 079bda0

Please sign in to comment.