Skip to content

Issue #680 : Crate owners can now remove themselves as owner #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,8 +1404,8 @@ fn modify_owners(req: &mut Request, add: bool) -> CargoResult<Response> {
} else {
// Removing the team that gives you rights is prevented because
// team members only have Rights::Publish
if *login == user.gh_login {
return Err(human("cannot remove yourself as an owner"));
if owners.len() == 1 {
return Err(human("cannot remove the sole owner of a crate"));
}
krate.owner_remove(&conn, user, login)?;
}
Expand Down
65 changes: 65 additions & 0 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,71 @@ fn following() {
assert_eq!(::json::<CrateList>(&mut response).crates.len(), 0);
}

// Ensures that so long as at least one owner remains associated with the crate,
// a user can still remove their own login as an owner
#[test]
fn owners_can_remove_self() {
Copy link
Contributor

@vignesh-sankaran vignesh-sankaran Aug 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test here, it's appreciated :). It helps in that it increases the test coverage of the backend.

#[derive(Deserialize)]
struct R {
users: Vec<EncodablePublicUser>,
}
#[derive(Deserialize)]
struct O {
ok: bool,
}

let (_b, app, middle) = ::app();
let mut req = ::req(
app.clone(),
Method::Get,
"/api/v1/crates/owners_selfremove/owners",
);
{
let conn = app.diesel_database.get().unwrap();
::new_user("secondowner").create_or_update(&conn).unwrap();
let user = ::new_user("firstowner").create_or_update(&conn).unwrap();
::sign_in_as(&mut req, &user);
::CrateBuilder::new("owners_selfremove", user.id).expect_build(&conn);
}

let mut response = ok_resp!(middle.call(&mut req));
let r: R = ::json(&mut response);
assert_eq!(r.users.len(), 1);

// Deleting yourself when you're the only owner isn't allowed.
let body = r#"{"users":["firstowner"]}"#;
let mut response = ok_resp!(middle.call(req.with_method(Method::Delete).with_body(
body.as_bytes(),
)));
let json = ::json::<::Bad>(&mut response);
assert!(json.errors[0].detail.contains(
"cannot remove the sole owner of a crate",
));

let body = r#"{"users":["secondowner"]}"#;
let mut response = ok_resp!(middle.call(req.with_method(Method::Put).with_body(
body.as_bytes(),
)));
assert!(::json::<O>(&mut response).ok);

// Deleting yourself when there are other owners is allowed.
let body = r#"{"users":["firstowner"]}"#;
let mut response = ok_resp!(middle.call(req.with_method(Method::Delete).with_body(
body.as_bytes(),
)));
assert!(::json::<O>(&mut response).ok);

// After you delete yourself, you no longer have permisions to manage the crate.
let body = r#"{"users":["secondowner"]}"#;
let mut response = ok_resp!(middle.call(req.with_method(Method::Delete).with_body(
body.as_bytes(),
)));
let json = ::json::<::Bad>(&mut response);
assert!(json.errors[0].detail.contains(
"only owners have permission to modify owners",
));
}

#[test]
fn owners() {
#[derive(Deserialize)]
Expand Down