Skip to content

Commit 0e13f66

Browse files
committed
Add tests for registry.default for login/logout
1 parent 91d39bc commit 0e13f66

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

tests/testsuite/login.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,35 @@ fn login_with_generate_asymmetric_token() {
368368
let credentials = fs::read_to_string(&credentials).unwrap();
369369
assert!(credentials.contains("secret-key = \"k3.secret."));
370370
}
371+
372+
#[cargo_test]
373+
fn default_registry_configured() {
374+
// When registry.default is set, login should use that one when
375+
// --registry is not used.
376+
let cargo_home = paths::home().join(".cargo");
377+
cargo_home.mkdir_p();
378+
cargo_util::paths::write(
379+
&cargo_home.join("config.toml"),
380+
r#"
381+
[registry]
382+
default = "dummy-registry"
383+
384+
[registries.dummy-registry]
385+
index = "https://127.0.0.1/index"
386+
"#,
387+
)
388+
.unwrap();
389+
390+
cargo_process("login")
391+
.arg("a-new-token")
392+
.with_stderr(
393+
"\
394+
[UPDATING] crates.io index
395+
[LOGIN] token for `crates.io` saved
396+
",
397+
)
398+
.run();
399+
400+
check_token(Some("a-new-token"), None);
401+
check_token(None, Some("alternative"));
402+
}

tests/testsuite/logout.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for the `cargo logout` command.
22
33
use super::login::check_token;
4+
use cargo_test_support::paths::{self, CargoPathExt};
45
use cargo_test_support::registry::TestRegistry;
56
use cargo_test_support::{cargo_process, registry};
67

@@ -51,7 +52,7 @@ If you need to revoke the token, visit {note} and follow the instructions there.
5152
}
5253

5354
#[cargo_test]
54-
fn default_registry() {
55+
fn default_registry_unconfigured() {
5556
let registry = registry::init();
5657
simple_logout_test(&registry, None, "", "<https://crates.io/me>");
5758
}
@@ -68,3 +69,54 @@ fn other_registry() {
6869
// It should not touch crates.io.
6970
check_token(Some("sekrit"), None);
7071
}
72+
73+
#[cargo_test]
74+
fn default_registry_configured() {
75+
// When registry.default is set, logout should use that one when
76+
// --registry is not used.
77+
let cargo_home = paths::home().join(".cargo");
78+
cargo_home.mkdir_p();
79+
cargo_util::paths::write(
80+
&cargo_home.join("config.toml"),
81+
r#"
82+
[registry]
83+
default = "dummy-registry"
84+
85+
[registries.dummy-registry]
86+
index = "https://127.0.0.1/index"
87+
"#,
88+
)
89+
.unwrap();
90+
cargo_util::paths::write(
91+
&cargo_home.join("credentials.toml"),
92+
r#"
93+
[registry]
94+
token = "crates-io-token"
95+
96+
[registries.dummy-registry]
97+
token = "dummy-token"
98+
"#,
99+
)
100+
.unwrap();
101+
check_token(Some("dummy-token"), Some("dummy-registry"));
102+
check_token(Some("crates-io-token"), None);
103+
104+
cargo_process("logout -Zunstable-options")
105+
.masquerade_as_nightly_cargo(&["cargo-logout"])
106+
.with_stderr(
107+
"\
108+
[LOGOUT] token for `crates-io` has been removed from local storage
109+
[NOTE] This does not revoke the token on the registry server.
110+
If you need to revoke the token, visit <https://crates.io/me> \
111+
and follow the instructions there.
112+
",
113+
)
114+
.run();
115+
check_token(Some("dummy-token"), Some("dummy-registry"));
116+
check_token(None, None);
117+
118+
cargo_process("logout -Zunstable-options")
119+
.masquerade_as_nightly_cargo(&["cargo-logout"])
120+
.with_stderr("[LOGOUT] not currently logged in to `crates-io`")
121+
.run();
122+
}

0 commit comments

Comments
 (0)