Skip to content

Commit f20ab0f

Browse files
committed
move login tests
1 parent 6f8b15c commit f20ab0f

File tree

2 files changed

+142
-140
lines changed

2 files changed

+142
-140
lines changed

tests/testsuite/login.rs

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Tests for the `cargo login` command.
22
3+
use cargo_test_support::cargo_process;
34
use cargo_test_support::install::cargo_home;
5+
use cargo_test_support::paths::{self, CargoPathExt};
46
use cargo_test_support::registry::{self, RegistryBuilder};
5-
use cargo_test_support::{cargo_process, t};
7+
use cargo_test_support::t;
68
use std::fs::{self};
79
use std::path::PathBuf;
810
use toml_edit::easy as toml;
@@ -176,3 +178,142 @@ fn asymmetric_requires_nightly() {
176178
See https://github.com/rust-lang/cargo/issues/10519 for more information about the `secret-key` flag.")
177179
.run();
178180
}
181+
182+
#[cargo_test]
183+
fn login_with_no_cargo_dir() {
184+
// Create a config in the root directory because `login` requires the
185+
// index to be updated, and we don't want to hit crates.io.
186+
let registry = registry::init();
187+
fs::rename(paths::home().join(".cargo"), paths::root().join(".cargo")).unwrap();
188+
paths::home().rm_rf();
189+
cargo_process("login foo -v")
190+
.replace_crates_io(registry.index_url())
191+
.run();
192+
let credentials = fs::read_to_string(paths::home().join(".cargo/credentials")).unwrap();
193+
assert_eq!(credentials, "[registry]\ntoken = \"foo\"\n");
194+
}
195+
196+
#[cargo_test]
197+
fn login_with_differently_sized_token() {
198+
// Verify that the configuration file gets properly truncated.
199+
let registry = registry::init();
200+
let credentials = paths::home().join(".cargo/credentials");
201+
fs::remove_file(&credentials).unwrap();
202+
cargo_process("login lmaolmaolmao -v")
203+
.replace_crates_io(registry.index_url())
204+
.run();
205+
cargo_process("login lmao -v")
206+
.replace_crates_io(registry.index_url())
207+
.run();
208+
cargo_process("login lmaolmaolmao -v")
209+
.replace_crates_io(registry.index_url())
210+
.run();
211+
let credentials = fs::read_to_string(&credentials).unwrap();
212+
assert_eq!(credentials, "[registry]\ntoken = \"lmaolmaolmao\"\n");
213+
}
214+
215+
#[cargo_test]
216+
fn login_with_token_on_stdin() {
217+
let registry = registry::init();
218+
let credentials = paths::home().join(".cargo/credentials");
219+
fs::remove_file(&credentials).unwrap();
220+
cargo_process("login lmao -v")
221+
.replace_crates_io(registry.index_url())
222+
.run();
223+
cargo_process("login")
224+
.replace_crates_io(registry.index_url())
225+
.with_stdout("please paste the token found on [..]/me below")
226+
.with_stdin("some token")
227+
.run();
228+
let credentials = fs::read_to_string(&credentials).unwrap();
229+
assert_eq!(credentials, "[registry]\ntoken = \"some token\"\n");
230+
}
231+
232+
#[cargo_test]
233+
fn login_with_asymmetric_token_and_subject_on_stdin() {
234+
let registry = registry::init();
235+
let credentials = paths::home().join(".cargo/credentials");
236+
fs::remove_file(&credentials).unwrap();
237+
cargo_process("login --key-subject=foo --secret-key -v -Z registry-auth")
238+
.masquerade_as_nightly_cargo(&["registry-auth"])
239+
.replace_crates_io(registry.index_url())
240+
.with_stdout(
241+
"\
242+
please paste the API secret key below
243+
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
244+
)
245+
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
246+
.run();
247+
let credentials = fs::read_to_string(&credentials).unwrap();
248+
assert!(credentials.starts_with("[registry]\n"));
249+
assert!(credentials.contains("secret-key-subject = \"foo\"\n"));
250+
assert!(credentials.contains("secret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n"));
251+
}
252+
253+
#[cargo_test]
254+
fn login_with_asymmetric_token_on_stdin() {
255+
let registry = registry::init();
256+
let credentials = paths::home().join(".cargo/credentials");
257+
fs::remove_file(&credentials).unwrap();
258+
cargo_process("login --secret-key -v -Z registry-auth")
259+
.masquerade_as_nightly_cargo(&["registry-auth"])
260+
.replace_crates_io(registry.index_url())
261+
.with_stdout(
262+
"\
263+
please paste the API secret key below
264+
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
265+
)
266+
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
267+
.run();
268+
let credentials = fs::read_to_string(&credentials).unwrap();
269+
assert_eq!(credentials, "[registry]\nsecret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n");
270+
}
271+
272+
#[cargo_test]
273+
fn login_with_asymmetric_key_subject_without_key() {
274+
let registry = registry::init();
275+
let credentials = paths::home().join(".cargo/credentials");
276+
fs::remove_file(&credentials).unwrap();
277+
cargo_process("login --key-subject=foo -Z registry-auth")
278+
.masquerade_as_nightly_cargo(&["registry-auth"])
279+
.replace_crates_io(registry.index_url())
280+
.with_stderr_contains("error: need a secret_key to set a key_subject")
281+
.with_status(101)
282+
.run();
283+
284+
// ok so add a secret_key to the credentials
285+
cargo_process("login --secret-key -v -Z registry-auth")
286+
.masquerade_as_nightly_cargo(&["registry-auth"])
287+
.replace_crates_io(registry.index_url())
288+
.with_stdout(
289+
"please paste the API secret key below
290+
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
291+
)
292+
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
293+
.run();
294+
295+
// and then it shuld work
296+
cargo_process("login --key-subject=foo -Z registry-auth")
297+
.masquerade_as_nightly_cargo(&["registry-auth"])
298+
.replace_crates_io(registry.index_url())
299+
.run();
300+
301+
let credentials = fs::read_to_string(&credentials).unwrap();
302+
assert!(credentials.starts_with("[registry]\n"));
303+
assert!(credentials.contains("secret-key-subject = \"foo\"\n"));
304+
assert!(credentials.contains("secret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n"));
305+
}
306+
307+
#[cargo_test]
308+
fn login_with_generate_asymmetric_token() {
309+
let registry = registry::init();
310+
let credentials = paths::home().join(".cargo/credentials");
311+
fs::remove_file(&credentials).unwrap();
312+
cargo_process("login --generate-keypair -Z registry-auth")
313+
.masquerade_as_nightly_cargo(&["registry-auth"])
314+
.replace_crates_io(registry.index_url())
315+
.with_stdout("k3.public.[..]")
316+
.run();
317+
let credentials = fs::read_to_string(&credentials).unwrap();
318+
assert!(credentials.contains("secret-key = \"k3.secret."));
319+
}

