diff options
author | Richard Mahn <richmahn@users.noreply.github.com> | 2019-06-29 16:51:10 -0400 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-06-29 16:51:10 -0400 |
commit | cd96dee9822c8b744526ba862fd8b5ec0e2c30ff (patch) | |
tree | d7bbf2f2b7adf80b17f3ab3971ae49bae7b010c4 /modules/repofiles/content_test.go | |
parent | 738285a4aac5df2e60f4038aa79be3e9fe921bdb (diff) | |
download | gitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.tar.gz gitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.zip |
Fixes #7292 - API File Contents bug (#7301)
Diffstat (limited to 'modules/repofiles/content_test.go')
-rw-r--r-- | modules/repofiles/content_test.go | 154 |
1 files changed, 128 insertions, 26 deletions
diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go index ce3f5f3678..ef6c5eafc2 100644 --- a/modules/repofiles/content_test.go +++ b/modules/repofiles/content_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/structs" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" @@ -19,7 +19,36 @@ func TestMain(m *testing.M) { models.MainTest(m, filepath.Join("..", "..")) } -func TestGetFileContents(t *testing.T) { +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) { models.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") @@ -30,37 +59,110 @@ func TestGetFileContents(t *testing.T) { treePath := "README.md" ref := ctx.Repo.Repository.DefaultBranch - expectedFileContentResponse := &structs.FileContentResponse{ - Name: treePath, - Path: treePath, - SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", - Size: 30, - URL: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md", - HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md", - GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f", - DownloadURL: "https://try.gitea.io/user2/repo1/raw/branch/master/README.md", - Type: "blob", - Links: &structs.FileLinksResponse{ - Self: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md", - GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f", - HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md", - }, + expectedContentsResponse := getExpectedReadmeContentsResponse() + + t.Run("Get README.md contents with GetContents()", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false) + assert.EqualValues(t, expectedContentsResponse, fileContentResponse) + assert.Nil(t, err) + }) + + t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false) + assert.EqualValues(t, expectedContentsResponse, fileContentResponse) + assert.Nil(t, err) + }) +} + +func TestGetContentsOrListForDir(t *testing.T) { + models.PrepareTestEnv(t) + 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) + 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, } - t.Run("Get README.md contents", func(t *testing.T) { - fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, ref) - assert.EqualValues(t, expectedFileContentResponse, fileContentResponse) + t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) + assert.Nil(t, err) + }) + + t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) + assert.Nil(t, err) + }) +} + +func TestGetContentsOrListForFile(t *testing.T) { + models.PrepareTestEnv(t) + 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) + treePath := "README.md" + ref := ctx.Repo.Repository.DefaultBranch + + expectedContentsResponse := getExpectedReadmeContentsResponse() + + t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.Nil(t, err) }) - t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch)", func(t *testing.T) { - fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, "") - assert.EqualValues(t, expectedFileContentResponse, fileContentResponse) + t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.Nil(t, err) }) } -func TestGetFileContentsErrors(t *testing.T) { +func TestGetContentsErrors(t *testing.T) { + models.PrepareTestEnv(t) + 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) + repo := ctx.Repo.Repository + treePath := "README.md" + ref := repo.DefaultBranch + + t.Run("bad treePath", func(t *testing.T) { + badTreePath := "bad/tree.md" + fileContentResponse, err := GetContents(repo, badTreePath, ref, false) + 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" + fileContentResponse, err := GetContents(repo, treePath, badRef, false) + 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) { models.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") @@ -74,7 +176,7 @@ func TestGetFileContentsErrors(t *testing.T) { t.Run("bad treePath", func(t *testing.T) { badTreePath := "bad/tree.md" - fileContentResponse, err := GetFileContents(repo, badTreePath, ref) + fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") assert.Nil(t, fileContentResponse) @@ -82,7 +184,7 @@ func TestGetFileContentsErrors(t *testing.T) { t.Run("bad ref", func(t *testing.T) { badRef := "bad_ref" - fileContentResponse, err := GetFileContents(repo, treePath, badRef) + fileContentResponse, err := GetContentsOrList(repo, treePath, badRef) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") assert.Nil(t, fileContentResponse) |