mod helpers;
use tradestation_api::{Credentials, auth::Scope};
use wiremock::matchers::{body_string_contains, method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_authorization_url() {
let creds = Credentials::new("my_client_id", "my_secret");
let url = creds.authorization_url(&[Scope::MarketData, Scope::ReadAccount]);
assert!(url.contains("response_type=code"));
assert!(url.contains("client_id=my_client_id"));
assert!(url.contains("scope=MarketData%20ReadAccount"));
assert!(url.starts_with("https://signin.tradestation.com/authorize"));
}
#[tokio::test]
async fn test_exchange_code() {
let server = helpers::setup_mock_server().await;
Mock::given(method("POST"))
.and(path("/oauth/token"))
.and(body_string_contains("grant_type=authorization_code"))
.and(body_string_contains("code=test_auth_code"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"access_token": "new_access_token",
"refresh_token": "new_refresh_token",
"token_type": "Bearer",
"expires_in": 1200
})))
.mount(&server)
.await;
let http = reqwest::Client::new();
let creds = Credentials::new("test_client_id", "test_client_secret")
.with_redirect_uri("http://localhost:3000/callback");
let _result = tradestation_api::auth::exchange_code(&http, &creds, "test_auth_code").await;
}
#[tokio::test]
async fn test_refresh_token() {
let http = reqwest::Client::new();
let creds = Credentials::new("test_client_id", "test_client_secret");
let _result = tradestation_api::auth::refresh_token(&http, &creds, "test_refresh_token").await;
}
#[tokio::test]
async fn test_revoke_token() {
let http = reqwest::Client::new();
let creds = Credentials::new("test_client_id", "test_client_secret");
let _result = tradestation_api::auth::revoke_token(&http, &creds, "test_refresh_token").await;
}