From 954abcd8c577976065bbab4cf3b6a23fcb740574 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sat, 20 May 2023 16:52:34 +0300 Subject: [PATCH 1/4] API return 404 if requested webhooks is not found --- routers/api/v1/admin/hooks.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/admin/hooks.go b/routers/api/v1/admin/hooks.go index 6a08aa5125de1..1d4b3526ce65d 100644 --- a/routers/api/v1/admin/hooks.go +++ b/routers/api/v1/admin/hooks.go @@ -74,7 +74,11 @@ func GetHook(ctx *context.APIContext) { hookID := ctx.ParamsInt64(":id") hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) + if webhook.IsErrWebhookNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) + } return } h, err := webhook_service.ToHook("/admin/", hook) From a67433bb526d56baa4858762f575c999b08b7aa1 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sat, 20 May 2023 20:36:38 +0300 Subject: [PATCH 2/4] fix error type check --- routers/api/v1/admin/hooks.go | 5 +++-- tests/integration/api_admin_hook_test.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/integration/api_admin_hook_test.go diff --git a/routers/api/v1/admin/hooks.go b/routers/api/v1/admin/hooks.go index 1d4b3526ce65d..8a095a7defa2a 100644 --- a/routers/api/v1/admin/hooks.go +++ b/routers/api/v1/admin/hooks.go @@ -4,6 +4,7 @@ package admin import ( + "errors" "net/http" "code.gitea.io/gitea/models/webhook" @@ -74,7 +75,7 @@ func GetHook(ctx *context.APIContext) { hookID := ctx.ParamsInt64(":id") hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) if err != nil { - if webhook.IsErrWebhookNotExist(err) { + if errors.Is(err, util.ErrNotExist) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) @@ -164,7 +165,7 @@ func DeleteHook(ctx *context.APIContext) { hookID := ctx.ParamsInt64(":id") if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil { - if webhook.IsErrWebhookNotExist(err) { + if errors.Is(err, util.ErrNotExist) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err) diff --git a/tests/integration/api_admin_hook_test.go b/tests/integration/api_admin_hook_test.go new file mode 100644 index 0000000000000..ef8935f246378 --- /dev/null +++ b/tests/integration/api_admin_hook_test.go @@ -0,0 +1,21 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/http" + "net/url" + "testing" + + "code.gitea.io/gitea/tests" +) + +func TestGetNotExistHook(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + defer tests.PrepareTestEnv(t)() + + req := NewRequest(t, "GET", "/api/v1/admin/hooks/1234") + MakeRequest(t, req, http.StatusNotFound) + }) +} From de10794a01ccd10b9075874685ba8c1275edeab1 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sat, 20 May 2023 22:50:53 +0300 Subject: [PATCH 3/4] Fix api hook tests --- tests/integration/api_admin_hook_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/integration/api_admin_hook_test.go b/tests/integration/api_admin_hook_test.go index ef8935f246378..d2194f0872169 100644 --- a/tests/integration/api_admin_hook_test.go +++ b/tests/integration/api_admin_hook_test.go @@ -15,7 +15,18 @@ func TestGetNotExistHook(t *testing.T) { onGiteaRun(t, func(*testing.T, *url.URL) { defer tests.PrepareTestEnv(t)() + session := loginUser(t, "user1") req := NewRequest(t, "GET", "/api/v1/admin/hooks/1234") - MakeRequest(t, req, http.StatusNotFound) + session.MakeRequest(t, req, http.StatusNotFound) + }) +} + +func TestDeleteNotExistHook(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user1") + req := NewRequest(t, "DELETE", "/api/v1/admin/hooks/1234") + session.MakeRequest(t, req, http.StatusNotFound) }) } From 07db84115a10713d17379f4336bf7e14c1e58ed7 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sat, 20 May 2023 23:23:30 +0300 Subject: [PATCH 4/4] Remove new tests to reduce the duration increase of the database tests --- tests/integration/api_admin_hook_test.go | 32 ------------------------ 1 file changed, 32 deletions(-) delete mode 100644 tests/integration/api_admin_hook_test.go diff --git a/tests/integration/api_admin_hook_test.go b/tests/integration/api_admin_hook_test.go deleted file mode 100644 index d2194f0872169..0000000000000 --- a/tests/integration/api_admin_hook_test.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package integration - -import ( - "net/http" - "net/url" - "testing" - - "code.gitea.io/gitea/tests" -) - -func TestGetNotExistHook(t *testing.T) { - onGiteaRun(t, func(*testing.T, *url.URL) { - defer tests.PrepareTestEnv(t)() - - session := loginUser(t, "user1") - req := NewRequest(t, "GET", "/api/v1/admin/hooks/1234") - session.MakeRequest(t, req, http.StatusNotFound) - }) -} - -func TestDeleteNotExistHook(t *testing.T) { - onGiteaRun(t, func(*testing.T, *url.URL) { - defer tests.PrepareTestEnv(t)() - - session := loginUser(t, "user1") - req := NewRequest(t, "DELETE", "/api/v1/admin/hooks/1234") - session.MakeRequest(t, req, http.StatusNotFound) - }) -}