Skip to content

Commit e632bdb

Browse files
committed
Update response handling to add tests for cargo owner -a/-r
Follow up 5e15286.
1 parent 9826222 commit e632bdb

File tree

3 files changed

+110
-16
lines changed

3 files changed

+110
-16
lines changed

crates/crates-io/lib.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,41 @@ impl Registry {
149149
pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> {
150150
let body = serde_json::to_string(&OwnersReq { users: owners })?;
151151
let body = self.put(&format!("/crates/{}/owners", krate), body.as_bytes())?;
152-
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
153-
Ok(serde_json::from_str::<OwnerResponse>(&body)?.msg)
152+
let body = if body.is_empty() {
153+
r#"{"ok":false,"msg":"response body is empty"}"#.parse()?
154+
} else {
155+
body
156+
};
157+
match serde_json::from_str::<OwnerResponse>(&body) {
158+
Ok(response) => {
159+
if response.ok {
160+
Ok(response.msg)
161+
} else {
162+
bail!("{}", response.msg)
163+
}
164+
}
165+
_ => bail!("failed to parse response body"),
166+
}
154167
}
155168

156-
pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<()> {
169+
pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> {
157170
let body = serde_json::to_string(&OwnersReq { users: owners })?;
158171
let body = self.delete(&format!("/crates/{}/owners", krate), Some(body.as_bytes()))?;
159-
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
160-
Ok(())
172+
let body = if body.is_empty() {
173+
r#"{"ok":false,"msg":"response body is empty"}"#.parse()?
174+
} else {
175+
body
176+
};
177+
match serde_json::from_str::<OwnerResponse>(&body) {
178+
Ok(response) => {
179+
if response.ok {
180+
Ok(response.msg)
181+
} else {
182+
bail!("{}", response.msg)
183+
}
184+
}
185+
_ => bail!("failed to parse response body"),
186+
}
161187
}
162188

163189
pub fn list_owners(&mut self, krate: &str) -> Result<Vec<User>> {

src/doc/src/reference/registries.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,9 @@ A successful response includes the JSON object:
546546
```javascript
547547
{
548548
// Indicates the remove succeeded, always true.
549-
"ok": true
549+
"ok": true,
550+
// A string to be displayed to the user.
551+
"msg": "user someone has been deleted from owners of crate cargo"
550552
}
551553
```
552554

tests/testsuite/owner.rs

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,55 @@ use cargo_test_support::paths::CargoPathExt;
66
use cargo_test_support::project;
77
use cargo_test_support::registry::{self, api_path, registry_url};
88

9-
fn setup(name: &str) {
9+
fn setup(name: &str, content: Option<&str>) {
1010
let dir = api_path().join(format!("api/v1/crates/{}", name));
1111
dir.mkdir_p();
12-
fs::write(
13-
dir.join("owners"),
14-
r#"{
12+
match content {
13+
Some(body) => {
14+
fs::write(dir.join("owners"), body).unwrap();
15+
}
16+
None => {}
17+
}
18+
}
19+
20+
#[cargo_test]
21+
fn simple_list() {
22+
registry::init();
23+
let content = r#"{
1524
"users": [
1625
{
1726
"id": 70,
1827
"login": "github:rust-lang:core",
1928
"name": "Core"
2029
}
2130
]
22-
}"#,
23-
)
24-
.unwrap();
31+
}"#;
32+
setup("foo", Some(content));
33+
34+
let p = project()
35+
.file(
36+
"Cargo.toml",
37+
r#"
38+
[project]
39+
name = "foo"
40+
version = "0.0.1"
41+
authors = []
42+
license = "MIT"
43+
description = "foo"
44+
"#,
45+
)
46+
.file("src/main.rs", "fn main() {}")
47+
.build();
48+
49+
p.cargo("owner -l --index")
50+
.arg(registry_url().to_string())
51+
.run();
2552
}
2653

2754
#[cargo_test]
28-
fn simple_list() {
55+
fn simple_add() {
2956
registry::init();
30-
setup("foo");
57+
setup("foo", None);
3158

3259
let p = project()
3360
.file(
@@ -44,7 +71,46 @@ fn simple_list() {
4471
.file("src/main.rs", "fn main() {}")
4572
.build();
4673

47-
p.cargo("owner -l --index")
74+
p.cargo("owner -a username --index")
4875
.arg(registry_url().to_string())
76+
.with_status(101)
77+
.with_stderr(
78+
" Updating `[..]` index
79+
error: failed to invite owners to crate foo: response body is empty",
80+
)
81+
.run();
82+
}
83+
84+
#[cargo_test]
85+
fn simple_remove() {
86+
registry::init();
87+
setup("foo", None);
88+
89+
let p = project()
90+
.file(
91+
"Cargo.toml",
92+
r#"
93+
[project]
94+
name = "foo"
95+
version = "0.0.1"
96+
authors = []
97+
license = "MIT"
98+
description = "foo"
99+
"#,
100+
)
101+
.file("src/main.rs", "fn main() {}")
102+
.build();
103+
104+
p.cargo("owner -r username --index")
105+
.arg(registry_url().to_string())
106+
.with_status(101)
107+
.with_stderr(
108+
" Updating `[..]` index
109+
Owner removing [\"username\"] from crate foo
110+
error: failed to remove owners from crate foo
111+
112+
Caused by:
113+
response body is empty",
114+
)
49115
.run();
50116
}

0 commit comments

Comments
 (0)