use auth_framework::{
auth::{AuthFramework, AuthResult},
authentication::credentials::Credential,
config::AuthConfig,
};
fn make_framework() -> AuthFramework {
let config = AuthConfig::new().secret("test_lifecycle_secret_key_32_bytes!".to_string());
AuthFramework::new(config)
}
#[tokio::test]
async fn test_deactivated_user_cannot_login() {
let mut fw = make_framework();
fw.initialize().await.unwrap();
let user_id = fw
.register_user("deact_user", "deact@example.com", "SecurePass123!")
.await
.expect("registration should succeed");
let pre = fw
.authenticate(
"password",
Credential::password("deact_user", "SecurePass123!"),
)
.await
.expect("authenticate call should not error");
assert!(
matches!(pre, AuthResult::Success(_)),
"active user should be able to log in; got: {:?}",
pre
);
fw.set_user_active(&user_id, false)
.await
.expect("set_user_active should succeed");
let post = fw
.authenticate(
"password",
Credential::password("deact_user", "SecurePass123!"),
)
.await
.expect("authenticate call should not error");
assert!(
matches!(post, AuthResult::Failure(_)),
"deactivated user must not be able to log in; got: {:?}",
post
);
}
#[tokio::test]
async fn test_reactivated_user_can_login_again() {
let mut fw = make_framework();
fw.initialize().await.unwrap();
let user_id = fw
.register_user("react_user", "react@example.com", "SecurePass123!")
.await
.expect("registration should succeed");
fw.set_user_active(&user_id, false).await.unwrap();
let blocked = fw
.authenticate(
"password",
Credential::password("react_user", "SecurePass123!"),
)
.await
.unwrap();
assert!(matches!(blocked, AuthResult::Failure(_)));
fw.set_user_active(&user_id, true).await.unwrap();
let restored = fw
.authenticate(
"password",
Credential::password("react_user", "SecurePass123!"),
)
.await
.unwrap();
assert!(
matches!(restored, AuthResult::Success(_)),
"re-activated user should be able to log in"
);
}
#[tokio::test]
async fn test_password_change_blocks_old_password() {
let mut fw = make_framework();
fw.initialize().await.unwrap();
fw.register_user("pw_change_user", "pwchange@example.com", "OldPass123!")
.await
.expect("registration should succeed");
let before = fw
.authenticate(
"password",
Credential::password("pw_change_user", "OldPass123!"),
)
.await
.unwrap();
assert!(
matches!(before, AuthResult::Success(_)),
"old password should work before change"
);
fw.update_user_password("pw_change_user", "NewPass456!")
.await
.expect("update_user_password should succeed");
let old_attempt = fw
.authenticate(
"password",
Credential::password("pw_change_user", "OldPass123!"),
)
.await
.unwrap();
assert!(
matches!(old_attempt, AuthResult::Failure(_)),
"old password should be rejected after change; got: {:?}",
old_attempt
);
let new_attempt = fw
.authenticate(
"password",
Credential::password("pw_change_user", "NewPass456!"),
)
.await
.unwrap();
assert!(
matches!(new_attempt, AuthResult::Success(_)),
"new password should be accepted after change; got: {:?}",
new_attempt
);
}
#[tokio::test]
async fn test_deleted_user_cannot_login() {
let mut fw = make_framework();
fw.initialize().await.unwrap();
fw.register_user("del_user", "del@example.com", "SecurePass123!")
.await
.expect("registration should succeed");
let pre = fw
.authenticate(
"password",
Credential::password("del_user", "SecurePass123!"),
)
.await
.unwrap();
assert!(matches!(pre, AuthResult::Success(_)));
fw.delete_user("del_user")
.await
.expect("delete_user should succeed");
let post = fw
.authenticate(
"password",
Credential::password("del_user", "SecurePass123!"),
)
.await
.unwrap();
assert!(
matches!(post, AuthResult::Failure(_)),
"deleted user must not be able to log in; got: {:?}",
post
);
}
#[tokio::test]
async fn test_admin_created_user_can_login() {
let mut fw = make_framework();
fw.initialize().await.unwrap();
fw.register_user(
"admin_created",
"admin_created@example.com",
"SecurePass123!",
)
.await
.expect("admin-path registration should succeed");
let result = fw
.authenticate(
"password",
Credential::password("admin_created", "SecurePass123!"),
)
.await
.expect("authenticate should not error");
assert!(
matches!(result, AuthResult::Success(_)),
"admin-created user should be able to log in; got: {:?}",
result
);
}