Skip to content
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: 4 additions & 0 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ impl Crate {

let users = CrateOwner::by_owner_kind(OwnerKind::User)
.filter(crate_owners::crate_id.eq(self.id))
.order((crate_owners::owner_id, crate_owners::owner_kind))
.inner_join(users::table)
.select(User::as_select())
.load(conn)
Expand All @@ -369,6 +370,7 @@ impl Crate {

let teams = CrateOwner::by_owner_kind(OwnerKind::Team)
.filter(crate_owners::crate_id.eq(self.id))
.order((crate_owners::owner_id, crate_owners::owner_kind))
.inner_join(teams::table)
.select(Team::as_select())
.load(conn)
Expand All @@ -384,13 +386,15 @@ impl Crate {

let users = CrateOwner::by_owner_kind(OwnerKind::User)
.filter(crate_owners::crate_id.eq(self.id))
.order((crate_owners::owner_id, crate_owners::owner_kind))
.inner_join(users::table)
.select(User::as_select())
.load(conn)?
.into_iter()
.map(Owner::User);
let teams = CrateOwner::by_owner_kind(OwnerKind::Team)
.filter(crate_owners::crate_id.eq(self.id))
.order((crate_owners::owner_id, crate_owners::owner_kind))
.inner_join(teams::table)
.select(Team::as_select())
.load(conn)?
Expand Down
95 changes: 95 additions & 0 deletions src/tests/issues/issue1205.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::tests::builders::CrateBuilder;
use crate::tests::util::TestApp;
use crates_io_github::{GitHubOrganization, GitHubTeam, GitHubTeamMembership, MockGitHubClient};
use http::StatusCode;
use insta::assert_snapshot;

/// See <https://github.com/rust-lang/crates.io/issues/1205>.
#[tokio::test(flavor = "multi_thread")]
async fn test_issue_1205() -> anyhow::Result<()> {
const CRATE_NAME: &str = "deepspeech-sys";

let (app, _, _, user) = TestApp::full().with_github(github_mock()).with_token();

let mut conn = app.db_conn();

let krate = CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn);

let response = user
.add_named_owner(CRATE_NAME, "github:rustaudio:owners")
.await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.text(), @r#"{"msg":"team github:rustaudio:owners has been added as an owner of crate deepspeech-sys","ok":true}"#);

let owners = krate.owners(&mut conn)?;
assert_eq!(owners.len(), 2);
assert_eq!(owners[0].login(), "foo");
assert_eq!(owners[1].login(), "github:rustaudio:owners");

let response = user
.add_named_owner(CRATE_NAME, "github:rustaudio:cratesio-push")
.await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.text(), @r#"{"msg":"team github:rustaudio:cratesio-push has been added as an owner of crate deepspeech-sys","ok":true}"#);

let owners = krate.owners(&mut conn)?;
assert_eq!(owners.len(), 2);
assert_eq!(owners[0].login(), "foo");
assert_eq!(owners[1].login(), "github:rustaudio:cratesio-push");

let response = user
.remove_named_owner(CRATE_NAME, "github:rustaudio:owners")
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"could not find team with login `github:rustaudio:owners`"}]}"#);

Ok(())
}

fn github_mock() -> MockGitHubClient {
use mockall::predicate::*;

let mut github_mock = MockGitHubClient::new();

github_mock
.expect_org_by_name()
.with(eq("rustaudio"), always())
.returning(|_, _| Ok(org()));

github_mock
.expect_team_by_name()
.with(eq("rustaudio"), eq("owners"), always())
.returning(|_, name, _| Ok(team(name)));

github_mock
.expect_team_by_name()
.with(eq("rustaudio"), eq("cratesio-push"), always())
.returning(|_, name, _| Ok(team(name)));

github_mock
.expect_team_membership()
.with(eq(1), eq(2), eq("foo"), always())
.returning(|_, _, _, _| Ok(active_membership()));

github_mock
}

fn org() -> GitHubOrganization {
GitHubOrganization {
id: 1,
avatar_url: None,
}
}

fn team(name: &str) -> GitHubTeam {
GitHubTeam {
id: 2,
name: Some(name.to_string()),
organization: org(),
}
}

fn active_membership() -> GitHubTeamMembership {
let state = "active".to_string();
GitHubTeamMembership { state }
}
1 change: 1 addition & 0 deletions src/tests/issues/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod issue1205;
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod categories;
mod cors;
mod dump_db;
mod github_secret_scanning;
mod issues;
mod krate;
mod middleware;
mod not_found_error;
Expand Down