use somnia::{Credentials, SomniaClient};
#[test]
fn credentials_constructors() {
assert!(matches!(
Credentials::root("root", "root"),
Credentials::Root { .. }
));
assert!(matches!(
Credentials::namespace("ns", "u", "p"),
Credentials::Namespace { .. }
));
assert!(matches!(
Credentials::database("ns", "db", "u", "p"),
Credentials::Database { .. }
));
assert!(matches!(Credentials::token("jwt"), Credentials::Token(_)));
}
#[tokio::test]
async fn connect_anonymous_selects_ns_db() {
let client = SomniaClient::connect_anonymous("mem://", "test", "test")
.await
.expect("anonymous connect");
let rows = client.raw("RETURN 1;").await.expect("raw query");
assert_eq!(rows, vec![serde_json::json!(1)]);
}
#[tokio::test]
async fn record_signup_signin_invalidate_roundtrip() {
let client = SomniaClient::connect_anonymous("mem://", "test", "test")
.await
.expect("anonymous connect");
client
.raw(
r#"
DEFINE TABLE user SCHEMALESS PERMISSIONS FULL;
DEFINE ACCESS account ON DATABASE TYPE RECORD
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
SIGNIN ( SELECT * FROM user WHERE email = $email
AND crypto::argon2::compare(pass, $pass) )
DURATION FOR TOKEN 15m, FOR SESSION 12h;
"#,
)
.await
.expect("define access");
let params = serde_json::json!({ "email": "a@b.com", "pass": "secret" });
let signup_token = client
.signup_record("test", "test", "account", ¶ms)
.await
.expect("signup");
assert!(!signup_token.is_empty(), "signup should mint a token");
let signin_token = client
.signin_record("test", "test", "account", ¶ms)
.await
.expect("signin");
assert!(!signin_token.is_empty(), "signin should mint a token");
let other = SomniaClient::connect_anonymous("mem://", "test", "test")
.await
.expect("second connect");
let reattach = other.authenticate(&signin_token).await;
let _ = reattach;
client.invalidate().await.expect("invalidate");
let bad = serde_json::json!({ "email": "a@b.com", "pass": "wrong" });
let err = client
.signin_record("test", "test", "account", &bad)
.await
.expect_err("wrong password must fail");
assert!(
matches!(err, somnia::SomniaError::Auth(_)),
"expected Auth error, got {err:?}"
);
}