refactor: extract inline tests to separate files in auth + storage adapters
This commit is contained in:
@@ -47,25 +47,3 @@ impl TokenIssuer for JwtTokenIssuer {
|
||||
Ok((SystemId::from_uuid(uuid), data.claims.role))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn issue_and_verify_roundtrip() {
|
||||
let issuer = JwtTokenIssuer::new("test-secret-key-long-enough-32chars!!");
|
||||
let user_id = SystemId::new();
|
||||
let token = issuer.issue(&user_id, "user").await.unwrap();
|
||||
let (verified_id, verified_role) = issuer.verify(&token).await.unwrap();
|
||||
assert_eq!(verified_id, user_id);
|
||||
assert_eq!(verified_role, "user");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_invalid_token() {
|
||||
let issuer = JwtTokenIssuer::new("test-secret-key-long-enough-32chars!!");
|
||||
let result = issuer.verify("not.a.valid.jwt").await;
|
||||
assert!(matches!(result, Err(DomainError::Unauthorized(_))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,16 +23,3 @@ impl PasswordHasher for BcryptPasswordHasher {
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn hash_and_verify_roundtrip() {
|
||||
let h = BcryptPasswordHasher;
|
||||
let hash = h.hash("mysecretpassword").await.unwrap();
|
||||
assert!(h.verify("mysecretpassword", &hash).await.unwrap());
|
||||
assert!(!h.verify("wrongpassword", &hash).await.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
2
crates/adapters/auth/tests/auth_tests.rs
Normal file
2
crates/adapters/auth/tests/auth_tests.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod jwt;
|
||||
mod password;
|
||||
21
crates/adapters/auth/tests/jwt.rs
Normal file
21
crates/adapters/auth/tests/jwt.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use adapters_auth::JwtTokenIssuer;
|
||||
use domain::errors::DomainError;
|
||||
use domain::ports::TokenIssuer;
|
||||
use domain::value_objects::SystemId;
|
||||
|
||||
#[tokio::test]
|
||||
async fn issue_and_verify_roundtrip() {
|
||||
let issuer = JwtTokenIssuer::new("test-secret-key-long-enough-32chars!!");
|
||||
let user_id = SystemId::new();
|
||||
let token = issuer.issue(&user_id, "user").await.unwrap();
|
||||
let (verified_id, verified_role) = issuer.verify(&token).await.unwrap();
|
||||
assert_eq!(verified_id, user_id);
|
||||
assert_eq!(verified_role, "user");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_invalid_token() {
|
||||
let issuer = JwtTokenIssuer::new("test-secret-key-long-enough-32chars!!");
|
||||
let result = issuer.verify("not.a.valid.jwt").await;
|
||||
assert!(matches!(result, Err(DomainError::Unauthorized(_))));
|
||||
}
|
||||
10
crates/adapters/auth/tests/password.rs
Normal file
10
crates/adapters/auth/tests/password.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use adapters_auth::BcryptPasswordHasher;
|
||||
use domain::ports::PasswordHasher;
|
||||
|
||||
#[tokio::test]
|
||||
async fn hash_and_verify_roundtrip() {
|
||||
let h = BcryptPasswordHasher;
|
||||
let hash = h.hash("mysecretpassword").await.unwrap();
|
||||
assert!(h.verify("mysecretpassword", &hash).await.unwrap());
|
||||
assert!(!h.verify("wrongpassword", &hash).await.unwrap());
|
||||
}
|
||||
Reference in New Issue
Block a user