diff --git a/src/auth/expiration.rs b/src/auth/expiration.rs index 46a29c08..934382ec 100644 --- a/src/auth/expiration.rs +++ b/src/auth/expiration.rs @@ -119,3 +119,61 @@ impl ExpiresAt { self.valid_until } } + +#[cfg(test)] +mod tests { + use crate::auth::Expiration; + + #[test] + fn expires_in_never_is_valid() { + let expires_in = super::ExpiresIn::never(); + assert_eq!(expires_in.to_seconds(), u64::MAX); + assert!(!expires_in.does_expire()); + } + + #[test] + fn expires_in_seconds_is_valid() { + let expires_in = super::ExpiresIn::seconds(10); + assert_eq!(expires_in.to_seconds(), 10); + assert!(expires_in.does_expire()); + } + + #[test] + fn expires_in_minutes_is_valid() { + let expires_in = super::ExpiresIn::minutes(10); + assert_eq!(expires_in.to_seconds(), 600); + assert!(expires_in.does_expire()); + } + + #[test] + fn expires_in_hours_is_valid() { + let expires_in = super::ExpiresIn::hours(10); + assert_eq!(expires_in.to_seconds(), 36000); + assert!(expires_in.does_expire()); + } + + #[test] + fn expires_in_days_is_valid() { + let expires_in = super::ExpiresIn::days(10); + assert_eq!(expires_in.to_seconds(), 864000); + assert!(expires_in.does_expire()); + } + + #[test] + fn expires_at_from_epoch_when_epoch_is_none() { + let expires_at = super::ExpiresAt::new(None); + assert_eq!(expires_at.epoch(), u64::MAX); + assert!(!expires_at.does_expire()); + } + + #[test] + fn expires_at_from_epoch_when_epoch_is_some() { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + let expires_at = super::ExpiresAt::new(Some(now)); + assert_eq!(expires_at.epoch(), now); + assert!(expires_at.does_expire()); + } +} diff --git a/tests/auth/disposable_tokens.rs b/tests/auth/disposable_tokens.rs index 84056a71..bbe661c1 100644 --- a/tests/auth/disposable_tokens.rs +++ b/tests/auth/disposable_tokens.rs @@ -1539,6 +1539,50 @@ mod disposable_tokens_all_data { assert_publish_success(&tc, second_cache, second_topic.key(), test_item.value()).await?; assert_subscribe_success(&tc, second_cache, second_topic.key()).await?; + // cannot create caches + match cc.create_cache(second_cache).await { + Ok(_) => Err(MomentoError { + message: "Expected creating cache using AllDataReadWrite disposable token to fail but it did not".into(), + error_code: MomentoErrorCode::UnknownError, + inner_error: None, + details: None, + }), + Err(e) => { + match e.error_code { + MomentoErrorCode::PermissionError => Ok(()), + _ => { + eprintln!( + "Expected creating cache using AllDataReadWrite disposable token to fail with permission error. Failed with error code '{:?}' instead", + e.error_code + ); + Err(e) + } + } + } + }?; + + // cannot delete caches + match cc.delete_cache(second_cache).await { + Ok(_) => Err(MomentoError { + message: "Expected deleting cache using AllDataReadWrite disposable token to fail but it did not".into(), + error_code: MomentoErrorCode::UnknownError, + inner_error: None, + details: None, + }), + Err(e) => { + match e.error_code { + MomentoErrorCode::PermissionError => Ok(()), + _ => { + eprintln!( + "Expected deleting cache using AllDataReadWrite disposable token to fail with permission error. Failed with error code '{:?}' instead", + e.error_code + ); + Err(e) + } + } + } + }?; + Ok(()) } }