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() } } fn declare_sections(bag: &mut AdminSectionBag) { bag.add(AdminSection::new("tools", Lc::n("Tools")).with_weight(35)); // Replaces the built-in section with the same key. bag.add( AdminSection::new("help", Lc::n("Support")) .with_path("support") .with_weight(5), ); } fn declare_pages(bag: &mut AdminPageBag) { for (path, weight) in [("tools/export", 10), ("/tools/import/", 0)] { bag.add(AdminPage::new(path, "tools", Lc::n(path), &ToolsPermission).with_weight(weight)); } } fn declare_tasks(bag: &mut AdminTaskBag) { for (path, weight) in [("tools/export/json", 1), ("tools/export/csv", 0)] { bag.add(AdminTask::new(path, "/tools/export", Lc::n(path)).with_weight(weight)); } } fn declare_actions(bag: &mut AdminActionBag) { bag.add(AdminAction::new( "tools/export/new/", "tools/export", Lc::n("New export"), )); } struct Ext; #[async_trait] impl Extension for Ext { fn actions(&self) -> Vec { actions![ action::admin::DeclareAdminSections::new(declare_sections), action::admin::DeclareAdminPages::new(declare_pages), action::admin::DeclareAdminTasks::new(declare_tasks), action::admin::DeclareAdminActions::new(declare_actions), ] } } async fn start() { // Starting more than once in the same process (as tests do) does not duplicate anything. let _ = Application::prepare(&Ext).await; let _ = Application::prepare(&Ext).await; } #[pagetop::test] async fn built_in_sections_come_first_and_can_be_replaced() { start().await; let keys: Vec<_> = AdminRegistry::global() .sections() .iter() .map(|s| (s.key(), s.path())) .collect(); assert_eq!( keys, [ ("help", "support"), ("people", "people"), ("structure", "structure"), ("config", "config"), ("tools", "tools"), ("reports", "reports"), ] ); } #[pagetop::test] async fn paths_are_kept_relative_without_surrounding_slashes() { start().await; let registry = AdminRegistry::global(); let pages: Vec<_> = registry .pages_for_section("tools") .iter() .map(|p| p.path()) .collect(); assert_eq!(pages, ["tools/import", "tools/export"]); let tasks: Vec<_> = registry .tasks_for("tools/export") .iter() .map(|t| t.path()) .collect(); assert_eq!(tasks, ["tools/export/csv", "tools/export/json"]); let actions = registry.actions_for("tools/export"); assert_eq!(actions.len(), 1); assert_eq!(actions[0].url(), "tools/export/new"); } #[pagetop::test] async fn admin_paths_are_built_from_the_root_and_back() { start().await; assert_eq!(admin_path(""), "/admin"); assert_eq!(admin_path("tools/export"), "/admin/tools/export"); assert_eq!(admin_path("/tools/export/"), "/admin/tools/export"); assert_eq!(strip_admin_path("/admin"), Some("")); assert_eq!(strip_admin_path("/admin/"), Some("")); assert_eq!( strip_admin_path("/admin/tools/export/"), Some("tools/export") ); assert_eq!(strip_admin_path("/administrator"), None); assert_eq!(strip_admin_path("/tools"), None); } #[pagetop::test] async fn without_an_authentication_extension_nobody_can_access_the_panel() { start().await; let request = web::test::TestRequest::get() .uri("/admin") .to_http_request(); let cx = Context::new(request); assert!(!can_access_admin(&cx)); }