|
1 | 1 | //! Tests for the `cargo login` command.
|
2 | 2 |
|
| 3 | +use cargo_test_support::cargo_process; |
3 | 4 | use cargo_test_support::install::cargo_home;
|
| 5 | +use cargo_test_support::paths::{self, CargoPathExt}; |
4 | 6 | use cargo_test_support::registry::{self, RegistryBuilder};
|
5 |
| -use cargo_test_support::{cargo_process, t}; |
| 7 | +use cargo_test_support::t; |
6 | 8 | use std::fs::{self};
|
7 | 9 | use std::path::PathBuf;
|
8 | 10 | use toml_edit::easy as toml;
|
@@ -176,3 +178,142 @@ fn asymmetric_requires_nightly() {
|
176 | 178 | See https://github.com/rust-lang/cargo/issues/10519 for more information about the `secret-key` flag.")
|
177 | 179 | .run();
|
178 | 180 | }
|
| 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 | +} |
0 commit comments