forge
Explore
Log in

Don't you dare go Hollow

The suite reads its tokens from the outbox

Browse files at this commit

d4dffdbc0aab520109dc3f07904542031255d42aAhmet Kaan GümüşParent: 3f29931
  • test
    • the suite reads its tokens from the outbox
Show full message
✅ test(web): the suite reads its tokens from the outbox
crates/izlek-web/tests/http.rsmodified+138 -27
@@ -17,7 +17,7 @@ use axum::body::Body;
1717 use axum::http::{HeaderValue, Request, StatusCode, header};
1818 use izlek_core::Role;
1919 use izlek_core::accounts::Accounts;
20-use izlek_core::store::TursoStore;
20+use izlek_core::store::{SendKind, Store, TursoStore};
2121 use izlek_web::server::SESSION_COOKIE;
2222 use leptos::prelude::LeptosOptions;
2323 use leptos::server_fn::ServerFn;
@@ -28,22 +28,25 @@ use uuid::Uuid;
2828 struct App {
2929 dir: PathBuf,
3030 router: Router,
31+ store: Arc<dyn Store>,
3132 }
3233
3334 impl App {
3435 async fn open() -> Self {
3536 let dir = std::env::temp_dir().join(format!("izlek-http-{}", Uuid::new_v4()));
3637 std::fs::create_dir_all(&dir).unwrap();
37- let store = TursoStore::open(dir.join("izlek.db").to_str().unwrap())
38- .await
39- .unwrap();
38+ let store: Arc<dyn Store> = Arc::new(
39+ TursoStore::open(dir.join("izlek.db").to_str().unwrap())
40+ .await
41+ .unwrap(),
42+ );
4043 let options = LeptosOptions::builder().output_name("izlek").build();
4144 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"),
4346 izlek_web::server::Mail::silent(),
4447 options,
4548 );
46- Self { dir, router }
49+ Self { dir, router, store }
4750 }
4851
4952 /// Like `open`, but with a live mail engine reading the workspace's SMTP
@@ -64,11 +67,11 @@ impl App {
6467 ));
6568 let options = LeptosOptions::builder().output_name("izlek").build();
6669 let router = izlek_web::server::router(
67- Accounts::new(store, "https://izlek.sh"),
70+ Accounts::new(store.clone(), "https://izlek.sh"),
6871 izlek_web::server::Mail::sending(engine),
6972 options,
7073 );
71- Self { dir, router }
74+ Self { dir, router, store }
7275 }
7376
7477 /// Posts a form to a server function, as the browser does, and returns the
@@ -366,6 +369,25 @@ async fn admin(app: &App) -> String {
366369 answer.session.expect("claiming set no session cookie")
367370 }
368371
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+
369391 /// Invites someone in the given role and signs them in, returning their cookie.
370392 async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) -> String {
371393 let role = match role {
@@ -381,12 +403,7 @@ async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) ->
381403 )
382404 .await;
383405 assert_eq!(answer.status, StatusCode::OK, "{}", answer.body);
384- let token = answer
385- .body
386- .rsplit_once("/join/")
387- .and_then(|(_, rest)| rest.split('"').next())
388- .expect("no invitation link in {answer.body}")
389- .to_string();
406+ let token = queued_join_token(app, email).await;
390407
391408 let answer = app
392409 .post(
@@ -403,6 +420,107 @@ async fn invited(app: &App, admin: &str, email: &str, name: &str, role: Role) ->
403420 answer.session.expect("first sign-in set no session cookie")
404421 }
405422
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+
406524 /// The id of the board's first column, read the way the board page reads it.
407525 async fn first_column(app: &App, cookie: &str) -> String {
408526 let answer = app
@@ -1561,12 +1679,8 @@ async fn a_resent_link_opens_the_same_account() {
15611679 &[("user_id", &mert_id)],
15621680 )
15631681 .await;
1564- let token = answer
1565- .body
1566- .rsplit_once("/join/")
1567- .and_then(|(_, rest)| rest.split('"').next())
1568- .expect("no link in the answer")
1569- .to_string();
1682+ assert_eq!(answer.body, "{\"Ok\":\"mert@izlek.sh\"}", "{}", answer.body);
1683+ let token = queued_join_token(&app, "mert@izlek.sh").await;
15701684
15711685 let redeemed = app
15721686 .post(
@@ -1599,12 +1713,7 @@ async fn an_invitation_names_the_admin_who_made_it_and_not_the_invitee() {
15991713 )
16001714 .await;
16011715 assert_eq!(answer.status, StatusCode::OK, "{}", answer.body);
1602- let token = answer
1603- .body
1604- .rsplit_once("/join/")
1605- .and_then(|(_, rest)| rest.split('"').next())
1606- .expect("no invitation link")
1607- .to_string();
1716+ let token = queued_join_token(&app, "grace@izlek.sh").await;
16081717
16091718 let answer = app
16101719 .post(
@@ -2659,7 +2768,9 @@ async fn an_admin_reads_the_logs() {
26592768 assert_eq!(moved.body, "null", "{}", moved.body);
26602769
26612770 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);
26632774
26642775 // The admin drops it back and moves it again: this time the mover is not
26652776 // the assignee, so the rule owes Emre a mail. With no sender configured