code.gitea.io/gitea@v1.22.3/services/repository/files/content_test.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package files 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/unittest" 10 "code.gitea.io/gitea/modules/gitrepo" 11 api "code.gitea.io/gitea/modules/structs" 12 "code.gitea.io/gitea/services/contexttest" 13 14 _ "code.gitea.io/gitea/models/actions" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestMain(m *testing.M) { 20 unittest.MainTest(m) 21 } 22 23 func getExpectedReadmeContentsResponse() *api.ContentsResponse { 24 treePath := "README.md" 25 sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f" 26 encoding := "base64" 27 content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x" 28 selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master" 29 htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath 30 gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha 31 downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath 32 return &api.ContentsResponse{ 33 Name: treePath, 34 Path: treePath, 35 SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", 36 LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", 37 Type: "file", 38 Size: 30, 39 Encoding: &encoding, 40 Content: &content, 41 URL: &selfURL, 42 HTMLURL: &htmlURL, 43 GitURL: &gitURL, 44 DownloadURL: &downloadURL, 45 Links: &api.FileLinksResponse{ 46 Self: &selfURL, 47 GitURL: &gitURL, 48 HTMLURL: &htmlURL, 49 }, 50 } 51 } 52 53 func TestGetContents(t *testing.T) { 54 unittest.PrepareTestEnv(t) 55 ctx, _ := contexttest.MockContext(t, "user2/repo1") 56 ctx.SetParams(":id", "1") 57 contexttest.LoadRepo(t, ctx, 1) 58 contexttest.LoadRepoCommit(t, ctx) 59 contexttest.LoadUser(t, ctx, 2) 60 contexttest.LoadGitRepo(t, ctx) 61 defer ctx.Repo.GitRepo.Close() 62 63 treePath := "README.md" 64 ref := ctx.Repo.Repository.DefaultBranch 65 66 expectedContentsResponse := getExpectedReadmeContentsResponse() 67 68 t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) { 69 fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false) 70 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 71 assert.NoError(t, err) 72 }) 73 74 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) { 75 fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false) 76 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 77 assert.NoError(t, err) 78 }) 79 } 80 81 func TestGetContentsOrListForDir(t *testing.T) { 82 unittest.PrepareTestEnv(t) 83 ctx, _ := contexttest.MockContext(t, "user2/repo1") 84 ctx.SetParams(":id", "1") 85 contexttest.LoadRepo(t, ctx, 1) 86 contexttest.LoadRepoCommit(t, ctx) 87 contexttest.LoadUser(t, ctx, 2) 88 contexttest.LoadGitRepo(t, ctx) 89 defer ctx.Repo.GitRepo.Close() 90 91 treePath := "" // root dir 92 ref := ctx.Repo.Repository.DefaultBranch 93 94 readmeContentsResponse := getExpectedReadmeContentsResponse() 95 // because will be in a list, doesn't have encoding and content 96 readmeContentsResponse.Encoding = nil 97 readmeContentsResponse.Content = nil 98 99 expectedContentsListResponse := []*api.ContentsResponse{ 100 readmeContentsResponse, 101 } 102 103 t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) { 104 fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) 105 assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) 106 assert.NoError(t, err) 107 }) 108 109 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) { 110 fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") 111 assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) 112 assert.NoError(t, err) 113 }) 114 } 115 116 func TestGetContentsOrListForFile(t *testing.T) { 117 unittest.PrepareTestEnv(t) 118 ctx, _ := contexttest.MockContext(t, "user2/repo1") 119 ctx.SetParams(":id", "1") 120 contexttest.LoadRepo(t, ctx, 1) 121 contexttest.LoadRepoCommit(t, ctx) 122 contexttest.LoadUser(t, ctx, 2) 123 contexttest.LoadGitRepo(t, ctx) 124 defer ctx.Repo.GitRepo.Close() 125 126 treePath := "README.md" 127 ref := ctx.Repo.Repository.DefaultBranch 128 129 expectedContentsResponse := getExpectedReadmeContentsResponse() 130 131 t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) { 132 fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) 133 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 134 assert.NoError(t, err) 135 }) 136 137 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) { 138 fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") 139 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 140 assert.NoError(t, err) 141 }) 142 } 143 144 func TestGetContentsErrors(t *testing.T) { 145 unittest.PrepareTestEnv(t) 146 ctx, _ := contexttest.MockContext(t, "user2/repo1") 147 ctx.SetParams(":id", "1") 148 contexttest.LoadRepo(t, ctx, 1) 149 contexttest.LoadRepoCommit(t, ctx) 150 contexttest.LoadUser(t, ctx, 2) 151 contexttest.LoadGitRepo(t, ctx) 152 defer ctx.Repo.GitRepo.Close() 153 154 repo := ctx.Repo.Repository 155 treePath := "README.md" 156 ref := repo.DefaultBranch 157 158 t.Run("bad treePath", func(t *testing.T) { 159 badTreePath := "bad/tree.md" 160 fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false) 161 assert.Error(t, err) 162 assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") 163 assert.Nil(t, fileContentResponse) 164 }) 165 166 t.Run("bad ref", func(t *testing.T) { 167 badRef := "bad_ref" 168 fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false) 169 assert.Error(t, err) 170 assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") 171 assert.Nil(t, fileContentResponse) 172 }) 173 } 174 175 func TestGetContentsOrListErrors(t *testing.T) { 176 unittest.PrepareTestEnv(t) 177 ctx, _ := contexttest.MockContext(t, "user2/repo1") 178 ctx.SetParams(":id", "1") 179 contexttest.LoadRepo(t, ctx, 1) 180 contexttest.LoadRepoCommit(t, ctx) 181 contexttest.LoadUser(t, ctx, 2) 182 contexttest.LoadGitRepo(t, ctx) 183 defer ctx.Repo.GitRepo.Close() 184 185 repo := ctx.Repo.Repository 186 treePath := "README.md" 187 ref := repo.DefaultBranch 188 189 t.Run("bad treePath", func(t *testing.T) { 190 badTreePath := "bad/tree.md" 191 fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref) 192 assert.Error(t, err) 193 assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") 194 assert.Nil(t, fileContentResponse) 195 }) 196 197 t.Run("bad ref", func(t *testing.T) { 198 badRef := "bad_ref" 199 fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef) 200 assert.Error(t, err) 201 assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") 202 assert.Nil(t, fileContentResponse) 203 }) 204 } 205 206 func TestGetContentsOrListOfEmptyRepos(t *testing.T) { 207 unittest.PrepareTestEnv(t) 208 ctx, _ := contexttest.MockContext(t, "user30/empty") 209 ctx.SetParams(":id", "52") 210 contexttest.LoadRepo(t, ctx, 52) 211 contexttest.LoadUser(t, ctx, 30) 212 contexttest.LoadGitRepo(t, ctx) 213 defer ctx.Repo.GitRepo.Close() 214 215 repo := ctx.Repo.Repository 216 217 t.Run("empty repo", func(t *testing.T) { 218 contents, err := GetContentsOrList(ctx, repo, "", "") 219 assert.NoError(t, err) 220 assert.Empty(t, contents) 221 }) 222 } 223 224 func TestGetBlobBySHA(t *testing.T) { 225 unittest.PrepareTestEnv(t) 226 ctx, _ := contexttest.MockContext(t, "user2/repo1") 227 contexttest.LoadRepo(t, ctx, 1) 228 contexttest.LoadRepoCommit(t, ctx) 229 contexttest.LoadUser(t, ctx, 2) 230 contexttest.LoadGitRepo(t, ctx) 231 defer ctx.Repo.GitRepo.Close() 232 233 sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" 234 ctx.SetParams(":id", "1") 235 ctx.SetParams(":sha", sha) 236 237 gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) 238 if err != nil { 239 t.Fail() 240 } 241 242 gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, gitRepo, ctx.Params(":sha")) 243 expectedGBR := &api.GitBlobResponse{ 244 Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", 245 Encoding: "base64", 246 URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d", 247 SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", 248 Size: 180, 249 } 250 assert.NoError(t, err) 251 assert.Equal(t, expectedGBR, gbr) 252 }