2024-03-29 12:04:26 +03:00
|
|
|
use std::path::PathBuf;
|
2024-06-09 08:18:44 +03:00
|
|
|
use std::str;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-08 14:23:03 +03:00
|
|
|
use gitbutler_command_context::ProjectRepo;
|
|
|
|
use gitbutler_core::{projects, users};
|
2024-07-08 14:01:38 +03:00
|
|
|
use gitbutler_repo::credentials::{Credential, Helper, SshCredential};
|
2024-03-31 00:27:56 +03:00
|
|
|
|
2024-04-06 11:08:50 +03:00
|
|
|
use gitbutler_testsupport::{temp_dir, test_repository};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct TestCase<'a> {
|
|
|
|
remote_url: &'a str,
|
2024-06-24 18:18:33 +03:00
|
|
|
with_github_login: bool,
|
2024-03-29 12:04:26 +03:00
|
|
|
preferred_key: projects::AuthKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCase<'_> {
|
|
|
|
fn run(&self) -> Vec<(String, Vec<Credential>)> {
|
|
|
|
let local_app_data = temp_dir();
|
|
|
|
|
2024-06-24 18:18:33 +03:00
|
|
|
gitbutler_testsupport::secrets::setup_blackhole_store();
|
2024-04-24 10:20:55 +03:00
|
|
|
let users = users::Controller::from_path(local_app_data.path());
|
2024-06-24 18:18:33 +03:00
|
|
|
let user: users::User = serde_json::from_str(if self.with_github_login {
|
2024-07-08 14:01:38 +03:00
|
|
|
include_str!("../tests/fixtures/users/with-github.v1")
|
2024-06-24 18:18:33 +03:00
|
|
|
} else {
|
2024-07-08 14:01:38 +03:00
|
|
|
include_str!("../tests/fixtures/users/login-only.v1")
|
2024-06-24 18:18:33 +03:00
|
|
|
})
|
|
|
|
.expect("valid v1 sample user");
|
2024-03-29 12:04:26 +03:00
|
|
|
users.set_user(&user).unwrap();
|
|
|
|
|
2024-07-05 16:56:24 +03:00
|
|
|
let helper = Helper::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let (repo, _tmp) = test_repository();
|
2024-06-05 02:11:10 +03:00
|
|
|
repo.remote("origin", self.remote_url).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
let project = projects::Project {
|
|
|
|
path: repo.workdir().unwrap().to_path_buf(),
|
|
|
|
preferred_key: self.preferred_key.clone(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2024-07-08 14:23:03 +03:00
|
|
|
let project_repository = ProjectRepo::open(&project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let flow = helper.help(&project_repository, "origin").unwrap();
|
|
|
|
flow.into_iter()
|
2024-06-02 00:30:25 +03:00
|
|
|
.map(|(remote, credentials)| (remote.url().as_ref().unwrap().to_string(), credentials))
|
2024-03-29 12:04:26 +03:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod not_github {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
mod with_preferred_key {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn https() {
|
|
|
|
let test_case = TestCase {
|
|
|
|
remote_url: "https://gitlab.com/test-gitbutler/test.git",
|
2024-06-24 18:18:33 +03:00
|
|
|
with_github_login: true,
|
2024-03-29 12:04:26 +03:00
|
|
|
preferred_key: projects::AuthKey::Local {
|
|
|
|
private_key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let flow = test_case.run();
|
|
|
|
assert_eq!(flow.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].0,
|
|
|
|
"git@gitlab.com:test-gitbutler/test.git".to_string(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].1,
|
|
|
|
vec![Credential::Ssh(SshCredential::Keyfile {
|
|
|
|
key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
passphrase: None,
|
|
|
|
})]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh() {
|
|
|
|
let test_case = TestCase {
|
|
|
|
remote_url: "git@gitlab.com:test-gitbutler/test.git",
|
2024-06-24 18:18:33 +03:00
|
|
|
with_github_login: true,
|
2024-03-29 12:04:26 +03:00
|
|
|
preferred_key: projects::AuthKey::Local {
|
|
|
|
private_key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let flow = test_case.run();
|
|
|
|
assert_eq!(flow.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].0,
|
|
|
|
"git@gitlab.com:test-gitbutler/test.git".to_string(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].1,
|
|
|
|
vec![Credential::Ssh(SshCredential::Keyfile {
|
|
|
|
key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
passphrase: None,
|
|
|
|
})]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod github {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
mod without_github_token {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
mod with_preferred_key {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn https() {
|
|
|
|
let test_case = TestCase {
|
|
|
|
remote_url: "https://github.com/gitbutlerapp/gitbutler.git",
|
2024-06-24 18:18:33 +03:00
|
|
|
with_github_login: true,
|
2024-03-29 12:04:26 +03:00
|
|
|
preferred_key: projects::AuthKey::Local {
|
|
|
|
private_key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let flow = test_case.run();
|
|
|
|
assert_eq!(flow.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].0,
|
|
|
|
"git@github.com:gitbutlerapp/gitbutler.git".to_string(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].1,
|
|
|
|
vec![Credential::Ssh(SshCredential::Keyfile {
|
|
|
|
key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
passphrase: None,
|
|
|
|
})]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ssh() {
|
|
|
|
let test_case = TestCase {
|
|
|
|
remote_url: "git@github.com:gitbutlerapp/gitbutler.git",
|
2024-06-24 18:18:33 +03:00
|
|
|
with_github_login: true,
|
2024-03-29 12:04:26 +03:00
|
|
|
preferred_key: projects::AuthKey::Local {
|
|
|
|
private_key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let flow = test_case.run();
|
|
|
|
assert_eq!(flow.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].0,
|
|
|
|
"git@github.com:gitbutlerapp/gitbutler.git".to_string(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
flow[0].1,
|
|
|
|
vec![Credential::Ssh(SshCredential::Keyfile {
|
|
|
|
key_path: PathBuf::from("/tmp/id_rsa"),
|
|
|
|
passphrase: None,
|
|
|
|
})]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|