forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·10.4 MiB·08e885b·Tahinli·10 minutes ago·335 commits
| 1 | use forge_core::{Issue, Reason, Role, Store, SubState, Visibility, WatchLevel}; |
| 2 | use futures_util::StreamExt; |
| 3 | use topcoat::router::request::Request; |
| 4 | use topcoat::router::{Body, Router}; |
| 5 | |
| 6 | use forge_web_app::test_support::fixture::{Reply, send_form}; |
| 7 | |
| 8 | use forge_web_app::auth::SESSION_COOKIE; |
| 9 | |
| 10 | const CSRF: &str = "notif-test-csrf"; |
| 11 | |
| 12 | struct Site { |
| 13 | router: Router, |
| 14 | store: Store, |
| 15 | git: std::sync::Arc<forge_git::GitEnv>, |
| 16 | _dir: tempfile::TempDir, |
| 17 | } |
| 18 | |
| 19 | async fn site() -> Site { |
| 20 | let dir = tempfile::tempdir().expect("tempdir"); |
| 21 | let state = forge_web_app::test_support::test_state(dir.path()).await; |
| 22 | let store = state.store.clone(); |
| 23 | let git = state.git.clone(); |
| 24 | let router = forge_web_app::test_support::app_router(state); |
| 25 | Site { |
| 26 | router, |
| 27 | store, |
| 28 | git, |
| 29 | _dir: dir, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | async fn sign_in(store: &Store, user_id: &str, token: &str) -> String { |
| 34 | forge_web_app::test_support::create_session(store, user_id, token, CSRF).await; |
| 35 | format!("__Host-{SESSION_COOKIE}={token}") |
| 36 | } |
| 37 | |
| 38 | async fn call( |
| 39 | site: &Site, |
| 40 | method: &str, |
| 41 | uri: &str, |
| 42 | cookie: Option<&str>, |
| 43 | form: Option<String>, |
| 44 | ) -> Reply { |
| 45 | send_form(&site.router, method, uri, cookie, form).await |
| 46 | } |
| 47 | |
| 48 | async fn get(site: &Site, uri: &str, cookie: Option<&str>) -> Reply { |
| 49 | call(site, "GET", uri, cookie, None).await |
| 50 | } |
| 51 | |
| 52 | async fn post(site: &Site, uri: &str, cookie: &str, extra: &str) -> Reply { |
| 53 | call( |
| 54 | site, |
| 55 | "POST", |
| 56 | uri, |
| 57 | Some(cookie), |
| 58 | Some(format!("csrf={CSRF}{extra}")), |
| 59 | ) |
| 60 | .await |
| 61 | } |
| 62 | |
| 63 | struct World { |
| 64 | tahinli: forge_core::User, |
| 65 | bob: forge_core::User, |
| 66 | repo: forge_core::Repo, |
| 67 | issue: Issue, |
| 68 | } |
| 69 | |
| 70 | async fn world(site: &Site, visibility: Visibility) -> World { |
| 71 | let store = &site.store; |
| 72 | let tahinli = store |
| 73 | .create_user("tahinli", None, "x", false) |
| 74 | .await |
| 75 | .expect("tahinli"); |
| 76 | let bob = store |
| 77 | .create_user("bob", None, "x", false) |
| 78 | .await |
| 79 | .expect("bob"); |
| 80 | let repo = store |
| 81 | .create_repo(&tahinli.id, "demo", "", visibility, "main") |
| 82 | .await |
| 83 | .expect("repo"); |
| 84 | let issue = store |
| 85 | .create_issue(&repo.id, &tahinli.id, "First", "body") |
| 86 | .await |
| 87 | .expect("issue"); |
| 88 | World { |
| 89 | tahinli, |
| 90 | bob, |
| 91 | repo, |
| 92 | issue, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | async fn notify(site: &Site, w: &World, reason: Reason) { |
| 97 | site.store |
| 98 | .upsert_notification( |
| 99 | &w.bob.id, |
| 100 | &w.repo.id, |
| 101 | &w.issue.id, |
| 102 | reason, |
| 103 | "comment", |
| 104 | Some(&w.tahinli.id), |
| 105 | ) |
| 106 | .await |
| 107 | .expect("notification"); |
| 108 | } |
| 109 | |
| 110 | async fn first_note_id(site: &Site, user_id: &str) -> String { |
| 111 | site.store |
| 112 | .list_notifications(user_id, false, None, 50) |
| 113 | .await |
| 114 | .expect("list") |
| 115 | .first() |
| 116 | .expect("one notification") |
| 117 | .id |
| 118 | .clone() |
| 119 | } |
| 120 | |
| 121 | #[tokio::test] |
| 122 | async fn inbox_lists_unread_and_all_shows_read() { |
| 123 | let site = site().await; |
| 124 | let w = world(&site, Visibility::Public).await; |
| 125 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 126 | notify(&site, &w, Reason::Mention).await; |
| 127 | let id = first_note_id(&site, &w.bob.id).await; |
| 128 | site.store.mark_read(&w.bob.id, &id).await.expect("read"); |
| 129 | |
| 130 | let unread = get(&site, "/notifications", Some(&cookie)).await; |
| 131 | assert_eq!(unread.status, 200); |
| 132 | assert!(!unread.body.contains("tahinli/demo #1 First")); |
| 133 | assert!(unread.body.contains("No unread notifications.")); |
| 134 | |
| 135 | let all = get(&site, "/notifications?all=1", Some(&cookie)).await; |
| 136 | assert_eq!(all.status, 200); |
| 137 | assert!(all.body.contains("tahinli/demo #1 First")); |
| 138 | assert!(all.body.contains("mentioned")); |
| 139 | } |
| 140 | |
| 141 | #[tokio::test] |
| 142 | async fn unread_inbox_lists_unread_rows_grouped_by_repo() { |
| 143 | let site = site().await; |
| 144 | let w = world(&site, Visibility::Public).await; |
| 145 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 146 | notify(&site, &w, Reason::Assign).await; |
| 147 | |
| 148 | let page = get(&site, "/notifications", Some(&cookie)).await; |
| 149 | assert_eq!(page.status, 200); |
| 150 | assert!(page.body.contains("tahinli/demo #1 First")); |
| 151 | assert!(page.body.contains("assigned")); |
| 152 | assert!(page.body.contains("/notifications/mark_all_read?repo=")); |
| 153 | assert!(page.body.contains("Mark all as read")); |
| 154 | } |
| 155 | |
| 156 | #[tokio::test] |
| 157 | async fn another_users_notification_is_404_and_the_owner_opens_it() { |
| 158 | let site = site().await; |
| 159 | let w = world(&site, Visibility::Public).await; |
| 160 | let tahinli_cookie = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; |
| 161 | let bob_cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 162 | notify(&site, &w, Reason::Comment).await; |
| 163 | let id = first_note_id(&site, &w.bob.id).await; |
| 164 | |
| 165 | let res = get( |
| 166 | &site, |
| 167 | &format!("/notifications/{id}/open"), |
| 168 | Some(&tahinli_cookie), |
| 169 | ) |
| 170 | .await; |
| 171 | assert_eq!(res.status, 404); |
| 172 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); |
| 173 | |
| 174 | let read = post( |
| 175 | &site, |
| 176 | &format!("/notifications/{id}/read"), |
| 177 | &tahinli_cookie, |
| 178 | "", |
| 179 | ) |
| 180 | .await; |
| 181 | assert_eq!(read.status, 303); |
| 182 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); |
| 183 | |
| 184 | let res = get( |
| 185 | &site, |
| 186 | &format!("/notifications/{id}/open"), |
| 187 | Some(&bob_cookie), |
| 188 | ) |
| 189 | .await; |
| 190 | assert_eq!(res.status, 303); |
| 191 | assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); |
| 192 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); |
| 193 | } |
| 194 | |
| 195 | #[tokio::test] |
| 196 | async fn mark_all_read_clears_the_inbox() { |
| 197 | let site = site().await; |
| 198 | let w = world(&site, Visibility::Public).await; |
| 199 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 200 | notify(&site, &w, Reason::Comment).await; |
| 201 | |
| 202 | let res = post(&site, "/notifications/mark_all_read", &cookie, "").await; |
| 203 | assert_eq!(res.status, 303); |
| 204 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); |
| 205 | } |
| 206 | |
| 207 | #[tokio::test] |
| 208 | async fn the_stream_sends_the_count_then_each_change() { |
| 209 | let site = site().await; |
| 210 | let w = world(&site, Visibility::Public).await; |
| 211 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 212 | notify(&site, &w, Reason::Mention).await; |
| 213 | |
| 214 | let request = Request::builder() |
| 215 | .method("GET") |
| 216 | .uri("/notifications/stream") |
| 217 | .header("cookie", cookie.as_str()) |
| 218 | .body(Body::empty()) |
| 219 | .expect("request"); |
| 220 | let res = site.router.handle(request).await; |
| 221 | assert_eq!(res.status().as_u16(), 200); |
| 222 | assert_eq!( |
| 223 | res.headers().get("content-type").unwrap(), |
| 224 | "text/event-stream" |
| 225 | ); |
| 226 | assert_eq!(res.headers().get("x-accel-buffering").unwrap(), "no"); |
| 227 | |
| 228 | let mut frames = res.into_body().into_data_stream(); |
| 229 | let retry = frames.next().await.expect("a frame").expect("data"); |
| 230 | assert!(retry.starts_with(b"retry: 1000"), "{retry:?}"); |
| 231 | let first = frames.next().await.expect("a frame").expect("data"); |
| 232 | assert_eq!(&first[..], b"event: unread\ndata: 1\n\n"); |
| 233 | |
| 234 | let read = post(&site, "/notifications/mark_all_read", &cookie, "").await; |
| 235 | assert_eq!(read.status, 303); |
| 236 | let next = frames.next().await.expect("a frame").expect("data"); |
| 237 | assert_eq!(&next[..], b"event: unread\ndata: 0\n\n"); |
| 238 | } |
| 239 | |
| 240 | #[tokio::test] |
| 241 | async fn corner_bell_shows_the_unread_count() { |
| 242 | let site = site().await; |
| 243 | let w = world(&site, Visibility::Public).await; |
| 244 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 245 | |
| 246 | let none = get(&site, "/explore", Some(&cookie)).await; |
| 247 | assert!(none.body.contains("href=\"/notifications\"")); |
| 248 | assert!(!none.body.contains("<span class=\"bell-count\">")); |
| 249 | |
| 250 | notify(&site, &w, Reason::Mention).await; |
| 251 | let one = get(&site, "/explore", Some(&cookie)).await; |
| 252 | assert!(one.body.contains("<span class=\"bell-count\">1</span>")); |
| 253 | } |
| 254 | |
| 255 | #[tokio::test] |
| 256 | async fn subscription_toggles_on_an_issue_and_unknown_pull_is_404() { |
| 257 | let site = site().await; |
| 258 | let w = world(&site, Visibility::Public).await; |
| 259 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 260 | |
| 261 | let res = post( |
| 262 | &site, |
| 263 | "/tahinli/demo/issues/1/subscription", |
| 264 | &cookie, |
| 265 | "&action=subscribe", |
| 266 | ) |
| 267 | .await; |
| 268 | assert_eq!(res.status, 303); |
| 269 | assert_eq!(res.location.as_deref(), Some("/tahinli/demo/issues/1")); |
| 270 | let sub = site |
| 271 | .store |
| 272 | .subscription(&w.bob.id, &w.issue.id) |
| 273 | .await |
| 274 | .expect("sub"); |
| 275 | assert_eq!(sub, Some((SubState::Subscribed, Reason::Subscribed))); |
| 276 | |
| 277 | let page = get(&site, "/tahinli/demo/issues/1", Some(&cookie)).await; |
| 278 | assert!(page.body.contains("value=\"unsubscribe\"")); |
| 279 | |
| 280 | post( |
| 281 | &site, |
| 282 | "/tahinli/demo/issues/1/subscription", |
| 283 | &cookie, |
| 284 | "&action=unsubscribe", |
| 285 | ) |
| 286 | .await; |
| 287 | let sub = site |
| 288 | .store |
| 289 | .subscription(&w.bob.id, &w.issue.id) |
| 290 | .await |
| 291 | .expect("sub"); |
| 292 | assert_eq!(sub, Some((SubState::Ignored, Reason::Subscribed))); |
| 293 | |
| 294 | let res = post( |
| 295 | &site, |
| 296 | "/tahinli/demo/pulls/1/subscription", |
| 297 | &cookie, |
| 298 | "&action=subscribe", |
| 299 | ) |
| 300 | .await; |
| 301 | assert_eq!(res.status, 404); |
| 302 | } |
| 303 | |
| 304 | #[tokio::test] |
| 305 | async fn watch_form_sets_the_level() { |
| 306 | let site = site().await; |
| 307 | let w = world(&site, Visibility::Public).await; |
| 308 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 309 | |
| 310 | let res = post(&site, "/tahinli/demo/watch", &cookie, "&level=watching").await; |
| 311 | assert_eq!(res.status, 303); |
| 312 | assert_eq!( |
| 313 | site.store |
| 314 | .watch_level(&w.bob.id, &w.repo.id) |
| 315 | .await |
| 316 | .expect("level"), |
| 317 | WatchLevel::Watching |
| 318 | ); |
| 319 | |
| 320 | let bare = forge_git::repo::repo_path(&site.git.data_dir, &w.repo.id).expect("repo path"); |
| 321 | forge_git::repo::init_bare(&site.git, &bare, "main") |
| 322 | .await |
| 323 | .expect("init bare"); |
| 324 | let header = get(&site, "/tahinli/demo", Some(&cookie)).await; |
| 325 | assert!(header.body.contains("action=\"/tahinli/demo/watch\"")); |
| 326 | assert!(header.body.contains("<details class=\"menu watch-menu\">")); |
| 327 | assert!(!header.body.contains("<select")); |
| 328 | assert_eq!(header.body.matches("name=\"level\"").count(), 3); |
| 329 | assert_eq!( |
| 330 | header |
| 331 | .body |
| 332 | .matches("class=\"watch-option is-current\"") |
| 333 | .count(), |
| 334 | 1 |
| 335 | ); |
| 336 | assert_eq!(header.body.matches("class=\"watch-option\"").count(), 2); |
| 337 | assert!(header.body.contains("1 watching")); |
| 338 | |
| 339 | let bad = post(&site, "/tahinli/demo/watch", &cookie, "&level=loud").await; |
| 340 | assert_eq!(bad.status, 400); |
| 341 | } |
| 342 | |
| 343 | #[tokio::test] |
| 344 | async fn anonymous_viewers_are_sent_to_login_and_the_stream_needs_a_session() { |
| 345 | let site = site().await; |
| 346 | let _w = world(&site, Visibility::Public).await; |
| 347 | let res = get(&site, "/notifications", None).await; |
| 348 | assert_eq!(res.status, 303); |
| 349 | assert!(res.location.unwrap_or_default().starts_with("/login")); |
| 350 | let stream = get(&site, "/notifications/stream", None).await; |
| 351 | assert_eq!(stream.status, 401); |
| 352 | } |
| 353 | |
| 354 | #[tokio::test] |
| 355 | async fn row_for_a_repo_the_viewer_lost_access_to_is_hidden() { |
| 356 | let site = site().await; |
| 357 | let w = world(&site, Visibility::Private).await; |
| 358 | let cookie = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 359 | site.store |
| 360 | .set_collaborator(&w.repo.id, &w.bob.id, Role::Read) |
| 361 | .await |
| 362 | .expect("collaborator"); |
| 363 | notify(&site, &w, Reason::Mention).await; |
| 364 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 1); |
| 365 | |
| 366 | site.store |
| 367 | .remove_collaborator(&w.repo.id, &w.bob.id) |
| 368 | .await |
| 369 | .expect("remove"); |
| 370 | let page = get(&site, "/notifications?all=1", Some(&cookie)).await; |
| 371 | assert_eq!(page.status, 200); |
| 372 | assert!(!page.body.contains("tahinli/demo #1 First")); |
| 373 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); |
| 374 | } |
| 375 | |
| 376 | #[tokio::test] |
| 377 | async fn discussion_replies_show_in_the_inbox_and_clear_by_open_or_mark_all() { |
| 378 | let site = site().await; |
| 379 | let w = world(&site, Visibility::Public).await; |
| 380 | let discussion = site |
| 381 | .store |
| 382 | .create_discussion( |
| 383 | &w.repo.id, |
| 384 | &w.tahinli.id, |
| 385 | forge_core::discussions::DiscussionCategory::General, |
| 386 | "Ideas", |
| 387 | "", |
| 388 | ) |
| 389 | .await |
| 390 | .expect("discussion"); |
| 391 | site.store |
| 392 | .add_discussion_comment(&discussion.id, &w.bob.id, None, "+1", false) |
| 393 | .await |
| 394 | .expect("reply"); |
| 395 | let tahinli = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; |
| 396 | let bob = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 397 | let open = format!("/notifications/discussions/{}/open", discussion.id); |
| 398 | |
| 399 | let page = get(&site, "/notifications", Some(&tahinli)).await; |
| 400 | assert_eq!(page.status, 200); |
| 401 | assert!(page.body.contains("Ideas")); |
| 402 | assert!(page.body.contains(&open)); |
| 403 | assert_eq!( |
| 404 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 405 | 1 |
| 406 | ); |
| 407 | |
| 408 | let other = get(&site, &open, Some(&bob)).await; |
| 409 | assert_eq!(other.status, 404); |
| 410 | |
| 411 | let res = get(&site, &open, Some(&tahinli)).await; |
| 412 | assert_eq!(res.status, 303); |
| 413 | assert_eq!(res.location.as_deref(), Some("/tahinli/demo/discussions/2")); |
| 414 | assert_eq!( |
| 415 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 416 | 0 |
| 417 | ); |
| 418 | |
| 419 | let discussion = site |
| 420 | .store |
| 421 | .create_discussion( |
| 422 | &w.repo.id, |
| 423 | &w.tahinli.id, |
| 424 | forge_core::discussions::DiscussionCategory::General, |
| 425 | "Ideas", |
| 426 | "", |
| 427 | ) |
| 428 | .await |
| 429 | .expect("discussion"); |
| 430 | site.store |
| 431 | .add_discussion_comment(&discussion.id, &w.bob.id, None, "+1", false) |
| 432 | .await |
| 433 | .expect("reply"); |
| 434 | assert_eq!( |
| 435 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 436 | 1 |
| 437 | ); |
| 438 | |
| 439 | let res = post(&site, "/notifications/mark_all_read", &tahinli, "").await; |
| 440 | assert_eq!(res.status, 303); |
| 441 | assert_eq!( |
| 442 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 443 | 0 |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | #[tokio::test] |
| 448 | async fn advisory_alerts_show_for_managers_and_open_marks_read() { |
| 449 | let site = site().await; |
| 450 | let w = world(&site, Visibility::Public).await; |
| 451 | site.store |
| 452 | .notify_new_alerts(&w.tahinli.id, &w.repo.id, 2) |
| 453 | .await |
| 454 | .expect("alerts"); |
| 455 | site.store |
| 456 | .notify_new_alerts(&w.bob.id, &w.repo.id, 1) |
| 457 | .await |
| 458 | .expect("alerts"); |
| 459 | let tahinli = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; |
| 460 | let bob = sign_in(&site.store, &w.bob.id, "bob-token").await; |
| 461 | let open = format!("/notifications/advisories/{}/open", w.repo.id); |
| 462 | |
| 463 | let page = get(&site, "/notifications", Some(&tahinli)).await; |
| 464 | assert_eq!(page.status, 200); |
| 465 | assert!(page.body.contains("Security alerts")); |
| 466 | assert!(page.body.contains("2 new alerts")); |
| 467 | assert!(page.body.contains(&open)); |
| 468 | assert_eq!( |
| 469 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 470 | 1 |
| 471 | ); |
| 472 | |
| 473 | let bob_page = get(&site, "/notifications", Some(&bob)).await; |
| 474 | assert_eq!(bob_page.status, 200); |
| 475 | assert!(!bob_page.body.contains(&open)); |
| 476 | assert_eq!(site.store.unread_count(&w.bob.id).await.expect("count"), 0); |
| 477 | let bob_open = get(&site, &open, Some(&bob)).await; |
| 478 | assert_eq!(bob_open.status, 404); |
| 479 | |
| 480 | let res = get(&site, &open, Some(&tahinli)).await; |
| 481 | assert_eq!(res.status, 303); |
| 482 | assert_eq!(res.location.as_deref(), Some("/tahinli/demo/security")); |
| 483 | assert_eq!( |
| 484 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 485 | 0 |
| 486 | ); |
| 487 | } |
| 488 | |
| 489 | #[tokio::test] |
| 490 | async fn advisory_rows_are_marked_read_by_mark_read_and_mark_all() { |
| 491 | let site = site().await; |
| 492 | let w = world(&site, Visibility::Public).await; |
| 493 | let tahinli = sign_in(&site.store, &w.tahinli.id, "tahinli-token").await; |
| 494 | let read = format!("/notifications/advisories/{}/read", w.repo.id); |
| 495 | |
| 496 | site.store |
| 497 | .notify_new_alerts(&w.tahinli.id, &w.repo.id, 1) |
| 498 | .await |
| 499 | .expect("alerts"); |
| 500 | assert_eq!( |
| 501 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 502 | 1 |
| 503 | ); |
| 504 | let res = post(&site, &read, &tahinli, "").await; |
| 505 | assert_eq!(res.status, 303); |
| 506 | assert_eq!( |
| 507 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 508 | 0 |
| 509 | ); |
| 510 | |
| 511 | site.store |
| 512 | .notify_new_alerts(&w.tahinli.id, &w.repo.id, 3) |
| 513 | .await |
| 514 | .expect("alerts"); |
| 515 | assert_eq!( |
| 516 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 517 | 1 |
| 518 | ); |
| 519 | let res = post(&site, "/notifications/mark_all_read", &tahinli, "").await; |
| 520 | assert_eq!(res.status, 303); |
| 521 | assert_eq!( |
| 522 | site.store.unread_count(&w.tahinli.id).await.expect("count"), |
| 523 | 0 |
| 524 | ); |
| 525 | } |