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 | //! Tests of the Gitea compatible read routes (api/gitea.rs). Each one asserts the JSON fields the |
| 2 | //! Forgejo Gitea downloader reads, plus the pagination headers and the token rules. |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | |
| 6 | use base64::prelude::{BASE64_STANDARD, Engine}; |
| 7 | use forge_core::reactions::{Reaction, ReactionTarget}; |
| 8 | use forge_core::reviews::{ReviewState, Side}; |
| 9 | use forge_core::token::{generate_token, hash_token}; |
| 10 | use forge_core::{Role, Store, Visibility}; |
| 11 | use forge_git::repo::{init_bare, repo_path}; |
| 12 | use http::HeaderMap; |
| 13 | use http::header::{AUTHORIZATION, CONTENT_TYPE}; |
| 14 | use serde_json::Value; |
| 15 | use topcoat::router::request::Request; |
| 16 | use topcoat::router::{Body, Router, to_bytes}; |
| 17 | |
| 18 | use crate::test_support::{app_router, test_state}; |
| 19 | |
| 20 | const OWNER: &str = "tahinli"; |
| 21 | const REPO: &str = "proj"; |
| 22 | |
| 23 | fn git(dir: &Path, args: &[&str]) -> String { |
| 24 | let out = std::process::Command::new("git") |
| 25 | .current_dir(dir) |
| 26 | .args(args) |
| 27 | .env("GIT_AUTHOR_NAME", "test") |
| 28 | .env("GIT_AUTHOR_EMAIL", "test@example.com") |
| 29 | .env("GIT_COMMITTER_NAME", "test") |
| 30 | .env("GIT_COMMITTER_EMAIL", "test@example.com") |
| 31 | .output() |
| 32 | .expect("git runs"); |
| 33 | assert!(out.status.success(), "git {args:?} failed"); |
| 34 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 35 | } |
| 36 | |
| 37 | struct Fixture { |
| 38 | _tmp: tempfile::TempDir, |
| 39 | router: Router, |
| 40 | owner_token: String, |
| 41 | bob_token: String, |
| 42 | carol_token: String, |
| 43 | oid: String, |
| 44 | } |
| 45 | |
| 46 | async fn token_for(store: &Store, user_id: &str) -> String { |
| 47 | let raw = generate_token(); |
| 48 | store |
| 49 | .create_token( |
| 50 | user_id, |
| 51 | "test", |
| 52 | &hash_token(&raw), |
| 53 | &["repo".to_owned()], |
| 54 | None, |
| 55 | ) |
| 56 | .await |
| 57 | .expect("token"); |
| 58 | raw |
| 59 | } |
| 60 | |
| 61 | async fn seed_git(state: &crate::state::AppState, tmp: &Path, repo_id: &str) -> String { |
| 62 | let bare: PathBuf = repo_path(&state.git.data_dir, repo_id).expect("repo path"); |
| 63 | init_bare(&state.git, &bare, "main").await.expect("init"); |
| 64 | let work = tmp.join(format!("work-{repo_id}")); |
| 65 | git(tmp, &["init", "-b", "main", work.to_str().expect("utf8")]); |
| 66 | std::fs::write(work.join("file.txt"), "one").expect("write"); |
| 67 | git(&work, &["add", "file.txt"]); |
| 68 | git(&work, &["commit", "-m", "one"]); |
| 69 | git(&work, &["push", bare.to_str().expect("utf8"), "main"]); |
| 70 | git(&work, &["rev-parse", "HEAD"]) |
| 71 | } |
| 72 | |
| 73 | /// `tahinli/proj` is public with an issue (label, milestone, assignee, two comments, reactions), |
| 74 | /// a pull request with one review and one line comment, and a release with one asset. |
| 75 | /// `tahinli/secret` is private; bob can read `proj` only, carol nothing. |
| 76 | async fn fixture() -> Fixture { |
| 77 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 78 | let state = test_state(tmp.path()).await; |
| 79 | let store = state.store.clone(); |
| 80 | let owner = store |
| 81 | .create_user(OWNER, None, "x", false) |
| 82 | .await |
| 83 | .expect("owner"); |
| 84 | let bob = store |
| 85 | .create_user("bob", None, "x", false) |
| 86 | .await |
| 87 | .expect("bob"); |
| 88 | let carol = store |
| 89 | .create_user("carol", None, "x", false) |
| 90 | .await |
| 91 | .expect("carol"); |
| 92 | let repo = store |
| 93 | .create_repo(&owner.id, REPO, "A project", Visibility::Public, "main") |
| 94 | .await |
| 95 | .expect("repo"); |
| 96 | store |
| 97 | .set_collaborator(&repo.id, &bob.id, Role::Read) |
| 98 | .await |
| 99 | .expect("collaborator"); |
| 100 | let secret = store |
| 101 | .create_repo(&owner.id, "secret", "", Visibility::Private, "main") |
| 102 | .await |
| 103 | .expect("secret"); |
| 104 | let oid = seed_git(&state, tmp.path(), &repo.id).await; |
| 105 | seed_git(&state, tmp.path(), &secret.id).await; |
| 106 | |
| 107 | let label = store |
| 108 | .create_label(&repo.id, "bug", "#d73a4a", "Something is broken") |
| 109 | .await |
| 110 | .expect("label"); |
| 111 | let milestone = store |
| 112 | .create_milestone(&repo.id, "v1", "First", Some("2026-12-01")) |
| 113 | .await |
| 114 | .expect("milestone"); |
| 115 | let issue = store |
| 116 | .create_issue(&repo.id, &owner.id, "It breaks", "Steps follow") |
| 117 | .await |
| 118 | .expect("issue"); |
| 119 | store |
| 120 | .set_issue_labels(&issue.id, &owner.id, std::slice::from_ref(&label.id)) |
| 121 | .await |
| 122 | .expect("labels"); |
| 123 | store |
| 124 | .set_issue_milestone(&issue.id, &owner.id, Some(&milestone.id)) |
| 125 | .await |
| 126 | .expect("milestone set"); |
| 127 | store |
| 128 | .set_issue_assignees(&issue.id, &owner.id, &[OWNER.to_owned()]) |
| 129 | .await |
| 130 | .expect("assignees"); |
| 131 | let first = store |
| 132 | .add_comment(&issue.id, &bob.id, "I see it too") |
| 133 | .await |
| 134 | .expect("comment"); |
| 135 | store |
| 136 | .add_comment(&issue.id, &owner.id, "Looking") |
| 137 | .await |
| 138 | .expect("comment 2"); |
| 139 | store |
| 140 | .toggle_reaction(ReactionTarget::Issue, &issue.id, &bob.id, Reaction::Heart) |
| 141 | .await |
| 142 | .expect("issue reaction"); |
| 143 | store |
| 144 | .toggle_reaction( |
| 145 | ReactionTarget::Comment, |
| 146 | &first.id, |
| 147 | &owner.id, |
| 148 | Reaction::PlusOne, |
| 149 | ) |
| 150 | .await |
| 151 | .expect("comment reaction"); |
| 152 | |
| 153 | let pull = store |
| 154 | .create_pull( |
| 155 | &repo.id, |
| 156 | &owner.id, |
| 157 | "Add thing", |
| 158 | "Body", |
| 159 | "main", |
| 160 | &repo.id, |
| 161 | "feature", |
| 162 | &oid, |
| 163 | &oid, |
| 164 | ) |
| 165 | .await |
| 166 | .expect("pull"); |
| 167 | store |
| 168 | .add_review_comment( |
| 169 | &pull.issue.id, |
| 170 | &bob.id, |
| 171 | true, |
| 172 | "file.txt", |
| 173 | Side::New, |
| 174 | 1, |
| 175 | &oid, |
| 176 | "@@ -1 +1 @@", |
| 177 | "nit", |
| 178 | ) |
| 179 | .await |
| 180 | .expect("review comment"); |
| 181 | store |
| 182 | .submit_review( |
| 183 | &pull.issue.id, |
| 184 | &bob.id, |
| 185 | ReviewState::ChangesRequested, |
| 186 | "please fix", |
| 187 | &oid, |
| 188 | ) |
| 189 | .await |
| 190 | .expect("review"); |
| 191 | |
| 192 | let owner_token = token_for(&store, &owner.id).await; |
| 193 | let bob_token = token_for(&store, &bob.id).await; |
| 194 | let carol_token = token_for(&store, &carol.id).await; |
| 195 | let router = app_router(state); |
| 196 | let f = Fixture { |
| 197 | _tmp: tmp, |
| 198 | router, |
| 199 | owner_token, |
| 200 | bob_token, |
| 201 | carol_token, |
| 202 | oid, |
| 203 | }; |
| 204 | |
| 205 | let (status, _, body) = f |
| 206 | .get_with( |
| 207 | "POST", |
| 208 | &format!("/api/v1/repos/{OWNER}/{REPO}/releases"), |
| 209 | Some(&format!("token {}", f.owner_token)), |
| 210 | br#"{"tag":"v1.0","target":"main","title":"One","body":"notes"}"#.to_vec(), |
| 211 | "application/json", |
| 212 | ) |
| 213 | .await; |
| 214 | assert_eq!(status, 201, "{body}"); |
| 215 | let uid = body["uid"].as_str().expect("uid").to_owned(); |
| 216 | let (status, _, body) = f |
| 217 | .get_with( |
| 218 | "PUT", |
| 219 | &format!("/api/v1/repos/{OWNER}/{REPO}/releases/{uid}/assets/app.bin"), |
| 220 | Some(&format!("token {}", f.owner_token)), |
| 221 | b"hello".to_vec(), |
| 222 | "application/octet-stream", |
| 223 | ) |
| 224 | .await; |
| 225 | assert_eq!(status, 201, "{body}"); |
| 226 | f |
| 227 | } |
| 228 | |
| 229 | impl Fixture { |
| 230 | async fn get_with( |
| 231 | &self, |
| 232 | method: &str, |
| 233 | uri: &str, |
| 234 | auth: Option<&str>, |
| 235 | body: Vec<u8>, |
| 236 | content_type: &str, |
| 237 | ) -> (u16, HeaderMap, Value) { |
| 238 | let mut builder = Request::builder().method(method).uri(uri); |
| 239 | if let Some(auth) = auth { |
| 240 | builder = builder.header(AUTHORIZATION, auth); |
| 241 | } |
| 242 | builder = builder.header(CONTENT_TYPE, content_type); |
| 243 | let res = self |
| 244 | .router |
| 245 | .handle(builder.body(Body::from(body)).unwrap()) |
| 246 | .await; |
| 247 | let status = res.status().as_u16(); |
| 248 | let headers = res.headers().clone(); |
| 249 | let bytes = to_bytes(res.into_body(), usize::MAX).await.unwrap(); |
| 250 | let json = serde_json::from_slice(&bytes).unwrap_or(Value::Null); |
| 251 | (status, headers, json) |
| 252 | } |
| 253 | |
| 254 | /// GET as `tahinli` with the `Authorization: token` style the Gitea SDK sends. |
| 255 | async fn get(&self, path: &str) -> (u16, HeaderMap, Value) { |
| 256 | self.get_with( |
| 257 | "GET", |
| 258 | path, |
| 259 | Some(&format!("token {}", self.owner_token)), |
| 260 | Vec::new(), |
| 261 | "application/json", |
| 262 | ) |
| 263 | .await |
| 264 | } |
| 265 | |
| 266 | async fn get_as(&self, path: &str, auth: Option<String>) -> (u16, HeaderMap, Value) { |
| 267 | self.get_with("GET", path, auth.as_deref(), Vec::new(), "application/json") |
| 268 | .await |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | fn repo_path_of(rest: &str) -> String { |
| 273 | format!("/api/v1/repos/{OWNER}/{REPO}{rest}") |
| 274 | } |
| 275 | |
| 276 | /// Read routes that share one fixture: version and settings, the repo, its issues, and |
| 277 | /// pagination. None of them changes state, so the scenarios cannot interfere. |
| 278 | #[tokio::test] |
| 279 | async fn version_repo_issues_and_pagination() { |
| 280 | let f = fixture().await; |
| 281 | let (status, _, body) = f.get_as("/api/v1/version", None).await; |
| 282 | assert_eq!(status, 200); |
| 283 | assert_eq!(body["version"], "1.22.0"); |
| 284 | let (status, _, body) = f.get_as("/api/v1/settings/api", None).await; |
| 285 | assert_eq!(status, 200); |
| 286 | assert_eq!(body["max_response_items"], 50); |
| 287 | |
| 288 | // The repo, its topics, labels and milestones. |
| 289 | let (status, _, repo) = f.get(&repo_path_of("")).await; |
| 290 | assert_eq!(status, 200); |
| 291 | assert_eq!(repo["name"], REPO); |
| 292 | assert_eq!(repo["owner"]["login"], OWNER); |
| 293 | assert_eq!(repo["private"], false); |
| 294 | assert_eq!(repo["description"], "A project"); |
| 295 | assert_eq!(repo["default_branch"], "main"); |
| 296 | assert_eq!( |
| 297 | repo["clone_url"], |
| 298 | format!("https://forge.test/{OWNER}/{REPO}.git") |
| 299 | ); |
| 300 | assert_eq!( |
| 301 | repo["html_url"], |
| 302 | format!("https://forge.test/{OWNER}/{REPO}") |
| 303 | ); |
| 304 | |
| 305 | let (_, _, topics) = f.get(&repo_path_of("/topics")).await; |
| 306 | assert_eq!(topics["topics"], serde_json::json!([])); |
| 307 | |
| 308 | let (_, headers, labels) = f.get(&repo_path_of("/labels?page=1&limit=10")).await; |
| 309 | let label = labels |
| 310 | .as_array() |
| 311 | .unwrap() |
| 312 | .iter() |
| 313 | .find(|l| l["name"] == "bug") |
| 314 | .unwrap(); |
| 315 | assert_eq!(label["color"], "d73a4a"); |
| 316 | assert_eq!(label["description"], "Something is broken"); |
| 317 | assert!(headers.contains_key("x-total-count")); |
| 318 | |
| 319 | let (_, _, milestones) = f |
| 320 | .get(&repo_path_of("/milestones?state=all&page=1&limit=10")) |
| 321 | .await; |
| 322 | let m = &milestones.as_array().unwrap()[0]; |
| 323 | assert_eq!(m["title"], "v1"); |
| 324 | assert_eq!(m["description"], "First"); |
| 325 | assert_eq!(m["state"], "open"); |
| 326 | assert_eq!(m["due_on"], "2026-12-01T00:00:00Z"); |
| 327 | assert!(m["created_at"].is_string()); |
| 328 | |
| 329 | // Issues with their labels, milestone, assignees, comments and reactions. |
| 330 | let (status, headers, issues) = f |
| 331 | .get(&repo_path_of( |
| 332 | "/issues?type=issues&state=all&page=1&limit=10", |
| 333 | )) |
| 334 | .await; |
| 335 | assert_eq!(status, 200); |
| 336 | assert_eq!(headers["x-total-count"], "1"); |
| 337 | let list = issues.as_array().unwrap(); |
| 338 | assert_eq!(list.len(), 1, "pulls are not issues"); |
| 339 | let issue = &list[0]; |
| 340 | assert_eq!(issue["number"], 1); |
| 341 | assert_eq!(issue["title"], "It breaks"); |
| 342 | assert_eq!(issue["body"], "Steps follow"); |
| 343 | assert_eq!(issue["state"], "open"); |
| 344 | assert_eq!(issue["is_locked"], false); |
| 345 | assert_eq!(issue["user"]["login"], OWNER); |
| 346 | assert!(issue["user"]["id"].as_i64().unwrap() > 0); |
| 347 | assert_eq!(issue["labels"][0]["name"], "bug"); |
| 348 | assert_eq!(issue["milestone"]["title"], "v1"); |
| 349 | assert_eq!(issue["assignees"][0]["login"], OWNER); |
| 350 | assert!(issue["created_at"].is_string() && issue["updated_at"].is_string()); |
| 351 | assert!(issue["pull_request"].is_null()); |
| 352 | |
| 353 | let (_, headers, comments) = f |
| 354 | .get(&repo_path_of("/issues/1/comments?page=1&limit=10")) |
| 355 | .await; |
| 356 | assert_eq!(headers["x-total-count"], "2"); |
| 357 | let comments = comments.as_array().unwrap(); |
| 358 | assert_eq!(comments[0]["body"], "I see it too"); |
| 359 | assert_eq!(comments[0]["user"]["login"], "bob"); |
| 360 | assert!(comments[0]["id"].as_i64().unwrap() > 0); |
| 361 | assert!(comments[0]["created_at"].is_string() && comments[0]["updated_at"].is_string()); |
| 362 | assert_ne!(comments[0]["id"], comments[1]["id"]); |
| 363 | |
| 364 | let (_, _, reactions) = f.get(&repo_path_of("/issues/1/reactions")).await; |
| 365 | assert_eq!(reactions[0]["reaction"], "heart"); |
| 366 | assert_eq!(reactions[0]["user"]["login"], "bob"); |
| 367 | |
| 368 | let id = comments[0]["id"].as_i64().unwrap(); |
| 369 | let (status, _, reactions) = f |
| 370 | .get(&repo_path_of(&format!("/issues/comments/{id}/reactions"))) |
| 371 | .await; |
| 372 | assert_eq!(status, 200); |
| 373 | assert_eq!(reactions[0]["reaction"], "+1"); |
| 374 | assert_eq!(reactions[0]["user"]["login"], OWNER); |
| 375 | |
| 376 | // Pagination headers, the Link header, and the empty last page. |
| 377 | let (status, headers, items) = f |
| 378 | .get(&repo_path_of("/issues/1/comments?page=2&limit=1")) |
| 379 | .await; |
| 380 | assert_eq!(status, 200); |
| 381 | assert_eq!(headers["x-total-count"], "2"); |
| 382 | assert_eq!(items.as_array().unwrap().len(), 1); |
| 383 | assert_eq!(items[0]["body"], "Looking"); |
| 384 | let link = headers["link"].to_str().unwrap(); |
| 385 | assert!( |
| 386 | link.contains("rel=\"prev\"") && link.contains("rel=\"first\""), |
| 387 | "{link}" |
| 388 | ); |
| 389 | assert!(!link.contains("rel=\"next\""), "{link}"); |
| 390 | assert!(link.contains("https://forge.test/api/v1/repos/tahinli/proj/issues/1/comments?")); |
| 391 | |
| 392 | let (_, headers, items) = f |
| 393 | .get(&repo_path_of("/issues/1/comments?page=1&limit=1")) |
| 394 | .await; |
| 395 | assert_eq!(items.as_array().unwrap().len(), 1); |
| 396 | let link = headers["link"].to_str().unwrap(); |
| 397 | assert!( |
| 398 | link.contains("rel=\"next\"") && link.contains("rel=\"last\""), |
| 399 | "{link}" |
| 400 | ); |
| 401 | |
| 402 | // Past the last page the list is empty, which ends the downloader's loop. |
| 403 | let (_, _, items) = f |
| 404 | .get(&repo_path_of("/issues/1/comments?page=3&limit=1")) |
| 405 | .await; |
| 406 | assert_eq!(items.as_array().unwrap().len(), 0); |
| 407 | } |
| 408 | |
| 409 | /// Pull requests, reviews and releases, which share one fixture as read-only scenarios. |
| 410 | #[tokio::test] |
| 411 | async fn pulls_reviews_and_releases_in_gitea_shape() { |
| 412 | let f = fixture().await; |
| 413 | // Pull requests and their reviews and review comments. |
| 414 | let (status, headers, pulls) = f |
| 415 | .get(&repo_path_of("/pulls?state=all&page=1&limit=10")) |
| 416 | .await; |
| 417 | assert_eq!(status, 200); |
| 418 | assert_eq!(headers["x-total-count"], "1"); |
| 419 | let pr = &pulls[0]; |
| 420 | assert_eq!(pr["number"], 2); |
| 421 | assert_eq!(pr["title"], "Add thing"); |
| 422 | assert_eq!(pr["state"], "open"); |
| 423 | assert_eq!(pr["merged"], false); |
| 424 | assert_eq!(pr["user"]["login"], OWNER); |
| 425 | assert_eq!(pr["head"]["ref"], "feature"); |
| 426 | assert_eq!(pr["head"]["sha"], f.oid); |
| 427 | assert_eq!(pr["head"]["repo"]["owner"]["login"], OWNER); |
| 428 | assert_eq!( |
| 429 | pr["head"]["repo"]["clone_url"], |
| 430 | format!("https://forge.test/{OWNER}/{REPO}.git") |
| 431 | ); |
| 432 | assert_eq!(pr["base"]["ref"], "main"); |
| 433 | assert_eq!(pr["base"]["sha"], f.oid); |
| 434 | assert!(pr["created_at"].is_string() && pr["updated_at"].is_string()); |
| 435 | |
| 436 | let (status, _, reviews) = f |
| 437 | .get(&repo_path_of("/pulls/2/reviews?page=1&limit=10")) |
| 438 | .await; |
| 439 | assert_eq!(status, 200); |
| 440 | let review = &reviews[0]; |
| 441 | assert_eq!(review["state"], "CHANGES_REQUESTED"); |
| 442 | assert_eq!(review["body"], "please fix"); |
| 443 | assert_eq!(review["user"]["login"], "bob"); |
| 444 | assert_eq!(review["commit_id"], f.oid); |
| 445 | assert_eq!(review["official"], true); |
| 446 | assert!(review["submitted_at"].is_string()); |
| 447 | let review_id = review["id"].as_i64().unwrap(); |
| 448 | |
| 449 | let (status, _, comments) = f |
| 450 | .get(&repo_path_of(&format!( |
| 451 | "/pulls/2/reviews/{review_id}/comments" |
| 452 | ))) |
| 453 | .await; |
| 454 | assert_eq!(status, 200); |
| 455 | let c = &comments[0]; |
| 456 | assert_eq!(c["body"], "nit"); |
| 457 | assert_eq!(c["path"], "file.txt"); |
| 458 | assert_eq!(c["diff_hunk"], "@@ -1 +1 @@"); |
| 459 | assert_eq!(c["line_num"], 1); |
| 460 | assert_eq!(c["old_line_num"], 0); |
| 461 | assert_eq!(c["commit_id"], f.oid); |
| 462 | assert_eq!(c["user"]["login"], "bob"); |
| 463 | assert_eq!(c["pull_request_review_id"], review_id); |
| 464 | |
| 465 | // A pull request has comments and reactions through the issue routes, as in Gitea. |
| 466 | let (status, _, comments) = f.get(&repo_path_of("/issues/2/comments")).await; |
| 467 | assert_eq!(status, 200); |
| 468 | assert_eq!(comments.as_array().unwrap().len(), 0); |
| 469 | let (status, _, _) = f.get(&repo_path_of("/issues/2/reactions")).await; |
| 470 | assert_eq!(status, 200); |
| 471 | |
| 472 | // An issue number is not a pull request. |
| 473 | let (status, _, _) = f.get(&repo_path_of("/pulls/1/reviews")).await; |
| 474 | assert_eq!(status, 404); |
| 475 | |
| 476 | // Releases and their assets in the Gitea shape. |
| 477 | let (status, headers, releases) = f.get(&repo_path_of("/releases?page=1&limit=10")).await; |
| 478 | assert_eq!(status, 200); |
| 479 | assert_eq!(headers["x-total-count"], "1"); |
| 480 | let release = &releases[0]; |
| 481 | assert!(release["id"].as_i64().unwrap() > 0); |
| 482 | assert_eq!(release["tag_name"], "v1.0"); |
| 483 | assert_eq!(release["name"], "One"); |
| 484 | assert_eq!(release["body"], "notes"); |
| 485 | assert_eq!(release["draft"], false); |
| 486 | assert_eq!(release["prerelease"], false); |
| 487 | assert_eq!(release["target_commitish"], f.oid); |
| 488 | assert_eq!(release["author"]["login"], OWNER); |
| 489 | assert!(release["created_at"].is_string() && release["published_at"].is_string()); |
| 490 | let asset = &release["assets"][0]; |
| 491 | assert_eq!(asset["name"], "app.bin"); |
| 492 | assert_eq!(asset["size"], 5); |
| 493 | assert_eq!(asset["download_count"], 0); |
| 494 | let url = asset["browser_download_url"].as_str().unwrap(); |
| 495 | assert_eq!( |
| 496 | url, |
| 497 | "https://forge.test/tahinli/proj/releases/download/v1.0/app.bin" |
| 498 | ); |
| 499 | |
| 500 | let rid = release["id"].as_i64().unwrap(); |
| 501 | let aid = asset["id"].as_i64().unwrap(); |
| 502 | let (status, _, one) = f |
| 503 | .get(&repo_path_of(&format!("/releases/{rid}/assets/{aid}"))) |
| 504 | .await; |
| 505 | assert_eq!(status, 200); |
| 506 | assert_eq!(one["name"], "app.bin"); |
| 507 | assert_eq!(one["browser_download_url"], url); |
| 508 | } |
| 509 | |
| 510 | /// Private repos and release asset downloads, which both follow the token rules. |
| 511 | #[tokio::test] |
| 512 | async fn private_repos_and_asset_downloads_follow_the_token() { |
| 513 | let f = fixture().await; |
| 514 | // Private repos follow the token. |
| 515 | let secret = "/api/v1/repos/tahinli/secret"; |
| 516 | let (status, _, _) = f.get_as(secret, None).await; |
| 517 | assert_eq!(status, 401, "anonymous"); |
| 518 | let (status, _, _) = f |
| 519 | .get_as(secret, Some(format!("token {}", f.carol_token))) |
| 520 | .await; |
| 521 | assert_eq!(status, 404, "a user without access"); |
| 522 | let (status, _, _) = f |
| 523 | .get_as(secret, Some(format!("token {}", f.bob_token))) |
| 524 | .await; |
| 525 | assert_eq!(status, 404, "a reader of another repo"); |
| 526 | |
| 527 | // The owner may use any of the three styles Gitea clients send. |
| 528 | let token = f.owner_token.clone(); |
| 529 | let basic = format!( |
| 530 | "Basic {}", |
| 531 | BASE64_STANDARD.encode(format!("whoever:{token}")) |
| 532 | ); |
| 533 | for auth in [format!("token {token}"), format!("Bearer {token}"), basic] { |
| 534 | let (status, _, repo) = f.get_as(secret, Some(auth.clone())).await; |
| 535 | assert_eq!(status, 200, "{auth}"); |
| 536 | assert_eq!(repo["private"], true); |
| 537 | // The clone URL carries the token so the migrating server can clone. |
| 538 | let clone = repo["clone_url"].as_str().unwrap(); |
| 539 | assert_eq!( |
| 540 | clone, |
| 541 | format!("https://oauth2:{token}@forge.test/tahinli/secret.git") |
| 542 | ); |
| 543 | } |
| 544 | let (status, _, repo) = f |
| 545 | .get_as(&format!("{secret}?access_token={token}"), None) |
| 546 | .await; |
| 547 | assert_eq!(status, 200); |
| 548 | assert_eq!(repo["name"], "secret"); |
| 549 | |
| 550 | // A bad token is refused, not treated as anonymous. |
| 551 | let (status, _, _) = f |
| 552 | .get_as(&repo_path_of(""), Some("token forge_nope".into())) |
| 553 | .await; |
| 554 | assert_eq!(status, 401); |
| 555 | |
| 556 | // Release asset downloads take the token too. |
| 557 | let path = "/tahinli/proj/releases/download/v1.0/app.bin"; |
| 558 | let owner = f.owner_token.clone(); |
| 559 | let basic = format!("Basic {}", BASE64_STANDARD.encode(format!("x:{owner}"))); |
| 560 | let (status, _, _) = f.get_as(path, Some(basic)).await; |
| 561 | assert_eq!(status, 200); |
| 562 | let (status, _, _) = f.get_as(path, Some("token forge_nope".into())).await; |
| 563 | assert_eq!(status, 401); |
| 564 | } |