pagetop/tests/admin_template.rs
Manuel Cillero 8e0c887ba6 ✨ (admin): Integra el panel de administración
El núcleo reúne al arrancar las secciones, páginas, pestañas y
botones que declaran las extensiones. La plantilla Admin pinta la
navegación (AdminMenu) y el marco de cada página (AdminFrame), y
PageTop monta el dashboard y las páginas de sección. La raíz del
panel se configura con admin_path.
2026-11-07 07:16:15 +01:00

233 lines
7.7 KiB
Rust

use pagetop::prelude::*;
// The administration registry is built once per process, at startup: every declaration of this
// test is in a single extension and the cases are different tests on top of it.
struct ToolsPermission;
impl Permission for ToolsPermission {
fn key(&self) -> CowStr {
"tools:use".into()
}
}
// Not granted by `grant_tools()`.
struct AuditPermission;
impl Permission for AuditPermission {
fn key(&self) -> CowStr {
"tools:audit".into()
}
}
// Grants `tools:use` to every request, as an authentication extension would.
fn grant_tools(_: &HttpRequest, perm: PermissionRef, granted: &mut bool) {
if perm.key() == "tools:use" {
*granted = true;
}
}
fn declare_pages(bag: &mut AdminPageBag) {
bag.add(AdminPage {
path: "config/export".to_owned(),
section: "config".to_owned(),
title: Lc::n("Export"),
description: Lc::none(),
weight: 0,
permission: &ToolsPermission,
});
bag.add(AdminPage {
path: "reports/audit".to_owned(),
section: "reports".to_owned(),
title: Lc::n("Audit"),
description: Lc::none(),
weight: 0,
permission: &AuditPermission,
});
}
fn declare_tasks(bag: &mut AdminTaskBag) {
bag.add(AdminTask {
path: "config/export".to_owned(),
parent_path: "config/export".to_owned(),
title: Lc::n("All"),
weight: 0,
is_default: true,
permission: None,
});
bag.add(AdminTask {
path: "config/export/csv".to_owned(),
parent_path: "config/export".to_owned(),
title: Lc::n("CSV"),
weight: 1,
is_default: false,
permission: None,
});
bag.add(AdminTask {
path: "config/export/log".to_owned(),
parent_path: "config/export".to_owned(),
title: Lc::n("Log"),
weight: 2,
is_default: false,
permission: Some(&AuditPermission),
});
}
fn declare_actions(bag: &mut AdminActionBag) {
bag.add(AdminAction {
url: "config/export/new".to_owned(),
for_path: "config/export".to_owned(),
title: Lc::n("New export"),
weight: 0,
permission: None,
});
bag.add(AdminAction {
url: "config/export/purge".to_owned(),
for_path: "config/export".to_owned(),
title: Lc::n("Purge exports"),
weight: 1,
permission: Some(&AuditPermission),
});
}
struct Ext;
#[async_trait]
impl Extension for Ext {
fn actions(&self) -> Vec<ActionBox> {
actions![
action::auth::CheckPermission::new(grant_tools),
action::admin::DeclareAdminPages::new(declare_pages),
action::admin::DeclareAdminTasks::new(declare_tasks),
action::admin::DeclareAdminActions::new(declare_actions),
]
}
// None of these handlers checks any permission: each route requires the one of its page.
fn configure_router(&self, routes: Routes) -> Routes {
routes
.admin_route("config/export", web::get(unchecked))
.admin_route("config/export/{id}/edit", web::get(unchecked))
.admin_route("reports/audit", web::get(unchecked))
.admin_route("reports/audit/{id}", web::get(unchecked))
}
}
async fn unchecked() -> &'static str {
"unchecked"
}
async fn render(page: Page) -> String {
let mut page = page.with_child(Html::with(|_| html! { p { "Body of the page." } }));
page.render().await.unwrap().into_string()
}
fn request(uri: &str) -> HttpRequest {
web::test::TestRequest::get().uri(uri).to_http_request()
}
#[pagetop::test]
async fn the_admin_template_frames_the_content_of_the_page() {
let _ = Application::prepare(&Ext).await;
let html =
render(Page::admin(request("/admin/config/export/csv")).with_title(Lc::n("CSV"))).await;
assert!(html.contains(r#"class="wrapper wrapper-admin""#), "{html}");
// Navigation: only the sections with an accessible page.
assert!(html.contains("admin-menu"), "{html}");
assert!(html.contains(r#"href="/admin/config/export""#), "{html}");
assert!(!html.contains(r#"href="/admin/reports/audit""#), "{html}");
// Breadcrumbs up to the nearest declared page, then the page title.
assert!(html.contains(r#"href="/admin/config""#), "{html}");
assert!(
html.contains(r#"<h1 class="admin-page-title">CSV</h1>"#),
"{html}"
);
// Tasks of the parent page, the current one active, without the one not granted.
assert!(
html.contains(
r#"<li class="admin-task admin-task-active"><a href="/admin/config/export/csv">"#
),
"{html}"
);
assert!(!html.contains("config/export/log"), "{html}");
// The content of the page, inside the frame.
assert!(html.contains(r#"<div class="admin-content">"#), "{html}");
assert!(html.contains("Body of the page."), "{html}");
}
#[pagetop::test]
async fn a_declared_page_shows_its_actions_and_its_declared_title() {
let _ = Application::prepare(&Ext).await;
let html = render(Page::admin(request("/admin/config/export"))).await;
assert!(
html.contains(r#"<h1 class="admin-page-title">Export</h1>"#),
"{html}"
);
assert!(
html.contains(r#"href="/admin/config/export/new""#),
"{html}"
);
// Without the permission of the action, its button is not shown.
assert!(!html.contains("config/export/purge"), "{html}");
// The declared title also reaches the <title> of the document.
assert!(html.contains("<title>"), "{html}");
assert!(html.contains(" | Export</title>"), "{html}");
}
#[pagetop::test]
async fn the_standard_template_has_no_admin_frame() {
let _ = Application::prepare(&Ext).await;
let html = render(Page::new(request("/admin/config/export")).with_title(Lc::n("Export"))).await;
assert!(!html.contains("wrapper-admin"), "{html}");
assert!(!html.contains("admin-menu"), "{html}");
assert!(!html.contains("admin-content"), "{html}");
assert!(html.contains("Body of the page."), "{html}");
}
async fn get(uri: &str) -> (u16, String) {
let app = Application::prepare(&Ext).await.test();
let request = web::test::TestRequest::get().uri(uri).to_request();
let response = web::test::send_request(&app, request).await;
let status = response.status().as_u16();
(status, web::test::read_body_text(response).await)
}
#[pagetop::test]
async fn the_dashboard_lists_the_accessible_pages_by_section() {
let (status, html) = get("/admin").await;
assert_eq!(status, 200, "{html}");
assert!(
html.contains(r#"<a href="/admin/config/export">Export</a>"#),
"{html}"
);
assert!(!html.contains("Audit"), "{html}");
}
#[pagetop::test]
async fn a_section_page_is_mounted_only_for_sections_with_pages() {
let (status, html) = get("/admin/config").await;
assert_eq!(status, 200, "{html}");
assert!(
html.contains(r#"<a href="/admin/config/export">Export</a>"#),
"{html}"
);
// Its only page needs a permission that nobody has.
assert_eq!(get("/admin/reports").await.0, 403);
// No page declared in it.
assert_eq!(get("/admin/people").await.0, 404);
}
#[pagetop::test]
async fn each_route_of_the_panel_requires_the_permission_of_its_page() {
// Granted permission, also on a route below the page.
assert_eq!(get("/admin/config/export").await.0, 200);
assert_eq!(get("/admin/config/export/5/edit").await.0, 200);
// Permission not granted, also on a route below the page.
assert_eq!(get("/admin/reports/audit").await.0, 403);
assert_eq!(get("/admin/reports/audit/5").await.0, 403);
// Routes that are not mounted are not affected.
assert_eq!(get("/admin/config/export/5/missing").await.0, 404);
}