2022-11-11 09:28:11 +00:00
|
|
|
use paypal_rs::{Client, PaypalEnv};
|
|
|
|
use wiremock::matchers::{basic_auth, body_string, header, method, path};
|
|
|
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
2022-09-28 15:48:00 +00:00
|
|
|
|
|
|
|
fn create_client(url: &str) -> Client {
|
2022-11-09 09:07:28 +00:00
|
|
|
Client::new(
|
|
|
|
"clientid".to_string(),
|
|
|
|
"secret".to_string(),
|
|
|
|
PaypalEnv::Mock(url.to_string()),
|
|
|
|
)
|
2022-09-28 15:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_auth() -> color_eyre::Result<()> {
|
|
|
|
color_eyre::install()?;
|
2022-11-09 09:07:28 +00:00
|
|
|
|
2022-09-28 15:48:00 +00:00
|
|
|
let mock_server = MockServer::start().await;
|
|
|
|
|
|
|
|
let access_token: serde_json::Value = serde_json::from_str(include_str!("resources/oauth_token.json")).unwrap();
|
|
|
|
|
|
|
|
Mock::given(method("POST"))
|
|
|
|
.and(path("/v1/oauth2/token"))
|
|
|
|
.and(basic_auth("clientid", "secret"))
|
|
|
|
.and(header("Content-Type", "x-www-form-urlencoded"))
|
|
|
|
.and(body_string("grant_type=client_credentials"))
|
|
|
|
.respond_with(ResponseTemplate::new(200).set_body_json(&access_token))
|
|
|
|
.mount(&mock_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let mut client = create_client(&mock_server.uri());
|
|
|
|
|
|
|
|
client.get_access_token().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|