From ee80fc87c95a4c94190b3bb74066aa3c05fa80af Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Thu, 31 Jul 2025 13:00:43 +0200 Subject: [PATCH] Add test for patch endpoint --- crates/meilisearch/src/routes/webhooks.rs | 6 +- crates/meilisearch/tests/common/server.rs | 5 ++ crates/meilisearch/tests/tasks/webhook.rs | 84 +++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/crates/meilisearch/src/routes/webhooks.rs b/crates/meilisearch/src/routes/webhooks.rs index 4b925c325..776667829 100644 --- a/crates/meilisearch/src/routes/webhooks.rs +++ b/crates/meilisearch/src/routes/webhooks.rs @@ -457,7 +457,11 @@ async fn patch_webhook( analytics.publish(PatchWebhooksAnalytics::patch_webhook(), &req); - Ok(HttpResponse::Ok().json(WebhookWithMetadata { uuid, is_editable: uuid != Uuid::nil(), webhook })) + Ok(HttpResponse::Ok().json(WebhookWithMetadata { + uuid, + is_editable: uuid != Uuid::nil(), + webhook, + })) } #[utoipa::path( diff --git a/crates/meilisearch/tests/common/server.rs b/crates/meilisearch/tests/common/server.rs index dd690c3db..0b57ca37a 100644 --- a/crates/meilisearch/tests/common/server.rs +++ b/crates/meilisearch/tests/common/server.rs @@ -200,6 +200,11 @@ impl Server { self.service.delete(url).await } + pub async fn patch_webhook(&self, uuid: impl AsRef, value: Value) -> (Value, StatusCode) { + let url = format!("/webhooks/{}", uuid.as_ref()); + self.service.patch(url, value).await + } + pub async fn get_metrics(&self) -> (Value, StatusCode) { self.service.get("/metrics").await } diff --git a/crates/meilisearch/tests/tasks/webhook.rs b/crates/meilisearch/tests/tasks/webhook.rs index 9d66800af..3d27d6be6 100644 --- a/crates/meilisearch/tests/tasks/webhook.rs +++ b/crates/meilisearch/tests/tasks/webhook.rs @@ -329,3 +329,87 @@ async fn post_get_delete() { let (_value, code) = server.get_webhook(uuid).await; snapshot!(code, @"404 Not Found"); } + +#[actix_web::test] +async fn patch() { + let server = Server::new().await; + + let uuid = Uuid::new_v4().to_string(); + let (value, code) = + server.patch_webhook(&uuid, json!({ "headers": { "authorization": "TOKEN" } })).await; + snapshot!(code, @"400 Bad Request"); + snapshot!(value, @r#" + { + "message": "The URL for the webhook `[uuid]` is missing.", + "code": "invalid_webhooks_url", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_webhooks_url" + } + "#); + + let (value, code) = + server.patch_webhook(&uuid, json!({ "url": "https://example.com/hook" })).await; + snapshot!(code, @"200 OK"); + snapshot!(json_string!(value, { ".uuid" => "[uuid]" }), @r#" + { + "uuid": "[uuid]", + "isEditable": true, + "url": "https://example.com/hook", + "headers": {} + } + "#); + + let (value, code) = + server.patch_webhook(&uuid, json!({ "headers": { "authorization": "TOKEN" } })).await; + snapshot!(code, @"200 OK"); + snapshot!(json_string!(value, { ".uuid" => "[uuid]" }), @r#" + { + "uuid": "[uuid]", + "isEditable": true, + "url": "https://example.com/hook", + "headers": { + "authorization": "TOKEN" + } + } + "#); + + let (value, code) = + server.patch_webhook(&uuid, json!({ "headers": { "authorization2": "TOKEN" } })).await; + snapshot!(code, @"200 OK"); + snapshot!(json_string!(value, { ".uuid" => "[uuid]" }), @r#" + { + "uuid": "[uuid]", + "isEditable": true, + "url": "https://example.com/hook", + "headers": { + "authorization": "TOKEN", + "authorization2": "TOKEN" + } + } + "#); + + let (value, code) = + server.patch_webhook(&uuid, json!({ "headers": { "authorization": null } })).await; + snapshot!(code, @"200 OK"); + snapshot!(json_string!(value, { ".uuid" => "[uuid]" }), @r#" + { + "uuid": "[uuid]", + "isEditable": true, + "url": "https://example.com/hook", + "headers": { + "authorization2": "TOKEN" + } + } + "#); + + let (value, code) = server.patch_webhook(&uuid, json!({ "url": null })).await; + snapshot!(code, @"400 Bad Request"); + snapshot!(value, @r#" + { + "message": "The URL for the webhook `[uuid]` is missing.", + "code": "invalid_webhooks_url", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_webhooks_url" + } + "#); +}