|
| 1 | +//! Closed sign-ups: an existing user signs in as before; someone new goes on the waitlist with no account, |
| 2 | +//! organization, session or email, and so never reaches an MCP client's consent step or gets a token. |
| 3 | +
|
| 4 | +use loco_rs::testing::prelude::*; |
| 5 | +use serial_test::serial; |
| 6 | +use visualci_server::{app::App, models::users}; |
| 7 | + |
| 8 | +/// Turns sign-ups off for one test, and back on when it ends (even when it fails). |
| 9 | +struct Closed; |
| 10 | + |
| 11 | +impl Closed { |
| 12 | + fn new() -> Self { |
| 13 | + // SAFETY: the tests here run one at a time (#[serial]), and nothing else reads the environment meanwhile. |
| 14 | + unsafe { std::env::set_var("VISUALCI_WAITLIST", "1") }; |
| 15 | + Self |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl Drop for Closed { |
| 20 | + fn drop(&mut self) { |
| 21 | + // SAFETY: as above. |
| 22 | + unsafe { std::env::remove_var("VISUALCI_WAITLIST") }; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +fn sign_in(email: &str, id: &str, login: &str, return_to: Option<&str>) -> serde_json::Value { |
| 27 | + serde_json::json!({ "email": email, "forge": { "id": id, "login": login }, "return_to": return_to }) |
| 28 | +} |
| 29 | + |
| 30 | +async fn waitlist_rows(ctx: &loco_rs::prelude::AppContext, login: &str) -> Vec<(String, Option<String>)> { |
| 31 | + use sea_orm::{ConnectionTrait, DbBackend, Statement}; |
| 32 | + ctx.db |
| 33 | + .query_all_raw(Statement::from_sql_and_values( |
| 34 | + DbBackend::Postgres, |
| 35 | + "SELECT source, email FROM waitlist WHERE login = $1", |
| 36 | + [login.into()], |
| 37 | + )) |
| 38 | + .await |
| 39 | + .unwrap() |
| 40 | + .into_iter() |
| 41 | + .map(|r| (r.try_get("", "source").unwrap(), r.try_get("", "email").unwrap())) |
| 42 | + .collect() |
| 43 | +} |
| 44 | + |
| 45 | +#[tokio::test] |
| 46 | +#[serial] |
| 47 | +async fn an_existing_user_signs_in_and_someone_new_goes_on_the_waitlist() { |
| 48 | + request::<App, _, _>(|request, ctx| async move { |
| 49 | + let suffix = &uuid::Uuid::new_v4().to_string()[..8]; |
| 50 | + let (old_email, old_login) = (format!("old-{suffix}@example.com"), format!("old-{suffix}")); |
| 51 | + // An account made while sign-ups were open |
| 52 | + let r = |
| 53 | + request.post("/auth/dev-login").json(&sign_in(&old_email, &format!("1{suffix}"), &old_login, None)).await; |
| 54 | + assert_eq!(r.status_code(), 200, "{}", r.text()); |
| 55 | + |
| 56 | + let _closed = Closed::new(); |
| 57 | + // They still sign in, and nothing goes on the waitlist for them |
| 58 | + let r = |
| 59 | + request.post("/auth/dev-login").json(&sign_in(&old_email, &format!("1{suffix}"), &old_login, None)).await; |
| 60 | + assert_eq!(r.status_code(), 200, "{}", r.text()); |
| 61 | + assert!(r.text().contains("\"ok\""), "signed in: {}", r.text()); |
| 62 | + assert!(waitlist_rows(&ctx, &old_login).await.is_empty()); |
| 63 | + |
| 64 | + // Someone new: the page, a row, and no account or session |
| 65 | + let (new_email, new_login) = (format!("new-{suffix}@example.com"), format!("new-{suffix}")); |
| 66 | + let r = |
| 67 | + request.post("/auth/dev-login").json(&sign_in(&new_email, &format!("2{suffix}"), &new_login, None)).await; |
| 68 | + assert_eq!(r.status_code(), 200, "{}", r.text()); |
| 69 | + assert!( |
| 70 | + r.text().contains(&format!("VisualCI opens in about 48 hours. We'll email you at {new_email}.")), |
| 71 | + "{}", |
| 72 | + r.text() |
| 73 | + ); |
| 74 | + assert!(r.headers().get("set-cookie").is_none(), "no session"); |
| 75 | + assert!(users::Model::find_by_email(&ctx.db, &new_email).await.is_err(), "no account"); |
| 76 | + assert_eq!(waitlist_rows(&ctx, &new_login).await, vec![("web".to_string(), Some(new_email.clone()))]); |
| 77 | + |
| 78 | + // Again, from an MCP client's sign-in: still no account, and the row says where they came from |
| 79 | + let r = request |
| 80 | + .post("/auth/dev-login") |
| 81 | + .json(&sign_in( |
| 82 | + &new_email, |
| 83 | + &format!("2{suffix}"), |
| 84 | + &new_login, |
| 85 | + Some("/oauth/authorize?client_id=claude-test&response_type=code"), |
| 86 | + )) |
| 87 | + .await; |
| 88 | + assert!(r.text().contains("VisualCI opens in about 48 hours"), "{}", r.text()); |
| 89 | + assert!(users::Model::find_by_email(&ctx.db, &new_email).await.is_err(), "still no account"); |
| 90 | + assert_eq!(waitlist_rows(&ctx, &new_login).await.len(), 1, "one row per person"); |
| 91 | + }) |
| 92 | + .await; |
| 93 | +} |
| 94 | + |
| 95 | +#[tokio::test] |
| 96 | +#[serial] |
| 97 | +async fn someone_on_the_waitlist_gets_no_mcp_token() { |
| 98 | + request::<App, _, _>(|request, ctx| async move { |
| 99 | + let _closed = Closed::new(); |
| 100 | + let suffix = &uuid::Uuid::new_v4().to_string()[..8]; |
| 101 | + let email = format!("mcp-{suffix}@example.com"); |
| 102 | + let r = request |
| 103 | + .post("/auth/dev-login") |
| 104 | + .json(&sign_in( |
| 105 | + &email, |
| 106 | + &format!("3{suffix}"), |
| 107 | + &format!("mcp-{suffix}"), |
| 108 | + Some("/oauth/authorize?client_id=x"), |
| 109 | + )) |
| 110 | + .await; |
| 111 | + assert!(r.text().contains("VisualCI opens in about 48 hours"), "{}", r.text()); |
| 112 | + // With no session, the authorization page asks them to sign in: there's no consent step to approve, so no |
| 113 | + // code and no token |
| 114 | + assert!(r.headers().get("set-cookie").is_none(), "no session"); |
| 115 | + let page: serde_json::Value = |
| 116 | + request.get("/api/ui/oauth/authorize?client_id=x&response_type=code").await.json(); |
| 117 | + assert_ne!(page["status"], "consent", "{page}"); |
| 118 | + assert!(users::Model::find_by_email(&ctx.db, &email).await.is_err()); |
| 119 | + }) |
| 120 | + .await; |
| 121 | +} |
0 commit comments