forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

section_nav_e2e.rs232 lines, 8.1 KiBHistoryRawDownload
1//! Black-box tests for the admin and settings section tabs: every page under `/admin` and
2//! `/settings` shows the section's own tab row above the page, marks the current tab with
3//! `aria-current="page"`, and has no " · "-joined link row.
4
5#![allow(dead_code)]
6
7use crate::common::{Http, Server, bin, describe, launch, login, pick_ports, write_config};
8use std::path::Path;
9use std::process::Command;
10
11const PW: &str = "tahinli-password-123";
12
13/// The `data-theme` attribute of `<html>`, if the page sets one.
14fn html_theme(body: &str) -> Option<String> {
15 let doc = scraper::Html::parse_document(body);
16 doc.root_element().attr("data-theme").map(str::to_owned)
17}
18
19/// The value of attribute `name` on `<html>`, if the page sets it.
20fn html_attr(body: &str, name: &str) -> Option<String> {
21 let doc = scraper::Html::parse_document(body);
22 doc.root_element().attr(name).map(str::to_owned)
23}
24
25/// The four colour attributes of `<html>`, in the order light accent, dark accent, light base,
26/// dark base.
27fn html_colours(body: &str) -> [Option<String>; 4] {
28 [
29 html_attr(body, "data-accent-light"),
30 html_attr(body, "data-accent-dark"),
31 html_attr(body, "data-base-light"),
32 html_attr(body, "data-base-dark"),
33 ]
34}
35
36/// The checked radio of each colour group, in the same order as `html_colours`.
37fn checked_colours(body: &str) -> [Option<String>; 4] {
38 [
39 checked_radio(body, "accent_light"),
40 checked_radio(body, "accent_dark"),
41 checked_radio(body, "base_light"),
42 checked_radio(body, "base_dark"),
43 ]
44}
45
46/// The value of the checked radio button in the group `name` on a page.
47fn checked_radio(body: &str, name: &str) -> Option<String> {
48 let doc = scraper::Html::parse_document(body);
49 let selector =
50 scraper::Selector::parse(&format!("input[type=\"radio\"][name=\"{name}\"][checked]"))
51 .expect("selector");
52 doc.select(&selector)
53 .next()
54 .and_then(|el| el.value().attr("value"))
55 .map(str::to_owned)
56}
57
58/// The option value that is selected in the `<select name="...">` on a page.
59fn selected_option(body: &str, select: &str) -> Option<String> {
60 let doc = scraper::Html::parse_document(body);
61 let selector = scraper::Selector::parse(&format!("select[name=\"{select}\"] option[selected]"))
62 .expect("selector");
63 doc.select(&selector)
64 .next()
65 .and_then(|el| el.value().attr("value"))
66 .map(str::to_owned)
67}
68
69/// The top-bar theme button: the value it posts and its accessible label.
70fn theme_button(body: &str) -> (String, String) {
71 let doc = scraper::Html::parse_document(body);
72 let form = scraper::Selector::parse("form.theme-form").expect("selector");
73 let form = doc
74 .select(&form)
75 .next()
76 .unwrap_or_else(|| panic!("no theme button on page"));
77 let value = scraper::Selector::parse("input[name=\"theme\"]").expect("selector");
78 let value = form
79 .select(&value)
80 .next()
81 .and_then(|el| el.value().attr("value"))
82 .expect("theme value")
83 .to_owned();
84 let button = scraper::Selector::parse("button").expect("selector");
85 let label = form
86 .select(&button)
87 .next()
88 .and_then(|el| el.value().attr("aria-label"))
89 .expect("aria-label")
90 .to_owned();
91 (value, label)
92}
93
94/// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded.
95fn admin(config: &Path, args: &[&str]) {
96 let out = Command::new(bin())
97 .arg("--config")
98 .arg(config)
99 .arg("admin")
100 .args(args)
101 .env("FORGE_PASSWORD", PW)
102 .output()
103 .expect("run admin command");
104 assert!(
105 out.status.success(),
106 "admin {args:?} failed:\n{}",
107 describe(&out)
108 );
109}
110
111/// A running server with user `tahinli`. The temp dir lives as long as the test.
112struct Env {
113 _root: tempfile::TempDir,
114 _server: Server,
115 http: Http,
116}
117
118async fn start() -> Env {
119 let root = tempfile::tempdir().expect("temp dir");
120 let dir = root.path().to_path_buf();
121 let ports = pick_ports();
122 let config = write_config(&dir, ports);
123 admin(&config, &["create-user", "tahinli", "--admin"]);
124 let (server, ports) = launch(&dir, ports).await;
125 let http = Http::new(ports.http);
126 Env {
127 _root: root,
128 _server: server,
129 http,
130 }
131}
132
133/// The section's tab row as (href, aria-current) pairs. The row is the first `nav.tabs` in `main`,
134/// which the shell puts before the page's own content, so a page's own tabs are not read here.
135fn tab_links(body: &str) -> Vec<(String, Option<String>)> {
136 let doc = scraper::Html::parse_document(body);
137 let row = scraper::Selector::parse("main nav.tabs").expect("selector");
138 let links = scraper::Selector::parse("a").expect("selector");
139 let row = doc.select(&row).next().expect("section tab row");
140 row.select(&links)
141 .map(|a| {
142 (
143 a.value().attr("href").unwrap_or("").to_owned(),
144 a.value().attr("aria-current").map(str::to_owned),
145 )
146 })
147 .collect()
148}
149
150/// Checks the section's tab row: its first tab is the section's first, every tab in `hrefs` is
151/// there, and exactly one tab, the one at `current`, is marked.
152fn assert_tabs(body: &str, current: &str, hrefs: &[&str]) {
153 let links = tab_links(body);
154 assert_eq!(links[0].0, hrefs[0], "first tab is the section's first");
155 for href in hrefs {
156 assert!(links.iter().any(|(h, _)| h == href), "missing {href}");
157 }
158 let marked: Vec<_> = links
159 .iter()
160 .filter(|(_, c)| c.as_deref() == Some("page"))
161 .collect();
162 assert_eq!(marked.len(), 1, "exactly one current tab: {links:?}");
163 assert_eq!(marked[0].0, current);
164 assert!(!body.contains("</a> · <a"), "no dot-separated link row");
165}
166
167#[tokio::test]
168async fn admin_pages_get_the_admin_tabs() {
169 let env = start().await;
170 let session = login(&env.http, "tahinli", PW).await;
171 let want = [
172 "/admin/users",
173 "/admin/runners",
174 "/admin/server",
175 "/admin/mail",
176 "/admin/backups",
177 "/admin/audit",
178 ];
179 // Invites live under Users and replication under Backups: their own tab is the one marked.
180 for (path, current) in [
181 ("/admin/users", "/admin/users"),
182 ("/admin/invites", "/admin/users"),
183 ("/admin/audit", "/admin/audit"),
184 ("/admin/runners", "/admin/runners"),
185 ("/admin/mail", "/admin/mail"),
186 ("/admin/server", "/admin/server"),
187 ("/admin/backups", "/admin/backups"),
188 ("/admin/replication", "/admin/backups"),
189 ] {
190 let page = env.http.get(path, Some(&session)).await;
191 assert_eq!(page.status, 200, "GET {path}");
192 assert_tabs(&page.body, current, &want);
193 assert!(
194 !tab_links(&page.body).iter().any(|(h, _)| h == "/explore"),
195 "global links are swapped out"
196 );
197 }
198}
199
200#[tokio::test]
201async fn settings_pages_get_the_settings_tabs() {
202 let env = start().await;
203 let session = login(&env.http, "tahinli", PW).await;
204 let tabs = [
205 "/settings/profile",
206 "/settings/appearance",
207 "/settings/notifications",
208 "/settings/security",
209 "/settings/keys",
210 "/settings/export",
211 ];
212 // A page folded into another tab marks that tab: GPG keys and
213 // tokens under keys.
214 let pages = [
215 ("/settings/profile", "/settings/profile"),
216 ("/settings/keys", "/settings/keys"),
217 ("/settings/security", "/settings/security"),
218 ("/settings/notifications", "/settings/notifications"),
219 ("/settings/appearance", "/settings/appearance"),
220 ];
221 for (path, current) in pages {
222 let page = env.http.get(path, Some(&session)).await;
223 assert_eq!(page.status, 200, "GET {path}");
224 assert_tabs(&page.body, current, &tabs);
225 }
226 // GPG keys and tokens are kinds of the Keys tab, each at its own address.
227 for path in ["/settings/gpg", "/settings/tokens"] {
228 let page = env.http.get(path, Some(&session)).await;
229 assert_eq!(page.status, 200, "GET {path}");
230 assert_tabs(&page.body, "/settings/keys", &tabs);
231 }
232}