code.gitea.io/gitea@v1.22.3/services/repository/files/file_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 "code.gitea.io/gitea/modules/setting" 12 api "code.gitea.io/gitea/modules/structs" 13 "code.gitea.io/gitea/services/contexttest" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestCleanUploadFileName(t *testing.T) { 19 t.Run("Clean regular file", func(t *testing.T) { 20 name := "this/is/test" 21 cleanName := CleanUploadFileName(name) 22 expectedCleanName := name 23 assert.EqualValues(t, expectedCleanName, cleanName) 24 }) 25 26 t.Run("Clean a .git path", func(t *testing.T) { 27 name := "this/is/test/.git" 28 cleanName := CleanUploadFileName(name) 29 expectedCleanName := "" 30 assert.EqualValues(t, expectedCleanName, cleanName) 31 }) 32 } 33 34 func getExpectedFileResponse() *api.FileResponse { 35 treePath := "README.md" 36 sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f" 37 encoding := "base64" 38 content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x" 39 selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master" 40 htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath 41 gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha 42 downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath 43 return &api.FileResponse{ 44 Content: &api.ContentsResponse{ 45 Name: treePath, 46 Path: treePath, 47 SHA: sha, 48 LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", 49 Type: "file", 50 Size: 30, 51 Encoding: &encoding, 52 Content: &content, 53 URL: &selfURL, 54 HTMLURL: &htmlURL, 55 GitURL: &gitURL, 56 DownloadURL: &downloadURL, 57 Links: &api.FileLinksResponse{ 58 Self: &selfURL, 59 GitURL: &gitURL, 60 HTMLURL: &htmlURL, 61 }, 62 }, 63 Commit: &api.FileCommitResponse{ 64 CommitMeta: api.CommitMeta{ 65 URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d", 66 SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", 67 }, 68 HTMLURL: "https://try.gitea.io/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d", 69 Author: &api.CommitUser{ 70 Identity: api.Identity{ 71 Name: "user1", 72 Email: "address1@example.com", 73 }, 74 Date: "2017-03-19T20:47:59Z", 75 }, 76 Committer: &api.CommitUser{ 77 Identity: api.Identity{ 78 Name: "Ethan Koenig", 79 Email: "ethantkoenig@gmail.com", 80 }, 81 Date: "2017-03-19T20:47:59Z", 82 }, 83 Parents: []*api.CommitMeta{}, 84 Message: "Initial commit\n", 85 Tree: &api.CommitMeta{ 86 URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6", 87 SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6", 88 }, 89 }, 90 Verification: &api.PayloadCommitVerification{ 91 Verified: false, 92 Reason: "gpg.error.not_signed_commit", 93 Signature: "", 94 Payload: "", 95 }, 96 } 97 } 98 99 func TestGetFileResponseFromCommit(t *testing.T) { 100 unittest.PrepareTestEnv(t) 101 ctx, _ := contexttest.MockContext(t, "user2/repo1") 102 ctx.SetParams(":id", "1") 103 contexttest.LoadRepo(t, ctx, 1) 104 contexttest.LoadRepoCommit(t, ctx) 105 contexttest.LoadUser(t, ctx, 2) 106 contexttest.LoadGitRepo(t, ctx) 107 defer ctx.Repo.GitRepo.Close() 108 109 repo := ctx.Repo.Repository 110 branch := repo.DefaultBranch 111 treePath := "README.md" 112 gitRepo, _ := gitrepo.OpenRepository(ctx, repo) 113 defer gitRepo.Close() 114 commit, _ := gitRepo.GetBranchCommit(branch) 115 expectedFileResponse := getExpectedFileResponse() 116 117 fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath) 118 assert.NoError(t, err) 119 assert.EqualValues(t, expectedFileResponse, fileResponse) 120 }