| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
use crate::harness::{BuildOptions, TestHarness}; |
| 38 |
use makenotwork::quasi; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
const SCREENS: &[(&str, &str, &str)] = &[ |
| 51 |
( |
| 52 |
"user_ssh_keys", |
| 53 |
quasi::ssh_keys::PATH, |
| 54 |
quasi::ssh_keys::REGION, |
| 55 |
), |
| 56 |
( |
| 57 |
"library_contacts", |
| 58 |
quasi::library_contacts::PATH, |
| 59 |
quasi::library_contacts::REGION, |
| 60 |
), |
| 61 |
|
| 62 |
|
| 63 |
( |
| 64 |
"buyer_contacts", |
| 65 |
quasi::buyer_contacts::PATH, |
| 66 |
"contacts-section", |
| 67 |
), |
| 68 |
( |
| 69 |
"payout_summary", |
| 70 |
quasi::payout_summary::PATH, |
| 71 |
quasi::payout_summary::REGION, |
| 72 |
), |
| 73 |
( |
| 74 |
"user_analytics", |
| 75 |
quasi::user_analytics::PATH, |
| 76 |
quasi::user_analytics::REGION, |
| 77 |
), |
| 78 |
( |
| 79 |
"library_communities", |
| 80 |
quasi::forum_memberships::LIBRARY_PATH, |
| 81 |
quasi::forum_memberships::LIBRARY_REGION, |
| 82 |
), |
| 83 |
( |
| 84 |
"user_forums", |
| 85 |
quasi::forum_memberships::SETTINGS_PATH, |
| 86 |
quasi::forum_memberships::SETTINGS_REGION, |
| 87 |
), |
| 88 |
]; |
| 89 |
|
| 90 |
|
| 91 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 92 |
struct Control { |
| 93 |
method: &'static str, |
| 94 |
address: String, |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
fn controls(html: &str) -> Vec<Control> { |
| 104 |
let mut found = Vec::new(); |
| 105 |
for (attr, method) in [ |
| 106 |
("hx-get=\"", "GET"), |
| 107 |
("hx-post=\"", "POST"), |
| 108 |
("hx-put=\"", "PUT"), |
| 109 |
("hx-delete=\"", "DELETE"), |
| 110 |
] { |
| 111 |
let mut rest = html; |
| 112 |
while let Some(at) = rest.find(attr) { |
| 113 |
rest = &rest[at + attr.len()..]; |
| 114 |
let Some(end) = rest.find('"') else { break }; |
| 115 |
let address = rest[..end].replace("&", "&"); |
| 116 |
rest = &rest[end..]; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
found.push(Control { method, address }); |
| 121 |
} |
| 122 |
} |
| 123 |
found |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
async fn viewing() -> TestHarness { |
| 131 |
let mut h = TestHarness::build(BuildOptions { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
mt_base_url: Some("http://127.0.0.1:9".to_owned()), |
| 138 |
internal_shared_secret: Some("press-the-buttons".to_owned()), |
| 139 |
..Default::default() |
| 140 |
}) |
| 141 |
.await; |
| 142 |
h.signup("presser", "presser@example.com", "password123") |
| 143 |
.await; |
| 144 |
h.login("presser", "password123").await; |
| 145 |
h.client.fetch_csrf_token().await; |
| 146 |
h |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#[tokio::test] |
| 156 |
async fn every_described_screen_answers_into_the_region_it_names() { |
| 157 |
for (screen, path, region) in SCREENS { |
| 158 |
let mut h = viewing().await; |
| 159 |
let resp = h.client.htmx_get(path).await; |
| 160 |
|
| 161 |
assert_eq!( |
| 162 |
resp.status, 200, |
| 163 |
"{screen} did not answer {path} (got {})", |
| 164 |
resp.status |
| 165 |
); |
| 166 |
let retarget = resp |
| 167 |
.headers |
| 168 |
.get("HX-Retarget") |
| 169 |
.and_then(|v| v.to_str().ok()) |
| 170 |
.unwrap_or_default(); |
| 171 |
assert_eq!( |
| 172 |
retarget, |
| 173 |
format!("#{region}"), |
| 174 |
"{screen} answered without naming {region} (got {retarget:?})" |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
#[tokio::test] |
| 185 |
async fn a_screen_offers_something() { |
| 186 |
|
| 187 |
|
| 188 |
let mut h = viewing().await; |
| 189 |
let html = h.client.htmx_get("/dashboard/tabs/ssh-keys").await.text; |
| 190 |
|
| 191 |
let found = controls(&html); |
| 192 |
assert!( |
| 193 |
found.len() >= 3, |
| 194 |
"the ssh-keys screen emitted {} controls: {found:?}", |
| 195 |
found.len() |
| 196 |
); |
| 197 |
assert!( |
| 198 |
found.iter().any(|c| c.method == "POST"), |
| 199 |
"no write among {found:?}" |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#[tokio::test] |
| 211 |
async fn every_described_control_is_answered() { |
| 212 |
for (screen, path, _) in SCREENS { |
| 213 |
let mut h = viewing().await; |
| 214 |
let html = h.client.htmx_get(path).await.text; |
| 215 |
|
| 216 |
for control in controls(&html) { |
| 217 |
let resp = match control.method { |
| 218 |
"GET" => h.client.htmx_get(&control.address).await, |
| 219 |
"POST" => h.client.htmx_post_form(&control.address, "").await, |
| 220 |
"PUT" => h.client.htmx_put_form(&control.address, "").await, |
| 221 |
"DELETE" => h.client.htmx_delete(&control.address).await, |
| 222 |
other => panic!("unhandled method {other}"), |
| 223 |
}; |
| 224 |
let code = resp.status.as_u16(); |
| 225 |
assert!( |
| 226 |
code != 404 && code != 405, |
| 227 |
"{screen}: [{}] {} answered {code}, so the control renders and \ |
| 228 |
does nothing. This is the S3 failure class.", |
| 229 |
control.method, |
| 230 |
control.address |
| 231 |
); |
| 232 |
assert!( |
| 233 |
code < 500, |
| 234 |
"{screen}: [{}] {} answered {code}", |
| 235 |
control.method, |
| 236 |
control.address |
| 237 |
); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
const SEED_KEY: &str = |
| 247 |
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB2n4ZVGJoGZ8pM5vJXVv0kL3T5V7wQ9dNqR8mY1uH6c"; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
async fn seed_a_deletable_row(h: &mut TestHarness, screen: &str) { |
| 259 |
if screen != "user_ssh_keys" { |
| 260 |
return; |
| 261 |
} |
| 262 |
let added = h |
| 263 |
.client |
| 264 |
.post_form( |
| 265 |
"/api/users/me/ssh-keys", |
| 266 |
&format!("public_key={}&label=fw13", urlencoding::encode(SEED_KEY)), |
| 267 |
) |
| 268 |
.await; |
| 269 |
assert_eq!( |
| 270 |
added.status, 200, |
| 271 |
"seeding a key failed: {} {}", |
| 272 |
added.status, added.text |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
#[tokio::test] |
| 282 |
async fn pressing_remove_on_a_key_removes_it_and_redraws_the_pane() { |
| 283 |
let mut h = viewing().await; |
| 284 |
|
| 285 |
seed_a_deletable_row(&mut h, "user_ssh_keys").await; |
| 286 |
|
| 287 |
let html = h.client.htmx_get("/dashboard/tabs/ssh-keys").await.text; |
| 288 |
assert!(html.contains("fw13"), "the key is on the screen: {html}"); |
| 289 |
|
| 290 |
let remove = controls(&html) |
| 291 |
.into_iter() |
| 292 |
.find(|c| c.method == "DELETE" && c.address.contains("/keys/")) |
| 293 |
.expect("the description emitted a Remove control"); |
| 294 |
|
| 295 |
let resp = h.client.htmx_delete(&remove.address).await; |
| 296 |
assert_eq!( |
| 297 |
resp.status, 200, |
| 298 |
"pressing Remove answered {} at {}", |
| 299 |
resp.status, remove.address |
| 300 |
); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
assert_eq!( |
| 310 |
resp.headers |
| 311 |
.get("HX-Retarget") |
| 312 |
.and_then(|v| v.to_str().ok()) |
| 313 |
.unwrap_or_default(), |
| 314 |
format!("#{}", quasi::ssh_keys::REGION), |
| 315 |
"the answer did not name the pane it changed" |
| 316 |
); |
| 317 |
assert!( |
| 318 |
!resp.text.contains("fw13"), |
| 319 |
"the removed key is still drawn: {}", |
| 320 |
resp.text |
| 321 |
); |
| 322 |
assert!( |
| 323 |
resp.text.contains("No SSH keys registered."), |
| 324 |
"the answer is the pane, empty: {}", |
| 325 |
resp.text |
| 326 |
); |
| 327 |
|
| 328 |
|
| 329 |
let after = h.client.htmx_get("/dashboard/tabs/ssh-keys").await.text; |
| 330 |
assert!(!after.contains("fw13"), "the key came back: {after}"); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#[tokio::test] |
| 349 |
async fn a_described_delete_answers_with_the_screen_it_is_on() { |
| 350 |
let mut pressed = 0_usize; |
| 351 |
for (screen, path, region) in SCREENS { |
| 352 |
let mut h = viewing().await; |
| 353 |
seed_a_deletable_row(&mut h, screen).await; |
| 354 |
let html = h.client.htmx_get(path).await.text; |
| 355 |
|
| 356 |
for control in controls(&html).into_iter().filter(|c| c.method == "DELETE") { |
| 357 |
let resp = h.client.htmx_delete(&control.address).await; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if resp.status == 404 { |
| 370 |
continue; |
| 371 |
} |
| 372 |
assert_eq!( |
| 373 |
resp.status, 200, |
| 374 |
"{screen}: DELETE {} answered {} rather than the region", |
| 375 |
control.address, resp.status |
| 376 |
); |
| 377 |
let retarget = resp |
| 378 |
.headers |
| 379 |
.get("HX-Retarget") |
| 380 |
.and_then(|v| v.to_str().ok()) |
| 381 |
.unwrap_or_default(); |
| 382 |
assert_eq!( |
| 383 |
retarget, |
| 384 |
format!("#{region}"), |
| 385 |
"{screen}: DELETE {} succeeded without naming {region}", |
| 386 |
control.address |
| 387 |
); |
| 388 |
pressed += 1; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
assert!( |
| 396 |
pressed > 0, |
| 397 |
"no described DELETE was pressed on any of the {} screens, so this test \ |
| 398 |
asserted nothing. Seed a row for whichever screen lost its fixture.", |
| 399 |
SCREENS.len() |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
#[tokio::test] |
| 409 |
async fn every_described_screen_is_pressed() { |
| 410 |
let mut pressed: Vec<&str> = SCREENS.iter().map(|(_, path, _)| *path).collect(); |
| 411 |
pressed.sort_unstable(); |
| 412 |
let mut mountable = quasi::PATHS.to_vec(); |
| 413 |
mountable.sort_unstable(); |
| 414 |
|
| 415 |
assert_eq!( |
| 416 |
pressed, mountable, |
| 417 |
"the pressed screens and the mountable ones have diverged" |
| 418 |
); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
#[tokio::test] |
| 430 |
async fn every_document_screen_serves_a_whole_document() { |
| 431 |
for path in quasi::DOCUMENT_PATHS { |
| 432 |
let mut h = viewing().await; |
| 433 |
let resp = h.client.get(path).await; |
| 434 |
|
| 435 |
assert_eq!(resp.status, 200, "{path} did not answer"); |
| 436 |
let html = resp.text; |
| 437 |
assert!(html.starts_with("<!doctype html>"), "{path}: {html}"); |
| 438 |
assert!(html.contains("/static/style.css"), "{path} lost its sheets"); |
| 439 |
assert!( |
| 440 |
html.contains("role=\"banner\""), |
| 441 |
"{path} lost the site header" |
| 442 |
); |
| 443 |
assert!( |
| 444 |
html.contains("name=\"csrf-token\""), |
| 445 |
"{path} lost the token meta the classic scripts read" |
| 446 |
); |
| 447 |
assert!(html.contains("skip-to-main"), "{path} lost its skip link"); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
#[tokio::test] |
| 458 |
async fn a_signed_out_reader_is_offered_the_way_in() { |
| 459 |
for path in quasi::DOCUMENT_PATHS { |
| 460 |
let mut h = TestHarness::new().await; |
| 461 |
let resp = h.client.get(path).await; |
| 462 |
|
| 463 |
assert_eq!( |
| 464 |
resp.status, 401, |
| 465 |
"{path} refused a stranger with the wrong code" |
| 466 |
); |
| 467 |
assert!( |
| 468 |
resp.text.contains("href=\"/login\""), |
| 469 |
"{path}: {}", |
| 470 |
resp.text |
| 471 |
); |
| 472 |
assert!( |
| 473 |
resp.text.contains("href=\"/join\""), |
| 474 |
"{path}: {}", |
| 475 |
resp.text |
| 476 |
); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
#[tokio::test] |
| 483 |
async fn a_described_screen_offers_the_shortcuts_key() { |
| 484 |
let mut h = viewing().await; |
| 485 |
let html = h.client.get("/pricing").await.text; |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
assert!( |
| 490 |
html.contains("data-chrome"), |
| 491 |
"the chrome is emitted: {html:.400}" |
| 492 |
); |
| 493 |
assert!( |
| 494 |
html.contains("Keyboard shortcuts"), |
| 495 |
"the binding is labelled: {html:.400}" |
| 496 |
); |
| 497 |
assert!( |
| 498 |
html.contains("/shortcuts"), |
| 499 |
"and it reaches the listing: {html:.400}" |
| 500 |
); |
| 501 |
|
| 502 |
|
| 503 |
assert!( |
| 504 |
!html.contains("toggleShortcutsHelp"), |
| 505 |
"the data-action link was retired: {html:.400}" |
| 506 |
); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
#[tokio::test] |
| 512 |
async fn the_shortcuts_listing_names_every_key_this_site_binds() { |
| 513 |
let mut h = viewing().await; |
| 514 |
let html = h.client.htmx_get("/shortcuts").await.text; |
| 515 |
|
| 516 |
|
| 517 |
assert!(html.contains('?'), "the described key: {html:.400}"); |
| 518 |
|
| 519 |
for key in ["Cmd+K", "Esc", "Cmd+S"] { |
| 520 |
assert!(html.contains(key), "{key} is listed: {html:.400}"); |
| 521 |
} |
| 522 |
} |
| 523 |
|