| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use crate::common::{Http, bin, describe, input_value, launch, login, pick_ports, write_config}; |
| 9 | use std::fs; |
| 10 | use std::path::Path; |
| 11 | use std::process::{Command, Output}; |
| 12 | |
| 13 | const TAHINLI_PW: &str = "tahinli-password-123"; |
| 14 | const FILE_SIZE: usize = 100 * 1024; |
| 15 | |
| 16 | fn run_admin(config: &Path, username: &str, extra: &[&str], password: &str) { |
| 17 | let out = Command::new(bin()) |
| 18 | .arg("--config") |
| 19 | .arg(config) |
| 20 | .args(["admin", "create-user", username]) |
| 21 | .args(extra) |
| 22 | .env("FORGE_PASSWORD", password) |
| 23 | .output() |
| 24 | .expect("run admin create-user"); |
| 25 | ok(&out, &format!("create-user {username}")); |
| 26 | } |
| 27 | |
| 28 | fn ok(out: &Output, what: &str) { |
| 29 | assert!(out.status.success(), "{what} failed:\n{}", describe(out)); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | fn git(cwd: &Path, args: &[&str], env: &[(&str, &str)]) -> Output { |
| 34 | let mut cmd = Command::new("git"); |
| 35 | cmd.current_dir(cwd) |
| 36 | .args(["-c", "credential.helper="]) |
| 37 | .args(args) |
| 38 | .env("GIT_TERMINAL_PROMPT", "0"); |
| 39 | for (key, value) in env { |
| 40 | cmd.env(key, value); |
| 41 | } |
| 42 | cmd.output().expect("run git") |
| 43 | } |
| 44 | |
| 45 | fn path_str(path: &Path) -> &str { |
| 46 | path.to_str().expect("utf-8 path") |
| 47 | } |
| 48 | |
| 49 | fn git_lfs_installed() -> bool { |
| 50 | Command::new("git") |
| 51 | .args(["lfs", "version"]) |
| 52 | .output() |
| 53 | .is_ok_and(|out| out.status.success()) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | fn payload() -> Vec<u8> { |
| 58 | (0..FILE_SIZE) |
| 59 | .map(|i| (i.wrapping_mul(31) % 251) as u8) |
| 60 | .collect() |
| 61 | } |
| 62 | |
| 63 | |
| 64 | async fn create_token(http: &Http, session: &str, name: &str) -> String { |
| 65 | let page = http.get("/settings/keys", Some(session)).await; |
| 66 | assert_eq!(page.status, 200, "GET /settings/keys"); |
| 67 | let csrf = input_value(&page.body, "csrf"); |
| 68 | let res = http |
| 69 | .post_form( |
| 70 | "/settings/tokens", |
| 71 | &[("csrf", &csrf), ("name", name), ("expires", "30")], |
| 72 | Some(session), |
| 73 | ) |
| 74 | .await; |
| 75 | assert_eq!(res.status, 200, "create token: {}", res.body); |
| 76 | let doc = scraper::Html::parse_document(&res.body); |
| 77 | let selector = scraper::Selector::parse("pre.code code").expect("selector"); |
| 78 | let token: String = doc |
| 79 | .select(&selector) |
| 80 | .next() |
| 81 | .expect("token shown after creation") |
| 82 | .text() |
| 83 | .collect(); |
| 84 | let token = token.trim().to_owned(); |
| 85 | assert!(token.starts_with("forge_"), "unexpected token shape"); |
| 86 | token |
| 87 | } |
| 88 | |
| 89 | |
| 90 | async fn create_repo(http: &Http, session: &str, name: &str, visibility: &str) { |
| 91 | let page = http.get("/new", Some(session)).await; |
| 92 | assert_eq!(page.status, 200, "GET /new"); |
| 93 | let csrf = input_value(&page.body, "csrf"); |
| 94 | let res = http |
| 95 | .post_form( |
| 96 | "/new", |
| 97 | &[ |
| 98 | ("csrf", &csrf), |
| 99 | ("name", name), |
| 100 | ("description", ""), |
| 101 | ("visibility", visibility), |
| 102 | ("default_branch", "main"), |
| 103 | ], |
| 104 | Some(session), |
| 105 | ) |
| 106 | .await; |
| 107 | assert_eq!(res.status, 303, "create repo {name}: {}", res.body); |
| 108 | assert_eq!( |
| 109 | res.location().as_deref(), |
| 110 | Some(format!("/tahinli/{name}").as_str()) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[tokio::test] |
| 115 | async fn lfs_end_to_end() { |
| 116 | if !git_lfs_installed() { |
| 117 | eprintln!("skipping: git-lfs not installed"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | let root = tempfile::Builder::new() |
| 123 | .prefix("flfs") |
| 124 | .tempdir_in("/tmp") |
| 125 | .expect("temp dir"); |
| 126 | let dir = root.path().to_path_buf(); |
| 127 | let ports = pick_ports(); |
| 128 | let config = write_config(&dir, ports); |
| 129 | run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW); |
| 130 | |
| 131 | let (_server, ports) = launch(&dir, ports).await; |
| 132 | let http = Http::new(ports.http); |
| 133 | |
| 134 | let session = login(&http, "tahinli", TAHINLI_PW).await; |
| 135 | let token = create_token(&http, &session, "lfs-e2e").await; |
| 136 | create_repo(&http, &session, "lfs", "public").await; |
| 137 | |
| 138 | |
| 139 | let global = dir.join("gitconfig"); |
| 140 | let iso = [ |
| 141 | ("HOME", path_str(&dir)), |
| 142 | ("GIT_CONFIG_GLOBAL", path_str(&global)), |
| 143 | ]; |
| 144 | let url = format!( |
| 145 | "http://tahinli:{token}@127.0.0.1:{}/tahinli/lfs.git", |
| 146 | ports.http |
| 147 | ); |
| 148 | |
| 149 | |
| 150 | let work = dir.join("work"); |
| 151 | ok( |
| 152 | &git(&dir, &["clone", &url, path_str(&work)], &iso), |
| 153 | "clone of empty repo", |
| 154 | ); |
| 155 | ok( |
| 156 | &git(&work, &["config", "user.name", "tahinli"], &iso), |
| 157 | "user.name", |
| 158 | ); |
| 159 | ok( |
| 160 | &git( |
| 161 | &work, |
| 162 | &["config", "user.email", "tahinli@example.com"], |
| 163 | &iso, |
| 164 | ), |
| 165 | "user.email", |
| 166 | ); |
| 167 | ok( |
| 168 | &git(&work, &["lfs", "install", "--local"], &iso), |
| 169 | "git lfs install", |
| 170 | ); |
| 171 | ok( |
| 172 | &git(&work, &["lfs", "track", "*.bin"], &iso), |
| 173 | "git lfs track", |
| 174 | ); |
| 175 | let content = payload(); |
| 176 | fs::write(work.join("data.bin"), &content).expect("write data.bin"); |
| 177 | ok( |
| 178 | &git(&work, &["add", ".gitattributes", "data.bin"], &iso), |
| 179 | "git add", |
| 180 | ); |
| 181 | ok( |
| 182 | &git( |
| 183 | &work, |
| 184 | &[ |
| 185 | "-c", |
| 186 | "commit.gpgsign=false", |
| 187 | "commit", |
| 188 | "-q", |
| 189 | "-m", |
| 190 | "add lfs file", |
| 191 | ], |
| 192 | &iso, |
| 193 | ), |
| 194 | "git commit", |
| 195 | ); |
| 196 | ok( |
| 197 | &git(&work, &["push", "origin", "HEAD:main"], &iso), |
| 198 | "git push", |
| 199 | ); |
| 200 | |
| 201 | |
| 202 | let verify = dir.join("verify"); |
| 203 | let smudge = [iso[0], iso[1], ("GIT_LFS_SKIP_SMUDGE", "0")]; |
| 204 | ok( |
| 205 | &git(&dir, &["clone", &url, path_str(&verify)], &smudge), |
| 206 | "clone for verification", |
| 207 | ); |
| 208 | ok( |
| 209 | &git(&verify, &["lfs", "install", "--local"], &iso), |
| 210 | "git lfs install in verify clone", |
| 211 | ); |
| 212 | ok(&git(&verify, &["lfs", "pull"], &smudge), "git lfs pull"); |
| 213 | let fetched = fs::read(verify.join("data.bin")).expect("read fetched data.bin"); |
| 214 | assert!( |
| 215 | fetched == content, |
| 216 | "downloaded LFS bytes differ from the pushed file ({} vs {} bytes)", |
| 217 | fetched.len(), |
| 218 | content.len() |
| 219 | ); |
| 220 | |
| 221 | |
| 222 | let page = http |
| 223 | .get("/tahinli/lfs/blob/main/data.bin", Some(&session)) |
| 224 | .await; |
| 225 | assert_eq!(page.status, 200, "GET blob page"); |
| 226 | assert!( |
| 227 | page.body.contains("Stored with Git LFS"), |
| 228 | "blob page does not show the LFS note" |
| 229 | ); |
| 230 | } |