Skip to content

Commit 2e85ad6

Browse files
zeripathtechknowlogicksapk6543guillep2k
authoredApr 15, 2020
Contents API should return 404 on not exist (#10323)
* Return 404 on not exist * swagger update and use git.IsErrNotExist * Handle delete too * Handle delete too x2 * Fix pr 10323 (#3) * fix TESTS * leafe a note for fututre * placate golangci-lint Signed-off-by: Andrew Thornton <art27@cantab.net> * Update integrations/api_repo_file_delete_test.go Co-Authored-By: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
1 parent 7c8e116 commit 2e85ad6

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed
 

‎integrations/api_repo_file_delete_test.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/modules/context"
15-
"code.gitea.io/gitea/modules/setting"
1614
api "code.gitea.io/gitea/modules/structs"
1715

1816
"github.com/stretchr/testify/assert"
@@ -109,18 +107,10 @@ func TestAPIDeleteFile(t *testing.T) {
109107
treePath = fmt.Sprintf("delete/file%d.txt", fileID)
110108
createFile(user2, repo1, treePath)
111109
deleteFileOptions = getDeleteFileOptions()
112-
correctSHA := deleteFileOptions.SHA
113110
deleteFileOptions.SHA = "badsha"
114111
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
115112
req = NewRequestWithJSON(t, "DELETE", url, &deleteFileOptions)
116-
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
117-
expectedAPIError := context.APIError{
118-
Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]",
119-
URL: setting.API.SwaggerURL,
120-
}
121-
var apiError context.APIError
122-
DecodeJSON(t, resp, &apiError)
123-
assert.Equal(t, expectedAPIError, apiError)
113+
resp = session.MakeRequest(t, req, http.StatusBadRequest)
124114

125115
// Test creating a file in repo16 by user4 who does not have write access
126116
fileID++

‎integrations/api_repo_get_contents_list_test.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/modules/context"
1514
"code.gitea.io/gitea/modules/git"
1615
repo_module "code.gitea.io/gitea/modules/repository"
1716
"code.gitea.io/gitea/modules/setting"
@@ -139,14 +138,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
139138
// Test file contents a file with a bad ref
140139
ref = "badref"
141140
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
142-
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
143-
expectedAPIError := context.APIError{
144-
Message: "object does not exist [id: " + ref + ", rel_path: ]",
145-
URL: setting.API.SwaggerURL,
146-
}
147-
var apiError context.APIError
148-
DecodeJSON(t, resp, &apiError)
149-
assert.Equal(t, expectedAPIError, apiError)
141+
resp = session.MakeRequest(t, req, http.StatusNotFound)
150142

151143
// Test accessing private ref with user token that does not have access - should fail
152144
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)

‎integrations/api_repo_get_contents_test.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models"
13-
"code.gitea.io/gitea/modules/context"
1413
"code.gitea.io/gitea/modules/git"
1514
repo_module "code.gitea.io/gitea/modules/repository"
1615
"code.gitea.io/gitea/modules/setting"
@@ -141,14 +140,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
141140
// Test file contents a file with a bad ref
142141
ref = "badref"
143142
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
144-
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
145-
expectedAPIError := context.APIError{
146-
Message: "object does not exist [id: " + ref + ", rel_path: ]",
147-
URL: setting.API.SwaggerURL,
148-
}
149-
var apiError context.APIError
150-
DecodeJSON(t, resp, &apiError)
151-
assert.Equal(t, expectedAPIError, apiError)
143+
resp = session.MakeRequest(t, req, http.StatusNotFound)
152144

153145
// Test accessing private ref with user token that does not have access - should fail
154146
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)

‎routers/api/v1/repo/file.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,15 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
362362
// responses:
363363
// "200":
364364
// "$ref": "#/responses/FileDeleteResponse"
365+
// "400":
366+
// "$ref": "#/responses/error"
367+
// "403":
368+
// "$ref": "#/responses/error"
369+
// "404":
370+
// "$ref": "#/responses/error"
365371

366372
if !CanWriteFiles(ctx.Repo) {
367-
ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
373+
ctx.Error(http.StatusForbidden, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
368374
UserID: ctx.User.ID,
369375
RepoName: ctx.Repo.Repository.LowerName,
370376
})
@@ -402,9 +408,23 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
402408
}
403409

404410
if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
411+
if git.IsErrBranchNotExist(err) || models.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) {
412+
ctx.Error(http.StatusNotFound, "DeleteFile", err)
413+
return
414+
} else if models.IsErrBranchAlreadyExists(err) ||
415+
models.IsErrFilenameInvalid(err) ||
416+
models.IsErrSHADoesNotMatch(err) ||
417+
models.IsErrCommitIDDoesNotMatch(err) ||
418+
models.IsErrSHAOrCommitIDNotProvided(err) {
419+
ctx.Error(http.StatusBadRequest, "DeleteFile", err)
420+
return
421+
} else if models.IsErrUserCannotCommit(err) {
422+
ctx.Error(http.StatusForbidden, "DeleteFile", err)
423+
return
424+
}
405425
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
406426
} else {
407-
ctx.JSON(http.StatusOK, fileResponse)
427+
ctx.JSON(http.StatusOK, fileResponse) // FIXME on APIv2: return http.StatusNoContent
408428
}
409429
}
410430

@@ -439,6 +459,8 @@ func GetContents(ctx *context.APIContext) {
439459
// responses:
440460
// "200":
441461
// "$ref": "#/responses/ContentsResponse"
462+
// "404":
463+
// "$ref": "#/responses/notFound"
442464

443465
if !CanReadFiles(ctx.Repo) {
444466
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
@@ -452,6 +474,10 @@ func GetContents(ctx *context.APIContext) {
452474
ref := ctx.QueryTrim("ref")
453475

454476
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
477+
if git.IsErrNotExist(err) {
478+
ctx.NotFound("GetContentsOrList", err)
479+
return
480+
}
455481
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
456482
} else {
457483
ctx.JSON(http.StatusOK, fileList)
@@ -484,6 +510,8 @@ func GetContentsList(ctx *context.APIContext) {
484510
// responses:
485511
// "200":
486512
// "$ref": "#/responses/ContentsListResponse"
513+
// "404":
514+
// "$ref": "#/responses/notFound"
487515

488516
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
489517
GetContents(ctx)

‎templates/swagger/v1_json.tmpl

+15
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,9 @@
26382638
"responses": {
26392639
"200": {
26402640
"$ref": "#/responses/ContentsListResponse"
2641+
},
2642+
"404": {
2643+
"$ref": "#/responses/notFound"
26412644
}
26422645
}
26432646
}
@@ -2684,6 +2687,9 @@
26842687
"responses": {
26852688
"200": {
26862689
"$ref": "#/responses/ContentsResponse"
2690+
},
2691+
"404": {
2692+
"$ref": "#/responses/notFound"
26872693
}
26882694
}
26892695
},
@@ -2831,6 +2837,15 @@
28312837
"responses": {
28322838
"200": {
28332839
"$ref": "#/responses/FileDeleteResponse"
2840+
},
2841+
"400": {
2842+
"$ref": "#/responses/error"
2843+
},
2844+
"403": {
2845+
"$ref": "#/responses/error"
2846+
},
2847+
"404": {
2848+
"$ref": "#/responses/error"
28342849
}
28352850
}
28362851
}

0 commit comments

Comments
 (0)
Please sign in to comment.