Skip to content

Commit

Permalink
chore: add unit tests for expiration objects and expand all_data_read…
Browse files Browse the repository at this point in the history
…_write disposable token test for completeness (#425)

* chore: add unit tests for expiration objects

* chore: expand all_data_read_write disposable token test for completeness

* fix lint errors
  • Loading branch information
anitarua authored Jan 3, 2025
1 parent 5f7600f commit feb9ec4
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/auth/expiration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
44 changes: 44 additions & 0 deletions tests/auth/disposable_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down

0 comments on commit feb9ec4

Please sign in to comment.