forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

shell_route_tests.rs214 lines, 7.8 KiBHistoryRawDownload
1//! Shell tests that render pages, so they need the page routes linked in.
2
3use topcoat::router::request::Request;
4use topcoat::router::{Body, to_bytes};
5
6use crate::views::NAV_SCRIPT_HREF;
7
8/// Every `<script` tag carries a `src`: an inline script could not run under the CSP.
9fn assert_no_inline_scripts(html: &str) {
10 for (at, _) in html.match_indices("<script") {
11 let tag = &html[at..];
12 let end = tag.find('>').expect("script tag closes");
13 assert!(
14 tag[..end].contains(" src="),
15 "inline script: {}",
16 &tag[..=end]
17 );
18 }
19}
20
21#[tokio::test]
22async fn shell_loads_the_deferred_nav_script_without_inline_scripts() {
23 let dir = tempfile::tempdir().expect("tempdir");
24 let state = crate::test_support::test_state(dir.path()).await;
25 let router = crate::test_support::app_router(state);
26 let req = Request::builder()
27 .method("GET")
28 .uri("/login")
29 .body(Body::empty())
30 .unwrap();
31 let res = router.handle(req).await;
32 assert_eq!(res.status(), 200);
33 let csp = res.headers()["content-security-policy"]
34 .to_str()
35 .unwrap()
36 .to_owned();
37 assert!(csp.contains("default-src 'self'"), "{csp}");
38 assert!(!csp.contains("unsafe-inline"), "{csp}");
39 let html = String::from_utf8(
40 to_bytes(res.into_body(), usize::MAX)
41 .await
42 .unwrap()
43 .to_vec(),
44 )
45 .unwrap();
46
47 let tag_at = html
48 .find(&format!("<script src=\"{NAV_SCRIPT_HREF}\""))
49 .expect("the shell loads the nav script");
50 let tag = &html[tag_at..tag_at + html[tag_at..].find('>').unwrap()];
51 assert!(tag.contains("defer"), "{tag}");
52 assert_no_inline_scripts(&html);
53
54 assert!(html.contains("<main tabindex=\"-1\" data-width=\"col\">"));
55 assert!(html.contains("data-hard=\"true\" method=\"post\" action=\"/login\""));
56
57 // An anonymous page renders the default colours.
58 let tag = html_tag(&html);
59 assert!(tag.contains("data-accent-light=\"sage\""), "{tag}");
60 assert!(tag.contains("data-accent-dark=\"sage\""), "{tag}");
61 assert!(tag.contains("data-base-light=\"ink\""), "{tag}");
62 assert!(tag.contains("data-base-dark=\"ink\""), "{tag}");
63}
64
65/// The `<html …>` start tag of a page.
66fn html_tag(page: &str) -> &str {
67 let at = page.find("<html").expect("the page has an html tag");
68 let len = page[at..].find('>').expect("the html tag closes");
69 &page[at..at + len + 1]
70}
71
72/// The `<html>` tag of `/settings/security` for a signed-in user whose stored colours are the
73/// four ids given, in the order light accent, dark accent, light base, dark base.
74async fn html_tag_for_colours(
75 router: &topcoat::router::Router,
76 store: &forge_core::Store,
77 name: &str,
78 token: &str,
79 colours: [&str; 4],
80) -> String {
81 use crate::auth::SESSION_COOKIE;
82 let user = store
83 .create_user(name, None, "x", false)
84 .await
85 .expect("user");
86 let [accent_light, accent_dark, base_light, base_dark] = colours;
87 store
88 .set_colours(&user.id, accent_light, accent_dark, base_light, base_dark)
89 .await
90 .expect("colours");
91 crate::test_support::create_session(store, &user.id, token, "colour-csrf").await;
92 let req = Request::builder()
93 .method("GET")
94 .uri("/settings/security")
95 .header("cookie", format!("__Host-{SESSION_COOKIE}={token}"))
96 .body(Body::empty())
97 .unwrap();
98 let res = router.handle(req).await;
99 assert_eq!(res.status().as_u16(), 200);
100 let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap();
101 html_tag(&String::from_utf8_lossy(&bytes)).to_owned()
102}
103
104/// Every colour case shares one fixture; each has its own user and session.
105#[tokio::test]
106async fn the_html_tag_carries_the_viewers_colours() {
107 let dir = tempfile::tempdir().expect("tempdir");
108 let state = crate::test_support::test_state(dir.path()).await;
109 let store = state.store.clone();
110 let router = crate::test_support::app_router(state);
111
112 let tag = html_tag_for_colours(
113 &router,
114 &store,
115 "tahinli",
116 "colour-token",
117 ["ocean", "plum", "ink", "dusk"],
118 )
119 .await;
120 assert!(tag.contains("data-accent-light=\"ocean\""), "{tag}");
121 assert!(tag.contains("data-accent-dark=\"plum\""), "{tag}");
122 assert!(tag.contains("data-base-light=\"ink\""), "{tag}");
123 assert!(tag.contains("data-base-dark=\"dusk\""), "{tag}");
124 assert!(!tag.contains("data-accent="), "{tag}");
125 // Stored ids that are no longer offered fall back to the defaults, never to nothing.
126 let removed = html_tag_for_colours(
127 &router,
128 &store,
129 "removed",
130 "removed-token",
131 ["removed-palette", "removed-palette", "x", "x"],
132 )
133 .await;
134 assert!(removed.contains("data-accent-light=\"sage\""), "{removed}");
135 assert!(removed.contains("data-accent-dark=\"sage\""), "{removed}");
136 assert!(removed.contains("data-base-light=\"ink\""), "{removed}");
137 assert!(removed.contains("data-base-dark=\"ink\""), "{removed}");
138 // A removed preset (`ochre`, `mist`) falls back too, and a hue passes through as itself.
139 let retired = html_tag_for_colours(
140 &router,
141 &store,
142 "retired",
143 "retired-token",
144 ["ochre", "terracotta", "mist", "stone"],
145 )
146 .await;
147 assert!(retired.contains("data-accent-light=\"sage\""), "{retired}");
148 assert!(retired.contains("data-accent-dark=\"sage\""), "{retired}");
149 assert!(retired.contains("data-base-light=\"ink\""), "{retired}");
150 assert!(retired.contains("data-base-dark=\"ink\""), "{retired}");
151 let hued = html_tag_for_colours(
152 &router,
153 &store,
154 "hued",
155 "hued-token",
156 ["hue-215", "hue-7", "hue-300", "ink"],
157 )
158 .await;
159 assert!(hued.contains("data-accent-light=\"hue-215\""), "{hued}");
160 assert!(hued.contains("data-accent-dark=\"hue-7\""), "{hued}");
161 assert!(hued.contains("data-base-light=\"hue-300\""), "{hued}");
162 assert!(hued.contains("data-base-dark=\"ink\""), "{hued}");
163}
164
165/// Renders `/settings/security` (a page that uses the shell) for a freshly signed-in user.
166async fn menu_html(
167 router: &topcoat::router::Router,
168 store: &forge_core::Store,
169 name: &str,
170 is_admin: bool,
171 token: &str,
172) -> String {
173 use crate::auth::SESSION_COOKIE;
174 use topcoat::router::request::Request;
175 use topcoat::router::to_bytes;
176
177 let user = store
178 .create_user(name, None, "x", is_admin)
179 .await
180 .expect("user");
181 crate::test_support::create_session(store, &user.id, token, "menu-csrf").await;
182 let req = Request::builder()
183 .method("GET")
184 .uri("/settings/security")
185 .header("cookie", format!("__Host-{SESSION_COOKIE}={token}"))
186 .body(Body::empty())
187 .expect("request");
188 let res = router.handle(req).await;
189 assert_eq!(res.status().as_u16(), 200);
190 let bytes = to_bytes(res.into_body(), usize::MAX).await.expect("body");
191 String::from_utf8_lossy(&bytes).into_owned()
192}
193
194/// A user sees no admin links and an admin sees the one Admin entry. Both render from one
195/// fixture.
196#[tokio::test]
197async fn admin_links_follow_the_role() {
198 let dir = tempfile::tempdir().expect("tempdir");
199 let state = crate::test_support::test_state(dir.path()).await;
200 let store = state.store.clone();
201 let router = crate::test_support::app_router(state);
202
203 let html = menu_html(&router, &store, "tahinli", false, "menu-token-user").await;
204 assert!(html.contains("Log out"), "user menu is rendered");
205 assert!(!html.contains("/admin/users"), "no admin link for a user");
206 assert!(!html.contains("/admin/audit"), "no audit link for a user");
207
208 let html = menu_html(&router, &store, "root", true, "menu-token-admin").await;
209 assert!(html.contains("href=\"/admin/users\""));
210 assert!(
211 !html.contains("Admin:"),
212 "one Admin entry, no per-page items"
213 );
214}