|
| 1 | +use crate::tests::builders::CrateBuilder; |
| 2 | +use crate::tests::util::TestApp; |
| 3 | +use crates_io_github::{GitHubOrganization, GitHubTeam, GitHubTeamMembership, MockGitHubClient}; |
| 4 | +use http::StatusCode; |
| 5 | +use insta::assert_snapshot; |
| 6 | + |
| 7 | +/// See <https://github.com/rust-lang/crates.io/issues/1205>. |
| 8 | +#[tokio::test(flavor = "multi_thread")] |
| 9 | +async fn test_issue_1205() -> anyhow::Result<()> { |
| 10 | + const CRATE_NAME: &str = "deepspeech-sys"; |
| 11 | + |
| 12 | + let (app, _, _, user) = TestApp::full().with_github(github_mock()).with_token(); |
| 13 | + |
| 14 | + let mut conn = app.db_conn(); |
| 15 | + |
| 16 | + let krate = CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn); |
| 17 | + |
| 18 | + let response = user |
| 19 | + .add_named_owner(CRATE_NAME, "github:rustaudio:owners") |
| 20 | + .await; |
| 21 | + assert_eq!(response.status(), StatusCode::OK); |
| 22 | + assert_snapshot!(response.text(), @r#"{"msg":"team github:rustaudio:owners has been added as an owner of crate deepspeech-sys","ok":true}"#); |
| 23 | + |
| 24 | + let owners = krate.owners(&mut conn)?; |
| 25 | + assert_eq!(owners.len(), 2); |
| 26 | + assert_eq!(owners[0].login(), "foo"); |
| 27 | + assert_eq!(owners[1].login(), "github:rustaudio:owners"); |
| 28 | + |
| 29 | + let response = user |
| 30 | + .add_named_owner(CRATE_NAME, "github:rustaudio:cratesio-push") |
| 31 | + .await; |
| 32 | + assert_eq!(response.status(), StatusCode::OK); |
| 33 | + assert_snapshot!(response.text(), @r#"{"msg":"team github:rustaudio:cratesio-push has been added as an owner of crate deepspeech-sys","ok":true}"#); |
| 34 | + |
| 35 | + let owners = krate.owners(&mut conn)?; |
| 36 | + assert_eq!(owners.len(), 2); |
| 37 | + assert_eq!(owners[0].login(), "foo"); |
| 38 | + assert_eq!(owners[1].login(), "github:rustaudio:cratesio-push"); |
| 39 | + |
| 40 | + let response = user |
| 41 | + .remove_named_owner(CRATE_NAME, "github:rustaudio:owners") |
| 42 | + .await; |
| 43 | + assert_eq!(response.status(), StatusCode::BAD_REQUEST); |
| 44 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"could not find team with login `github:rustaudio:owners`"}]}"#); |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +fn github_mock() -> MockGitHubClient { |
| 50 | + use mockall::predicate::*; |
| 51 | + |
| 52 | + let mut github_mock = MockGitHubClient::new(); |
| 53 | + |
| 54 | + github_mock |
| 55 | + .expect_org_by_name() |
| 56 | + .with(eq("rustaudio"), always()) |
| 57 | + .returning(|_, _| Ok(org())); |
| 58 | + |
| 59 | + github_mock |
| 60 | + .expect_team_by_name() |
| 61 | + .with(eq("rustaudio"), eq("owners"), always()) |
| 62 | + .returning(|_, name, _| Ok(team(name))); |
| 63 | + |
| 64 | + github_mock |
| 65 | + .expect_team_by_name() |
| 66 | + .with(eq("rustaudio"), eq("cratesio-push"), always()) |
| 67 | + .returning(|_, name, _| Ok(team(name))); |
| 68 | + |
| 69 | + github_mock |
| 70 | + .expect_team_membership() |
| 71 | + .with(eq(1), eq(2), eq("foo"), always()) |
| 72 | + .returning(|_, _, _, _| Ok(active_membership())); |
| 73 | + |
| 74 | + github_mock |
| 75 | +} |
| 76 | + |
| 77 | +fn org() -> GitHubOrganization { |
| 78 | + GitHubOrganization { |
| 79 | + id: 1, |
| 80 | + avatar_url: None, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +fn team(name: &str) -> GitHubTeam { |
| 85 | + GitHubTeam { |
| 86 | + id: 2, |
| 87 | + name: Some(name.to_string()), |
| 88 | + organization: org(), |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn active_membership() -> GitHubTeamMembership { |
| 93 | + let state = "active".to_string(); |
| 94 | + GitHubTeamMembership { state } |
| 95 | +} |
0 commit comments