2019-04-17 19:06:35 +03:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-11-24 10:56:24 +03:00
|
|
|
package files
|
2019-04-17 19:06:35 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-21 18:17:57 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-04-21 18:17:57 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-06-29 23:51:10 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-04-17 19:06:35 +03:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 16:58:21 +03:00
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", "..", ".."),
|
|
|
|
})
|
2019-04-17 19:06:35 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
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",
|
|
|
|
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) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-04-17 19:06:35 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-04-17 19:06:35 +03:00
|
|
|
treePath := "README.md"
|
|
|
|
ref := ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
expectedContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-06-29 23:51:10 +03:00
|
|
|
})
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-06-29 23:51:10 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListForDir(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-06-29 23:51:10 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
treePath := "" // root dir
|
|
|
|
ref := ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
|
|
|
readmeContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
// because will be in a list, doesn't have encoding and content
|
|
|
|
readmeContentsResponse.Encoding = nil
|
|
|
|
readmeContentsResponse.Content = nil
|
|
|
|
|
|
|
|
expectedContentsListResponse := []*api.ContentsResponse{
|
|
|
|
readmeContentsResponse,
|
2019-04-17 19:06:35 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-06-29 23:51:10 +03:00
|
|
|
})
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-06-29 23:51:10 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListForFile(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-06-29 23:51:10 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
treePath := "README.md"
|
|
|
|
ref := ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
|
|
|
expectedContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-04-17 19:06:35 +03:00
|
|
|
})
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
|
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2020-07-19 12:53:40 +03:00
|
|
|
assert.NoError(t, err)
|
2019-04-17 19:06:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
func TestGetContentsErrors(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-06-29 23:51:10 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-06-29 23:51:10 +03:00
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
treePath := "README.md"
|
|
|
|
ref := repo.DefaultBranch
|
|
|
|
|
|
|
|
t.Run("bad treePath", func(t *testing.T) {
|
|
|
|
badTreePath := "bad/tree.md"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
|
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad ref", func(t *testing.T) {
|
|
|
|
badRef := "bad_ref"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false)
|
2019-06-29 23:51:10 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
|
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListErrors(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2019-04-17 19:06:35 +03:00
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-04-17 19:06:35 +03:00
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
treePath := "README.md"
|
|
|
|
ref := repo.DefaultBranch
|
|
|
|
|
|
|
|
t.Run("bad treePath", func(t *testing.T) {
|
|
|
|
badTreePath := "bad/tree.md"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
|
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad ref", func(t *testing.T) {
|
|
|
|
badRef := "bad_ref"
|
2022-01-20 02:26:57 +03:00
|
|
|
fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef)
|
2019-04-17 19:06:35 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
|
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
}
|
2019-10-19 18:38:49 +03:00
|
|
|
|
|
|
|
func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
unittest.PrepareTestEnv(t)
|
2022-02-26 09:15:25 +03:00
|
|
|
ctx := test.MockContext(t, "user30/empty")
|
|
|
|
ctx.SetParams(":id", "52")
|
|
|
|
test.LoadRepo(t, ctx, 52)
|
|
|
|
test.LoadUser(t, ctx, 30)
|
2019-10-19 18:38:49 +03:00
|
|
|
test.LoadGitRepo(t, ctx)
|
2019-11-13 10:01:19 +03:00
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
2019-10-19 18:38:49 +03:00
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
|
|
|
t.Run("empty repo", func(t *testing.T) {
|
2022-01-20 02:26:57 +03:00
|
|
|
contents, err := GetContentsOrList(ctx, repo, "", "")
|
2019-10-19 18:38:49 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, contents)
|
|
|
|
})
|
|
|
|
}
|
2021-11-24 10:56:24 +03:00
|
|
|
|
|
|
|
func TestGetBlobBySHA(t *testing.T) {
|
|
|
|
unittest.PrepareTestEnv(t)
|
|
|
|
ctx := test.MockContext(t, "user2/repo1")
|
|
|
|
test.LoadRepo(t, ctx, 1)
|
|
|
|
test.LoadRepoCommit(t, ctx)
|
|
|
|
test.LoadUser(t, ctx, 2)
|
|
|
|
test.LoadGitRepo(t, ctx)
|
|
|
|
defer ctx.Repo.GitRepo.Close()
|
|
|
|
|
|
|
|
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
|
|
|
|
ctx.SetParams(":id", "1")
|
|
|
|
ctx.SetParams(":sha", sha)
|
|
|
|
|
2022-04-21 18:17:57 +03:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, gitRepo, ctx.Params(":sha"))
|
2021-11-24 10:56:24 +03:00
|
|
|
expectedGBR := &api.GitBlobResponse{
|
|
|
|
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
|
|
|
|
Encoding: "base64",
|
|
|
|
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
Size: 180,
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedGBR, gbr)
|
|
|
|
}
|