#![allow(unused)]
use rsclaw::{
channel::{DmPolicyEnforcer, PolicyResult},
config::schema::DmPolicy,
};
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_open() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Open, vec![]);
for peer in ["alice", "bob", "0123456789", "unknown_user_99"] {
let result = enforcer.check(peer).await;
assert_eq!(
result,
PolicyResult::Allow,
"Open policy should allow '{peer}', got: {result:?}"
);
}
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_allowlist() {
let allowed = vec!["alice".to_owned(), "charlie".to_owned()];
let enforcer = DmPolicyEnforcer::new(DmPolicy::Allowlist, allowed);
assert_eq!(
enforcer.check("alice").await,
PolicyResult::Allow,
"alice is in allowlist"
);
assert_eq!(
enforcer.check("charlie").await,
PolicyResult::Allow,
"charlie is in allowlist"
);
for denied in ["bob", "dave", "ALICE", " alice ", ""] {
let result = enforcer.check(denied).await;
assert_eq!(
result,
PolicyResult::Deny,
"'{denied}' should be denied, got: {result:?}"
);
}
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_allowlist_wildcard() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Allowlist, vec!["*".to_owned()]);
for peer in ["alice", "bob", "anyone"] {
assert_eq!(
enforcer.check(peer).await,
PolicyResult::Allow,
"wildcard allowlist should allow '{peer}'"
);
}
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_disabled() {
let enforcer =
DmPolicyEnforcer::new(DmPolicy::Disabled, vec!["alice".to_owned(), "*".to_owned()]);
for peer in ["alice", "bob", "admin", ""] {
let result = enforcer.check(peer).await;
assert_eq!(
result,
PolicyResult::Deny,
"Disabled policy should deny '{peer}', got: {result:?}"
);
}
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_pairing_issues_code() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Pairing, vec![]);
let result = enforcer.check("new_user").await;
assert!(
matches!(result, PolicyResult::SendPairingCode(_)),
"first contact in Pairing mode should yield a code, got: {result:?}"
);
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_pairing_approved_allows() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Pairing, vec![]);
let code = match enforcer.check("peer_42").await {
PolicyResult::SendPairingCode(c) => c,
other => panic!("expected SendPairingCode, got {other:?}"),
};
let approved_peer = enforcer.approve_pairing(&code).await;
assert_eq!(
approved_peer.as_deref(),
Some("peer_42"),
"approve_pairing should return the peer ID"
);
assert_eq!(
enforcer.check("peer_42").await,
PolicyResult::Allow,
"approved peer should be allowed on subsequent checks"
);
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_pairing_revoke() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Pairing, vec![]);
let code = match enforcer.check("peer_rev").await {
PolicyResult::SendPairingCode(c) => c,
other => panic!("expected code, got {other:?}"),
};
enforcer.approve_pairing(&code).await;
assert_eq!(enforcer.check("peer_rev").await, PolicyResult::Allow);
enforcer.revoke("peer_rev").await;
let after_revoke = enforcer.check("peer_rev").await;
assert!(
matches!(after_revoke, PolicyResult::SendPairingCode(_)),
"revoked peer should require re-pairing, got: {after_revoke:?}"
);
}
#[tokio::test(flavor = "current_thread")]
async fn test_dm_policy_pairing_wrong_code_rejected() {
let enforcer = DmPolicyEnforcer::new(DmPolicy::Pairing, vec![]);
let _ = enforcer.check("peer_x").await;
let result = enforcer.approve_pairing("0000-0000").await;
assert!(result.is_none(), "wrong code should not be approved");
let check = enforcer.check("peer_x").await;
assert_ne!(
check,
PolicyResult::Allow,
"peer_x should not be allowed after failed approval attempt"
);
}