forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Import tests. The remote API is a fake behind [`RemoteApi`], and the git side is a bare repo on |
| 2 | //! disk fetched over the local transport, so nothing here touches the network. |
| 3 | |
| 4 | use std::collections::HashMap; |
| 5 | use std::path::Path; |
| 6 | use std::sync::Mutex; |
| 7 | |
| 8 | use forge_core::issues::IssueFilter; |
| 9 | use forge_core::store::imports::{ImportJob, ImportState}; |
| 10 | use forge_core::{IssueState, Visibility}; |
| 11 | use topcoat::router::request::Request; |
| 12 | use topcoat::router::{Body, to_bytes}; |
| 13 | |
| 14 | use crate::auth::SESSION_COOKIE; |
| 15 | use crate::importer::api::{ |
| 16 | ApiError, ApiResult, AssetStream, ExtAsset, ExtComment, ExtIssue, ExtLabel, ExtMilestone, |
| 17 | ExtRelease, ExtRepo, RemoteApi, |
| 18 | }; |
| 19 | use crate::importer::{ |
| 20 | ImportOptions, JobSpec, Source, Transport, asset_name, origin_note, run_job, with_note, |
| 21 | }; |
| 22 | use crate::state::AppState; |
| 23 | use crate::test_support::{app_router, create_session, test_state}; |
| 24 | |
| 25 | const TOKEN: &str = "ghp_supersecret_token_value"; |
| 26 | |
| 27 | #[derive(Default)] |
| 28 | struct Fake { |
| 29 | labels: Vec<ExtLabel>, |
| 30 | milestones: Vec<ExtMilestone>, |
| 31 | issues: Vec<ExtIssue>, |
| 32 | comments: HashMap<i64, Vec<ExtComment>>, |
| 33 | releases: Vec<ExtRelease>, |
| 34 | files: HashMap<String, Vec<u8>>, |
| 35 | /// Errors handed out by `comments`, one per call, before it starts answering. |
| 36 | comment_failures: Mutex<Vec<ApiError>>, |
| 37 | comment_calls: Mutex<usize>, |
| 38 | } |
| 39 | |
| 40 | impl RemoteApi for Fake { |
| 41 | async fn list_repos(&self, _owner: &str) -> ApiResult<Vec<ExtRepo>> { |
| 42 | Ok(vec![ExtRepo { |
| 43 | name: "project".into(), |
| 44 | full_name: "tahinli/project".into(), |
| 45 | ..ExtRepo::default() |
| 46 | }]) |
| 47 | } |
| 48 | |
| 49 | async fn labels(&self, _repo: &str) -> ApiResult<Vec<ExtLabel>> { |
| 50 | Ok(self.labels.clone()) |
| 51 | } |
| 52 | |
| 53 | async fn milestones(&self, _repo: &str) -> ApiResult<Vec<ExtMilestone>> { |
| 54 | Ok(self.milestones.clone()) |
| 55 | } |
| 56 | |
| 57 | async fn issues(&self, _repo: &str) -> ApiResult<Vec<ExtIssue>> { |
| 58 | Ok(self.issues.clone()) |
| 59 | } |
| 60 | |
| 61 | async fn comments(&self, _repo: &str, issue: &ExtIssue) -> ApiResult<Vec<ExtComment>> { |
| 62 | *self.comment_calls.lock().unwrap() += 1; |
| 63 | if let Some(e) = self.comment_failures.lock().unwrap().pop() { |
| 64 | return Err(e); |
| 65 | } |
| 66 | Ok(self |
| 67 | .comments |
| 68 | .get(&issue.number) |
| 69 | .cloned() |
| 70 | .unwrap_or_default()) |
| 71 | } |
| 72 | |
| 73 | async fn releases(&self, _repo: &str) -> ApiResult<Vec<ExtRelease>> { |
| 74 | Ok(self.releases.clone()) |
| 75 | } |
| 76 | |
| 77 | async fn download(&self, asset: &ExtAsset, _max: u64) -> ApiResult<AssetStream> { |
| 78 | let bytes = self |
| 79 | .files |
| 80 | .get(&asset.url) |
| 81 | .cloned() |
| 82 | .ok_or_else(|| ApiError::NotFound("asset".into()))?; |
| 83 | Ok(Box::pin(futures_util::stream::once(async move { |
| 84 | Ok::<_, std::io::Error>(bytes::Bytes::from(bytes)) |
| 85 | }))) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn plumbing(git_dir: &Path, args: &[&str]) -> String { |
| 90 | let out = std::process::Command::new("git") |
| 91 | .arg(format!("--git-dir={}", git_dir.display())) |
| 92 | .args(args) |
| 93 | .env("GIT_AUTHOR_NAME", "t") |
| 94 | .env("GIT_AUTHOR_EMAIL", "t@example.com") |
| 95 | .env("GIT_COMMITTER_NAME", "t") |
| 96 | .env("GIT_COMMITTER_EMAIL", "t@example.com") |
| 97 | .output() |
| 98 | .expect("git runs"); |
| 99 | assert!(out.status.success(), "git {args:?}: {out:?}"); |
| 100 | String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 101 | } |
| 102 | |
| 103 | fn commit_to(git_dir: &Path, refname: &str, message: &str) { |
| 104 | let tree = plumbing(git_dir, &["hash-object", "-t", "tree", "-w", "--stdin"]); |
| 105 | let commit = plumbing(git_dir, &["commit-tree", &tree, "-m", message]); |
| 106 | plumbing(git_dir, &["update-ref", refname, &commit]); |
| 107 | } |
| 108 | |
| 109 | /// A bare "remote" with `master`, a `dev` branch and a `v1` tag, HEAD on `master`. |
| 110 | fn upstream(dir: &Path) -> String { |
| 111 | let path = dir.join("upstream.git"); |
| 112 | let out = std::process::Command::new("git") |
| 113 | .args(["init", "--bare", "-q"]) |
| 114 | .arg(&path) |
| 115 | .output() |
| 116 | .expect("git init"); |
| 117 | assert!(out.status.success()); |
| 118 | plumbing(&path, &["symbolic-ref", "HEAD", "refs/heads/master"]); |
| 119 | commit_to(&path, "refs/heads/master", "one"); |
| 120 | commit_to(&path, "refs/heads/dev", "dev work"); |
| 121 | commit_to(&path, "refs/tags/v1", "tagged"); |
| 122 | path.display().to_string() |
| 123 | } |
| 124 | |
| 125 | struct Fixture { |
| 126 | state: AppState, |
| 127 | repo: forge_core::Repo, |
| 128 | user: forge_core::User, |
| 129 | _dir: tempfile::TempDir, |
| 130 | upstream: String, |
| 131 | } |
| 132 | |
| 133 | async fn fixture() -> Fixture { |
| 134 | let dir = tempfile::tempdir().expect("tempdir"); |
| 135 | let state = test_state(dir.path()).await; |
| 136 | let user = state |
| 137 | .store |
| 138 | .create_user("tahinli", None, "unused", false) |
| 139 | .await |
| 140 | .expect("user"); |
| 141 | let repo = state |
| 142 | .store |
| 143 | .create_repo(&user.id, "project", "", Visibility::Public, "main") |
| 144 | .await |
| 145 | .expect("repo"); |
| 146 | let bare = forge_git::repo::repo_path(&state.git.data_dir, &repo.id).expect("path"); |
| 147 | forge_git::repo::init_bare(&state.git, &bare, "main") |
| 148 | .await |
| 149 | .expect("init bare"); |
| 150 | let upstream = upstream(dir.path()); |
| 151 | Fixture { |
| 152 | state, |
| 153 | repo, |
| 154 | user, |
| 155 | _dir: dir, |
| 156 | upstream, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | fn spec(source: Source, options: ImportOptions) -> JobSpec { |
| 161 | JobSpec { |
| 162 | source, |
| 163 | remote: "tahinli/project".into(), |
| 164 | instance: String::new(), |
| 165 | token: Some(TOKEN.into()), |
| 166 | git_user: String::new(), |
| 167 | options, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | async fn new_job(fx: &Fixture, source: Source) -> ImportJob { |
| 172 | fx.state |
| 173 | .store |
| 174 | .create_import_job( |
| 175 | &fx.user.id, |
| 176 | &fx.repo.id, |
| 177 | source.as_str(), |
| 178 | "tahinli/project", |
| 179 | "{}", |
| 180 | ) |
| 181 | .await |
| 182 | .expect("job") |
| 183 | } |
| 184 | |
| 185 | fn everything() -> ImportOptions { |
| 186 | ImportOptions { |
| 187 | labels: true, |
| 188 | issues: true, |
| 189 | pulls: true, |
| 190 | releases: true, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fn fake() -> Fake { |
| 195 | let mut fake = Fake { |
| 196 | labels: vec![ExtLabel { |
| 197 | name: "bug".into(), |
| 198 | color: "#d73a4a".into(), |
| 199 | description: "Broken".into(), |
| 200 | }], |
| 201 | milestones: vec![ExtMilestone { |
| 202 | id: "1".into(), |
| 203 | title: "v1".into(), |
| 204 | closed: true, |
| 205 | due_on: Some("2020-05-06".into()), |
| 206 | ..ExtMilestone::default() |
| 207 | }], |
| 208 | issues: vec![ |
| 209 | ExtIssue { |
| 210 | number: 1, |
| 211 | title: "It breaks".into(), |
| 212 | body: "steps".into(), |
| 213 | closed: true, |
| 214 | author: "octocat".into(), |
| 215 | labels: vec!["bug".into()], |
| 216 | milestone: Some("v1".into()), |
| 217 | created_at: "2020-01-02T03:04:05Z".into(), |
| 218 | url: "https://github.com/tahinli/project/issues/1".into(), |
| 219 | ..ExtIssue::default() |
| 220 | }, |
| 221 | ExtIssue { |
| 222 | number: 2, |
| 223 | title: "Add the thing".into(), |
| 224 | body: "patch".into(), |
| 225 | closed: true, |
| 226 | merged: true, |
| 227 | is_pr: true, |
| 228 | author: "hubot".into(), |
| 229 | created_at: "2020-02-02T00:00:00Z".into(), |
| 230 | url: "https://github.com/tahinli/project/pull/2".into(), |
| 231 | ..ExtIssue::default() |
| 232 | }, |
| 233 | ], |
| 234 | releases: vec![ExtRelease { |
| 235 | id: "7".into(), |
| 236 | tag: "v1".into(), |
| 237 | title: "First".into(), |
| 238 | body: "notes".into(), |
| 239 | author: "octocat".into(), |
| 240 | assets: vec![ExtAsset { |
| 241 | id: "9".into(), |
| 242 | name: "app 1.tar.gz".into(), |
| 243 | size: 5, |
| 244 | content_type: "application/gzip".into(), |
| 245 | url: "https://api.github.com/assets/9".into(), |
| 246 | }], |
| 247 | ..ExtRelease::default() |
| 248 | }], |
| 249 | ..Fake::default() |
| 250 | }; |
| 251 | fake.comments.insert( |
| 252 | 1, |
| 253 | vec![ExtComment { |
| 254 | id: "100".into(), |
| 255 | author: "hubot".into(), |
| 256 | body: "same here".into(), |
| 257 | created_at: "2020-01-03T00:00:00Z".into(), |
| 258 | url: "https://github.com/tahinli/project/issues/1#c100".into(), |
| 259 | }], |
| 260 | ); |
| 261 | fake.files |
| 262 | .insert("https://api.github.com/assets/9".into(), b"hello".to_vec()); |
| 263 | fake |
| 264 | } |
| 265 | |
| 266 | #[tokio::test] |
| 267 | async fn git_url_import_copies_all_branches_and_tags_into_a_normal_repo() { |
| 268 | let fx = fixture().await; |
| 269 | let job = new_job(&fx, Source::Git).await; |
| 270 | let git_spec = spec(Source::Git, ImportOptions::default()); |
| 271 | run_job::<Fake>( |
| 272 | &fx.state, |
| 273 | &job, |
| 274 | &git_spec, |
| 275 | None, |
| 276 | &Transport::Local(fx.upstream.clone()), |
| 277 | ) |
| 278 | .await; |
| 279 | |
| 280 | let job = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); |
| 281 | assert_eq!(job.state, ImportState::Done, "{:?}", job.error); |
| 282 | let dir = forge_git::repo::repo_path(&fx.state.git.data_dir, &fx.repo.id).unwrap(); |
| 283 | let refs = crate::mirrors::ref_snapshot(&fx.state, &dir).await.unwrap(); |
| 284 | assert!(refs.contains_key("refs/heads/master")); |
| 285 | assert!(refs.contains_key("refs/heads/dev")); |
| 286 | assert!(refs.contains_key("refs/tags/v1")); |
| 287 | // The remote's default branch becomes the repo's, and no mirror row exists: it is a normal repo. |
| 288 | let repo = fx |
| 289 | .state |
| 290 | .store |
| 291 | .repo_by_id(&fx.repo.id) |
| 292 | .await |
| 293 | .unwrap() |
| 294 | .unwrap(); |
| 295 | assert_eq!(repo.default_branch, "master"); |
| 296 | assert!( |
| 297 | fx.state |
| 298 | .store |
| 299 | .pull_mirror(&fx.repo.id) |
| 300 | .await |
| 301 | .unwrap() |
| 302 | .is_none() |
| 303 | ); |
| 304 | |
| 305 | // A fetch from a missing remote fails its job, and the failure is recorded. |
| 306 | let bad_job = new_job(&fx, Source::Git).await; |
| 307 | let bad_spec = spec(Source::Git, ImportOptions::default()); |
| 308 | run_job::<Fake>( |
| 309 | &fx.state, |
| 310 | &bad_job, |
| 311 | &bad_spec, |
| 312 | None, |
| 313 | &Transport::Local("/nonexistent/path.git".into()), |
| 314 | ) |
| 315 | .await; |
| 316 | let bad_job = fx |
| 317 | .state |
| 318 | .store |
| 319 | .import_job(&bad_job.id) |
| 320 | .await |
| 321 | .unwrap() |
| 322 | .unwrap(); |
| 323 | assert_eq!(bad_job.state, ImportState::Failed); |
| 324 | assert!(bad_job.error.is_some()); |
| 325 | } |
| 326 | |
| 327 | #[tokio::test] |
| 328 | async fn full_api_import_brings_issues_pulls_comments_labels_and_releases() { |
| 329 | let fx = fixture().await; |
| 330 | let api = fake(); |
| 331 | let job = new_job(&fx, Source::GitHub).await; |
| 332 | let spec = spec(Source::GitHub, everything()); |
| 333 | run_job( |
| 334 | &fx.state, |
| 335 | &job, |
| 336 | &spec, |
| 337 | Some(&api), |
| 338 | &Transport::Local(fx.upstream.clone()), |
| 339 | ) |
| 340 | .await; |
| 341 | |
| 342 | let store = &fx.state.store; |
| 343 | let job = store.import_job(&job.id).await.unwrap().unwrap(); |
| 344 | assert_eq!(job.state, ImportState::Done, "{:?}", job.error); |
| 345 | |
| 346 | let all = IssueFilter { |
| 347 | state: None, |
| 348 | per_page: 100, |
| 349 | ..IssueFilter::default() |
| 350 | }; |
| 351 | let (items, total) = store.list_issues(&fx.repo.id, &all).await.unwrap(); |
| 352 | assert_eq!(total, 2); |
| 353 | let issue = items.iter().find(|i| i.issue.title == "It breaks").unwrap(); |
| 354 | assert_eq!(issue.issue.state, IssueState::Closed); |
| 355 | assert_eq!( |
| 356 | issue.issue.author_id, fx.user.id, |
| 357 | "the importer owns the row" |
| 358 | ); |
| 359 | assert!( |
| 360 | issue |
| 361 | .issue |
| 362 | .body |
| 363 | .starts_with("> Originally by \\@octocat on GitHub, 2020-01-02"), |
| 364 | "{}", |
| 365 | issue.issue.body |
| 366 | ); |
| 367 | assert!(issue.labels.iter().any(|l| l.name == "bug")); |
| 368 | assert!(issue.issue.milestone_id.is_some()); |
| 369 | |
| 370 | let pull = items |
| 371 | .iter() |
| 372 | .find(|i| i.issue.title == "Add the thing") |
| 373 | .unwrap(); |
| 374 | assert!(pull.labels.iter().any(|l| l.name == "imported PR")); |
| 375 | assert!(pull.issue.body.contains("Imported pull request (merged)")); |
| 376 | assert!(pull.issue.body.contains("Originally by \\@hubot")); |
| 377 | |
| 378 | let comments = store.list_comments(&issue.issue.id).await.unwrap(); |
| 379 | assert_eq!(comments.len(), 1); |
| 380 | assert!( |
| 381 | comments[0] |
| 382 | .body |
| 383 | .contains("Originally by \\@hubot on GitHub") |
| 384 | ); |
| 385 | assert!(comments[0].body.ends_with("same here")); |
| 386 | |
| 387 | let release = store |
| 388 | .release_by_tag(&fx.repo.id, "v1") |
| 389 | .await |
| 390 | .unwrap() |
| 391 | .unwrap(); |
| 392 | assert!(release.body.contains("notes")); |
| 393 | let assets = store.list_release_assets(&release.id).await.unwrap(); |
| 394 | assert_eq!(assets.len(), 1); |
| 395 | assert_eq!(assets[0].name, "app_1.tar.gz"); |
| 396 | assert_eq!(assets[0].size, 5); |
| 397 | |
| 398 | // A second run of the same import creates nothing twice. |
| 399 | let again = new_job(&fx, Source::GitHub).await; |
| 400 | run_job( |
| 401 | &fx.state, |
| 402 | &again, |
| 403 | &spec, |
| 404 | Some(&api), |
| 405 | &Transport::Local(fx.upstream.clone()), |
| 406 | ) |
| 407 | .await; |
| 408 | let again = store.import_job(&again.id).await.unwrap().unwrap(); |
| 409 | assert_eq!(again.state, ImportState::Done, "{:?}", again.error); |
| 410 | let (items, total) = store.list_issues(&fx.repo.id, &all).await.unwrap(); |
| 411 | assert_eq!(total, 2); |
| 412 | let first = items.iter().find(|i| i.issue.title == "It breaks").unwrap(); |
| 413 | assert_eq!(store.list_comments(&first.issue.id).await.unwrap().len(), 1); |
| 414 | assert_eq!(store.list_releases(&fx.repo.id).await.unwrap().len(), 1); |
| 415 | let release = store |
| 416 | .release_by_tag(&fx.repo.id, "v1") |
| 417 | .await |
| 418 | .unwrap() |
| 419 | .unwrap(); |
| 420 | assert_eq!( |
| 421 | store.list_release_assets(&release.id).await.unwrap().len(), |
| 422 | 1 |
| 423 | ); |
| 424 | // The labels: the imported one, plus "imported PR", each once. |
| 425 | let labels = store.list_labels(&fx.repo.id).await.unwrap(); |
| 426 | assert_eq!(labels.iter().filter(|l| l.name == "bug").count(), 1); |
| 427 | } |
| 428 | |
| 429 | #[tokio::test] |
| 430 | async fn a_rate_limit_fails_the_job_clearly_and_a_retry_finishes_it() { |
| 431 | let fx = fixture().await; |
| 432 | let api = fake(); |
| 433 | api.comment_failures |
| 434 | .lock() |
| 435 | .unwrap() |
| 436 | .push(ApiError::RateLimited { wait_secs: 3600 }); |
| 437 | let job = new_job(&fx, Source::GitHub).await; |
| 438 | let spec = spec(Source::GitHub, everything()); |
| 439 | let transport = Transport::Local(fx.upstream.clone()); |
| 440 | run_job(&fx.state, &job, &spec, Some(&api), &transport).await; |
| 441 | |
| 442 | let failed = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); |
| 443 | assert_eq!(failed.state, ImportState::Failed); |
| 444 | let message = failed.error.clone().unwrap(); |
| 445 | assert!(message.contains("rate limit"), "{message}"); |
| 446 | let events = fx |
| 447 | .state |
| 448 | .store |
| 449 | .list_import_events(&job.id, 50) |
| 450 | .await |
| 451 | .unwrap(); |
| 452 | assert!(events.iter().any(|e| e.level == "error")); |
| 453 | |
| 454 | // Retry on the same job: the first issue exists already and is not created again. |
| 455 | run_job(&fx.state, &job, &spec, Some(&api), &transport).await; |
| 456 | let done = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); |
| 457 | assert_eq!(done.state, ImportState::Done, "{:?}", done.error); |
| 458 | assert!(done.error.is_none()); |
| 459 | let all = IssueFilter { |
| 460 | state: None, |
| 461 | per_page: 100, |
| 462 | ..IssueFilter::default() |
| 463 | }; |
| 464 | let (_, total) = fx.state.store.list_issues(&fx.repo.id, &all).await.unwrap(); |
| 465 | assert_eq!(total, 2); |
| 466 | } |
| 467 | |
| 468 | #[tokio::test] |
| 469 | async fn the_token_is_never_written_to_the_database() { |
| 470 | let fx = fixture().await; |
| 471 | let api = fake(); |
| 472 | api.comment_failures |
| 473 | .lock() |
| 474 | .unwrap() |
| 475 | .push(ApiError::Http(format!("boom {}", "x"))); |
| 476 | let job = new_job(&fx, Source::GitHub).await; |
| 477 | let spec = spec(Source::GitHub, everything()); |
| 478 | run_job( |
| 479 | &fx.state, |
| 480 | &job, |
| 481 | &spec, |
| 482 | Some(&api), |
| 483 | &Transport::Local(fx.upstream.clone()), |
| 484 | ) |
| 485 | .await; |
| 486 | let job = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); |
| 487 | let events = fx |
| 488 | .state |
| 489 | .store |
| 490 | .list_import_events(&job.id, 500) |
| 491 | .await |
| 492 | .unwrap(); |
| 493 | let mut text = format!("{job:?}"); |
| 494 | for e in events { |
| 495 | text.push_str(&e.message); |
| 496 | } |
| 497 | assert!( |
| 498 | !text.contains(TOKEN), |
| 499 | "the token leaked into stored job data" |
| 500 | ); |
| 501 | assert!( |
| 502 | !format!("{spec:?}").contains(TOKEN), |
| 503 | "Debug prints the token" |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | #[tokio::test] |
| 508 | async fn retry_submit_leaves_another_users_failed_job_alone() { |
| 509 | let fx = fixture().await; |
| 510 | let other = fx |
| 511 | .state |
| 512 | .store |
| 513 | .create_user("mallory", None, "unused", false) |
| 514 | .await |
| 515 | .unwrap(); |
| 516 | let job = new_job(&fx, Source::GitHub).await; |
| 517 | fx.state |
| 518 | .store |
| 519 | .set_import_state(&job.id, ImportState::Failed, Some("rate limit")) |
| 520 | .await |
| 521 | .unwrap(); |
| 522 | create_session( |
| 523 | &fx.state.store, |
| 524 | &other.id, |
| 525 | "retry-other-session", |
| 526 | "retry-csrf", |
| 527 | ) |
| 528 | .await; |
| 529 | |
| 530 | let router = app_router(fx.state.clone()); |
| 531 | let cookie = format!("__Host-{SESSION_COOKIE}=retry-other-session"); |
| 532 | |
| 533 | // The static /import/retry is not taken for /import/{job_id}: mallory sees her own empty list. |
| 534 | let page = router |
| 535 | .handle( |
| 536 | Request::builder() |
| 537 | .method("GET") |
| 538 | .uri("/import/retry") |
| 539 | .header("cookie", cookie.as_str()) |
| 540 | .body(Body::empty()) |
| 541 | .unwrap(), |
| 542 | ) |
| 543 | .await; |
| 544 | assert_eq!(page.status().as_u16(), 200); |
| 545 | let bytes = to_bytes(page.into_body(), usize::MAX).await.unwrap(); |
| 546 | assert!(String::from_utf8_lossy(&bytes).contains("No failed imports")); |
| 547 | |
| 548 | // The form names the owner's job, and mallory is signed in: the page redirects, nothing starts. |
| 549 | let form = format!("csrf=retry-csrf&token=&sel_0={}", job.id); |
| 550 | let res = router |
| 551 | .handle( |
| 552 | Request::builder() |
| 553 | .method("POST") |
| 554 | .uri("/import/retry") |
| 555 | .header("cookie", cookie.as_str()) |
| 556 | .header("content-type", "application/x-www-form-urlencoded") |
| 557 | .body(Body::from(form)) |
| 558 | .unwrap(), |
| 559 | ) |
| 560 | .await; |
| 561 | assert_eq!(res.status().as_u16(), 303); |
| 562 | let _ = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 563 | |
| 564 | let after = fx.state.store.import_job(&job.id).await.unwrap().unwrap(); |
| 565 | assert_eq!(after.state, ImportState::Failed); |
| 566 | assert_eq!(after.error.as_deref(), Some("rate limit")); |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn the_origin_note_escapes_the_mention_and_cleans_the_name() { |
| 571 | let note = origin_note( |
| 572 | "GitHub", |
| 573 | "evil @tahinli\n", |
| 574 | "2021-03-04T00:00:00Z", |
| 575 | "https://github.com/o/r/issues/1", |
| 576 | ); |
| 577 | assert!(note.starts_with("> Originally by \\@eviltahinli on GitHub, 2021-03-04")); |
| 578 | assert!(note.contains("[original](https://github.com/o/r/issues/1)")); |
| 579 | assert!(note.ends_with("\n\n")); |
| 580 | let none = origin_note("GitHub", "", "", "javascript:alert(1)"); |
| 581 | assert!(none.contains("\\@unknown")); |
| 582 | assert!(!none.contains("javascript")); |
| 583 | } |
| 584 | |
| 585 | #[test] |
| 586 | fn bodies_are_cut_at_the_limit_on_a_char_boundary() { |
| 587 | let long = "\u{e9}".repeat(forge_core::issues::MAX_BODY); |
| 588 | let text = with_note("> note\n\n", &long); |
| 589 | assert!(text.len() <= forge_core::issues::MAX_BODY); |
| 590 | assert!(text.ends_with("(cut)")); |
| 591 | } |
| 592 | |
| 593 | #[test] |
| 594 | fn asset_names_are_made_safe() { |
| 595 | assert_eq!(asset_name("app 1.tar.gz"), "app_1.tar.gz"); |
| 596 | assert_eq!(asset_name("../../etc/passwd"), "_.._etc_passwd"); |
| 597 | assert_eq!(asset_name(".hidden"), "hidden"); |
| 598 | assert_eq!(asset_name("###"), "___"); |
| 599 | assert_eq!(asset_name(""), "asset"); |
| 600 | assert!(forge_core::releases::valid_asset_name(&asset_name( |
| 601 | "a b/c?d" |
| 602 | ))); |
| 603 | } |
| 604 | |
| 605 | #[path = "forgejo_tests.rs"] |
| 606 | mod forgejo; |