use mpesa::MpesaError; use serde_json::json; use wiremock::matchers::{method, path}; use wiremock::{Mock, ResponseTemplate}; use crate::get_mpesa_client; #[tokio::test] async fn transaction_status_success() { let (client, server) = get_mpesa_client!(); let sample_response_body = json!({ "OriginatorConversationID": "29464-48063588-1", "ConversationID": "AG_20230206_201056794190723278ff", "ResponseDescription": "Accept the service request successfully.", }); Mock::given(method("POST")) .and(path("/mpesa/transactionstatus/v1/query")) .respond_with(ResponseTemplate::new(200).set_body_json(sample_response_body)) .expect(1) .mount(&server) .await; let response = client .transaction_status("testapi496") .result_url("https://testdomain.com/ok") .timeout_url("https://testdomain.com/err") .transaction_id("OEI2AK4Q16") .party_a("600111") .remarks("status") .occasion("work") .send() .await .unwrap(); assert_eq!(response.originator_conversation_id, "29464-48063588-1"); assert_eq!(response.conversation_id, "AG_20230206_201056794190723278ff"); assert_eq!( response.response_description, "Accept the service request successfully." ); } #[tokio::test] async fn transaction_status_fails_if_transaction_id_is_not_provided() { let (client, server) = get_mpesa_client!(expected_auth_requests = 0); let sample_response_body = json!({ "OriginatorConversationID": "29464-48063588-1", "ConversationID": "AG_20230206_201056794190723278ff", "ResponseDescription": "Accept the service request successfully.", }); Mock::given(method("POST")) .and(path("/mpesa/transactionstatus/v1/query")) .respond_with(ResponseTemplate::new(200).set_body_json(sample_response_body)) .expect(0) .mount(&server) .await; if let Err(e) = client .transaction_status("testapi496") .result_url("https://testdomain.com/ok") .timeout_url("https://testdomain.com/err") .party_a("600111") .send() .await { let MpesaError::Message(msg) = e else { panic!("Expected MpesaError::Message, but found {}", e); }; assert_eq!(msg, "transaction_id is required"); } else { panic!("Expected error") } } #[tokio::test] async fn transaction_status_fails_if_party_a_is_not_provided() { let (client, server) = get_mpesa_client!(expected_auth_requests = 0); let sample_response_body = json!({ "OriginatorConversationID": "29464-48063588-1", "ConversationID": "AG_20230206_201056794190723278ff", "ResponseDescription": "Accept the service request successfully.", }); Mock::given(method("POST")) .and(path("/mpesa/transactionstatus/v1/query")) .respond_with(ResponseTemplate::new(200).set_body_json(sample_response_body)) .expect(0) .mount(&server) .await; if let Err(e) = client .transaction_status("testapi496") .result_url("https://testdomain.com/ok") .timeout_url("https://testdomain.com/err") .transaction_id("OEI2AK4Q16") .send() .await { let MpesaError::Message(msg) = e else { panic!("Expected MpesaError::Message, but found {}", e); }; assert_eq!(msg, "party_a is required"); } else { panic!("Expected error") } } #[tokio::test] async fn transaction_status_fails_if_result_url_is_not_provided() { let (client, server) = get_mpesa_client!(expected_auth_requests = 0); let sample_response_body = json!({ "OriginatorConversationID": "29464-48063588-1", "ConversationID": "AG_20230206_201056794190723278ff", "ResponseDescription": "Accept the service request successfully.", }); Mock::given(method("POST")) .and(path("/mpesa/transactionstatus/v1/query")) .respond_with(ResponseTemplate::new(200).set_body_json(sample_response_body)) .expect(0) .mount(&server) .await; if let Err(e) = client .transaction_status("testapi496") .timeout_url("https://testdomain.com/err") .transaction_id("OEI2AK4Q16") .party_a("600111") .send() .await { let MpesaError::Message(msg) = e else { panic!("Expected MpesaError::Message, but found {}", e); }; assert_eq!(msg, "result_url is required"); } else { panic!("Expected error") } } #[tokio::test] async fn transaction_status_fails_if_timeout_url_is_not_provided() { let (client, server) = get_mpesa_client!(expected_auth_requests = 0); let sample_response_body = json!({ "OriginatorConversationID": "29464-48063588-1", "ConversationID": "AG_20230206_201056794190723278ff", "ResponseDescription": "Accept the service request successfully.", }); Mock::given(method("POST")) .and(path("/mpesa/transactionstatus/v1/query")) .respond_with(ResponseTemplate::new(200).set_body_json(sample_response_body)) .expect(0) .mount(&server) .await; if let Err(e) = client .transaction_status("testapi496") .result_url("https://testdomain.com/ok") .transaction_id("OEI2AK4Q16") .party_a("600111") .send() .await { let MpesaError::Message(msg) = e else { panic!("Expected MpesaError::Message, but found {}", e); }; assert_eq!(msg, "timeout_url is required"); } else { panic!("Expected error") } }