From e9093d131ac5d8181d084f59457e64e15432d297 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Sun, 18 Oct 2026 14:56:39 +0200 Subject: [PATCH] Added token repo tests. --- server/src/repository/access_tokens.rs | 82 +++++++++++++++++++++++++ server/src/repository/refresh_tokens.rs | 64 +++++++++++++++++++ 2 files changed, 146 insertions(+) diff --git a/server/src/repository/access_tokens.rs b/server/src/repository/access_tokens.rs index 53a17b3..0fae4e3 100644 --- a/server/src/repository/access_tokens.rs +++ b/server/src/repository/access_tokens.rs @@ -108,3 +108,85 @@ fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow { issued_at: token.issued_at, } } + +#[cfg(test)] +mod tests { + use chrono::{Duration, Utc}; + use uuid::Uuid; + + use super::*; + + fn row() -> AccessTokenRow { + let issued_at = Utc::now(); + AccessTokenRow { + id: Uuid::new_v4(), + user_id: Uuid::new_v4(), + user_app_id: Some(Uuid::new_v4()), + secret_hash: vec![0; 32], + expires_at: issued_at + Duration::days(1), + issued_at, + } + } + + fn foreign_key_violation(constraint: Constraint) -> DatabaseError { + DatabaseError::ForeignKeyViolation { + constraint, + source: sqlx::Error::Protocol("test database cause".to_owned()), + } + } + + #[test] + fn classifies_access_token_user_foreign_key_violation() { + let error = classify_upsert_error(foreign_key_violation(Constraint::AccessTokenUser)); + assert!(matches!( + error, + RepositoryError::ReferenceViolation { + relation: ReferenceRelation::User, + kind: ReferenceViolationKind::Missing, + .. + } + )); + } + + #[test] + fn classifies_access_token_user_app_foreign_key_violation() { + let error = classify_upsert_error(foreign_key_violation(Constraint::AccessTokenUserApp)); + assert!(matches!( + error, + RepositoryError::ReferenceViolation { + relation: ReferenceRelation::UserApp, + kind: ReferenceViolationKind::Missing, + .. + } + )); + } + + #[test] + fn keeps_unknown_upsert_foreign_keys_generic() { + let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed)); + assert!(matches!(error, RepositoryError::Database(_))); + } + + #[test] + fn decodes_valid_rows() { + let row = row(); + let (id, user_id, user_app_id, secret_hash) = ( + row.id, + row.user_id, + row.user_app_id, + row.secret_hash.clone(), + ); + let token = decode_access_token(row).unwrap(); + assert_eq!(token.id, id); + assert_eq!(token.user_id, user_id); + assert_eq!(token.user_app_id, user_app_id); + assert_eq!(token.secret_hash.hash(), secret_hash.as_slice()); + } + + #[test] + fn round_trips_through_rows() { + let token = decode_access_token(row()).unwrap(); + let round_trip = decode_access_token(encode_access_token(token.clone())).unwrap(); + assert!(round_trip == token); + } +} diff --git a/server/src/repository/refresh_tokens.rs b/server/src/repository/refresh_tokens.rs index afdbbe2..90268b1 100644 --- a/server/src/repository/refresh_tokens.rs +++ b/server/src/repository/refresh_tokens.rs @@ -87,3 +87,67 @@ fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow { issued_at: token.issued_at, } } + +#[cfg(test)] +mod tests { + use chrono::{Duration, Utc}; + use uuid::Uuid; + + use super::*; + + fn row() -> RefreshTokenRow { + let issued_at = Utc::now(); + RefreshTokenRow { + id: Uuid::new_v4(), + access_token_id: Uuid::new_v4(), + secret_hash: vec![0; 32], + expires_at: issued_at + Duration::days(1), + issued_at, + } + } + + fn foreign_key_violation(constraint: Constraint) -> DatabaseError { + DatabaseError::ForeignKeyViolation { + constraint, + source: sqlx::Error::Protocol("test database cause".to_owned()), + } + } + + #[test] + fn classifies_refresh_token_upsert_foreign_key_violation() { + let error = + classify_upsert_error(foreign_key_violation(Constraint::RefreshTokenAccessToken)); + assert!(matches!( + error, + RepositoryError::ReferenceViolation { + relation: ReferenceRelation::AccessToken, + kind: ReferenceViolationKind::Missing, + .. + } + )); + } + + #[test] + fn keeps_unknown_upsert_foreign_keys_generic() { + let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed)); + assert!(matches!(error, RepositoryError::Database(_))); + } + + #[test] + fn decodes_valid_rows() { + let row = row(); + let (id, access_token_id, secret_hash) = + (row.id, row.access_token_id, row.secret_hash.clone()); + let token = decode_refresh_token(row).unwrap(); + assert_eq!(token.id, id); + assert_eq!(token.token_id, access_token_id); + assert_eq!(token.secret_hash.hash(), secret_hash.as_slice()); + } + + #[test] + fn round_trips_through_rows() { + let token = decode_refresh_token(row()).unwrap(); + let round_trip = decode_refresh_token(encode_refresh_token(token.clone())).unwrap(); + assert!(round_trip == token); + } +}