0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-04 22:57:34 -04:00
This commit is contained in:
wxiaoguang 2025-07-02 20:30:42 +08:00
parent 08bf2d64fd
commit 58bc9cb107
4 changed files with 33 additions and 106 deletions

View File

@ -5,57 +5,21 @@ package files
import ( import (
"testing" "testing"
"time"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/contexttest" "code.gitea.io/gitea/services/contexttest"
_ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/actions"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
unittest.MainTest(m) unittest.MainTest(m)
} }
func getExpectedReadmeContentsResponse() *api.ContentsResponse {
treePath := "README.md"
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
encoding := "base64"
content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
return &api.ContentsResponse{
Name: treePath,
Path: treePath,
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
LastCommitSHA: util.ToPointer("65f1bf27bc3bf70f64657658635e66094edbcb4d"),
LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
LastCommitMessage: util.ToPointer("Initial commit\n"),
Type: "file",
Size: 30,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
HTMLURL: &htmlURL,
},
}
}
func TestGetContents(t *testing.T) { func TestGetContents(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") ctx, _ := contexttest.MockContext(t, "user2/repo1")
@ -64,45 +28,8 @@ func TestGetContents(t *testing.T) {
contexttest.LoadRepoCommit(t, ctx) contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2) contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx) contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo, gitRepo := ctx.Repo.Repository, ctx.Repo.GitRepo
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
require.NoError(t, err)
t.Run("GetContentsOrList(README.md)-MetaOnly", func(t *testing.T) { // GetContentsOrList's behavior is fully tested in integration tests, so we don't need to test it here.
expectedContentsResponse := getExpectedReadmeContentsResponse()
expectedContentsResponse.Encoding = nil // because will be in a list, doesn't have encoding and content
expectedContentsResponse.Content = nil
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "README.md", IncludeSingleFileContent: false})
assert.Equal(t, expectedContentsResponse, extResp.FileContents)
assert.NoError(t, err)
})
t.Run("GetContentsOrList(README.md)", func(t *testing.T) {
expectedContentsResponse := getExpectedReadmeContentsResponse()
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "README.md", IncludeSingleFileContent: true})
assert.Equal(t, expectedContentsResponse, extResp.FileContents)
assert.NoError(t, err)
})
t.Run("GetContentsOrList(RootDir)", func(t *testing.T) {
readmeContentsResponse := getExpectedReadmeContentsResponse()
readmeContentsResponse.Encoding = nil // because will be in a list, doesn't have encoding and content
readmeContentsResponse.Content = nil
expectedContentsListResponse := []*api.ContentsResponse{readmeContentsResponse}
// even if IncludeFileContent is true, it has no effect for directory listing
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "", IncludeSingleFileContent: true})
assert.Equal(t, expectedContentsListResponse, extResp.DirContents)
assert.NoError(t, err)
})
t.Run("GetContentsOrList(NoSuchTreePath)", func(t *testing.T) {
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "no-such/file.md"})
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: , rel_path: no-such]")
assert.Nil(t, extResp.DirContents)
assert.Nil(t, extResp.FileContents)
})
t.Run("GetBlobBySHA", func(t *testing.T) { t.Run("GetBlobBySHA", func(t *testing.T) {
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"

View File

@ -39,7 +39,6 @@ func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA stri
LastCommitSHA: util.ToPointer(lastCommitSHA), LastCommitSHA: util.ToPointer(lastCommitSHA),
LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))), LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))), LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
LastCommitMessage: util.ToPointer("Initial commit"),
Type: "file", Type: "file",
Size: 30, Size: 30,
URL: &selfURL, URL: &selfURL,
@ -67,7 +66,6 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
treePath := "" // root dir
// Get user2's token // Get user2's token
session := loginUser(t, user2.Name) session := loginUser(t, user2.Name)
@ -96,7 +94,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// ref is default ref // ref is default ref
ref := repo1.DefaultBranch ref := repo1.DefaultBranch
refType := "branch" refType := "branch"
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents?ref=%s", user2.Name, repo1.Name, ref)
resp := MakeRequest(t, req, http.StatusOK) resp := MakeRequest(t, req, http.StatusOK)
var contentsListResponse []*api.ContentsResponse var contentsListResponse []*api.ContentsResponse
DecodeJSON(t, resp, &contentsListResponse) DecodeJSON(t, resp, &contentsListResponse)
@ -108,7 +106,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// No ref // No ref
refType = "branch" refType = "branch"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo1.Name)
resp = MakeRequest(t, req, http.StatusOK) resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse) DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse) assert.NotNil(t, contentsListResponse)
@ -119,7 +117,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// ref is the branch we created above in setup // ref is the branch we created above in setup
ref = newBranch ref = newBranch
refType = "branch" refType = "branch"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents?ref=%s", user2.Name, repo1.Name, ref)
resp = MakeRequest(t, req, http.StatusOK) resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse) DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse) assert.NotNil(t, contentsListResponse)
@ -133,7 +131,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// ref is the new tag we created above in setup // ref is the new tag we created above in setup
ref = newTag ref = newTag
refType = "tag" refType = "tag"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
resp = MakeRequest(t, req, http.StatusOK) resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse) DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse) assert.NotNil(t, contentsListResponse)
@ -147,7 +145,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// ref is a commit // ref is a commit
ref = commitID ref = commitID
refType = "commit" refType = "commit"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
resp = MakeRequest(t, req, http.StatusOK) resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse) DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse) assert.NotNil(t, contentsListResponse)
@ -156,21 +154,21 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
// Test file contents a file with a bad ref // Test file contents a file with a bad ref
ref = "badref" ref = "badref"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
// Test accessing private ref with user token that does not have access - should fail // Test accessing private ref with user token that does not have access - should fail
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath). req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo16.Name).
AddTokenAuth(token4) AddTokenAuth(token4)
MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
// Test access private ref of owner of token // Test access private ref of owner of token
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md", user2.Name, repo16.Name). req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo16.Name).
AddTokenAuth(token2) AddTokenAuth(token2)
MakeRequest(t, req, http.StatusOK) MakeRequest(t, req, http.StatusOK)
// Test access of org org3 private repo file by owner user2 // Test access of org org3 private repo file by owner user2
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", org3.Name, repo3.Name, treePath). req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", org3.Name, repo3.Name).
AddTokenAuth(token2) AddTokenAuth(token2)
MakeRequest(t, req, http.StatusOK) MakeRequest(t, req, http.StatusOK)
} }

