pub mod common;
use common::TestDbExt;
use ave_http::auth::database::DatabaseError;
use reqwest::{Client, StatusCode};
use serde_json::json;
use test_log::test;
#[test(tokio::test)]
async fn test_api_keys_revoked_when_role_added() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user_transactional(
"testuser",
"TestPass123!",
None,
None,
Some(false),
None,
)
.unwrap();
let (api_key, _) = db
.create_api_key(user.id, Some("key1"), None, None, false)
.unwrap();
assert!(
db.authenticate_api_key_request(&api_key, None, "/peer-id")
.is_ok()
);
let role = db.create_role_transactional("editor", None, None).unwrap();
db.assign_role_to_user_transactional(user.id, role.id, None, None)
.unwrap();
let result = db.authenticate_api_key_request(&api_key, None, "/peer-id");
assert!(matches!(result, Err(DatabaseError::PermissionDenied(_))));
}
#[test(tokio::test)]
async fn test_api_keys_revoked_when_role_removed() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role = db.create_role("editor", None).unwrap();
db.assign_role_to_user(user.id, role.id, None).unwrap();
let (api_key, _) = db
.create_api_key(user.id, Some("key1"), None, None, false)
.unwrap();
assert!(
db.authenticate_api_key_request(&api_key, None, "/peer-id")
.is_ok()
);
db.remove_role_from_user(user.id, role.id).unwrap();
let result = db.authenticate_api_key_request(&api_key, None, "/peer-id");
assert!(matches!(result, Err(DatabaseError::PermissionDenied(_))));
}
#[test(tokio::test)]
async fn test_permissions_change_when_role_added() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role = db.create_role("editor", None).unwrap();
db.set_role_permission(role.id, "node_subject", "get", true)
.unwrap();
let perms_before = db.get_user_effective_permissions(user.id).unwrap();
assert!(!perms_before.iter().any(|p| p.resource == "node_subject"
&& p.action == "get"
&& p.allowed));
db.assign_role_to_user(user.id, role.id, None).unwrap();
let perms_after = db.get_user_effective_permissions(user.id).unwrap();
assert!(perms_after.iter().any(|p| p.resource == "node_subject"
&& p.action == "get"
&& p.allowed));
}
#[test(tokio::test)]
async fn test_permissions_change_when_role_removed() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role = db.create_role("editor", None).unwrap();
db.set_role_permission(role.id, "node_request", "post", true)
.unwrap();
db.assign_role_to_user(user.id, role.id, None).unwrap();
let perms_before = db.get_user_effective_permissions(user.id).unwrap();
assert!(perms_before.iter().any(|p| p.resource == "node_request"
&& p.action == "post"
&& p.allowed));
db.remove_role_from_user(user.id, role.id).unwrap();
let perms_after = db.get_user_effective_permissions(user.id).unwrap();
assert!(!perms_after.iter().any(|p| p.resource == "node_request"
&& p.action == "post"
&& p.allowed));
}
#[test(tokio::test)]
async fn test_multiple_role_changes() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let (api_key1, _) = db
.create_api_key(user.id, Some("key1"), None, None, false)
.unwrap();
let role1 = db.create_role("role1", None).unwrap();
db.assign_role_to_user(user.id, role1.id, None).unwrap();
assert!(matches!(
db.authenticate_api_key_request(&api_key1, None, "/peer-id"),
Err(DatabaseError::PermissionDenied(_))
));
let (api_key2, _) = db
.create_api_key(user.id, Some("key2"), None, None, false)
.unwrap();
let role2 = db.create_role("role2", None).unwrap();
db.assign_role_to_user(user.id, role2.id, None).unwrap();
assert!(matches!(
db.authenticate_api_key_request(&api_key2, None, "/peer-id"),
Err(DatabaseError::PermissionDenied(_))
));
assert!(matches!(
db.authenticate_api_key_request(&api_key1, None, "/peer-id"),
Err(DatabaseError::PermissionDenied(_))
));
}
#[test(tokio::test)]
async fn test_user_with_multiple_roles_permission_merge() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role1 = db.create_role("reader", None).unwrap();
let role2 = db.create_role("writer", None).unwrap();
db.set_role_permission(role1.id, "node_subject", "get", true)
.unwrap();
db.set_role_permission(role2.id, "node_subject", "post", true)
.unwrap();
db.assign_role_to_user(user.id, role1.id, None).unwrap();
db.assign_role_to_user(user.id, role2.id, None).unwrap();
let perms = db.get_user_effective_permissions(user.id).unwrap();
assert!(perms.iter().any(|p| p.resource == "node_subject"
&& p.action == "get"
&& p.allowed));
assert!(perms.iter().any(|p| p.resource == "node_subject"
&& p.action == "post"
&& p.allowed));
}
#[test(tokio::test)]
async fn test_user_override_persists_through_role_changes() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
db.set_user_permission(user.id, "admin_users", "delete", false, None)
.unwrap();
let role = db.create_role("user_admin", None).unwrap();
db.set_role_permission(role.id, "admin_users", "delete", true)
.unwrap();
db.assign_role_to_user(user.id, role.id, None).unwrap();
let perms = db.get_user_effective_permissions(user.id).unwrap();
let perm = perms
.iter()
.find(|p| p.resource == "admin_users" && p.action == "delete")
.unwrap();
assert!(!perm.allowed);
}
#[test(tokio::test)]
async fn test_role_permission_modification_affects_all_users() {
let (db, _dirs) = common::create_test_db();
let user1 = db
.create_user("user1", "TestPass123!", None, None, Some(false))
.unwrap();
let user2 = db
.create_user("user2", "TestPass123!", None, None, Some(false))
.unwrap();
let role = db.create_role("editor", None).unwrap();
db.assign_role_to_user(user1.id, role.id, None).unwrap();
db.assign_role_to_user(user2.id, role.id, None).unwrap();
db.set_role_permission(role.id, "node_subject", "post", true)
.unwrap();
let perms1 = db.get_user_effective_permissions(user1.id).unwrap();
let perms2 = db.get_user_effective_permissions(user2.id).unwrap();
assert!(perms1.iter().any(|p| p.resource == "node_subject"
&& p.action == "post"
&& p.allowed));
assert!(perms2.iter().any(|p| p.resource == "node_subject"
&& p.action == "post"
&& p.allowed));
db.set_role_permission(role.id, "node_subject", "post", false)
.unwrap();
let perms1 = db.get_user_effective_permissions(user1.id).unwrap();
let perms2 = db.get_user_effective_permissions(user2.id).unwrap();
assert!(!perms1.iter().any(|p| p.resource == "node_subject"
&& p.action == "post"
&& p.allowed));
assert!(!perms2.iter().any(|p| p.resource == "node_subject"
&& p.action == "post"
&& p.allowed));
}
#[test(tokio::test)]
async fn test_deny_permission_overrides_multiple_allows() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role1 = db.create_role("role1", None).unwrap();
let role2 = db.create_role("role2", None).unwrap();
db.set_role_permission(role1.id, "node_subject", "delete", true)
.unwrap();
db.set_role_permission(role2.id, "node_subject", "delete", true)
.unwrap();
db.assign_role_to_user(user.id, role1.id, None).unwrap();
db.assign_role_to_user(user.id, role2.id, None).unwrap();
db.set_user_permission(user.id, "node_subject", "delete", false, None)
.unwrap();
let perms = db.get_user_effective_permissions(user.id).unwrap();
let perm = perms
.iter()
.find(|p| p.resource == "node_subject" && p.action == "delete")
.unwrap();
assert!(!perm.allowed);
}
#[test(tokio::test)]
async fn test_deleted_roles_removed_from_users() {
let (db, _dirs) = common::create_test_db();
let user = db
.create_user("testuser", "TestPass123!", None, None, Some(false))
.unwrap();
let role = db.create_role("temp_role", None).unwrap();
db.assign_role_to_user(user.id, role.id, None).unwrap();
let roles_before = db.get_user_roles(user.id).unwrap();
assert!(roles_before.contains(&"temp_role".to_string()));
db.delete_role(role.id).unwrap();
let roles_after = db.get_user_roles(user.id).unwrap();
assert!(!roles_after.contains(&"temp_role".to_string()));
}
fn has_access(status: StatusCode) -> bool {
!matches!(status, StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN)
}
async fn test_node_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
default_access: bool,
overrides: &[(&str, &str, bool)],
) {
let endpoints = vec![
("GET", "/public-key", None),
("GET", "/peer-id", None),
("GET", "/network-state", None),
("GET", "/config", None),
(
"GET",
"/state/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=10&page=1",
None,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI/1",
None,
),
(
"GET",
"/events-first-last/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=5",
None,
),
(
"GET",
"/aborts/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
("GET", "/subjects?active=true", None),
(
"GET",
"/subjects/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?active=true",
None,
),
("GET", "/approval", None),
(
"GET",
"/approval/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
("GET", "/auth", None),
(
"GET",
"/auth/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
("GET", "/pending-transfers", None),
(
"PATCH",
"/approval/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
Some(json!("Accepted")),
),
(
"PUT",
"/auth/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
Some(json!(["ExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI"])),
),
(
"DELETE",
"/auth/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
(
"POST",
"/request-abort/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
(
"POST",
"/update/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
(
"POST",
"/manual-distribution/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
(
"POST",
"/request",
Some(json!({"request": {}, "signature": null})),
),
("GET", "/request", None),
(
"GET",
"/request/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
("GET", "/requests-in-manager", None),
(
"GET",
"/requests-in-manager/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
None,
),
];
for (method, path, body) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"PUT" => client.put(&url).json(&body.unwrap_or(json!({}))),
"PATCH" => client.patch(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let expected_access = overrides
.iter()
.find(|(m, p, _)| *m == method && *p == path)
.map(|(_, _, allow)| *allow)
.unwrap_or(default_access);
let access = has_access(status);
assert_eq!(
access, expected_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, expected_access, status, access
);
}
}
async fn test_user_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
should_have_access: bool,
is_management_key: bool,
) {
let endpoints = vec![
("GET", "/me", None),
("GET", "/me/permissions", None),
("GET", "/me/permissions/detailed", None),
];
for (method, path, body) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
assert_eq!(
access, should_have_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, should_have_access, status, access
);
}
let api_key_endpoints = vec![
("GET", "/me/api-keys?include_revoked=false", None),
(
"POST",
"/me/api-keys",
Some(json!({"name": "test_key", "description": "test"})),
),
("DELETE", "/me/api-keys/test_key", None),
];
for (method, path, body) in api_key_endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
let expected_access = should_have_access && is_management_key;
assert_eq!(
access, expected_access,
"{} {} (is_management: {}) - Expected access: {}, Got status: {} (access: {})",
method, path, is_management_key, expected_access, status, access
);
}
}
async fn test_admin_users_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
should_have_access: bool,
) {
let endpoints = vec![
("GET", "/admin/users?include_inactive=false", None),
(
"POST",
"/admin/users",
Some(json!({"username": "test", "password": "Test123!"})),
),
("GET", "/admin/users/999", None),
(
"PUT",
"/admin/users/999",
Some(json!({"password": "NewPass123!"})),
),
("DELETE", "/admin/users/999", None),
("POST", "/admin/users/999/roles/2", None),
("DELETE", "/admin/users/999/roles/2", None),
("GET", "/admin/users/999/permissions", None),
(
"POST",
"/admin/users/999/permissions",
Some(json!({"resource": "test", "action": "get", "allowed": true})),
),
(
"DELETE",
"/admin/users/999/permissions?resource=test&action=get",
None,
),
(
"POST",
"/admin/users/999/password",
Some(json!({"password": "NewPass123!"})),
),
];
for (method, path, body) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"PUT" => client.put(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
assert_eq!(
access, should_have_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, should_have_access, status, access
);
}
}
async fn test_admin_roles_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
should_have_access: bool,
is_superadmin: bool,
) {
let endpoints = vec![
("GET", "/admin/roles", None, should_have_access),
(
"POST",
"/admin/roles",
Some(json!({"name": "test_role"})),
should_have_access,
),
("GET", "/admin/roles/999", None, should_have_access),
(
"PUT",
"/admin/roles/999",
Some(json!({"description": "updated"})),
should_have_access,
),
("DELETE", "/admin/roles/999", None, should_have_access),
(
"GET",
"/admin/roles/999/permissions",
None,
should_have_access,
),
(
"POST",
"/admin/roles/999/permissions",
Some(json!({"resource": "test", "action": "get", "allowed": true})),
should_have_access && is_superadmin,
),
(
"DELETE",
"/admin/roles/999/permissions?resource=test&action=get",
None,
should_have_access && is_superadmin,
),
];
for (method, path, body, expected_access) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"PUT" => client.put(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
assert_eq!(
access, expected_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, expected_access, status, access
);
}
}
async fn test_admin_apikeys_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
should_have_access: bool,
is_superadmin: bool,
) {
let endpoints = vec![
(
"GET",
"/admin/api-keys/user/999?include_revoked=false",
None,
should_have_access,
),
(
"GET",
"/admin/api-keys?include_revoked=false",
None,
should_have_access,
),
("GET", "/admin/api-keys/999", None, should_have_access),
(
"POST",
"/admin/api-keys/user/999",
Some(json!({"name": "test_key"})),
should_have_access && is_superadmin,
),
(
"DELETE",
"/admin/api-keys/999",
Some(json!({"reason": "test"})),
should_have_access,
),
(
"POST",
"/admin/api-keys/999/rotate",
Some(json!({"name": "rotated_key"})),
should_have_access,
),
];
for (method, path, body, expected_access) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"POST" => client.post(&url).json(&body.unwrap_or(json!({}))),
"DELETE" => client.delete(&url).json(&body.unwrap_or(json!({}))),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
assert_eq!(
access, expected_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, expected_access, status, access
);
}
}
async fn test_admin_system_endpoints(
client: &Client,
base_url: &str,
api_key: &str,
should_have_access: bool,
) {
let endpoints = vec![
("GET", "/admin/resources", None),
("GET", "/admin/actions", None),
("GET", "/admin/audit-logs?limit=10", None),
("GET", "/admin/audit-logs/stats?days=7", None),
("GET", "/admin/rate-limits/stats?hours=24", None),
("GET", "/admin/config", None),
(
"PUT",
"/admin/config/test_key",
Some(json!({"value": 1234})),
),
];
for (method, path, body) in endpoints {
let url = format!("{}{}", base_url, path);
let request = match method {
"GET" => client.get(&url),
"PUT" => client.put(&url).json(&body.unwrap_or(json!({}))),
_ => panic!("Unsupported method: {}", method),
};
let response =
request.header("X-API-Key", api_key).send().await.unwrap();
let status = response.status();
let access = has_access(status);
assert_eq!(
access, should_have_access,
"{} {} - Expected access: {}, Got status: {} (access: {})",
method, path, should_have_access, status, access
);
}
}
#[test(tokio::test)]
async fn test_superadmin_all_endpoints_access() {
let Some((server, _dirs)) =
common::TestServer::build(true, false, None).await
else {
return;
};
let client = Client::new();
let base_url = server.url("");
let (status, login_response) = common::make_request(
&client,
&server.url("/login"),
"POST",
None,
Some(json!({"username": "admin", "password": "AdminPass123!"})),
)
.await;
assert!(status.is_success(), "Login failed with status: {}", status);
let mgmt_key = login_response["api_key"]
.as_str()
.expect("No api_key in login response")
.to_string();
let (status, service_key_response) = common::make_request(
&client,
&server.url("/me/api-keys"),
"POST",
Some(&mgmt_key),
Some(json!({"name": "service_test"})),
)
.await;
assert!(
status.is_success(),
"Service key creation failed with status: {}",
status
);
let service_key = service_key_response["api_key"]
.as_str()
.expect("No api_key in service key response")
.to_string();
test_node_endpoints(&client, &base_url, &mgmt_key, true, &[]).await;
test_user_endpoints(&client, &base_url, &mgmt_key, true, true).await;
test_admin_users_endpoints(&client, &base_url, &mgmt_key, true).await;
test_admin_roles_endpoints(&client, &base_url, &mgmt_key, true, true).await;
test_admin_apikeys_endpoints(&client, &base_url, &mgmt_key, true, true)
.await;
test_admin_system_endpoints(&client, &base_url, &mgmt_key, true).await;
test_node_endpoints(&client, &base_url, &service_key, true, &[]).await;
test_user_endpoints(&client, &base_url, &service_key, true, false).await;
test_admin_users_endpoints(&client, &base_url, &service_key, false).await;
test_admin_roles_endpoints(&client, &base_url, &service_key, false, true)
.await;
test_admin_apikeys_endpoints(&client, &base_url, &service_key, false, true)
.await;
test_admin_system_endpoints(&client, &base_url, &service_key, false).await;
}
#[test(tokio::test)]
async fn test_admin_role_endpoints_access() {
let Some((server, _dirs)) =
common::TestServer::build(true, false, None).await
else {
return;
};
let client = Client::new();
let base_url = server.url("");
let login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "admin", "password": "AdminPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let admin_api_key = login_response["api_key"].as_str().unwrap();
let roles_response: serde_json::Value = client
.get(&server.url("/admin/roles"))
.header("X-API-Key", admin_api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let admin_role_id = roles_response
.as_array()
.unwrap()
.iter()
.find(|r| r["name"] == "admin")
.unwrap()["id"]
.as_i64()
.unwrap();
let _create_user_response: serde_json::Value = client
.post(&server.url("/admin/users"))
.header("X-API-Key", admin_api_key)
.json(&json!({
"username": "test_admin_user",
"password": "TestPass123!",
"is_superadmin": false,
"role_ids": [admin_role_id],
"must_change_password": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(
&json!({"username": "test_admin_user", "password": "TestPass123!"}),
)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_mgmt_key = test_login_response["api_key"].as_str().unwrap();
let service_key_response: serde_json::Value = client
.post(&server.url("/me/api-keys"))
.header("X-API-Key", test_mgmt_key)
.json(&json!({
"name": "service_key",
"is_management": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
eprintln!(
"ADMIN TEST - Service key response: {:?}",
service_key_response
);
let test_service_key = service_key_response["api_key"].as_str().unwrap();
test_user_endpoints(&client, &base_url, test_mgmt_key, true, true).await;
test_admin_users_endpoints(&client, &base_url, test_mgmt_key, true).await;
test_admin_roles_endpoints(&client, &base_url, test_mgmt_key, true, false)
.await;
test_admin_apikeys_endpoints(
&client,
&base_url,
test_mgmt_key,
true,
false,
)
.await;
test_admin_system_endpoints(&client, &base_url, test_mgmt_key, true).await;
test_node_endpoints(&client, &base_url, test_mgmt_key, false, &[]).await;
test_user_endpoints(&client, &base_url, test_service_key, true, false)
.await;
test_admin_users_endpoints(&client, &base_url, test_service_key, false)
.await;
test_admin_roles_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await;
test_admin_apikeys_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await;
test_admin_system_endpoints(&client, &base_url, test_service_key, false)
.await;
test_node_endpoints(&client, &base_url, test_service_key, false, &[]).await; }
#[test(tokio::test)]
async fn test_sender_role_endpoints_access() {
let Some((server, _dirs)) =
common::TestServer::build(true, false, None).await
else {
return;
};
let client = Client::new();
let base_url = server.url("");
let login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "admin", "password": "AdminPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let admin_api_key = login_response["api_key"].as_str().unwrap();
let roles_response: serde_json::Value = client
.get(&server.url("/admin/roles"))
.header("X-API-Key", admin_api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let sender_role_id = roles_response
.as_array()
.unwrap()
.iter()
.find(|r| r["name"] == "sender")
.unwrap()["id"]
.as_i64()
.unwrap();
let _create_user_response: serde_json::Value = client
.post(&server.url("/admin/users"))
.header("X-API-Key", admin_api_key)
.json(&json!({
"username": "test_sender_user",
"password": "TestPass123!",
"is_superadmin": false,
"role_ids": [sender_role_id],
"must_change_password": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "test_sender_user", "password": "TestPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_mgmt_key = test_login_response["api_key"].as_str().unwrap();
let service_key_response: serde_json::Value = client
.post(&server.url("/me/api-keys"))
.header("X-API-Key", test_mgmt_key)
.json(&json!({
"name": "service_key",
"is_management": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_service_key = service_key_response["api_key"].as_str().unwrap();
let sender_node_access = [
("POST", "/request", true),
("GET", "/request", true),
(
"GET",
"/request/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/requests-in-manager", true),
(
"GET",
"/requests-in-manager/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
(
"GET",
"/state/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=10&page=1",
true,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI/1",
true,
),
(
"GET",
"/events-first-last/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=5",
true,
),
(
"GET",
"/aborts/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/subjects?active=true", true),
(
"GET",
"/subjects/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?active=true",
true,
),
("GET", "/approval", true),
(
"GET",
"/approval/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/auth", true),
(
"GET",
"/auth/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/pending-transfers", true),
("GET", "/public-key", true),
("GET", "/peer-id", true),
("GET", "/network-state", true),
];
test_user_endpoints(&client, &base_url, test_mgmt_key, true, true).await;
test_node_endpoints(
&client,
&base_url,
test_mgmt_key,
false,
&sender_node_access,
)
.await; test_admin_users_endpoints(&client, &base_url, test_mgmt_key, false).await; test_admin_roles_endpoints(&client, &base_url, test_mgmt_key, false, false)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_mgmt_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_mgmt_key, false).await;
test_user_endpoints(&client, &base_url, test_service_key, true, false)
.await;
test_node_endpoints(
&client,
&base_url,
test_service_key,
false,
&sender_node_access,
)
.await; test_admin_users_endpoints(&client, &base_url, test_service_key, false)
.await; test_admin_roles_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_service_key, false)
.await; }
#[test(tokio::test)]
async fn test_manager_role_endpoints_access() {
let Some((server, _dirs)) =
common::TestServer::build(true, false, None).await
else {
return;
};
let client = Client::new();
let base_url = server.url("");
let login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "admin", "password": "AdminPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let admin_api_key = login_response["api_key"].as_str().unwrap();
let roles_response: serde_json::Value = client
.get(&server.url("/admin/roles"))
.header("X-API-Key", admin_api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let manager_role_id = roles_response
.as_array()
.unwrap()
.iter()
.find(|r| r["name"] == "manager")
.unwrap()["id"]
.as_i64()
.unwrap();
let _create_user_response: serde_json::Value = client
.post(&server.url("/admin/users"))
.header("X-API-Key", admin_api_key)
.json(&json!({
"username": "test_manager_user",
"password": "TestPass123!",
"is_superadmin": false,
"role_ids": [manager_role_id],
"must_change_password": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "test_manager_user", "password": "TestPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_mgmt_key = test_login_response["api_key"].as_str().unwrap();
let service_key_response: serde_json::Value = client
.post(&server.url("/me/api-keys"))
.header("X-API-Key", test_mgmt_key)
.json(&json!({
"name": "service_key",
"is_management": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_service_key = service_key_response["api_key"].as_str().unwrap();
let manager_overrides: &[(&str, &str, bool)] = &[];
test_user_endpoints(&client, &base_url, test_mgmt_key, true, true).await;
test_node_endpoints(
&client,
&base_url,
test_mgmt_key,
true,
manager_overrides,
)
.await;
test_admin_users_endpoints(&client, &base_url, test_mgmt_key, false).await; test_admin_roles_endpoints(&client, &base_url, test_mgmt_key, false, false)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_mgmt_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_mgmt_key, false).await;
test_user_endpoints(&client, &base_url, test_service_key, true, false)
.await;
test_node_endpoints(
&client,
&base_url,
test_service_key,
true,
manager_overrides,
)
.await;
test_admin_users_endpoints(&client, &base_url, test_service_key, false)
.await; test_admin_roles_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_service_key, false)
.await; }
#[test(tokio::test)]
async fn test_data_role_endpoints_access() {
let Some((server, _dirs)) =
common::TestServer::build(true, false, None).await
else {
return;
};
let client = Client::new();
let base_url = server.url("");
let login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(&json!({"username": "admin", "password": "AdminPass123!"}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let admin_api_key = login_response["api_key"].as_str().unwrap();
let roles_response: serde_json::Value = client
.get(&server.url("/admin/roles"))
.header("X-API-Key", admin_api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let data_role_id = roles_response
.as_array()
.unwrap()
.iter()
.find(|r| r["name"] == "data")
.unwrap()["id"]
.as_i64()
.unwrap();
let _create_user_response: serde_json::Value = client
.post(&server.url("/admin/users"))
.header("X-API-Key", admin_api_key)
.json(&json!({
"username": "test_data_user",
"password": "TestPass123!",
"is_superadmin": false,
"role_ids": [data_role_id],
"must_change_password": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_login_response: serde_json::Value = client
.post(&server.url("/login"))
.json(
&json!({"username": "test_data_user", "password": "TestPass123!"}),
)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_mgmt_key = test_login_response["api_key"].as_str().unwrap();
let service_key_response: serde_json::Value = client
.post(&server.url("/me/api-keys"))
.header("X-API-Key", test_mgmt_key)
.json(&json!({
"name": "service_key",
"is_management": false
}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let test_service_key = service_key_response["api_key"].as_str().unwrap();
let data_read_access = [
("GET", "/public-key", true),
("GET", "/peer-id", true),
("GET", "/network-state", true),
(
"GET",
"/state/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=10&page=1",
true,
),
(
"GET",
"/events/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI/1",
true,
),
(
"GET",
"/events-first-last/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?quantity=5",
true,
),
(
"GET",
"/aborts/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/subjects?active=true", true),
(
"GET",
"/subjects/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI?active=true",
true,
),
("GET", "/approval", true),
(
"GET",
"/approval/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/auth", true),
(
"GET",
"/auth/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/pending-transfers", true),
("GET", "/request", true),
(
"GET",
"/request/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
("GET", "/requests-in-manager", true),
(
"GET",
"/requests-in-manager/JxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxI",
true,
),
];
test_user_endpoints(&client, &base_url, test_mgmt_key, true, true).await;
test_node_endpoints(
&client,
&base_url,
test_mgmt_key,
false,
&data_read_access,
)
.await; test_admin_users_endpoints(&client, &base_url, test_mgmt_key, false).await; test_admin_roles_endpoints(&client, &base_url, test_mgmt_key, false, false)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_mgmt_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_mgmt_key, false).await;
test_user_endpoints(&client, &base_url, test_service_key, true, false)
.await;
test_node_endpoints(
&client,
&base_url,
test_service_key,
false,
&data_read_access,
)
.await; test_admin_users_endpoints(&client, &base_url, test_service_key, false)
.await; test_admin_roles_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_apikeys_endpoints(
&client,
&base_url,
test_service_key,
false,
false,
)
.await; test_admin_system_endpoints(&client, &base_url, test_service_key, false)
.await; }