From 254d3296936a2c318efdf47c5a1eeb99f2363c01 Mon Sep 17 00:00:00 2001 From: Jake Wilkins Date: Thu, 25 Jan 2024 18:37:27 -0800 Subject: [PATCH] Add limited expiration calculation testing --- src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f859450..6ad1c30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -254,3 +254,44 @@ fn refresh_access_token(client_id: &str, refresh_token: &str, maybe_host: Option Ok(credential.clone()) } } + +#[cfg(test)] +mod tests { + use crate::{Credential}; + use chrono::offset::Utc; + use chrono::{DateTime, Duration}; + + #[test] + fn credential_expiry_is_expired_returns_false_when_expiry_is_in_the_future() { + let expires_in = Duration::seconds(28800); + let mut expiry: DateTime = Utc::now(); + expiry = expiry + expires_in; + let calculated_expiry = expiry.to_rfc3339(); + + let credential = Credential { + token: String::from("irrelevant"), + expiry: calculated_expiry, + refresh_token: String::from("irrelevant"), + }; + + eprintln!("{:?}", credential); + + assert_eq!(true, credential.is_expired()); + } + + #[test] + fn credential_expiry_is_expired_returns_true_when_expiry_is_in_the_past() { + let expires_in = Duration::seconds(42); + let mut expiry: DateTime = Utc::now(); + expiry = expiry - expires_in; + let calculated_expiry = expiry.to_rfc3339(); + + let credential = Credential { + token: String::from("irrelevant"), + expiry: calculated_expiry, + refresh_token: String::from("irrelevant"), + }; + + assert_eq!(true, credential.is_expired()); + } +}