cargo/tests/testsuite/login.rs

347 lines
11 KiB
Rust
Raw Normal View History

2019-11-25 02:42:45 +00:00
//! Tests for the `cargo login` command.
2022-12-14 20:40:59 +00:00
use cargo_test_support::cargo_process;
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::{self, RegistryBuilder};
2022-12-14 20:40:59 +00:00
use cargo_test_support::t;
use std::fs;
use std::path::PathBuf;
const TOKEN: &str = "test-token";
const TOKEN2: &str = "test-token2";
const ORIGINAL_TOKEN: &str = "api-token";
fn credentials_toml() -> PathBuf {
paths::home().join(".cargo/credentials.toml")
}
fn setup_new_credentials() {
setup_new_credentials_at(credentials_toml());
}
fn setup_new_credentials_at(config: PathBuf) {
t!(fs::create_dir_all(config.parent().unwrap()));
t!(fs::write(
&config,
format!(r#"token = "{token}""#, token = ORIGINAL_TOKEN)
));
}
/// Asserts whether or not the token is set to the given value for the given registry.
pub fn check_token(expected_token: Option<&str>, registry: Option<&str>) {
let credentials = credentials_toml();
2018-08-29 06:11:10 +00:00
assert!(credentials.is_file());
let contents = fs::read_to_string(&credentials).unwrap();
let toml: toml::Table = contents.parse().unwrap();
let actual_token = match registry {
// A registry has been provided, so check that the token exists in a
// table for the registry.
Some(registry) => toml
2018-03-14 15:17:44 +00:00
.get("registries")
.and_then(|registries_table| registries_table.get(registry))
.and_then(|registry_table| 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
2018-03-14 15:17:44 +00:00
.get("registry")
.and_then(|registry_table| registry_table.get("token"))
.and_then(|v| match v {
toml::Value::String(ref token) => Some(token.as_str().to_string()),
2018-03-14 15:17:44 +00:00
_ => None,
}),
};
match (actual_token, expected_token) {
(None, None) => {}
(Some(actual), Some(expected)) => assert_eq!(actual, expected),
(None, Some(expected)) => {
panic!("expected `{registry:?}` to be `{expected}`, but was not set")
}
(Some(actual), None) => {
panic!("expected `{registry:?}` to be unset, but was set to `{actual}`")
}
}
}
#[cargo_test]
fn registry_credentials() {
let _alternative = RegistryBuilder::new().alternative().build();
let _alternative2 = RegistryBuilder::new()
.alternative_named("alternative2")
.build();
2019-12-17 00:59:45 +00:00
setup_new_credentials();
2018-12-20 01:40:01 +00:00
let reg = "alternative";
2019-12-16 23:33:48 +00:00
cargo_process("login --registry").arg(reg).arg(TOKEN).run();
// Ensure that we have not updated the default token
check_token(Some(ORIGINAL_TOKEN), None);
// Also ensure that we get the new token for the registry
check_token(Some(TOKEN), Some(reg));
let reg2 = "alternative2";
cargo_process("login --registry")
.arg(reg2)
.arg(TOKEN2)
.run();
// Ensure not overwriting 1st alternate registry token with
// 2nd alternate registry token (see rust-lang/cargo#7701).
check_token(Some(ORIGINAL_TOKEN), None);
check_token(Some(TOKEN), Some(reg));
check_token(Some(TOKEN2), Some(reg2));
}
#[cargo_test]
fn empty_login_token() {
let registry = RegistryBuilder::new()
.no_configure_registry()
.no_configure_token()
.build();
setup_new_credentials();
cargo_process("login")
.replace_crates_io(registry.index_url())
.with_stdin("\t\n")
.with_stderr(
"\
[UPDATING] crates.io index
2023-06-27 16:26:27 +00:00
[ERROR] credential provider `cargo:token` failed action `login`
Caused by:
please provide a non-empty token
",
)
.with_status(101)
.run();
cargo_process("login")
.replace_crates_io(registry.index_url())
.arg("")
.with_stderr(
"\
2023-06-27 16:26:27 +00:00
[ERROR] credential provider `cargo:token` failed action `login`
Caused by:
please provide a non-empty token
",
)
.with_status(101)
.run();
}
2022-12-12 17:49:22 +00:00
#[cargo_test]
fn invalid_login_token() {
let registry = RegistryBuilder::new()
.no_configure_registry()
.no_configure_token()
.build();
setup_new_credentials();
let check = |stdin: &str, stderr: &str, status: i32| {
cargo_process("login")
.replace_crates_io(registry.index_url())
.with_stdin(stdin)
.with_stderr(stderr)
.with_status(status)
.run();
};
let invalid = |stdin: &str| {
check(
stdin,
2023-06-27 16:26:27 +00:00
"[ERROR] credential provider `cargo:token` failed action `login`
Caused by:
token contains invalid characters.
2023-06-27 16:26:27 +00:00
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
101,
)
};
2023-06-27 16:26:27 +00:00
let valid = |stdin: &str| check(stdin, "[LOGIN] token for `crates-io` saved", 0);
// Update config.json so that the rest of the tests don't need to care
// whether or not `Updating` is printed.
check(
"test",
"\
[UPDATING] crates.io index
2023-06-27 16:26:27 +00:00
[LOGIN] token for `crates-io` saved
",
0,
);
invalid("😄");
invalid("\u{0016}");
invalid("\u{0000}");
invalid("你好");
valid("foo\tbar");
valid("foo bar");
valid(
r##"!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"##,
);
}
#[cargo_test]
fn bad_asymmetric_token_args() {
let registry = RegistryBuilder::new()
.credential_provider(&["cargo:paseto"])
.no_configure_token()
.build();
// These cases are kept brief as the implementation is covered by clap, so this is only smoke testing that we have clap configured correctly.
cargo_process("login -Zasymmetric-token -- --key-subject")
.masquerade_as_nightly_cargo(&["asymmetric-token"])
.replace_crates_io(registry.index_url())
.with_stderr_contains(
" error: a value is required for '--key-subject <SUBJECT>' but none was supplied",
)
.with_status(101)
.run();
}
2022-12-14 20:40:59 +00:00
#[cargo_test]
fn login_with_no_cargo_dir() {
// Create a config in the root directory because `login` requires the
// index to be updated, and we don't want to hit crates.io.
let registry = registry::init();
fs::rename(paths::home().join(".cargo"), paths::root().join(".cargo")).unwrap();
paths::home().rm_rf();
cargo_process("login foo -v")
.replace_crates_io(registry.index_url())
.run();
let credentials = fs::read_to_string(credentials_toml()).unwrap();
2022-12-14 20:40:59 +00:00
assert_eq!(credentials, "[registry]\ntoken = \"foo\"\n");
}
#[cargo_test]
fn login_with_asymmetric_token_and_subject_on_stdin() {
let registry = RegistryBuilder::new()
.credential_provider(&["cargo:paseto"])
.no_configure_token()
.build();
let credentials = credentials_toml();
cargo_process("login -v -Z asymmetric-token -- --key-subject=foo")
.masquerade_as_nightly_cargo(&["asymmetric-token"])
.replace_crates_io(registry.index_url())
.with_stderr_contains(
"\
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
)
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
.run();
let credentials = fs::read_to_string(&credentials).unwrap();
assert!(credentials.starts_with("[registry]\n"));
assert!(credentials.contains("secret-key-subject = \"foo\"\n"));
assert!(credentials.contains("secret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n"));
}
2022-12-14 20:40:59 +00:00
#[cargo_test]
fn login_with_differently_sized_token() {
// Verify that the configuration file gets properly truncated.
let registry = registry::init();
let credentials = credentials_toml();
2022-12-14 20:40:59 +00:00
fs::remove_file(&credentials).unwrap();
cargo_process("login lmaolmaolmao -v")
.replace_crates_io(registry.index_url())
.run();
cargo_process("login lmao -v")
.replace_crates_io(registry.index_url())
.run();
cargo_process("login lmaolmaolmao -v")
.replace_crates_io(registry.index_url())
.run();
let credentials = fs::read_to_string(&credentials).unwrap();
assert_eq!(credentials, "[registry]\ntoken = \"lmaolmaolmao\"\n");
}
#[cargo_test]
fn login_with_token_on_stdin() {
let registry = registry::init();
let credentials = credentials_toml();
2022-12-14 20:40:59 +00:00
fs::remove_file(&credentials).unwrap();
cargo_process("login lmao -v")
.replace_crates_io(registry.index_url())
.run();
cargo_process("login")
.replace_crates_io(registry.index_url())
.with_stdin("some token")
.run();
let credentials = fs::read_to_string(&credentials).unwrap();
assert_eq!(credentials, "[registry]\ntoken = \"some token\"\n");
}
#[cargo_test]
fn login_with_asymmetric_token_on_stdin() {
2023-06-27 16:26:27 +00:00
let _registry = RegistryBuilder::new()
.credential_provider(&["cargo:paseto"])
.alternative()
.no_configure_token()
.build();
let credentials = credentials_toml();
cargo_process("login -v -Z asymmetric-token --registry alternative")
.masquerade_as_nightly_cargo(&["asymmetric-token"])
2023-06-27 16:26:27 +00:00
.with_stderr(
2022-12-14 20:40:59 +00:00
"\
2023-06-27 16:26:27 +00:00
[UPDATING] [..]
[CREDENTIAL] cargo:paseto login alternative
2022-12-14 20:40:59 +00:00
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
)
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
.run();
let credentials = fs::read_to_string(&credentials).unwrap();
2023-06-27 16:26:27 +00:00
assert_eq!(credentials, "[registries.alternative]\nsecret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n");
2022-12-14 20:40:59 +00:00
}
#[cargo_test]
fn login_with_generate_asymmetric_token() {
2023-06-27 16:26:27 +00:00
let _registry = RegistryBuilder::new()
.credential_provider(&["cargo:paseto"])
.alternative()
.no_configure_token()
.build();
let credentials = credentials_toml();
cargo_process("login -Z asymmetric-token --registry alternative")
.masquerade_as_nightly_cargo(&["asymmetric-token"])
2023-06-27 16:26:27 +00:00
.with_stderr("[UPDATING] `alternative` index\nk3.public.[..]")
2022-12-14 20:40:59 +00:00
.run();
let credentials = fs::read_to_string(&credentials).unwrap();
assert!(credentials.contains("secret-key = \"k3.secret."));
}
#[cargo_test]
fn default_registry_configured() {
// When registry.default is set, login should use that one when
// --registry is not used.
2023-04-09 16:30:36 +00:00
let _alternative = RegistryBuilder::new().alternative().build();
let cargo_home = paths::home().join(".cargo");
2023-04-09 16:30:36 +00:00
cargo_util::paths::append(
2024-01-26 19:40:46 +00:00
&cargo_home.join("config.toml"),
2023-04-09 16:30:36 +00:00
br#"
[registry]
2023-04-09 16:30:36 +00:00
default = "alternative"
"#,
)
.unwrap();
cargo_process("login")
.arg("a-new-token")
.with_stderr(
"\
2023-04-09 16:30:36 +00:00
[UPDATING] `alternative` index
[LOGIN] token for `alternative` saved
",
)
.run();
2023-04-09 16:30:36 +00:00
check_token(None, None);
check_token(Some("a-new-token"), Some("alternative"));
}