2017-06-09 10:07:06 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::fs::{self, File};
|
|
|
|
|
2018-02-08 00:13:17 +00:00
|
|
|
use toml;
|
2017-10-30 20:56:23 +00:00
|
|
|
use cargotest::{ChannelChanger, cargo_process};
|
2017-06-09 10:07:06 +00:00
|
|
|
use cargotest::support::execs;
|
|
|
|
use cargotest::support::registry::registry;
|
|
|
|
use cargotest::install::cargo_home;
|
2017-06-21 16:48:40 +00:00
|
|
|
use cargo::util::config::Config;
|
|
|
|
use cargo::core::Shell;
|
2017-06-09 10:07:06 +00:00
|
|
|
use hamcrest::{assert_that, existing_file, is_not};
|
|
|
|
|
|
|
|
const TOKEN: &str = "test-token";
|
2017-10-30 14:29:37 +00:00
|
|
|
const ORIGINAL_TOKEN: &str = "api-token";
|
2017-06-09 10:07:06 +00:00
|
|
|
const CONFIG_FILE: &str = r#"
|
|
|
|
[registry]
|
|
|
|
token = "api-token"
|
2017-10-30 14:29:37 +00:00
|
|
|
|
|
|
|
[registries.test-reg]
|
2017-10-30 21:25:24 +00:00
|
|
|
index = "http://dummy_index/"
|
2017-06-09 10:07:06 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
fn setup_old_credentials() {
|
|
|
|
let config = cargo_home().join("config");
|
|
|
|
t!(fs::create_dir_all(config.parent().unwrap()));
|
2017-09-24 14:26:37 +00:00
|
|
|
t!(t!(File::create(&config)).write_all(CONFIG_FILE.as_bytes()));
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_new_credentials() {
|
|
|
|
let config = cargo_home().join("credentials");
|
|
|
|
t!(fs::create_dir_all(config.parent().unwrap()));
|
2017-06-22 14:13:35 +00:00
|
|
|
t!(t!(File::create(&config)).write_all(format!(r#"
|
2017-10-30 14:29:37 +00:00
|
|
|
token = "{token}"
|
|
|
|
"#, token = ORIGINAL_TOKEN)
|
2017-06-22 14:13:35 +00:00
|
|
|
.as_bytes()));
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
fn check_token(expected_token: &str, registry: Option<&str>) -> bool {
|
|
|
|
|
|
|
|
let credentials = cargo_home().join("credentials");
|
|
|
|
assert_that(&credentials, existing_file());
|
2017-06-22 14:13:35 +00:00
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
let mut contents = String::new();
|
|
|
|
File::open(&credentials).unwrap().read_to_string(&mut contents).unwrap();
|
|
|
|
let toml: toml::Value = contents.parse().unwrap();
|
|
|
|
|
|
|
|
let token = match (registry, toml) {
|
|
|
|
// A registry has been provided, so check that the token exists in a
|
|
|
|
// table for the registry.
|
|
|
|
(Some(registry), toml::Value::Table(table)) => {
|
2017-12-20 03:37:14 +00:00
|
|
|
table.get("registries")
|
|
|
|
.and_then(|registries_table| registries_table.get(registry))
|
|
|
|
.and_then(|registry_table| {
|
2017-10-30 14:29:37 +00:00
|
|
|
match registry_table.get("token") {
|
|
|
|
Some(&toml::Value::String(ref token)) => Some(token.as_str().to_string()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// There is no registry provided, so check the global token instead.
|
|
|
|
(None, toml::Value::Table(table)) => {
|
2017-12-20 03:37:14 +00:00
|
|
|
table.get("registry")
|
|
|
|
.and_then(|registry_table| registry_table.get("token"))
|
|
|
|
.and_then(|v| {
|
2017-10-30 14:29:37 +00:00
|
|
|
match v {
|
|
|
|
&toml::Value::String(ref token) => Some(token.as_str().to_string()),
|
|
|
|
_ => None,
|
2017-06-22 14:13:35 +00:00
|
|
|
}
|
2017-10-30 14:29:37 +00:00
|
|
|
})
|
2017-06-22 14:13:35 +00:00
|
|
|
}
|
2017-10-30 14:29:37 +00:00
|
|
|
_ => None
|
|
|
|
};
|
2017-06-22 14:13:35 +00:00
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
if let Some(token_val) = token {
|
|
|
|
token_val == expected_token
|
|
|
|
} else {
|
|
|
|
false
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn login_with_old_credentials() {
|
|
|
|
setup_old_credentials();
|
|
|
|
|
|
|
|
assert_that(cargo_process().arg("login")
|
|
|
|
.arg("--host").arg(registry().to_string()).arg(TOKEN),
|
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
let config = cargo_home().join("config");
|
|
|
|
assert_that(&config, existing_file());
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
File::open(&config).unwrap().read_to_string(&mut contents).unwrap();
|
2017-09-24 14:26:37 +00:00
|
|
|
assert_eq!(CONFIG_FILE, contents);
|
2017-06-09 10:07:06 +00:00
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
// Ensure that we get the new token for the registry
|
|
|
|
assert!(check_token(TOKEN, None));
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn login_with_new_credentials() {
|
|
|
|
setup_new_credentials();
|
|
|
|
|
|
|
|
assert_that(cargo_process().arg("login")
|
|
|
|
.arg("--host").arg(registry().to_string()).arg(TOKEN),
|
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
let config = cargo_home().join("config");
|
|
|
|
assert_that(&config, is_not(existing_file()));
|
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
// Ensure that we get the new token for the registry
|
|
|
|
assert!(check_token(TOKEN, None));
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn login_with_old_and_new_credentials() {
|
|
|
|
setup_new_credentials();
|
|
|
|
login_with_old_credentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn login_without_credentials() {
|
|
|
|
assert_that(cargo_process().arg("login")
|
|
|
|
.arg("--host").arg(registry().to_string()).arg(TOKEN),
|
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
let config = cargo_home().join("config");
|
|
|
|
assert_that(&config, is_not(existing_file()));
|
|
|
|
|
2017-10-30 14:29:37 +00:00
|
|
|
// Ensure that we get the new token for the registry
|
|
|
|
assert!(check_token(TOKEN, None));
|
2017-06-09 10:07:06 +00:00
|
|
|
}
|
2017-06-21 16:48:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_credentials_is_used_instead_old() {
|
|
|
|
setup_old_credentials();
|
|
|
|
setup_new_credentials();
|
|
|
|
|
|
|
|
assert_that(cargo_process().arg("login")
|
|
|
|
.arg("--host").arg(registry().to_string()).arg(TOKEN),
|
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
let config = Config::new(Shell::new(), cargo_home(), cargo_home());
|
2017-06-22 14:13:35 +00:00
|
|
|
|
2017-06-21 16:48:40 +00:00
|
|
|
let token = config.get_string("registry.token").unwrap().map(|p| p.val);
|
2017-09-24 14:26:37 +00:00
|
|
|
assert_eq!(token.unwrap(), TOKEN);
|
2017-10-30 14:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn registry_credentials() {
|
|
|
|
setup_old_credentials();
|
|
|
|
setup_new_credentials();
|
|
|
|
|
|
|
|
let reg = "test-reg";
|
|
|
|
|
2017-10-30 20:56:23 +00:00
|
|
|
assert_that(cargo_process().arg("login").masquerade_as_nightly_cargo()
|
|
|
|
.arg("--registry").arg(reg).arg(TOKEN).arg("-Zunstable-options"),
|
2017-10-30 14:29:37 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
// Ensure that we have not updated the default token
|
|
|
|
assert!(check_token(ORIGINAL_TOKEN, None));
|
|
|
|
|
|
|
|
// Also ensure that we get the new token for the registry
|
|
|
|
assert!(check_token(TOKEN, Some(reg)));
|
|
|
|
}
|