Added token repo tests.
This commit is contained in:
@@ -108,3 +108,85 @@ fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
|||||||
issued_at: token.issued_at,
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -87,3 +87,67 @@ fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
|||||||
issued_at: token.issued_at,
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user