The suite reads its tokens from the outbox
Browse files at this commitd4dffdbc0aab520109dc3f07904542031255d42aParent: 3f29931- test
- the suite reads its tokens from the outbox
1 changed file +138 -27
- crates/izlek-web/tests/http.rs +138 -27
crates/izlek-web/tests/http.rsmodified+138 -27
| @@ -17,7 +17,7 @@ use axum::body::Body; | |||
| 17 | use axum::http::{HeaderValue, Request, StatusCode, header}; | 17 | use axum::http::{HeaderValue, Request, StatusCode, header}; |
| 18 | use izlek_core::Role; | 18 | use izlek_core::Role; |
| 19 | use izlek_core::accounts::Accounts; | 19 | use izlek_core::accounts::Accounts; |
| 20 | -use izlek_core::store::TursoStore; | 20 | +use izlek_core::store::{SendKind, Store, TursoStore}; |
| 21 | use izlek_web::server::SESSION_COOKIE; | 21 | use izlek_web::server::SESSION_COOKIE; |
| 22 | use leptos::prelude::LeptosOptions; | 22 | use leptos::prelude::LeptosOptions; |
| 23 | use leptos::server_fn::ServerFn; | 23 | use leptos::server_fn::ServerFn; |
| @@ -28,22 +28,25 @@ use uuid::Uuid; | |||
| 28 | struct App { | 28 | struct App { |
| 29 | dir: PathBuf, | 29 | dir: PathBuf, |
| 30 | router: Router, | 30 | router: Router, |
| 31 | + store: Arc<dyn Store>, | ||
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | impl App { | 34 | impl App { |
| 34 | async fn open() -> Self { | 35 | async fn open() -> Self { |
| 35 | let dir = std::env::temp_dir().join(format!("izlek-http-{}", Uuid::new_v4())); | 36 | let dir = std::env::temp_dir().join(format!("izlek-http-{}", Uuid::new_v4())); |
| 36 | std::fs::create_dir_all(&dir).unwrap(); | 37 | std::fs::create_dir_all(&dir).unwrap(); |
| 37 | - let store = TursoStore::open(dir.join("izlek.db").to_str().unwrap()) | 38 | + let store: Arc<dyn Store> = Arc::new( |
| 38 | - .await | 39 | + TursoStore::open(dir.join("izlek.db").to_str().unwrap()) |
| 39 | - .unwrap(); | 40 | + .await |
| 41 | + .unwrap(), | ||
| 42 | + ); | ||
| 40 | let options = LeptosOptions::builder().output_name("izlek").build(); | 43 | let options = LeptosOptions::builder().output_name("izlek").build(); |
| 41 | let router = izlek_web::server::router( | 44 | let router = izlek_web::server::router( |
| 42 | - Accounts::new(Arc::new(store), "http://127.0.0.1:3000"), | 45 | + Accounts::new(store.clone(), "http://127.0.0.1:3000"), |
| 43 | izlek_web::server::Mail::silent(), | 46 | izlek_web::server::Mail::silent(), |
| 44 | options, | 47 | options, |
| 45 | ); | 48 | ); |
| 46 | - Self { dir, router } | 49 | + Self { dir, router, store } |
| 47 | } | 50 | } |
| 48 | 51 | ||
| 49 | /// Like `open`, but with a live mail engine reading the workspace's SMTP | 52 | /// Like `open`, but with a live mail engine reading the workspace's SMTP |
| @@ -64,11 +67,11 @@ impl App { | |||
| 64 | )); | 67 | )); |
| 65 | let options = LeptosOptions::builder().output_name("izlek").build(); | 68 | let options = LeptosOptions::builder().output_name("izlek").build(); |
| 66 | let router = izlek_web::server::router( | 69 | let router = izlek_web::server::router( |
| 67 | - Accounts::new(store, "https://izlek.sh"), | 70 | + Accounts::new(store.clone(), "https://izlek.sh"), |
| 68 | izlek_web::server::Mail::sending(engine), | 71 | izlek_web::server::Mail::sending(engine), |
| 69 | options, | 72 | options, |
| 70 | ); | 73 | ); |
| 71 | - Self { dir, router } | 74 | + Self { dir, router, store } |
| 72 | } | 75 | } |
| 73 | 76 | ||
| 74 | /// Posts a form to a server function, as the browser does, and returns the | 77 | /// Posts a form to a server function, as the browser does, and returns the |
| @@ -366,6 +369,25 @@ async fn admin(app: &App) -> String { | |||
| 366 | answer.session.expect("claiming set no session cookie") | 369 | answer.session.expect("claiming set no session cookie") |
| 367 | } | 370 | } |
| 368 | 371 | ||
| 372 | +/// The join token no longer rides the server function's answer — only the | ||
| 373 | +/// invitee's address does. It rides the invite mail this function digs out of | ||
| 374 | +/// the outbox instead: the newest pending invite queued for that address. | ||
| 375 | +async fn queued_join_token(app: &App, email: &str) -> String { | ||
| 376 | + let sends = app.store.mail_queue(10).await.unwrap(); | ||
| 377 | + let body = sends | ||
| 378 | + .into_iter() | ||
| 379 | + .rev() | ||
| 380 | + .find(|send| { | ||
| 381 | + send.kind == SendKind::Invite && send.rule_id.is_none() && send.recipient == email | ||
| 382 | + }) | ||
| 383 | + .and_then(|send| send.body) | ||
| 384 | + .expect("no invite mail queued for {email}"); | ||
| 385 | + body.rsplit_once("/join/") | ||
| 386 | + .and_then(|(_, rest)| rest.split_whitespace().next()) | ||
| 387 | + .expect("no invitation link in the mail body") | ||
| 388 | + .to_string() | ||
| 389 | +} | ||
| 390 | + | ||
| 369 | /// Invites someone in the given role and signs them in, returning their cookie. | 391 | /// Invites someone in the given role and signs them in, returning their cookie. |
| 370 | async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> String { | 392 | async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> String { |
| 371 | let role = match role { | 393 | let role = match role { |
| @@ -381,12 +403,7 @@ async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> | |||
| 381 | ) | 403 | ) |
| 382 | .await; | 404 | .await; |
| 383 | assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); | 405 | assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); |
| 384 | - let token = answer | 406 | + let token = queued_join_token(app, email).await; |
| 385 | - .body | ||
| 386 | - .rsplit_once("/join/") | ||
| 387 | - .and_then(|(_, rest)| rest.split('"').next()) | ||
| 388 | - .expect("no invitation link in {answer.body}") | ||
| 389 | - .to_string(); | ||
| 390 | 407 | ||
| 391 | let answer = app | 408 | let answer = app |
| 392 | .post( | 409 | .post( |
| @@ -403,6 +420,107 @@ async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> | |||
| 403 | answer.session.expect("first sign-in set no session cookie") | 420 | answer.session.expect("first sign-in set no session cookie") |
| 404 | } | 421 | } |
| 405 | 422 | ||
| 423 | +/// Inviting a member never hands the join link to the browser: the address | ||
| 424 | +/// comes back, and the link travels only in the mail the invitee gets. | ||
| 425 | +#[tokio::test] | ||
| 426 | +async fn adding_a_member_mails_them_the_link() { | ||
| 427 | + let app = App::open().await; | ||
| 428 | + let admin_cookie = admin(&app).await; | ||
| 429 | + | ||
| 430 | + let answer = app | ||
| 431 | + .post( | ||
| 432 | + path::<izlek_web::auth::InviteMember>(), | ||
| 433 | + Some(&admin_cookie), | ||
| 434 | + &[ | ||
| 435 | + ("email", "nour@izlek.sh"), | ||
| 436 | + ("display_name", "Nour"), | ||
| 437 | + ("role", "member"), | ||
| 438 | + ], | ||
| 439 | + ) | ||
| 440 | + .await; | ||
| 441 | + assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); | ||
| 442 | + assert!(answer.body.contains("nour@izlek.sh"), "{}", answer.body); | ||
| 443 | + assert!(!answer.body.contains("/join/"), "{}", answer.body); | ||
| 444 | + | ||
| 445 | + let sends = app.store.mail_queue(10).await.unwrap(); | ||
| 446 | + let invites: Vec<_> = sends | ||
| 447 | + .iter() | ||
| 448 | + .filter(|send| { | ||
| 449 | + send.kind == SendKind::Invite | ||
| 450 | + && send.rule_id.is_none() | ||
| 451 | + && send.recipient == "nour@izlek.sh" | ||
| 452 | + }) | ||
| 453 | + .collect(); | ||
| 454 | + assert_eq!(invites.len(), 1, "{invites:?}"); | ||
| 455 | + assert!( | ||
| 456 | + invites[0].body.as_deref().unwrap_or_default().contains("/join/"), | ||
| 457 | + "{:?}", | ||
| 458 | + invites[0] | ||
| 459 | + ); | ||
| 460 | +} | ||
| 461 | + | ||
| 462 | +/// A resend queues a second invite mail rather than replacing the first — | ||
| 463 | +/// the outbox keeps both attempts. | ||
| 464 | +#[tokio::test] | ||
| 465 | +async fn a_resend_queues_another_mail() { | ||
| 466 | + let app = App::open().await; | ||
| 467 | + let admin_cookie = admin(&app).await; | ||
| 468 | + | ||
| 469 | + let invitation = app | ||
| 470 | + .post( | ||
| 471 | + path::<izlek_web::auth::InviteMember>(), | ||
| 472 | + Some(&admin_cookie), | ||
| 473 | + &[ | ||
| 474 | + ("email", "sena@izlek.sh"), | ||
| 475 | + ("display_name", "Sena"), | ||
| 476 | + ("role", "member"), | ||
| 477 | + ], | ||
| 478 | + ) | ||
| 479 | + .await; | ||
| 480 | + assert_eq!(invitation.status, StatusCode::OK, "{}", invitation.body); | ||
| 481 | + | ||
| 482 | + let list = app | ||
| 483 | + .post( | ||
| 484 | + path::<izlek_web::settings::CurrentSettings>(), | ||
| 485 | + Some(&admin_cookie), | ||
| 486 | + &[], | ||
| 487 | + ) | ||
| 488 | + .await; | ||
| 489 | + let sena_id = list | ||
| 490 | + .body | ||
| 491 | + .split_once("sena@izlek.sh") | ||
| 492 | + .map(|(before, _)| before) | ||
| 493 | + .and_then(|before| before.rsplit_once("\"id\":\"")) | ||
| 494 | + .and_then(|(_, rest)| rest.split('"').next()) | ||
| 495 | + .expect("no member id") | ||
| 496 | + .to_string(); | ||
| 497 | + | ||
| 498 | + let count = |sends: &[izlek_core::store::MailSend]| { | ||
| 499 | + sends | ||
| 500 | + .iter() | ||
| 501 | + .filter(|send| { | ||
| 502 | + send.kind == SendKind::Invite | ||
| 503 | + && send.rule_id.is_none() | ||
| 504 | + && send.recipient == "sena@izlek.sh" | ||
| 505 | + }) | ||
| 506 | + .count() | ||
| 507 | + }; | ||
| 508 | + let before = count(&app.store.mail_queue(10).await.unwrap()); | ||
| 509 | + assert_eq!(before, 1); | ||
| 510 | + | ||
| 511 | + let resent = app | ||
| 512 | + .post( | ||
| 513 | + path::<izlek_web::settings::ResendLink>(), | ||
| 514 | + Some(&admin_cookie), | ||
| 515 | + &[("user_id", &sena_id)], | ||
| 516 | + ) | ||
| 517 | + .await; | ||
| 518 | + assert_eq!(resent.body, "{\"Ok\":\"sena@izlek.sh\"}", "{}", resent.body); | ||
| 519 | + | ||
| 520 | + let after = count(&app.store.mail_queue(10).await.unwrap()); | ||
| 521 | + assert_eq!(after, 2); | ||
| 522 | +} | ||
| 523 | + | ||
| 406 | /// The id of the board's first column, read the way the board page reads it. | 524 | /// The id of the board's first column, read the way the board page reads it. |
| 407 | async fn first_column(app: &App, cookie: &str) -> String { | 525 | async fn first_column(app: &App, cookie: &str) -> String { |
| 408 | let answer = app | 526 | let answer = app |
| @@ -1561,12 +1679,8 @@ async fn a_resent_link_opens_the_same_account() { | |||
| 1561 | &[("user_id", &mert_id)], | 1679 | &[("user_id", &mert_id)], |
| 1562 | ) | 1680 | ) |
| 1563 | .await; | 1681 | .await; |
| 1564 | - let token = answer | 1682 | + assert_eq!(answer.body, "{\"Ok\":\"mert@izlek.sh\"}", "{}", answer.body); |
| 1565 | - .body | 1683 | + let token = queued_join_token(&app, "mert@izlek.sh").await; |
| 1566 | - .rsplit_once("/join/") | ||
| 1567 | - .and_then(|(_, rest)| rest.split('"').next()) | ||
| 1568 | - .expect("no link in the answer") | ||
| 1569 | - .to_string(); | ||
| 1570 | 1684 | ||
| 1571 | let redeemed = app | 1685 | let redeemed = app |
| 1572 | .post( | 1686 | .post( |
| @@ -1599,12 +1713,7 @@ async fn an_invitation_names_the_admin_who_made_it_and_not_the_invitee() { | |||
| 1599 | ) | 1713 | ) |
| 1600 | .await; | 1714 | .await; |
| 1601 | assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); | 1715 | assert_eq!(answer.status, StatusCode::OK, "{}", answer.body); |
| 1602 | - let token = answer | 1716 | + let token = queued_join_token(&app, "grace@izlek.sh").await; |
| 1603 | - .body | ||
| 1604 | - .rsplit_once("/join/") | ||
| 1605 | - .and_then(|(_, rest)| rest.split('"').next()) | ||
| 1606 | - .expect("no invitation link") | ||
| 1607 | - .to_string(); | ||
| 1608 | 1717 | ||
| 1609 | let answer = app | 1718 | let answer = app |
| 1610 | .post( | 1719 | .post( |
| @@ -2659,7 +2768,9 @@ async fn an_admin_reads_the_logs() { | |||
| 2659 | assert_eq!(moved.body, "null", "{}", moved.body); | 2768 | assert_eq!(moved.body, "null", "{}", moved.body); |
| 2660 | 2769 | ||
| 2661 | let snapshot = until_logs_contains(&app, &admin_cookie, "\"outcome\":\"nobody to mail\"").await; | 2770 | let snapshot = until_logs_contains(&app, &admin_cookie, "\"outcome\":\"nobody to mail\"").await; |
| 2662 | - assert!(snapshot.contains("\"queue\":[]"), "{}", snapshot); | 2771 | + // The queue still carries Emre's invite mail — unrelated to this rule — |
| 2772 | + // so the check is that the rule itself queued nothing, not an empty queue. | ||
| 2773 | + assert!(!snapshot.contains("\"subject\":\"Task completed\""), "{}", snapshot); | ||
| 2663 | 2774 | ||
| 2664 | // The admin drops it back and moves it again: this time the mover is not | 2775 | // The admin drops it back and moves it again: this time the mover is not |
| 2665 | // the assignee, so the rule owes Emre a mail. With no sender configured | 2776 | // the assignee, so the rule owes Emre a mail. With no sender configured |