| 
 | 1 | +use crate::tests::builders::CrateBuilder;  | 
 | 2 | +use crate::tests::util::{RequestHelper, TestApp};  | 
 | 3 | +use crates_io_database::models::trustpub::{GitHubConfig, NewGitHubConfig};  | 
 | 4 | +use diesel::prelude::*;  | 
 | 5 | +use diesel_async::AsyncPgConnection;  | 
 | 6 | +use http::StatusCode;  | 
 | 7 | +use insta::{assert_json_snapshot, assert_snapshot};  | 
 | 8 | +use serde_json::json;  | 
 | 9 | + | 
 | 10 | +const URL: &str = "/api/v1/trusted_publishing/github_configs";  | 
 | 11 | + | 
 | 12 | +async fn create_config(  | 
 | 13 | +    conn: &mut AsyncPgConnection,  | 
 | 14 | +    crate_id: i32,  | 
 | 15 | +    repository_name: &str,  | 
 | 16 | +) -> QueryResult<GitHubConfig> {  | 
 | 17 | +    let config = NewGitHubConfig {  | 
 | 18 | +        crate_id,  | 
 | 19 | +        repository_owner: "rust-lang",  | 
 | 20 | +        repository_owner_id: 42,  | 
 | 21 | +        repository_name,  | 
 | 22 | +        workflow_filename: "publish.yml",  | 
 | 23 | +        environment: None,  | 
 | 24 | +    };  | 
 | 25 | + | 
 | 26 | +    config.insert(conn).await  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 30 | +async fn test_happy_path() -> anyhow::Result<()> {  | 
 | 31 | +    let (app, _client, cookie_client) = TestApp::full().with_user().await;  | 
 | 32 | +    let mut conn = app.db_conn().await;  | 
 | 33 | + | 
 | 34 | +    let owner_id = cookie_client.as_model().id;  | 
 | 35 | +    let foo = CrateBuilder::new("foo", owner_id).build(&mut conn).await?;  | 
 | 36 | +    let bar = CrateBuilder::new("bar", owner_id).build(&mut conn).await?;  | 
 | 37 | + | 
 | 38 | +    create_config(&mut conn, foo.id, "foo-rs").await?;  | 
 | 39 | +    create_config(&mut conn, foo.id, "foo").await?;  | 
 | 40 | +    create_config(&mut conn, bar.id, "BAR").await?;  | 
 | 41 | + | 
 | 42 | +    let response = cookie_client.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 43 | +    assert_eq!(response.status(), StatusCode::OK);  | 
 | 44 | +    assert_json_snapshot!(response.json(), {  | 
 | 45 | +        ".github_configs[].created_at" => "[datetime]",  | 
 | 46 | +    });  | 
 | 47 | + | 
 | 48 | +    let response = cookie_client.get_with_query::<()>(URL, "crate=Bar").await;  | 
 | 49 | +    assert_eq!(response.status(), StatusCode::OK);  | 
 | 50 | +    assert_json_snapshot!(response.json(), {  | 
 | 51 | +        ".github_configs[].created_at" => "[datetime]",  | 
 | 52 | +    });  | 
 | 53 | + | 
 | 54 | +    Ok(())  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 58 | +async fn test_unauthorized() -> anyhow::Result<()> {  | 
 | 59 | +    let (app, anon_client, cookie_client) = TestApp::full().with_user().await;  | 
 | 60 | +    let mut conn = app.db_conn().await;  | 
 | 61 | + | 
 | 62 | +    let owner_id = cookie_client.as_model().id;  | 
 | 63 | +    let krate = CrateBuilder::new("foo", owner_id).build(&mut conn).await?;  | 
 | 64 | +    create_config(&mut conn, krate.id, "foo-rs").await?;  | 
 | 65 | + | 
 | 66 | +    let response = anon_client.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 67 | +    assert_eq!(response.status(), StatusCode::FORBIDDEN);  | 
 | 68 | +    assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#);  | 
 | 69 | + | 
 | 70 | +    Ok(())  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 74 | +async fn test_not_owner() -> anyhow::Result<()> {  | 
 | 75 | +    let (app, _, cookie_client) = TestApp::full().with_user().await;  | 
 | 76 | +    let mut conn = app.db_conn().await;  | 
 | 77 | + | 
 | 78 | +    // Create a different user who will be the owner of the crate  | 
 | 79 | +    let owner_id = cookie_client.as_model().id;  | 
 | 80 | +    let krate = CrateBuilder::new("foo", owner_id).build(&mut conn).await?;  | 
 | 81 | +    create_config(&mut conn, krate.id, "foo-rs").await?;  | 
 | 82 | + | 
 | 83 | +    // The authenticated user is not an owner of the crate  | 
 | 84 | +    let other_user = app.db_new_user("other").await;  | 
 | 85 | +    let response = other_user.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 86 | +    assert_eq!(response.status(), StatusCode::BAD_REQUEST);  | 
 | 87 | +    assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You are not an owner of this crate"}]}"#);  | 
 | 88 | + | 
 | 89 | +    Ok(())  | 
 | 90 | +}  | 
 | 91 | + | 
 | 92 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 93 | +async fn test_team_owner() -> anyhow::Result<()> {  | 
 | 94 | +    let (app, _) = TestApp::full().empty().await;  | 
 | 95 | +    let mut conn = app.db_conn().await;  | 
 | 96 | + | 
 | 97 | +    let user = app.db_new_user("user-org-owner").await;  | 
 | 98 | +    let user2 = app.db_new_user("user-one-team").await;  | 
 | 99 | + | 
 | 100 | +    let owner_id = user.as_model().id;  | 
 | 101 | +    let krate = CrateBuilder::new("foo", owner_id).build(&mut conn).await?;  | 
 | 102 | +    create_config(&mut conn, krate.id, "foo-rs").await?;  | 
 | 103 | + | 
 | 104 | +    let body = json!({ "owners": ["github:test-org:all"] }).to_string();  | 
 | 105 | +    let response = user.put::<()>("/api/v1/crates/foo/owners", body).await;  | 
 | 106 | +    assert_eq!(response.status(), StatusCode::OK);  | 
 | 107 | + | 
 | 108 | +    let response = user2.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 109 | +    assert_eq!(response.status(), StatusCode::BAD_REQUEST);  | 
 | 110 | +    assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You are not an owner of this crate"}]}"#);  | 
 | 111 | + | 
 | 112 | +    Ok(())  | 
 | 113 | +}  | 
 | 114 | + | 
 | 115 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 116 | +async fn test_crate_not_found() -> anyhow::Result<()> {  | 
 | 117 | +    let (_, _, cookie_client) = TestApp::full().with_user().await;  | 
 | 118 | + | 
 | 119 | +    let response = cookie_client.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 120 | +    assert_eq!(response.status(), StatusCode::NOT_FOUND);  | 
 | 121 | +    assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crate `foo` does not exist"}]}"#);  | 
 | 122 | + | 
 | 123 | +    Ok(())  | 
 | 124 | +}  | 
 | 125 | + | 
 | 126 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 127 | +async fn test_no_query_param() -> anyhow::Result<()> {  | 
 | 128 | +    let (_, _, cookie_client) = TestApp::full().with_user().await;  | 
 | 129 | + | 
 | 130 | +    let response = cookie_client.get::<()>(URL).await;  | 
 | 131 | +    assert_eq!(response.status(), StatusCode::BAD_REQUEST);  | 
 | 132 | +    assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Failed to deserialize query string: missing field `crate`"}]}"#);  | 
 | 133 | + | 
 | 134 | +    Ok(())  | 
 | 135 | +}  | 
 | 136 | + | 
 | 137 | +#[tokio::test(flavor = "multi_thread")]  | 
 | 138 | +async fn test_crate_with_no_configs() -> anyhow::Result<()> {  | 
 | 139 | +    let (app, _, cookie_client) = TestApp::full().with_user().await;  | 
 | 140 | +    let mut conn = app.db_conn().await;  | 
 | 141 | + | 
 | 142 | +    let owner_id = cookie_client.as_model().id;  | 
 | 143 | +    CrateBuilder::new("foo", owner_id).build(&mut conn).await?;  | 
 | 144 | + | 
 | 145 | +    // No configs have been created for this crate  | 
 | 146 | +    let response = cookie_client.get_with_query::<()>(URL, "crate=foo").await;  | 
 | 147 | +    assert_eq!(response.status(), StatusCode::OK);  | 
 | 148 | +    assert_json_snapshot!(response.json(), {  | 
 | 149 | +        ".github_configs[].created_at" => "[datetime]",  | 
 | 150 | +    });  | 
 | 151 | + | 
 | 152 | +    Ok(())  | 
 | 153 | +}  | 
0 commit comments