fix(api): use not found status code when appropriate (#6885)

- Use a 404 error when the issue not found instead of returning an internal server error.
- Resolves #4005
- Added integration test.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6885
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: ThomasBoom89 <thomasboom89@noreply.codeberg.org>
Co-committed-by: ThomasBoom89 <thomasboom89@noreply.codeberg.org>
This commit is contained in:
ThomasBoom89 2025-02-12 20:18:33 +00:00 committed by Gusted
parent ab69057327
commit 5feca875ea
4 changed files with 119 additions and 8 deletions

View file

@ -25,6 +25,8 @@ import (
"github.com/stretchr/testify/require"
)
const IssueIDNotExist = 10000
func TestAPIListRepoComments(t *testing.T) {
defer tests.PrepareTestEnv(t)()
@ -89,6 +91,10 @@ func TestAPIListIssueComments(t *testing.T) {
expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID},
unittest.Cond("type = ?", issues_model.CommentTypeComment))
assert.Len(t, comments, expectedCount)
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments", repoOwner.Name, repo.Name, IssueIDNotExist).
AddTokenAuth(token)
MakeRequest(t, req, http.StatusNotFound)
}
func TestAPICreateComment(t *testing.T) {
@ -111,6 +117,13 @@ func TestAPICreateComment(t *testing.T) {
DecodeJSON(t, resp, &updatedComment)
assert.EqualValues(t, commentBody, updatedComment.Body)
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments",
repoOwner.Name, repo.Name, IssueIDNotExist)
req = NewRequestWithValues(t, "POST", urlStr, map[string]string{
"body": commentBody,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusNotFound)
}
func TestAPICreateCommentAutoDate(t *testing.T) {
@ -464,4 +477,7 @@ func TestAPIListIssueTimeline(t *testing.T) {
DecodeJSON(t, resp, &comments)
expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID})
assert.Len(t, comments, expectedCount)
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/timeline", repoOwner.Name, repo.Name, IssueIDNotExist)
MakeRequest(t, req, http.StatusNotFound)
}