use forge_core::{Issue, Reason, Role, Store, SubState, Visibility, WatchLevel}; use futures_util::StreamExt; use topcoat::router::request::Request; use topcoat::router::{Body, Router}; use forge_web_app::test_support::fixture::{Reply, send_form}; use forge_web_app::auth::SESSION_COOKIE; const CSRF: &str = "notif-test-csrf"; struct Site { router: Router, store: Store, git: std::sync::Arc, _dir: tempfile::TempDir, } async fn site() -> Site { let dir = tempfile::tempdir().expect("tempdir"); let state = forge_web_app::test_support::test_state(dir.path()).await; let store = state.store.clone(); let git = state.git.clone(); let router = forge_web_app::test_support::app_router(state); Site { router, store, git, _dir: dir, } } async fn sign_in(store: &Store, user_id: &str, token: &str) -> String { forge_web_app::test_support::create_session(store, user_id, token, CSRF).await; format!("__Host-{SESSION_COOKIE}={token}") } async fn call( site: &Site, method: &str, uri: &str, cookie: Option<&str>, form: Option, ) -> Reply { send_form(&site.router, method, uri, cookie, form).await } async fn get(site: &Site, uri: &str, cookie: Option<&str>) -> Reply { call(site, "GET", uri, cookie, None).await } async fn post(site: &Site, uri: &str, cookie: &str, extra: &str) -> Reply { call( site, "POST", uri, Some(cookie), Some(format!("csrf={CSRF}{extra}")), ) .await } struct World { tahinli: forge_core::User, bob: forge_core::User, repo: forge_core::Repo, issue: Issue, } async fn world(site: &Site, visibility: Visibility) -> World { let store = &site.store; let tahinli = store .create_user("tahinli", None, "x", false) .await .expect("tahinli"); let bob = store .create_user("bob", None, "x", false) .await .expect("bob"); let repo = store .create_repo(&tahinli.id, "demo", "", visibility, "main") .await .expect("repo"); let issue = store .create_issue(&repo.id, &tahinli.id, "First", "body") .await .expect("issue"); World { tahinli, bob, repo, issue, } } async fn notify(site: &Site, w: &World, reason: Reason) { site.store .upsert_notification( &w.bob.id, &w.repo.id, &w.issue.id, reason, "comment", Some(&w.tahinli.id), ) .await .expect("notification"); } async fn first_note_id(site: &Site, user_id: &str) -> String { site.store .list_notifications(user_id, false, None, 50) .await .expect("list") .first() .expect("one notification") .id .clone() } #[tokio::test] async fn inbox_lists_unread_and_all_shows_read() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; notify(&site, &w, Reason::Mention).await; let id = first_note_id(&site, &w.bob.id).await; site.store.mark_read(&w.bob.id, &id).await.expect("read"); let unread = get(&site, "/notifications", Some(&cookie)).await; assert_eq!(unread.status, 200); assert!(!unread.body.contains("tahinli/demo #1 First")); assert!(unread.body.contains("No unread notifications.")); let all = get(&site, "/notifications?all=1", Some(&cookie)).await; assert_eq!(all.status, 200); assert!(all.body.contains("tahinli/demo #1 First")); assert!(all.body.contains("mentioned")); } #[tokio::test] async fn another_users_notification_is_404_and_the_owner_opens_it() { let site = site().await; let w = world(&site, Visibility::Public).await; let tahinli_cookie = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; let bob_cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; notify(&site, &w, Reason::Comment).await; let id = first_note_id(&site, &w.bob.id).await; let res = get( &site, &format!("/notifications/{id}/open"), Some(&tahinli_cookie), ) .await; assert_eq!(res.status, 404); assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); let read = post( &site, &format!("/notifications/{id}/read"), &tahinli_cookie, "", ) .await; assert_eq!(read.status, 303); assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); let res = get( &site, &format!("/notifications/{id}/open"), Some(&bob_cookie), ) .await; assert_eq!(res.status, 303); assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); } #[tokio::test] async fn mark_all_read_clears_the_inbox() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; notify(&site, &w, Reason::Comment).await; let res = post(&site, "/notifications/mark_all_read", &cookie, "").await; assert_eq!(res.status, 303); assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); } #[tokio::test] async fn the_stream_sends_the_count_then_each_change() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; notify(&site, &w, Reason::Mention).await; let request = Request::builder() .method("GET") .uri("/notifications/stream") .header("cookie", cookie.as_str()) .body(Body::empty()) .expect("request"); let res = site.router.handle(request).await; assert_eq!(res.status().as_u16(), 200); assert_eq!( res.headers().get("content-type").unwrap(), "text/event-stream" ); assert_eq!(res.headers().get("x-accel-buffering").unwrap(), "no"); let mut frames = res.into_body().into_data_stream(); let retry = frames.next().await.expect("a frame").expect("data"); assert!(retry.starts_with(b"retry: 1000"), "{retry:?}"); let first = frames.next().await.expect("a frame").expect("data"); assert_eq!(&first[..], b"event: unread\ndata: 1\n\n"); let read = post(&site, "/notifications/mark_all_read", &cookie, "").await; assert_eq!(read.status, 303); let next = frames.next().await.expect("a frame").expect("data"); assert_eq!(&next[..], b"event: unread\ndata: 0\n\n"); } #[tokio::test] async fn subscription_toggles_on_an_issue_and_unknown_pull_is_404() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; let res = post( &site, "/tahinli/demo/issues/1/subscription", &cookie, "&action=subscribe", ) .await; assert_eq!(res.status, 303); assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); let sub = site .store .subscription(&w.bob.id, &w.issue.id) .await .expect("sub"); assert_eq!(sub, Some((SubState::Subscribed, Reason::Subscribed))); let page = get(&site, "/tahinli/demo/issues/1", Some(&cookie)).await; assert!(page.body.contains("value=\"unsubscribe\"")); post( &site, "/tahinli/demo/issues/1/subscription", &cookie, "&action=unsubscribe", ) .await; let sub = site .store .subscription(&w.bob.id, &w.issue.id) .await .expect("sub"); assert_eq!(sub, Some((SubState::Ignored, Reason::Subscribed))); let res = post( &site, "/tahinli/demo/pulls/1/subscription", &cookie, "&action=subscribe", ) .await; assert_eq!(res.status, 404); } #[tokio::test] async fn watch_form_sets_the_level() { let site = site().await; let w = world(&site, Visibility::Public).await; let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; let res = post(&site, "/tahinli/demo/watch", &cookie, "&level=watching").await; assert_eq!(res.status, 303); assert_eq!( site.store .watch_level(&w.bob.id, &w.repo.id) .await .expect("level"), WatchLevel::Watching ); let bare = forge_git::repo::repo_path(&site.git.data_dir, &w.repo.id).expect("repo path"); forge_git::repo::init_bare(&site.git, &bare, "main") .await .expect("init bare"); let header = get(&site, "/tahinli/demo", Some(&cookie)).await; assert!(header.body.contains("action=\"/tahinli/demo/watch\"")); assert!(header.body.contains("
")); assert!(!header.body.contains("