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_audience() { 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 audience = Some("test_audience".to_string()); let result = retrieve_auth_token_client_credentials( "client_id".to_string(), "client_secret".to_string(), server.url() + "/oauth/token", audience, 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_invalid_audience() { 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) // Server responds with an error status .with_body(r#"{"error": "invalid_audience"}"#) // Error message indicating invalid audience .create_async() .await; let audience = Some("invalid_audience".to_string()); // Provide an invalid audience let result = retrieve_auth_token_client_credentials( "client_id".to_string(), "client_secret".to_string(), server.url() + "/oauth/token", audience, None, ) .await; assert!(result.is_err(), "Expected Err but got Ok: {:?}", result); let error = result.err().unwrap(); assert!(matches!(error.0.code(), ErrorCode::UnhandledError)); } #[tokio::test] async fn test_retrieve_auth_token_scope() { 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 scope = Some("test_scope".to_string()); let result = retrieve_auth_token_client_credentials( "client_id".to_string(), "client_secret".to_string(), server.url() + "/oauth/token", None, scope, ) .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)); } #[tokio::test] async fn test_retrieve_auth_token_response_shape() { 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#"{"foo": "something"}"#) .create_async() .await; let scope = Some("test_scope".to_string()); let result = retrieve_auth_token_client_credentials( "client_id".to_string(), "client_secret".to_string(), server.url() + "/oauth/token", None, scope, ) .await; assert!(result.is_err()); let error = result.err().unwrap(); println!("{:?}", error); assert!(matches!(error.0.code(), ErrorCode::None)); }