forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Black-box tests for the theme and language preferences: the appearance settings page, the |
| 2 | //! `data-theme` attribute on `<html>`, and the anonymous footer cookie. Like the other e2e |
| 3 | //! tests, this starts the real `forge` binary and drives it over HTTP. Cookies are set by hand |
| 4 | //! because the session cookie is `__Host-` and `Secure`. |
| 5 | |
| 6 | use crate::common::{ |
| 7 | Http, Server, bin, cookie_named, describe, input_value, launch, login, pick_ports, write_config, |
| 8 | }; |
| 9 | use std::path::Path; |
| 10 | use std::process::Command; |
| 11 | |
| 12 | const PW: &str = "tahinli-password-123"; |
| 13 | |
| 14 | /// The `data-theme` attribute of `<html>`, if the page sets one. |
| 15 | fn html_theme(body: &str) -> Option<String> { |
| 16 | let doc = scraper::Html::parse_document(body); |
| 17 | doc.root_element().attr("data-theme").map(str::to_owned) |
| 18 | } |
| 19 | |
| 20 | /// The value of attribute `name` on `<html>`, if the page sets it. |
| 21 | fn html_attr(body: &str, name: &str) -> Option<String> { |
| 22 | let doc = scraper::Html::parse_document(body); |
| 23 | doc.root_element().attr(name).map(str::to_owned) |
| 24 | } |
| 25 | |
| 26 | /// The four colour attributes of `<html>`, in the order light accent, dark accent, light base, |
| 27 | /// dark base. |
| 28 | fn html_colours(body: &str) -> [Option<String>; 4] { |
| 29 | [ |
| 30 | html_attr(body, "data-accent-light"), |
| 31 | html_attr(body, "data-accent-dark"), |
| 32 | html_attr(body, "data-base-light"), |
| 33 | html_attr(body, "data-base-dark"), |
| 34 | ] |
| 35 | } |
| 36 | |
| 37 | /// The checked radio of each colour group, in the same order as `html_colours`. |
| 38 | fn checked_colours(body: &str) -> [Option<String>; 4] { |
| 39 | [ |
| 40 | checked_radio(body, "accent_light"), |
| 41 | checked_radio(body, "accent_dark"), |
| 42 | checked_radio(body, "base_light"), |
| 43 | checked_radio(body, "base_dark"), |
| 44 | ] |
| 45 | } |
| 46 | |
| 47 | /// The value of the checked radio button in the group `name` on a page. |
| 48 | fn checked_radio(body: &str, name: &str) -> Option<String> { |
| 49 | let doc = scraper::Html::parse_document(body); |
| 50 | let selector = |
| 51 | scraper::Selector::parse(&format!("input[type=\"radio\"][name=\"{name}\"][checked]")) |
| 52 | .expect("selector"); |
| 53 | doc.select(&selector) |
| 54 | .next() |
| 55 | .and_then(|el| el.value().attr("value")) |
| 56 | .map(str::to_owned) |
| 57 | } |
| 58 | |
| 59 | /// The option value that is selected in the `<select name="...">` on a page. |
| 60 | fn selected_option(body: &str, select: &str) -> Option<String> { |
| 61 | let doc = scraper::Html::parse_document(body); |
| 62 | let selector = scraper::Selector::parse(&format!( |
| 63 | "select[name=\"{select}\"] option[selected], input[name=\"{select}\"][checked], button[name=\"{select}\"][aria-pressed=\"true\"]" |
| 64 | )) |
| 65 | .expect("selector"); |
| 66 | doc.select(&selector) |
| 67 | .next() |
| 68 | .and_then(|el| el.value().attr("value")) |
| 69 | .map(str::to_owned) |
| 70 | } |
| 71 | |
| 72 | /// The top-bar theme button: the value it posts and its accessible label. |
| 73 | fn theme_button(body: &str) -> (String, String) { |
| 74 | let doc = scraper::Html::parse_document(body); |
| 75 | let form = scraper::Selector::parse("form.theme-form").expect("selector"); |
| 76 | let form = doc |
| 77 | .select(&form) |
| 78 | .next() |
| 79 | .unwrap_or_else(|| panic!("no theme button on page")); |
| 80 | let value = scraper::Selector::parse("input[name=\"theme\"]").expect("selector"); |
| 81 | let value = form |
| 82 | .select(&value) |
| 83 | .next() |
| 84 | .and_then(|el| el.value().attr("value")) |
| 85 | .expect("theme value") |
| 86 | .to_owned(); |
| 87 | let button = scraper::Selector::parse("button").expect("selector"); |
| 88 | let label = form |
| 89 | .select(&button) |
| 90 | .next() |
| 91 | .and_then(|el| el.value().attr("aria-label")) |
| 92 | .expect("aria-label") |
| 93 | .to_owned(); |
| 94 | (value, label) |
| 95 | } |
| 96 | |
| 97 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 98 | fn admin(config: &Path, args: &[&str]) { |
| 99 | let out = Command::new(bin()) |
| 100 | .arg("--config") |
| 101 | .arg(config) |
| 102 | .arg("admin") |
| 103 | .args(args) |
| 104 | .env("FORGE_PASSWORD", PW) |
| 105 | .output() |
| 106 | .expect("run admin command"); |
| 107 | assert!( |
| 108 | out.status.success(), |
| 109 | "admin {args:?} failed:\n{}", |
| 110 | describe(&out) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /// A running server with user `tahinli`. The temp dir lives as long as the test. |
| 115 | struct Env { |
| 116 | _root: tempfile::TempDir, |
| 117 | _server: Server, |
| 118 | http: Http, |
| 119 | } |
| 120 | |
| 121 | async fn start() -> Env { |
| 122 | let root = tempfile::tempdir().expect("temp dir"); |
| 123 | let dir = root.path().to_path_buf(); |
| 124 | let ports = pick_ports(); |
| 125 | let config = write_config(&dir, ports); |
| 126 | admin(&config, &["create-user", "tahinli"]); |
| 127 | let (server, ports) = launch(&dir, ports).await; |
| 128 | let http = Http::new(ports.http); |
| 129 | Env { |
| 130 | _root: root, |
| 131 | _server: server, |
| 132 | http, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[tokio::test] |
| 137 | async fn appearance_setting_persists_and_renders_the_html_attribute() { |
| 138 | let env = start().await; |
| 139 | let session = login(&env.http, "tahinli", PW).await; |
| 140 | |
| 141 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 142 | assert_eq!(page.status, 200); |
| 143 | assert_eq!( |
| 144 | selected_option(&page.body, "theme").as_deref(), |
| 145 | Some("light") |
| 146 | ); |
| 147 | assert_eq!( |
| 148 | html_theme(&page.body).as_deref(), |
| 149 | Some("light"), |
| 150 | "the default is light, which renders data-theme=light" |
| 151 | ); |
| 152 | let csrf = input_value(&page.body, "csrf"); |
| 153 | |
| 154 | let saved = env |
| 155 | .http |
| 156 | .post_form( |
| 157 | "/settings/appearance", |
| 158 | &[("csrf", &csrf), ("theme", "dark"), ("language", "tr")], |
| 159 | Some(&session), |
| 160 | ) |
| 161 | .await; |
| 162 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 163 | assert_eq!( |
| 164 | saved.header(reqwest::header::LOCATION).as_deref(), |
| 165 | Some("/settings/appearance"), |
| 166 | "a save redirects back with no saved notice" |
| 167 | ); |
| 168 | |
| 169 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 170 | assert_eq!( |
| 171 | selected_option(&page.body, "theme").as_deref(), |
| 172 | Some("dark") |
| 173 | ); |
| 174 | assert_eq!( |
| 175 | selected_option(&page.body, "language").as_deref(), |
| 176 | Some("tr") |
| 177 | ); |
| 178 | assert_eq!(html_theme(&page.body).as_deref(), Some("dark")); |
| 179 | } |
| 180 | |
| 181 | #[tokio::test] |
| 182 | async fn the_form_has_no_save_button_and_every_field_autosubmits() { |
| 183 | let env = start().await; |
| 184 | let session = login(&env.http, "tahinli", PW).await; |
| 185 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 186 | let doc = scraper::Html::parse_document(&page.body); |
| 187 | |
| 188 | let form = scraper::Selector::parse("form[data-appearance]").expect("selector"); |
| 189 | assert_eq!(doc.select(&form).count(), 1, "one appearance form"); |
| 190 | let buttons = scraper::Selector::parse("form[data-appearance] button:not([type=\"button\"])") |
| 191 | .expect("selector"); |
| 192 | assert_eq!(doc.select(&buttons).count(), 0, "no Save button"); |
| 193 | let submits = |
| 194 | scraper::Selector::parse("form[data-appearance] input[type=\"submit\"]").expect("selector"); |
| 195 | assert_eq!(doc.select(&submits).count(), 0, "no submit input"); |
| 196 | |
| 197 | let fields = |
| 198 | scraper::Selector::parse("form[data-appearance] input, form[data-appearance] select") |
| 199 | .expect("selector"); |
| 200 | for field in doc.select(&fields) { |
| 201 | // The hidden fields never save, and the colour tab switch only shows a panel. |
| 202 | if field.value().attr("type") == Some("hidden") |
| 203 | || field.value().attr("data-colour-tab").is_some() |
| 204 | { |
| 205 | continue; |
| 206 | } |
| 207 | assert!( |
| 208 | field.value().attr("data-autosubmit").is_some(), |
| 209 | "{:?} does not autosubmit", |
| 210 | field.value().attr("name") |
| 211 | ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[tokio::test] |
| 216 | async fn light_and_dark_colours_save_independently_and_render_on_html() { |
| 217 | let env = start().await; |
| 218 | let session = login(&env.http, "tahinli", PW).await; |
| 219 | |
| 220 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 221 | let defaults = ["sage", "sage", "ink", "ink"].map(str::to_owned); |
| 222 | assert_eq!(checked_colours(&page.body), defaults.clone().map(Some)); |
| 223 | assert_eq!(html_colours(&page.body), defaults.map(Some)); |
| 224 | let csrf = input_value(&page.body, "csrf"); |
| 225 | |
| 226 | let saved = env |
| 227 | .http |
| 228 | .post_form( |
| 229 | "/settings/appearance", |
| 230 | &[ |
| 231 | ("csrf", &csrf), |
| 232 | ("theme", "light"), |
| 233 | ("language", "en"), |
| 234 | ("accent_light", "ocean"), |
| 235 | ("accent_dark", "plum"), |
| 236 | ("base_light", "ink"), |
| 237 | ("base_dark", "dusk"), |
| 238 | ], |
| 239 | Some(&session), |
| 240 | ) |
| 241 | .await; |
| 242 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 243 | |
| 244 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 245 | let chosen = ["ocean", "plum", "ink", "dusk"].map(str::to_owned); |
| 246 | assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); |
| 247 | assert_eq!(html_colours(&page.body), chosen.map(Some)); |
| 248 | |
| 249 | // A single field changes on its own; the other three keep their values. |
| 250 | let saved = env |
| 251 | .http |
| 252 | .post_form( |
| 253 | "/settings/appearance", |
| 254 | &[ |
| 255 | ("csrf", &csrf), |
| 256 | ("theme", "light"), |
| 257 | ("language", "en"), |
| 258 | ("base_dark", "heather"), |
| 259 | ], |
| 260 | Some(&session), |
| 261 | ) |
| 262 | .await; |
| 263 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 264 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 265 | let chosen = ["ocean", "plum", "ink", "heather"].map(str::to_owned); |
| 266 | assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); |
| 267 | assert_eq!(html_colours(&page.body), chosen.map(Some)); |
| 268 | |
| 269 | // Malformed or unknown ids change nothing. |
| 270 | for (field, bad) in [ |
| 271 | ("accent_light", "Ocean"), |
| 272 | ("accent_dark", "../x"), |
| 273 | ("accent_dark", "neon"), |
| 274 | ("accent_light", ""), |
| 275 | ("base_light", "neon"), |
| 276 | ("base_dark", ""), |
| 277 | ] { |
| 278 | let form = [ |
| 279 | ("csrf", csrf.as_str()), |
| 280 | ("theme", "light"), |
| 281 | ("language", "en"), |
| 282 | (field, bad), |
| 283 | ]; |
| 284 | let res = env |
| 285 | .http |
| 286 | .post_form("/settings/appearance", &form, Some(&session)) |
| 287 | .await; |
| 288 | assert_ne!(res.status, 303, "bad {field} {bad:?} must not save"); |
| 289 | } |
| 290 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 291 | let chosen = ["ocean", "plum", "ink", "heather"].map(str::to_owned); |
| 292 | assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); |
| 293 | assert_eq!(html_colours(&page.body), chosen.map(Some)); |
| 294 | |
| 295 | // The newer accents and backgrounds save and show on the page the same way. |
| 296 | let saved = env |
| 297 | .http |
| 298 | .post_form( |
| 299 | "/settings/appearance", |
| 300 | &[ |
| 301 | ("csrf", &csrf), |
| 302 | ("theme", "light"), |
| 303 | ("language", "tr"), |
| 304 | ("accent_light", "hue-200"), |
| 305 | ("accent_dark", "rose"), |
| 306 | ("base_light", "heather"), |
| 307 | ("base_dark", "dusk"), |
| 308 | ], |
| 309 | Some(&session), |
| 310 | ) |
| 311 | .await; |
| 312 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 313 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 314 | // The Custom radio carries the hue at the default vividness; the stored id stays `hue-200`. |
| 315 | let checked = ["hue-200-v50", "rose", "heather", "dusk"].map(str::to_owned); |
| 316 | assert_eq!(checked_colours(&page.body), checked.map(Some)); |
| 317 | let chosen = ["hue-200", "rose", "heather", "dusk"].map(str::to_owned); |
| 318 | assert_eq!(html_colours(&page.body), chosen.map(Some)); |
| 319 | |
| 320 | // The newest accents and backgrounds save and show the same way. |
| 321 | let saved = env |
| 322 | .http |
| 323 | .post_form( |
| 324 | "/settings/appearance", |
| 325 | &[ |
| 326 | ("csrf", &csrf), |
| 327 | ("theme", "light"), |
| 328 | ("language", "en"), |
| 329 | ("accent_light", "sage"), |
| 330 | ("accent_dark", "ocean"), |
| 331 | ("base_light", "ink"), |
| 332 | ("base_dark", "heather"), |
| 333 | ], |
| 334 | Some(&session), |
| 335 | ) |
| 336 | .await; |
| 337 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 338 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 339 | let chosen = ["sage", "ocean", "ink", "heather"].map(str::to_owned); |
| 340 | assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); |
| 341 | assert_eq!(html_colours(&page.body), chosen.map(Some)); |
| 342 | } |
| 343 | |
| 344 | #[tokio::test] |
| 345 | async fn a_save_without_colour_fields_keeps_the_stored_colours() { |
| 346 | let env = start().await; |
| 347 | let session = login(&env.http, "tahinli", PW).await; |
| 348 | |
| 349 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 350 | let csrf = input_value(&page.body, "csrf"); |
| 351 | let saved = env |
| 352 | .http |
| 353 | .post_form( |
| 354 | "/settings/appearance", |
| 355 | &[ |
| 356 | ("csrf", &csrf), |
| 357 | ("theme", "light"), |
| 358 | ("language", "en"), |
| 359 | ("accent_light", "rose"), |
| 360 | ("accent_dark", "ocean"), |
| 361 | ("base_light", "blush"), |
| 362 | ("base_dark", "dusk"), |
| 363 | ], |
| 364 | Some(&session), |
| 365 | ) |
| 366 | .await; |
| 367 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 368 | let before = env.http.get("/settings/appearance", Some(&session)).await; |
| 369 | let stored = ["rose", "ocean", "blush", "dusk"].map(str::to_owned); |
| 370 | assert_eq!(html_colours(&before.body), stored.clone().map(Some)); |
| 371 | |
| 372 | let saved = env |
| 373 | .http |
| 374 | .post_form( |
| 375 | "/settings/appearance", |
| 376 | &[("csrf", &csrf), ("theme", "dark"), ("language", "en")], |
| 377 | Some(&session), |
| 378 | ) |
| 379 | .await; |
| 380 | assert_eq!(saved.status, 303, "save without colours: {}", saved.body); |
| 381 | |
| 382 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 383 | assert_eq!( |
| 384 | selected_option(&page.body, "theme").as_deref(), |
| 385 | Some("dark") |
| 386 | ); |
| 387 | assert_eq!(checked_colours(&page.body), stored.clone().map(Some)); |
| 388 | assert_eq!(html_colours(&page.body), stored.map(Some)); |
| 389 | } |
| 390 | |
| 391 | #[tokio::test] |
| 392 | async fn appearance_save_without_a_valid_csrf_token_is_403() { |
| 393 | let env = start().await; |
| 394 | let session = login(&env.http, "tahinli", PW).await; |
| 395 | let res = env |
| 396 | .http |
| 397 | .post_form( |
| 398 | "/settings/appearance", |
| 399 | &[("csrf", "forged"), ("theme", "dark"), ("language", "en")], |
| 400 | Some(&session), |
| 401 | ) |
| 402 | .await; |
| 403 | assert_eq!(res.status, 403); |
| 404 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 405 | assert_eq!( |
| 406 | selected_option(&page.body, "theme").as_deref(), |
| 407 | Some("light"), |
| 408 | "nothing saved" |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | #[tokio::test] |
| 413 | async fn unoffered_theme_or_language_is_rejected_and_nothing_is_saved() { |
| 414 | let env = start().await; |
| 415 | let session = login(&env.http, "tahinli", PW).await; |
| 416 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 417 | let csrf = input_value(&page.body, "csrf"); |
| 418 | for form in [ |
| 419 | [ |
| 420 | ("csrf", csrf.as_str()), |
| 421 | ("theme", "neon"), |
| 422 | ("language", "en"), |
| 423 | ], |
| 424 | [ |
| 425 | ("csrf", csrf.as_str()), |
| 426 | ("theme", "light"), |
| 427 | ("language", "de"), |
| 428 | ], |
| 429 | ] { |
| 430 | let res = env |
| 431 | .http |
| 432 | .post_form("/settings/appearance", &form, Some(&session)) |
| 433 | .await; |
| 434 | assert_ne!(res.status, 303, "bad choice must not redirect as saved"); |
| 435 | } |
| 436 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 437 | assert_eq!( |
| 438 | selected_option(&page.body, "theme").as_deref(), |
| 439 | Some("light") |
| 440 | ); |
| 441 | assert_eq!( |
| 442 | selected_option(&page.body, "language").as_deref(), |
| 443 | Some("auto") |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | #[tokio::test] |
| 448 | async fn anonymous_footer_cookie_sets_the_theme() { |
| 449 | let env = start().await; |
| 450 | let page = env.http.get("/login", None).await; |
| 451 | assert_eq!( |
| 452 | html_theme(&page.body).as_deref(), |
| 453 | Some("light"), |
| 454 | "no cookie defaults to light" |
| 455 | ); |
| 456 | assert!( |
| 457 | page.body.contains("action=\"/-/theme\""), |
| 458 | "anonymous footer offers the theme form" |
| 459 | ); |
| 460 | |
| 461 | let set = env |
| 462 | .http |
| 463 | .post_form("/-/theme", &[("theme", "light")], None) |
| 464 | .await; |
| 465 | assert_eq!(set.status, 303, "set theme: {}", set.body); |
| 466 | let cookie = cookie_named(&set, "forge_theme").expect("forge_theme cookie"); |
| 467 | assert_eq!(cookie, "forge_theme=light"); |
| 468 | |
| 469 | let page = env.http.get("/login", Some(&cookie)).await; |
| 470 | assert_eq!(html_theme(&page.body).as_deref(), Some("light")); |
| 471 | } |
| 472 | |
| 473 | #[tokio::test] |
| 474 | async fn anonymous_theme_cookie_ignores_unknown_values() { |
| 475 | let env = start().await; |
| 476 | let res = env |
| 477 | .http |
| 478 | .post_form("/-/theme", &[("theme", "neon")], None) |
| 479 | .await; |
| 480 | assert_ne!(res.status, 303); |
| 481 | assert!(cookie_named(&res, "forge_theme").is_none()); |
| 482 | } |
| 483 | |
| 484 | #[tokio::test] |
| 485 | async fn signed_in_preference_wins_over_the_anonymous_cookie() { |
| 486 | let env = start().await; |
| 487 | let session = login(&env.http, "tahinli", PW).await; |
| 488 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 489 | assert!( |
| 490 | !page.body.contains("class=\"theme-picker\""), |
| 491 | "signed-in viewers get no footer theme form" |
| 492 | ); |
| 493 | let cookie = format!("{session}; forge_theme=dark"); |
| 494 | let page = env.http.get("/settings/appearance", Some(&cookie)).await; |
| 495 | assert_eq!( |
| 496 | html_theme(&page.body).as_deref(), |
| 497 | Some("light"), |
| 498 | "a light user ignores the cookie" |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | #[tokio::test] |
| 503 | async fn appearance_page_needs_a_session() { |
| 504 | let env = start().await; |
| 505 | let res = env.http.get("/settings/appearance", None).await; |
| 506 | assert_eq!(res.status, 303); |
| 507 | assert!( |
| 508 | res.header(reqwest::header::LOCATION) |
| 509 | .is_some_and(|l| l.starts_with("/login")) |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | #[tokio::test] |
| 514 | async fn signed_in_toggle_saves_the_preference_and_returns_to_back() { |
| 515 | let env = start().await; |
| 516 | let session = login(&env.http, "tahinli", PW).await; |
| 517 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 518 | let csrf = input_value(&page.body, "csrf"); |
| 519 | let saved = env |
| 520 | .http |
| 521 | .post_form( |
| 522 | "/settings/appearance", |
| 523 | &[ |
| 524 | ("csrf", &csrf), |
| 525 | ("theme", "auto"), |
| 526 | ("accent_light", "sage"), |
| 527 | ("language", "tr"), |
| 528 | ], |
| 529 | Some(&session), |
| 530 | ) |
| 531 | .await; |
| 532 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 533 | |
| 534 | let toggle = env |
| 535 | .http |
| 536 | .post_form( |
| 537 | "/-/theme", |
| 538 | &[ |
| 539 | ("csrf", &csrf), |
| 540 | ("theme", "dark"), |
| 541 | ("back", "/explore?q=rust"), |
| 542 | ], |
| 543 | Some(&session), |
| 544 | ) |
| 545 | .await; |
| 546 | assert_eq!(toggle.status, 303, "toggle: {}", toggle.body); |
| 547 | assert_eq!( |
| 548 | toggle.header(reqwest::header::LOCATION).as_deref(), |
| 549 | Some("/explore?q=rust") |
| 550 | ); |
| 551 | assert!( |
| 552 | cookie_named(&toggle, "forge_theme").is_none(), |
| 553 | "no cookie for a signed-in user" |
| 554 | ); |
| 555 | |
| 556 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 557 | assert_eq!( |
| 558 | selected_option(&page.body, "theme").as_deref(), |
| 559 | Some("dark") |
| 560 | ); |
| 561 | assert_eq!( |
| 562 | selected_option(&page.body, "language").as_deref(), |
| 563 | Some("tr"), |
| 564 | "language is kept" |
| 565 | ); |
| 566 | } |
| 567 | |
| 568 | #[tokio::test] |
| 569 | async fn theme_toggle_with_an_unsafe_back_returns_to_the_root() { |
| 570 | let env = start().await; |
| 571 | let session = login(&env.http, "tahinli", PW).await; |
| 572 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 573 | let csrf = input_value(&page.body, "csrf"); |
| 574 | for back in ["//evil.com", "https://evil.com", "/\\evil"] { |
| 575 | let res = env |
| 576 | .http |
| 577 | .post_form( |
| 578 | "/-/theme", |
| 579 | &[("csrf", &csrf), ("theme", "light"), ("back", back)], |
| 580 | Some(&session), |
| 581 | ) |
| 582 | .await; |
| 583 | assert_eq!(res.status, 303, "{back}: {}", res.body); |
| 584 | assert_eq!( |
| 585 | res.header(reqwest::header::LOCATION).as_deref(), |
| 586 | Some("/"), |
| 587 | "back {back:?}" |
| 588 | ); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #[tokio::test] |
| 593 | async fn signed_in_theme_toggle_without_a_valid_csrf_is_refused() { |
| 594 | let env = start().await; |
| 595 | let session = login(&env.http, "tahinli", PW).await; |
| 596 | for form in [ |
| 597 | vec![("csrf", "forged"), ("theme", "dark")], |
| 598 | vec![("theme", "dark")], |
| 599 | ] { |
| 600 | let res = env.http.post_form("/-/theme", &form, Some(&session)).await; |
| 601 | assert_eq!(res.status, 403, "{form:?}: {}", res.body); |
| 602 | } |
| 603 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 604 | assert_eq!( |
| 605 | selected_option(&page.body, "theme").as_deref(), |
| 606 | Some("light"), |
| 607 | "nothing saved" |
| 608 | ); |
| 609 | } |
| 610 | |
| 611 | #[tokio::test] |
| 612 | async fn anonymous_theme_toggle_sets_the_cookie_and_returns_to_back() { |
| 613 | let env = start().await; |
| 614 | let set = env |
| 615 | .http |
| 616 | .post_form("/-/theme", &[("theme", "dark"), ("back", "/login")], None) |
| 617 | .await; |
| 618 | assert_eq!(set.status, 303, "set theme: {}", set.body); |
| 619 | assert_eq!( |
| 620 | set.header(reqwest::header::LOCATION).as_deref(), |
| 621 | Some("/login") |
| 622 | ); |
| 623 | let cookie = cookie_named(&set, "forge_theme").expect("forge_theme cookie"); |
| 624 | assert_eq!(cookie, "forge_theme=dark"); |
| 625 | } |
| 626 | |
| 627 | #[tokio::test] |
| 628 | async fn theme_button_cycles_light_dark_auto_light_for_signed_in_users() { |
| 629 | let env = start().await; |
| 630 | let session = login(&env.http, "tahinli", PW).await; |
| 631 | let back = "/settings/appearance"; |
| 632 | |
| 633 | // A new user starts on light, the default. |
| 634 | let page = env.http.get(back, Some(&session)).await; |
| 635 | let csrf = input_value(&page.body, "csrf"); |
| 636 | assert_eq!(html_theme(&page.body).as_deref(), Some("light")); |
| 637 | |
| 638 | let step = env |
| 639 | .http |
| 640 | .post_form( |
| 641 | "/-/theme", |
| 642 | &[("csrf", &csrf), ("theme", "dark"), ("back", back)], |
| 643 | Some(&session), |
| 644 | ) |
| 645 | .await; |
| 646 | assert_eq!(step.status, 303, "light to dark: {}", step.body); |
| 647 | let page = env.http.get(back, Some(&session)).await; |
| 648 | assert_eq!(html_theme(&page.body).as_deref(), Some("dark")); |
| 649 | |
| 650 | let step = env |
| 651 | .http |
| 652 | .post_form( |
| 653 | "/-/theme", |
| 654 | &[("csrf", &csrf), ("theme", "auto"), ("back", back)], |
| 655 | Some(&session), |
| 656 | ) |
| 657 | .await; |
| 658 | assert_eq!(step.status, 303, "dark to auto: {}", step.body); |
| 659 | let page = env.http.get(back, Some(&session)).await; |
| 660 | assert_eq!( |
| 661 | selected_option(&page.body, "theme").as_deref(), |
| 662 | Some("auto"), |
| 663 | "signed-in toggle to auto stores 'auto'" |
| 664 | ); |
| 665 | assert_eq!(html_theme(&page.body), None, "auto renders no data-theme"); |
| 666 | |
| 667 | let step = env |
| 668 | .http |
| 669 | .post_form( |
| 670 | "/-/theme", |
| 671 | &[("csrf", &csrf), ("theme", "light"), ("back", back)], |
| 672 | Some(&session), |
| 673 | ) |
| 674 | .await; |
| 675 | assert_eq!(step.status, 303, "auto to light: {}", step.body); |
| 676 | let page = env.http.get(back, Some(&session)).await; |
| 677 | assert_eq!(html_theme(&page.body).as_deref(), Some("light")); |
| 678 | } |
| 679 | |
| 680 | #[tokio::test] |
| 681 | async fn theme_button_cycles_for_anonymous_visitors_through_the_cookie() { |
| 682 | let env = start().await; |
| 683 | // No cookie is the default, light, so the button offers dark next. |
| 684 | let page = env.http.get("/login", None).await; |
| 685 | assert_eq!( |
| 686 | theme_button(&page.body), |
| 687 | ("dark".into(), "Theme: Light. Switch to Dark".into()) |
| 688 | ); |
| 689 | |
| 690 | let page = env.http.get("/login", Some("forge_theme=light")).await; |
| 691 | assert_eq!( |
| 692 | theme_button(&page.body), |
| 693 | ("dark".into(), "Theme: Light. Switch to Dark".into()) |
| 694 | ); |
| 695 | assert_eq!(html_theme(&page.body).as_deref(), Some("light")); |
| 696 | |
| 697 | let page = env.http.get("/login", Some("forge_theme=dark")).await; |
| 698 | assert_eq!( |
| 699 | theme_button(&page.body), |
| 700 | ( |
| 701 | "auto".into(), |
| 702 | "Theme: Dark. Switch to match the system".into() |
| 703 | ) |
| 704 | ); |
| 705 | } |
| 706 | |
| 707 | #[tokio::test] |
| 708 | async fn appearance_sidebar_side_saves_and_renders_on_html() { |
| 709 | let env = start().await; |
| 710 | let session = login(&env.http, "tahinli", PW).await; |
| 711 | |
| 712 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 713 | assert_eq!( |
| 714 | html_attr(&page.body, "data-sidebar").as_deref(), |
| 715 | Some("left") |
| 716 | ); |
| 717 | let csrf = input_value(&page.body, "csrf"); |
| 718 | |
| 719 | let saved = env |
| 720 | .http |
| 721 | .post_form( |
| 722 | "/settings/appearance", |
| 723 | &[ |
| 724 | ("csrf", &csrf), |
| 725 | ("theme", "light"), |
| 726 | ("language", "en"), |
| 727 | ("sidebar_side", "right"), |
| 728 | ], |
| 729 | Some(&session), |
| 730 | ) |
| 731 | .await; |
| 732 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 733 | |
| 734 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 735 | assert_eq!( |
| 736 | html_attr(&page.body, "data-sidebar").as_deref(), |
| 737 | Some("right") |
| 738 | ); |
| 739 | |
| 740 | // A save without the field keeps the side; a malformed side changes nothing. |
| 741 | let saved = env |
| 742 | .http |
| 743 | .post_form( |
| 744 | "/settings/appearance", |
| 745 | &[("csrf", &csrf), ("theme", "light"), ("language", "en")], |
| 746 | Some(&session), |
| 747 | ) |
| 748 | .await; |
| 749 | assert_eq!(saved.status, 303, "save: {}", saved.body); |
| 750 | for bad in ["", "top", "Right"] { |
| 751 | let res = env |
| 752 | .http |
| 753 | .post_form( |
| 754 | "/settings/appearance", |
| 755 | &[ |
| 756 | ("csrf", &csrf), |
| 757 | ("theme", "light"), |
| 758 | ("language", "en"), |
| 759 | ("sidebar_side", bad), |
| 760 | ], |
| 761 | Some(&session), |
| 762 | ) |
| 763 | .await; |
| 764 | assert_ne!(res.status, 303, "bad sidebar {bad:?} must not save"); |
| 765 | } |
| 766 | let page = env.http.get("/settings/appearance", Some(&session)).await; |
| 767 | assert_eq!( |
| 768 | html_attr(&page.body, "data-sidebar").as_deref(), |
| 769 | Some("right") |
| 770 | ); |
| 771 | } |