use genius_core_client::{auth::utils::retrieve_auth_token_client_credentials, ErrorCode};
use mockito::{Matcher, Server};
use tokio;
#[tokio::test]
async fn test_retrieve_auth_token_success() {
let mut server = Server::new_async().await;
server
.mock("POST", "/oauth/token")
.match_header("content-type", "application/x-www-form-urlencoded")
.match_body(Matcher::Regex("grant_type=client_credentials".to_string()))
.with_status(200)
.with_body(r#"{"access_token": "valid_token"}"#)
.create_async()
.await;
let result = retrieve_auth_token_client_credentials(
"client_id".to_string(),
"client_secret".to_string(),
server.url() + "/oauth/token",
None,
None,
)
.await;
assert!(result.is_ok(), "Expected Ok but got Err: {:?}", result);
let token_response = result.unwrap();
assert_eq!(token_response.access_token, "valid_token");
}
#[tokio::test]
async fn test_retrieve_auth_token_error() {
let mut server = Server::new_async().await;
server
.mock("POST", "/oauth/token")
.match_header("content-type", "application/x-www-form-urlencoded")
.match_body(Matcher::Regex("grant_type=client_credentials".to_string()))
.with_status(400)
.with_body(r#"{"error": "invalid_client"}"#)
.create_async()
.await;
let result = retrieve_auth_token_client_credentials(
"client_id".to_string(),
"client_secret".to_string(),
server.url() + "/oauth/token",
None,
None,
)
.await;
assert!(result.is_err());
let error = result.err().unwrap();
println!("{:?}", error);
assert!(matches!(error.0.code(), ErrorCode::UnhandledError));
}