|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + |
| 12 | + auth_model "code.gitea.io/gitea/models/auth" |
| 13 | + repo_model "code.gitea.io/gitea/models/repo" |
| 14 | + "code.gitea.io/gitea/models/unittest" |
| 15 | + user_model "code.gitea.io/gitea/models/user" |
| 16 | + api "code.gitea.io/gitea/modules/structs" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +func getApplyDiffPatchFileOptions() *api.ApplyDiffPatchFileOptions { |
| 22 | + return &api.ApplyDiffPatchFileOptions{ |
| 23 | + FileOptions: api.FileOptions{ |
| 24 | + BranchName: "master", |
| 25 | + }, |
| 26 | + Content: `diff --git a/patch-file-1.txt b/patch-file-1.txt |
| 27 | +new file mode 100644 |
| 28 | +index 0000000000..aaaaaaaaaa |
| 29 | +--- /dev/null |
| 30 | ++++ b/patch-file-1.txt |
| 31 | +@@ -0,0 +1 @@ |
| 32 | ++File 1 |
| 33 | +`, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestAPIApplyDiffPatchFileOptions(t *testing.T) { |
| 38 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 39 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16 |
| 40 | + org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3, is an org |
| 41 | + user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos |
| 42 | + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo |
| 43 | + repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo |
| 44 | + repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo |
| 45 | + |
| 46 | + session2 := loginUser(t, user2.Name) |
| 47 | + token2 := getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 48 | + session4 := loginUser(t, user4.Name) |
| 49 | + token4 := getTokenForLoggedInUser(t, session4, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 50 | + |
| 51 | + req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/diffpatch", getApplyDiffPatchFileOptions()).AddTokenAuth(token2) |
| 52 | + resp := MakeRequest(t, req, http.StatusCreated) |
| 53 | + var fileResponse api.FileResponse |
| 54 | + DecodeJSON(t, resp, &fileResponse) |
| 55 | + assert.Nil(t, fileResponse.Content) |
| 56 | + assert.NotEmpty(t, fileResponse.Commit.HTMLURL) |
| 57 | + req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/raw/patch-file-1.txt") |
| 58 | + resp = MakeRequest(t, req, http.StatusOK) |
| 59 | + assert.Equal(t, "File 1\n", resp.Body.String()) |
| 60 | + |
| 61 | + // Test creating a file in repo1 by user4 who does not have write access |
| 62 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions()). |
| 63 | + AddTokenAuth(token4) |
| 64 | + MakeRequest(t, req, http.StatusNotFound) |
| 65 | + |
| 66 | + // Tests a repo with no token given so will fail |
| 67 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions()) |
| 68 | + MakeRequest(t, req, http.StatusNotFound) |
| 69 | + |
| 70 | + // Test using access token for a private repo that the user of the token owns |
| 71 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo16.Name), getApplyDiffPatchFileOptions()). |
| 72 | + AddTokenAuth(token2) |
| 73 | + MakeRequest(t, req, http.StatusCreated) |
| 74 | + |
| 75 | + // Test using org repo "org3/repo3" where user2 is a collaborator |
| 76 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", org3.Name, repo3.Name), getApplyDiffPatchFileOptions()). |
| 77 | + AddTokenAuth(token2) |
| 78 | + MakeRequest(t, req, http.StatusCreated) |
| 79 | + |
| 80 | + // Test using org repo "org3/repo3" with no user token |
| 81 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", org3.Name, repo3.Name), getApplyDiffPatchFileOptions()) |
| 82 | + MakeRequest(t, req, http.StatusNotFound) |
| 83 | + |
| 84 | + // Test using repo "user2/repo1" where user4 is a NOT collaborator |
| 85 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/diffpatch", user2.Name, repo1.Name), getApplyDiffPatchFileOptions()). |
| 86 | + AddTokenAuth(token4) |
| 87 | + MakeRequest(t, req, http.StatusForbidden) |
| 88 | + }) |
| 89 | +} |
0 commit comments