|
| 1 | +use crate::api; |
| 2 | +use crate::api::error::Error; |
| 3 | +use crate::api::Context; |
| 4 | +use crate::axum_extra::{Path, Query}; |
| 5 | +use crate::store::Webhook; |
| 6 | +use axum::extract::State; |
| 7 | +use axum::response::IntoResponse; |
| 8 | +use axum::routing::post; |
| 9 | +use axum::{Json, Router}; |
| 10 | +use axum_auth::AuthBearer; |
| 11 | +use radicle::identity::RepoId; |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use serde_json::json; |
| 14 | + |
| 15 | +pub fn router(ctx: Context) -> Router { |
| 16 | + Router::new() |
| 17 | + .route( |
| 18 | + "/projects/:repo_id/webhooks", |
| 19 | + post(webhooks_post_handler) |
| 20 | + .delete(webhooks_delete_handler) |
| 21 | + .get(webhooks_get_handler), |
| 22 | + ) |
| 23 | + .with_state(ctx) |
| 24 | +} |
| 25 | + |
| 26 | +/// Return the webhooks for the repo. |
| 27 | +/// `GET /projects/:repo_id/webhooks` |
| 28 | +async fn webhooks_get_handler( |
| 29 | + State(ctx): State<Context>, |
| 30 | + AuthBearer(token): AuthBearer, |
| 31 | + Path(repo_id): Path<RepoId>, |
| 32 | +) -> impl IntoResponse { |
| 33 | + api::auth::validate(&ctx, &token).await?; |
| 34 | + let (_, _) = ctx.repo(repo_id)?; |
| 35 | + let mut db = ctx.open_webhooks_db()?; |
| 36 | + let webhooks = db.get(repo_id.to_string())?; |
| 37 | + Ok::<_, Error>(Json(json!(webhooks))) |
| 38 | +} |
| 39 | + |
| 40 | +/// Creates a webhook for the repo. |
| 41 | +/// `POST /projects/:repo_id/webhooks` |
| 42 | +async fn webhooks_post_handler( |
| 43 | + State(ctx): State<Context>, |
| 44 | + AuthBearer(token): AuthBearer, |
| 45 | + Path(repo_id): Path<RepoId>, |
| 46 | + Json(mut webhook): Json<Webhook>, |
| 47 | +) -> impl IntoResponse { |
| 48 | + api::auth::validate(&ctx, &token).await?; |
| 49 | + let (repo, _) = ctx.repo(repo_id)?; |
| 50 | + webhook.repo_id = repo.id.to_string(); |
| 51 | + let mut db = ctx.open_webhooks_db()?; |
| 52 | + db.insert(&webhook)?; |
| 53 | + Ok::<_, Error>(Json(json!(webhook))) |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Serialize, Deserialize, Clone)] |
| 57 | +#[serde(rename_all = "camelCase")] |
| 58 | +pub struct QueryUrl { |
| 59 | + pub url: Option<String>, |
| 60 | +} |
| 61 | + |
| 62 | +/// Deletes the webhooks for the repo, or the webhook with the specific url, if-provided |
| 63 | +/// `DELETE /projects/:repo_id/webhooks` |
| 64 | +async fn webhooks_delete_handler( |
| 65 | + State(ctx): State<Context>, |
| 66 | + AuthBearer(token): AuthBearer, |
| 67 | + Path(repo_id): Path<RepoId>, |
| 68 | + Query(qs): Query<QueryUrl>, |
| 69 | +) -> impl IntoResponse { |
| 70 | + api::auth::validate(&ctx, &token).await?; |
| 71 | + let (_, _) = ctx.repo(repo_id)?; |
| 72 | + let mut db = ctx.open_webhooks_db()?; |
| 73 | + db.remove(repo_id.to_string(), qs.url)?; |
| 74 | + // returning OK without checking if we actually deleted anything |
| 75 | + |
| 76 | + Ok::<_, Error>(Json(json!({"repo_id": repo_id}))) |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod webhooks_api_tests { |
| 81 | + use crate::store::Webhook; |
| 82 | + use crate::test::{create_session, delete, get_auth, post, seed, Response, RID, SESSION_ID}; |
| 83 | + use axum::body::Body; |
| 84 | + use axum::http::StatusCode; |
| 85 | + use axum::Router; |
| 86 | + use serde_json::json; |
| 87 | + |
| 88 | + #[tokio::test] |
| 89 | + async fn test_webhooks() { |
| 90 | + let tmp = tempfile::tempdir().unwrap(); |
| 91 | + let ctx = seed(tmp.path()); |
| 92 | + let app = super::router(ctx.to_owned()); |
| 93 | + |
| 94 | + create_session(ctx).await; |
| 95 | + |
| 96 | + let webhook = gen_webhook(1); |
| 97 | + |
| 98 | + let response = create_webhook(&app, &webhook).await; |
| 99 | + assert_eq!(response.status(), StatusCode::OK); |
| 100 | + |
| 101 | + // get webhooks, we should find the one created |
| 102 | + let mut get_whs = get_webhooks(&app).await; |
| 103 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 1); |
| 104 | + assert_eq!(get_whs.as_array_mut().unwrap()[0], json!(webhook)); |
| 105 | + |
| 106 | + // delete all under repo |
| 107 | + let del_resp = delete( |
| 108 | + &app, |
| 109 | + format!("/projects/{RID}/webhooks"), |
| 110 | + None, |
| 111 | + Some(SESSION_ID.to_string()), |
| 112 | + ) |
| 113 | + .await; |
| 114 | + assert_eq!(del_resp.status(), StatusCode::OK); |
| 115 | + |
| 116 | + // get should return empty array now |
| 117 | + let mut get_whs = get_webhooks(&app).await; |
| 118 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 0); |
| 119 | + } |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn test_multiple_webhooks() { |
| 123 | + let tmp = tempfile::tempdir().unwrap(); |
| 124 | + let ctx = seed(tmp.path()); |
| 125 | + let app = super::router(ctx.to_owned()); |
| 126 | + |
| 127 | + create_session(ctx).await; |
| 128 | + |
| 129 | + let webhook1 = gen_webhook(1); |
| 130 | + let response = create_webhook(&app, &webhook1).await; |
| 131 | + assert_eq!(response.status(), StatusCode::OK); |
| 132 | + |
| 133 | + let webhook2 = gen_webhook(2); |
| 134 | + let response = create_webhook(&app, &webhook2).await; |
| 135 | + assert_eq!(response.status(), StatusCode::OK); |
| 136 | + |
| 137 | + // get webhooks, we should find both created |
| 138 | + let mut get_whs = get_webhooks(&app).await; |
| 139 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 2); |
| 140 | + |
| 141 | + // add webhook with same url as webhook1 again, it should be "ignored" |
| 142 | + let response = create_webhook(&app, &webhook1).await; |
| 143 | + assert_eq!(response.status(), StatusCode::OK); |
| 144 | + |
| 145 | + let mut get_whs = get_webhooks(&app).await; |
| 146 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 2); |
| 147 | + |
| 148 | + // delete by url |
| 149 | + let url1 = webhook1.url; |
| 150 | + let del_resp = delete( |
| 151 | + &app, |
| 152 | + format!("/projects/{RID}/webhooks?url={url1}"), |
| 153 | + None, |
| 154 | + Some(SESSION_ID.to_string()), |
| 155 | + ) |
| 156 | + .await; |
| 157 | + assert_eq!(del_resp.status(), StatusCode::OK); |
| 158 | + |
| 159 | + //verify we only have webhook2 now |
| 160 | + let mut get_whs = get_webhooks(&app).await; |
| 161 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 1); |
| 162 | + assert_eq!(get_whs.as_array_mut().unwrap()[0], json!(webhook2)); |
| 163 | + |
| 164 | + // add multiple other webhooks again |
| 165 | + for i in 0..5 { |
| 166 | + let wh = gen_webhook(10 + i); |
| 167 | + create_webhook(&app, &wh).await; |
| 168 | + } |
| 169 | + |
| 170 | + // we should have webhook2 + the 5 new ones |
| 171 | + let mut get_whs = get_webhooks(&app).await; |
| 172 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 1 + 5); |
| 173 | + |
| 174 | + // delete all webhooks in repo |
| 175 | + let del_resp = delete( |
| 176 | + &app, |
| 177 | + format!("/projects/{RID}/webhooks"), |
| 178 | + None, |
| 179 | + Some(SESSION_ID.to_string()), |
| 180 | + ) |
| 181 | + .await; |
| 182 | + assert_eq!(del_resp.status(), StatusCode::OK); |
| 183 | + |
| 184 | + // verify we have 0 webhooks now |
| 185 | + let mut get_whs = get_webhooks(&app).await; |
| 186 | + assert_eq!(get_whs.as_array_mut().unwrap().len(), 0); |
| 187 | + } |
| 188 | + |
| 189 | + fn gen_webhook(id: u64) -> Webhook { |
| 190 | + Webhook { |
| 191 | + repo_id: RID.to_string(), |
| 192 | + url: format!("test_url_{id}"), |
| 193 | + secret: "test_secret".to_string(), |
| 194 | + content_type: "content type".to_string(), |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + async fn create_webhook(app: &Router, webhook: &Webhook) -> Response { |
| 199 | + let body = Some(Body::from(json!(webhook).to_string())); |
| 200 | + let s = Some(SESSION_ID.to_string()); |
| 201 | + post(app, format!("/projects/{RID}/webhooks"), body, s).await |
| 202 | + } |
| 203 | + |
| 204 | + async fn get_webhooks(app: &Router) -> serde_json::Value { |
| 205 | + let s = Some(SESSION_ID.to_string()); |
| 206 | + let get_resp = get_auth(app, format!("/projects/{RID}/webhooks"), s).await; |
| 207 | + assert_eq!(get_resp.status(), StatusCode::OK); |
| 208 | + get_resp.json().await |
| 209 | + } |
| 210 | +} |
0 commit comments