|
| 1 | +use crate::tests::builders::CrateBuilder; |
| 2 | +use crate::tests::util::{RequestHelper, TestApp}; |
| 3 | +use crates_io_database::models::Crate; |
| 4 | +use crates_io_database::models::trustpub::{GitHubConfig, NewGitHubConfig}; |
| 5 | +use crates_io_database::schema::trustpub_configs_github; |
| 6 | +use diesel::prelude::*; |
| 7 | +use diesel_async::{AsyncPgConnection, RunQueryDsl}; |
| 8 | +use http::StatusCode; |
| 9 | +use insta::assert_snapshot; |
| 10 | +use serde_json::json; |
| 11 | + |
| 12 | +const BASE_URL: &str = "/api/v1/trusted_publishing/github_configs"; |
| 13 | +const CRATE_NAME: &str = "foo"; |
| 14 | + |
| 15 | +fn delete_url(id: i32) -> String { |
| 16 | + format!("{BASE_URL}/{id}") |
| 17 | +} |
| 18 | + |
| 19 | +async fn create_crate(conn: &mut AsyncPgConnection, author_id: i32) -> anyhow::Result<Crate> { |
| 20 | + CrateBuilder::new(CRATE_NAME, author_id).build(conn).await |
| 21 | +} |
| 22 | + |
| 23 | +async fn create_config(conn: &mut AsyncPgConnection, crate_id: i32) -> QueryResult<GitHubConfig> { |
| 24 | + let config = NewGitHubConfig { |
| 25 | + crate_id, |
| 26 | + repository_owner: "rust-lang", |
| 27 | + repository_owner_id: 42, |
| 28 | + repository_name: "foo-rs", |
| 29 | + workflow_filename: "publish.yml", |
| 30 | + environment: None, |
| 31 | + }; |
| 32 | + |
| 33 | + config.insert(conn).await |
| 34 | +} |
| 35 | + |
| 36 | +async fn get_all_configs(conn: &mut AsyncPgConnection) -> QueryResult<Vec<GitHubConfig>> { |
| 37 | + trustpub_configs_github::table |
| 38 | + .select(GitHubConfig::as_select()) |
| 39 | + .load::<GitHubConfig>(conn) |
| 40 | + .await |
| 41 | +} |
| 42 | + |
| 43 | +/// Delete the config with a valid user that is an owner of the crate. |
| 44 | +#[tokio::test(flavor = "multi_thread")] |
| 45 | +async fn test_happy_path() -> anyhow::Result<()> { |
| 46 | + let (app, _client, cookie_client) = TestApp::full().with_user().await; |
| 47 | + let mut conn = app.db_conn().await; |
| 48 | + |
| 49 | + let krate = create_crate(&mut conn, cookie_client.as_model().id).await?; |
| 50 | + let config = create_config(&mut conn, krate.id).await?; |
| 51 | + |
| 52 | + let response = cookie_client.delete::<()>(&delete_url(config.id)).await; |
| 53 | + assert_eq!(response.status(), StatusCode::NO_CONTENT); |
| 54 | + assert_eq!(response.text(), ""); |
| 55 | + |
| 56 | + // Verify the config was deleted from the database |
| 57 | + let configs = get_all_configs(&mut conn).await?; |
| 58 | + assert_eq!(configs.len(), 0); |
| 59 | + |
| 60 | + // Verify emails were sent to crate owners |
| 61 | + assert_snapshot!(app.emails_snapshot().await); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +/// Try to delete the config with an unauthenticated client. |
| 67 | +#[tokio::test(flavor = "multi_thread")] |
| 68 | +async fn test_unauthenticated() -> anyhow::Result<()> { |
| 69 | + let (app, client, cookie_client) = TestApp::full().with_user().await; |
| 70 | + let mut conn = app.db_conn().await; |
| 71 | + |
| 72 | + let krate = create_crate(&mut conn, cookie_client.as_model().id).await?; |
| 73 | + let config = create_config(&mut conn, krate.id).await?; |
| 74 | + |
| 75 | + let response = client.delete::<()>(&delete_url(config.id)).await; |
| 76 | + assert_eq!(response.status(), StatusCode::FORBIDDEN); |
| 77 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#); |
| 78 | + |
| 79 | + // Verify the config was not deleted |
| 80 | + let configs = get_all_configs(&mut conn).await?; |
| 81 | + assert_eq!(configs.len(), 1); |
| 82 | + |
| 83 | + // Verify no emails were sent to crate owners |
| 84 | + assert_eq!(app.emails().await.len(), 0); |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +/// Try to delete the config with API token authentication. |
| 90 | +#[tokio::test(flavor = "multi_thread")] |
| 91 | +async fn test_token_auth() -> anyhow::Result<()> { |
| 92 | + let (app, _client, cookie_client, token_client) = TestApp::full().with_token().await; |
| 93 | + let mut conn = app.db_conn().await; |
| 94 | + |
| 95 | + let krate = create_crate(&mut conn, cookie_client.as_model().id).await?; |
| 96 | + let config = create_config(&mut conn, krate.id).await?; |
| 97 | + |
| 98 | + let response = token_client.delete::<()>(&delete_url(config.id)).await; |
| 99 | + assert_eq!(response.status(), StatusCode::FORBIDDEN); |
| 100 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action can only be performed on the crates.io website"}]}"#); |
| 101 | + |
| 102 | + // Verify the config was not deleted |
| 103 | + let configs = get_all_configs(&mut conn).await?; |
| 104 | + assert_eq!(configs.len(), 1); |
| 105 | + |
| 106 | + // Verify no emails were sent to crate owners |
| 107 | + assert_eq!(app.emails().await.len(), 0); |
| 108 | + |
| 109 | + Ok(()) |
| 110 | +} |
| 111 | + |
| 112 | +/// Try to delete a config that does not exist. |
| 113 | +#[tokio::test(flavor = "multi_thread")] |
| 114 | +async fn test_config_not_found() -> anyhow::Result<()> { |
| 115 | + let (app, _client, cookie_client) = TestApp::full().with_user().await; |
| 116 | + |
| 117 | + let response = cookie_client.delete::<()>(&delete_url(42)).await; |
| 118 | + assert_eq!(response.status(), StatusCode::NOT_FOUND); |
| 119 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Not Found"}]}"#); |
| 120 | + |
| 121 | + // Verify no emails were sent to crate owners |
| 122 | + assert_eq!(app.emails().await.len(), 0); |
| 123 | + |
| 124 | + Ok(()) |
| 125 | +} |
| 126 | + |
| 127 | +/// Try to delete the config with a user who is not an owner of the crate. |
| 128 | +#[tokio::test(flavor = "multi_thread")] |
| 129 | +async fn test_non_owner() -> anyhow::Result<()> { |
| 130 | + let (app, _client, cookie_client) = TestApp::full().with_user().await; |
| 131 | + let mut conn = app.db_conn().await; |
| 132 | + |
| 133 | + let krate = create_crate(&mut conn, cookie_client.as_model().id).await?; |
| 134 | + let config = create_config(&mut conn, krate.id).await?; |
| 135 | + |
| 136 | + // Create another user who is not an owner of the crate |
| 137 | + let other_client = app.db_new_user("other_user").await; |
| 138 | + |
| 139 | + let response = other_client.delete::<()>(&delete_url(config.id)).await; |
| 140 | + assert_eq!(response.status(), StatusCode::BAD_REQUEST); |
| 141 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You are not an owner of this crate"}]}"#); |
| 142 | + |
| 143 | + // Verify the config was not deleted |
| 144 | + let configs = get_all_configs(&mut conn).await?; |
| 145 | + assert_eq!(configs.len(), 1); |
| 146 | + |
| 147 | + // Verify no emails were sent to crate owners |
| 148 | + assert_eq!(app.emails().await.len(), 0); |
| 149 | + |
| 150 | + Ok(()) |
| 151 | +} |
| 152 | + |
| 153 | +/// Try to delete the config with a user that is part of a team that owns |
| 154 | +/// the crate. |
| 155 | +#[tokio::test(flavor = "multi_thread")] |
| 156 | +async fn test_team_owner() -> anyhow::Result<()> { |
| 157 | + let (app, _client) = TestApp::full().empty().await; |
| 158 | + let mut conn = app.db_conn().await; |
| 159 | + |
| 160 | + let user = app.db_new_user("user-org-owner").await; |
| 161 | + let user2 = app.db_new_user("user-one-team").await; |
| 162 | + |
| 163 | + let krate = create_crate(&mut conn, user.as_model().id).await?; |
| 164 | + let config = create_config(&mut conn, krate.id).await?; |
| 165 | + |
| 166 | + let body = json!({ "owners": ["github:test-org:all"] }).to_string(); |
| 167 | + let response = user.put::<()>("/api/v1/crates/foo/owners", body).await; |
| 168 | + assert_eq!(response.status(), StatusCode::OK); |
| 169 | + |
| 170 | + let response = user2.delete::<()>(&delete_url(config.id)).await; |
| 171 | + assert_eq!(response.status(), StatusCode::BAD_REQUEST); |
| 172 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You are not an owner of this crate"}]}"#); |
| 173 | + |
| 174 | + // Verify the config was not deleted |
| 175 | + let configs = get_all_configs(&mut conn).await?; |
| 176 | + assert_eq!(configs.len(), 1); |
| 177 | + |
| 178 | + // Verify no emails were sent to crate owners |
| 179 | + assert_eq!(app.emails().await.len(), 0); |
| 180 | + |
| 181 | + Ok(()) |
| 182 | +} |
0 commit comments