From 06ecc388f3aefc784223c40cf3f14d0ca8884ff6 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Tue, 14 Nov 2023 17:52:55 +0300 Subject: [PATCH 1/2] Fix auth response deserializion errors --- Cargo.toml | 1 + src/auth.rs | 8 +++++--- tests/mpesa-rust/helpers.rs | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 461944b2a..d437f958a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ serde_repr = "0.1" thiserror = "1.0.37" wiremock = "0.5" secrecy = "0.8.0" +serde-aux = "4.2.0" [dev-dependencies] dotenv = "0.15" diff --git a/src/auth.rs b/src/auth.rs index dcaffa944..4392d3073 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,5 +1,6 @@ use cached::proc_macro::cached; use serde::{Deserialize, Serialize}; +use serde_aux::field_attributes::deserialize_number_from_string; use crate::{ApiEnvironment, ApiError, Mpesa, MpesaError, MpesaResult}; @@ -39,7 +40,8 @@ pub struct AuthenticationResponse { /// Access token which is used as the Bearer-Auth-Token pub access_token: String, /// Expiry time in seconds - pub expiry_in: u64, + #[serde(deserialize_with = "deserialize_number_from_string")] + pub expires_in: u64, } impl std::fmt::Display for AuthenticationResponse { @@ -47,7 +49,7 @@ impl std::fmt::Display for AuthenticationResponse { write!( f, "token :{} expires in: {}", - self.access_token, self.expiry_in + self.access_token, self.expires_in ) } } @@ -97,7 +99,7 @@ mod tests { .respond_with(wiremock::ResponseTemplate::new(200).set_body_json( AuthenticationResponse { access_token: "test_token".to_string(), - expiry_in: 3600, + expires_in: 3600, }, )) .expect(1) diff --git a/tests/mpesa-rust/helpers.rs b/tests/mpesa-rust/helpers.rs index cc3a589be..cc332888a 100644 --- a/tests/mpesa-rust/helpers.rs +++ b/tests/mpesa-rust/helpers.rs @@ -46,7 +46,7 @@ macro_rules! get_mpesa_client { .and(query_param("grant_type", "client_credentials")) .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "access_token": "dummy_access_token", - "expiry_in": 3600 + "expires_in": 3600 }))) .mount(&server) .await; @@ -73,7 +73,7 @@ macro_rules! get_mpesa_client { .and(query_param("grant_type", "client_credentials")) .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "access_token": "dummy_access_token", - "expiry_in": 3600 + "expires_in": "3600" }))) .mount(&server) .await; From 84448a4221d35773f114ff63af123ed52dd509f8 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Tue, 14 Nov 2023 18:18:05 +0300 Subject: [PATCH 2/2] Maintain consistency with auth response in helpers --- tests/mpesa-rust/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mpesa-rust/helpers.rs b/tests/mpesa-rust/helpers.rs index cc332888a..8626b03f3 100644 --- a/tests/mpesa-rust/helpers.rs +++ b/tests/mpesa-rust/helpers.rs @@ -46,7 +46,7 @@ macro_rules! get_mpesa_client { .and(query_param("grant_type", "client_credentials")) .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "access_token": "dummy_access_token", - "expires_in": 3600 + "expires_in": "3600" }))) .mount(&server) .await;