premium keys

This commit is contained in:
Brandon Thomas committed 2024-07-21 00:17:47 -04:00
1 parent a06b583488
commit f17a9a034f
4 files changed
+97 -8

No files matched your search

@@ -1 +1,3 @@
pub mod user_token_premium_key;
pub mod premium_avt_redis_key;
pub mod premium_ip_address_redis_key;
pub mod premium_user_token_redis_key;
@@ -0,0 +1,40 @@
use chrono::{Datelike, DateTime, Duration, Utc};
use tokens::tokens::anonymous_visitor_tracking::AnonymousVisitorTrackingToken;
pub struct PremiumAvtRedisKey(pub String);
impl_string_key!(PremiumAvtRedisKey);
const DURATION : Duration = Duration::milliseconds(1000 * 60 * 60 * 24 * 32); // 32 days
impl PremiumAvtRedisKey {
pub fn new_for_user(avt: &AnonymousVisitorTrackingToken, time: DateTime<Utc>) -> Self {
let month = time.month();
let key = format!("premium:avt:{}:{}", avt.as_str(), month);
Self(key)
}
pub fn get_redis_ttl() -> Duration {
DURATION
}
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
#[test]
fn test_new_for_user() {
let avt = AnonymousVisitorTrackingToken::new_from_str("tracking");
let time = Utc.with_ymd_and_hms(2021, 7, 4, 0, 0, 0).unwrap();
let key = PremiumAvtRedisKey::new_for_user(&avt, time);
assert_eq!(key.as_str(), "premium:avt:tracking:7");
}
#[test]
fn test_duration() {
assert_eq!(PremiumAvtRedisKey::get_redis_ttl().num_days(), 32);
}
}
@@ -0,0 +1,38 @@
use chrono::{Datelike, DateTime, Duration, Utc};
pub struct PremiumIpAddressRedisKey(pub String);
impl_string_key!(PremiumIpAddressRedisKey);
const DURATION : Duration = Duration::milliseconds(1000 * 60 * 60 * 24 * 32); // 32 days
impl PremiumIpAddressRedisKey {
pub fn new_for_user(ip_addr: &str, time: DateTime<Utc>) -> Self {
let month = time.month();
let key = format!("premium:ip:{}:{}", ip_addr, month);
Self(key)
}
pub fn get_redis_ttl() -> Duration {
DURATION
}
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
#[test]
fn test_new_for_user() {
let time = Utc.with_ymd_and_hms(2021, 7, 4, 0, 0, 0).unwrap();
let key = PremiumIpAddressRedisKey::new_for_user("127.0.0.1", time);
assert_eq!(key.as_str(), "premium:ip:127.0.0.1:7");
}
#[test]
fn test_duration() {
assert_eq!(PremiumIpAddressRedisKey::get_redis_ttl().num_days(), 32);
}
}
@@ -1,32 +1,41 @@
use chrono::{Datelike, DateTime, Utc};
use chrono::{Datelike, DateTime, Duration, Utc};
use tokens::tokens::users::UserToken;
pub struct UserTokenPremiumKey(pub String);
pub struct PremiumUserTokenRedisKey(pub String);
impl_string_key!(UserTokenPremiumKey);
impl_string_key!(PremiumUserTokenRedisKey);
impl UserTokenPremiumKey {
const DURATION : Duration = Duration::milliseconds(1000 * 60 * 60 * 24 * 32); // 32 days
impl PremiumUserTokenRedisKey {
pub fn new_for_user(user_token: &UserToken, time: DateTime<Utc>) -> Self {
let month = time.month();
let key = format!("premium:user:{}:{}", user_token.as_str(), month);
Self(key)
}
pub fn get_redis_ttl() -> Duration {
DURATION
}
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use tokens::tokens::users::UserToken;
use super::*;
#[test]
fn test_new_for_user() {
let user_token = UserToken::new_from_str("token");
let time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();
let key = UserTokenPremiumKey::new_for_user(&user_token, time);
let key = PremiumUserTokenRedisKey::new_for_user(&user_token, time);
assert_eq!(key.as_str(), "premium:user:token:1");
}
#[test]
fn test_duration() {
assert_eq!(PremiumUserTokenRedisKey::get_redis_ttl().num_days(), 32);
}
}