Skip to content

Commit

Permalink
fix session token serialization and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill-K-1 committed Jul 30, 2024
1 parent 713b6fc commit 0fd9511
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gov-portal-db/src/session_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SessionManager {
) -> Result<SessionToken, anyhow::Error> {
SessionToken::new(
RawSessionToken {
kind: SessionTokenKind::Internal,
kind: SessionTokenKind::Internal {},
expires_at: (Utc::now() + lifetime).timestamp_millis() as u64,
},
self.config.secret.as_bytes(),
Expand Down
59 changes: 53 additions & 6 deletions shared/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl From<&str> for SessionToken {
#[serde(rename_all = "camelCase")]
pub struct RawSessionToken {
/// Session token kind
#[serde(flatten)]
pub kind: SessionTokenKind,
/// Expiration date for a session JWT token
pub expires_at: u64,
Expand All @@ -365,7 +366,7 @@ pub enum SessionTokenKind {
#[serde(rename = "wallet")]
checksum_wallet: String,
},
Internal,
Internal {},
}

impl RawSessionToken {
Expand Down Expand Up @@ -430,7 +431,7 @@ impl SessionToken {

match token {
RawSessionToken {
kind: SessionTokenKind::Internal,
kind: SessionTokenKind::Internal {},
..
} => Ok(()),
_ => Err(anyhow!("Invalid session token kind")),
Expand Down Expand Up @@ -700,14 +701,60 @@ impl Serialize for WrappedCid {

#[cfg(test)]
mod tests {
use std::time::Duration;

use chrono::Utc;
use ethereum_types::Address;
use hex::ToHex;
use serde_email::Email;
use std::time::Duration;

use super::{SessionTokenKind, UserDbEntry, UserProfile, UserProfileStatus, WrappedCid};
use crate::common::{CompletionToken, RawSessionToken, User};

#[test]
fn test_session_token() {
let now = Utc::now().timestamp_millis() as u64;
let wallet = Address::random().encode_hex::<String>();
let token = RawSessionToken {
kind: SessionTokenKind::Wallet {
checksum_wallet: wallet.clone(),
},
expires_at: now,
};

assert_eq!(
serde_json::to_string(&token).unwrap(),
format!(r#"{{"wallet":"{wallet}","expiresAt":{now}}}"#)
);

use super::{UserDbEntry, UserProfile, UserProfileStatus, WrappedCid};
use crate::common::{CompletionToken, User};
assert_matches::assert_matches!(
serde_json::from_str::<RawSessionToken>(
r#"{"wallet":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","expiresAt":1000}"#
)
.unwrap(),
RawSessionToken {
kind: SessionTokenKind::Wallet { checksum_wallet: _ },
expires_at: _
}
);

let token = RawSessionToken {
kind: SessionTokenKind::Internal {},
expires_at: now,
};

assert_eq!(
serde_json::to_string(&token).unwrap(),
format!(r#"{{"expiresAt":{now}}}"#)
);

assert_matches::assert_matches!(
serde_json::from_str::<RawSessionToken>(r#"{"expiresAt":1000}"#).unwrap(),
RawSessionToken {
kind: SessionTokenKind::Internal {},
expires_at: _
}
);
}

#[test]
fn test_de_raw_user_profile() {
Expand Down

0 comments on commit 0fd9511

Please sign in to comment.