View File

@ -97,11 +97,16 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
require.NoError(t, err) require.NoError(t, err)
/*** END SETUP ***/ /*** END SETUP ***/
// not found
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/no-such/file.md", user2.Name, repo1.Name)
resp := MakeRequest(t, req, http.StatusNotFound)
assert.Contains(t, resp.Body.String(), "object does not exist [id: , rel_path: no-such]")
// ref is default ref // ref is default ref
ref := repo1.DefaultBranch ref := repo1.DefaultBranch
refType := "branch" refType := "branch"
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
resp := MakeRequest(t, req, http.StatusOK) resp = MakeRequest(t, req, http.StatusOK)
var contentsResponse api.ContentsResponse var contentsResponse api.ContentsResponse
DecodeJSON(t, resp, &contentsResponse) DecodeJSON(t, resp, &contentsResponse)
lastCommit, _ := gitRepo.GetCommitByPath("README.md") lastCommit, _ := gitRepo.GetCommitByPath("README.md")
@ -116,7 +121,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String()) expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
assert.Equal(t, *expectedContentsResponse, contentsResponse) assert.Equal(t, *expectedContentsResponse, contentsResponse)
// ref is the branch we created above in setup // ref is the branch we created above in setup
ref = newBranch ref = newBranch
refType = "branch" refType = "branch"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
@ -256,7 +261,7 @@ func testAPIGetContentsExt(t *testing.T) {
assert.Equal(t, util.ToPointer("0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351"), respFile.LfsOid) assert.Equal(t, util.ToPointer("0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351"), respFile.LfsOid)
}) })
t.Run("FileContents", func(t *testing.T) { t.Run("FileContents", func(t *testing.T) {
// by default, no file content is returned // by default, no file content or commit info is returned
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs/README.md?ref=sub-home-md-img-check") req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs/README.md?ref=sub-home-md-img-check")
resp := MakeRequest(t, req, http.StatusOK) resp := MakeRequest(t, req, http.StatusOK)
var contentsResponse api.ContentsExtResponse var contentsResponse api.ContentsExtResponse
@ -277,8 +282,8 @@ func testAPIGetContentsExt(t *testing.T) {
assert.Equal(t, "README.md", contentsResponse.FileContents.Name) assert.Equal(t, "README.md", contentsResponse.FileContents.Name)
assert.NotNil(t, contentsResponse.FileContents.Encoding) assert.NotNil(t, contentsResponse.FileContents.Encoding)
assert.NotNil(t, contentsResponse.FileContents.Content) assert.NotNil(t, contentsResponse.FileContents.Content)
assert.NotNil(t, contentsResponse.FileContents.LastCommitSHA) assert.Equal(t, "4649299398e4d39a5c09eb4f534df6f1e1eb87cc", *contentsResponse.FileContents.LastCommitSHA)
assert.NotNil(t, contentsResponse.FileContents.LastCommitMessage) assert.Equal(t, "Test how READMEs render images when found in a subfolder\n", *contentsResponse.FileContents.LastCommitMessage)
req = NewRequestf(t, "GET", "/api/v1/repos/user2/lfs/contents-ext/jpeg.jpg?includes=file_content").AddTokenAuth(token2) req = NewRequestf(t, "GET", "/api/v1/repos/user2/lfs/contents-ext/jpeg.jpg?includes=file_content").AddTokenAuth(token2)
resp = session.MakeRequest(t, req, http.StatusOK) resp = session.MakeRequest(t, req, http.StatusOK)

View File

@ -158,7 +158,6 @@ func getExpectedFileResponseForRepoFilesCreate(commitID string, lastCommit *git.
LastCommitSHA: util.ToPointer(lastCommit.ID.String()), LastCommitSHA: util.ToPointer(lastCommit.ID.String()),
LastCommitterDate: util.ToPointer(lastCommit.Committer.When), LastCommitterDate: util.ToPointer(lastCommit.Committer.When),
LastAuthorDate: util.ToPointer(lastCommit.Author.When), LastAuthorDate: util.ToPointer(lastCommit.Author.When),
LastCommitMessage: util.ToPointer("Creates new/file.txt\n"),
Type: "file", Type: "file",
Size: 18, Size: 18,
Encoding: &encoding, Encoding: &encoding,
@ -229,7 +228,6 @@ func getExpectedFileResponseForRepoFilesUpdate(commitID, filename, lastCommitSHA
LastCommitSHA: util.ToPointer(lastCommitSHA), LastCommitSHA: util.ToPointer(lastCommitSHA),
LastCommitterDate: util.ToPointer(lastCommitterWhen), LastCommitterDate: util.ToPointer(lastCommitterWhen),
LastAuthorDate: util.ToPointer(lastAuthorWhen), LastAuthorDate: util.ToPointer(lastAuthorWhen),
LastCommitMessage: util.ToPointer("Updates README.md\n"),
Type: "file", Type: "file",
Size: 43, Size: 43,
Encoding: &encoding, Encoding: &encoding,
@ -330,19 +328,18 @@ func getExpectedFileResponseForRepoFilesUpdateRename(commitID, lastCommitSHA str
downloadURL := setting.AppURL + "user2/lfs/raw/branch/master/" + detail.filename downloadURL := setting.AppURL + "user2/lfs/raw/branch/master/" + detail.filename
// don't set time related fields because there might be different time in one operation // don't set time related fields because there might be different time in one operation
responses = append(responses, &api.ContentsResponse{ responses = append(responses, &api.ContentsResponse{
Name: detail.filename, Name: detail.filename,
Path: detail.filename, Path: detail.filename,
SHA: detail.sha, SHA: detail.sha,
LastCommitSHA: util.ToPointer(lastCommitSHA), LastCommitSHA: util.ToPointer(lastCommitSHA),
LastCommitMessage: util.ToPointer("Rename files\n"), Type: "file",
Type: "file", Size: detail.size,
Size: detail.size, Encoding: util.ToPointer("base64"),
Encoding: util.ToPointer("base64"), Content: &detail.content,
Content: &detail.content, URL: &selfURL,
URL: &selfURL, HTMLURL: &htmlURL,
HTMLURL: &htmlURL, GitURL: &gitURL,
GitURL: &gitURL, DownloadURL: &downloadURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{ Links: &api.FileLinksResponse{
Self: &selfURL, Self: &selfURL,
GitURL: &gitURL, GitURL: &gitURL,