tests/testsuite/registry.rs

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,145 +1077,6 @@ fn dev_dependency_not_used(cargo: fn(&Project, &str) -> Execs) {
10771077
.run();
10781078
}
10791079

1080-
#[cargo_test]
1081-
fn login_with_no_cargo_dir() {
1082-
// Create a config in the root directory because `login` requires the
1083-
// index to be updated, and we don't want to hit crates.io.
1084-
let registry = registry::init();
1085-
fs::rename(paths::home().join(".cargo"), paths::root().join(".cargo")).unwrap();
1086-
paths::home().rm_rf();
1087-
cargo_process("login foo -v")
1088-
.replace_crates_io(registry.index_url())
1089-
.run();
1090-
let credentials = fs::read_to_string(paths::home().join(".cargo/credentials")).unwrap();
1091-
assert_eq!(credentials, "[registry]\ntoken = \"foo\"\n");
1092-
}
1093-
1094-
#[cargo_test]
1095-
fn login_with_differently_sized_token() {
1096-
// Verify that the configuration file gets properly truncated.
1097-
let registry = registry::init();
1098-
let credentials = paths::home().join(".cargo/credentials");
1099-
fs::remove_file(&credentials).unwrap();
1100-
cargo_process("login lmaolmaolmao -v")
1101-
.replace_crates_io(registry.index_url())
1102-
.run();
1103-
cargo_process("login lmao -v")
1104-
.replace_crates_io(registry.index_url())
1105-
.run();
1106-
cargo_process("login lmaolmaolmao -v")
1107-
.replace_crates_io(registry.index_url())
1108-
.run();
1109-
let credentials = fs::read_to_string(&credentials).unwrap();
1110-
assert_eq!(credentials, "[registry]\ntoken = \"lmaolmaolmao\"\n");
1111-
}
1112-
1113-
#[cargo_test]
1114-
fn login_with_token_on_stdin() {
1115-
let registry = registry::init();
1116-
let credentials = paths::home().join(".cargo/credentials");
1117-
fs::remove_file(&credentials).unwrap();
1118-
cargo_process("login lmao -v")
1119-
.replace_crates_io(registry.index_url())
1120-
.run();
1121-
cargo_process("login")
1122-
.replace_crates_io(registry.index_url())
1123-
.with_stdout("please paste the token found on [..]/me below")
1124-
.with_stdin("some token")
1125-
.run();
1126-
let credentials = fs::read_to_string(&credentials).unwrap();
1127-
assert_eq!(credentials, "[registry]\ntoken = \"some token\"\n");
1128-
}
1129-
1130-
#[cargo_test]
1131-
fn login_with_asymmetric_token_and_subject_on_stdin() {
1132-
let registry = registry::init();
1133-
let credentials = paths::home().join(".cargo/credentials");
1134-
fs::remove_file(&credentials).unwrap();
1135-
cargo_process("login --key-subject=foo --secret-key -v -Z registry-auth")
1136-
.masquerade_as_nightly_cargo(&["registry-auth"])
1137-
.replace_crates_io(registry.index_url())
1138-
.with_stdout(
1139-
"\
1140-
please paste the API secret key below
1141-
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
1142-
)
1143-
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
1144-
.run();
1145-
let credentials = fs::read_to_string(&credentials).unwrap();
1146-
assert!(credentials.starts_with("[registry]\n"));
1147-
assert!(credentials.contains("secret-key-subject = \"foo\"\n"));
1148-
assert!(credentials.contains("secret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n"));
1149-
}
1150-
1151-
#[cargo_test]
1152-
fn login_with_asymmetric_token_on_stdin() {
1153-
let registry = registry::init();
1154-
let credentials = paths::home().join(".cargo/credentials");
1155-
fs::remove_file(&credentials).unwrap();
1156-
cargo_process("login --secret-key -v -Z registry-auth")
1157-
.masquerade_as_nightly_cargo(&["registry-auth"])
1158-
.replace_crates_io(registry.index_url())
1159-
.with_stdout(
1160-
"\
1161-
please paste the API secret key below
1162-
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
1163-
)
1164-
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
1165-
.run();
1166-
let credentials = fs::read_to_string(&credentials).unwrap();
1167-
assert_eq!(credentials, "[registry]\nsecret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n");
1168-
}
1169-
1170-
#[cargo_test]
1171-
fn login_with_asymmetric_key_subject_without_key() {
1172-
let registry = registry::init();
1173-
let credentials = paths::home().join(".cargo/credentials");
1174-
fs::remove_file(&credentials).unwrap();
1175-
cargo_process("login --key-subject=foo -Z registry-auth")
1176-
.masquerade_as_nightly_cargo(&["registry-auth"])
1177-
.replace_crates_io(registry.index_url())
1178-
.with_stderr_contains("error: need a secret_key to set a key_subject")
1179-
.with_status(101)
1180-
.run();
1181-
1182-
// ok so add a secret_key to the credentials
1183-
cargo_process("login --secret-key -v -Z registry-auth")
1184-
.masquerade_as_nightly_cargo(&["registry-auth"])
1185-
.replace_crates_io(registry.index_url())
1186-
.with_stdout(
1187-
"please paste the API secret key below
1188-
k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
1189-
)
1190-
.with_stdin("k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36")
1191-
.run();
1192-
1193-
// and then it shuld work
1194-
cargo_process("login --key-subject=foo -Z registry-auth")
1195-
.masquerade_as_nightly_cargo(&["registry-auth"])
1196-
.replace_crates_io(registry.index_url())
1197-
.run();
1198-
1199-
let credentials = fs::read_to_string(&credentials).unwrap();
1200-
assert!(credentials.starts_with("[registry]\n"));
1201-
assert!(credentials.contains("secret-key-subject = \"foo\"\n"));
1202-
assert!(credentials.contains("secret-key = \"k3.secret.fNYVuMvBgOlljt9TDohnaYLblghqaHoQquVZwgR6X12cBFHZLFsaU3q7X3k1Zn36\"\n"));
1203-
}
1204-
1205-
#[cargo_test]
1206-
fn login_with_generate_asymmetric_token() {
1207-
let registry = registry::init();
1208-
let credentials = paths::home().join(".cargo/credentials");
1209-
fs::remove_file(&credentials).unwrap();
1210-
cargo_process("login --generate-keypair -Z registry-auth")
1211-
.masquerade_as_nightly_cargo(&["registry-auth"])
1212-
.replace_crates_io(registry.index_url())
1213-
.with_stdout("k3.public.[..]")
1214-
.run();
1215-
let credentials = fs::read_to_string(&credentials).unwrap();
1216-
assert!(credentials.contains("secret-key = \"k3.secret."));
1217-
}
1218-
12191080
#[cargo_test]
12201081
fn bad_license_file_http() {
12211082
let registry = setup_http();

0 commit comments

Comments
 (0)