You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repofiles_update_test.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "net/url"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/repofiles"
  13. "code.gitea.io/gitea/modules/setting"
  14. api "code.gitea.io/gitea/modules/structs"
  15. "code.gitea.io/gitea/modules/test"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func getCreateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
  19. return &repofiles.UpdateRepoFileOptions{
  20. OldBranch: repo.DefaultBranch,
  21. NewBranch: repo.DefaultBranch,
  22. TreePath: "new/file.txt",
  23. Message: "Creates new/file.txt",
  24. Content: "This is a NEW file",
  25. IsNewFile: true,
  26. Author: nil,
  27. Committer: nil,
  28. }
  29. }
  30. func getUpdateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
  31. return &repofiles.UpdateRepoFileOptions{
  32. OldBranch: repo.DefaultBranch,
  33. NewBranch: repo.DefaultBranch,
  34. TreePath: "README.md",
  35. Message: "Updates README.md",
  36. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  37. Content: "This is UPDATED content for the README file",
  38. IsNewFile: false,
  39. Author: nil,
  40. Committer: nil,
  41. }
  42. }
  43. func getExpectedFileResponseForRepofilesCreate(commitID string) *api.FileResponse {
  44. treePath := "new/file.txt"
  45. encoding := "base64"
  46. content := "VGhpcyBpcyBhIE5FVyBmaWxl"
  47. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  48. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
  49. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/103ff9234cefeee5ec5361d22b49fbb04d385885"
  50. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
  51. return &api.FileResponse{
  52. Content: &api.ContentsResponse{
  53. Name: filepath.Base(treePath),
  54. Path: treePath,
  55. SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
  56. Type: "file",
  57. Size: 18,
  58. Encoding: &encoding,
  59. Content: &content,
  60. URL: &selfURL,
  61. HTMLURL: &htmlURL,
  62. GitURL: &gitURL,
  63. DownloadURL: &downloadURL,
  64. Links: &api.FileLinksResponse{
  65. Self: &selfURL,
  66. GitURL: &gitURL,
  67. HTMLURL: &htmlURL,
  68. },
  69. },
  70. Commit: &api.FileCommitResponse{
  71. CommitMeta: api.CommitMeta{
  72. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  73. SHA: commitID,
  74. },
  75. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  76. Author: &api.CommitUser{
  77. Identity: api.Identity{
  78. Name: "User Two",
  79. Email: "user2@noreply.example.org",
  80. },
  81. Date: time.Now().UTC().Format(time.RFC3339),
  82. },
  83. Committer: &api.CommitUser{
  84. Identity: api.Identity{
  85. Name: "User Two",
  86. Email: "user2@noreply.example.org",
  87. },
  88. Date: time.Now().UTC().Format(time.RFC3339),
  89. },
  90. Parents: []*api.CommitMeta{
  91. {
  92. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  93. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  94. },
  95. },
  96. Message: "Updates README.md\n",
  97. Tree: &api.CommitMeta{
  98. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/trees/f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  99. SHA: "f93e3a1a1525fb5b91020git dda86e44810c87a2d7bc",
  100. },
  101. },
  102. Verification: &api.PayloadCommitVerification{
  103. Verified: false,
  104. Reason: "unsigned",
  105. Signature: "",
  106. Payload: "",
  107. },
  108. }
  109. }
  110. func getExpectedFileResponseForRepofilesUpdate(commitID, filename string) *api.FileResponse {
  111. encoding := "base64"
  112. content := "VGhpcyBpcyBVUERBVEVEIGNvbnRlbnQgZm9yIHRoZSBSRUFETUUgZmlsZQ=="
  113. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + filename + "?ref=master"
  114. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + filename
  115. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/dbf8d00e022e05b7e5cf7e535de857de57925647"
  116. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + filename
  117. return &api.FileResponse{
  118. Content: &api.ContentsResponse{
  119. Name: filename,
  120. Path: filename,
  121. SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
  122. Type: "file",
  123. Size: 43,
  124. Encoding: &encoding,
  125. Content: &content,
  126. URL: &selfURL,
  127. HTMLURL: &htmlURL,
  128. GitURL: &gitURL,
  129. DownloadURL: &downloadURL,
  130. Links: &api.FileLinksResponse{
  131. Self: &selfURL,
  132. GitURL: &gitURL,
  133. HTMLURL: &htmlURL,
  134. },
  135. },
  136. Commit: &api.FileCommitResponse{
  137. CommitMeta: api.CommitMeta{
  138. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  139. SHA: commitID,
  140. },
  141. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  142. Author: &api.CommitUser{
  143. Identity: api.Identity{
  144. Name: "User Two",
  145. Email: "user2@noreply.example.org",
  146. },
  147. Date: time.Now().UTC().Format(time.RFC3339),
  148. },
  149. Committer: &api.CommitUser{
  150. Identity: api.Identity{
  151. Name: "User Two",
  152. Email: "user2@noreply.example.org",
  153. },
  154. Date: time.Now().UTC().Format(time.RFC3339),
  155. },
  156. Parents: []*api.CommitMeta{
  157. {
  158. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/65f1bf27bc3bf70f64657658635e66094edbcb4d",
  159. SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
  160. },
  161. },
  162. Message: "Updates README.md\n",
  163. Tree: &api.CommitMeta{
  164. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/trees/f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  165. SHA: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc",
  166. },
  167. },
  168. Verification: &api.PayloadCommitVerification{
  169. Verified: false,
  170. Reason: "unsigned",
  171. Signature: "",
  172. Payload: "",
  173. },
  174. }
  175. }
  176. func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
  177. // setup
  178. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  179. ctx := test.MockContext(t, "user2/repo1")
  180. ctx.SetParams(":id", "1")
  181. test.LoadRepo(t, ctx, 1)
  182. test.LoadRepoCommit(t, ctx)
  183. test.LoadUser(t, ctx, 2)
  184. test.LoadGitRepo(t, ctx)
  185. defer ctx.Repo.GitRepo.Close()
  186. repo := ctx.Repo.Repository
  187. doer := ctx.User
  188. opts := getCreateRepoFileOptions(repo)
  189. // test
  190. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  191. // asserts
  192. assert.Nil(t, err)
  193. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  194. defer gitRepo.Close()
  195. commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
  196. expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID)
  197. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  198. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  199. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  200. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  201. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  202. })
  203. }
  204. func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
  205. // setup
  206. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  207. ctx := test.MockContext(t, "user2/repo1")
  208. ctx.SetParams(":id", "1")
  209. test.LoadRepo(t, ctx, 1)
  210. test.LoadRepoCommit(t, ctx)
  211. test.LoadUser(t, ctx, 2)
  212. test.LoadGitRepo(t, ctx)
  213. defer ctx.Repo.GitRepo.Close()
  214. repo := ctx.Repo.Repository
  215. doer := ctx.User
  216. opts := getUpdateRepoFileOptions(repo)
  217. // test
  218. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  219. // asserts
  220. assert.Nil(t, err)
  221. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  222. defer gitRepo.Close()
  223. commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
  224. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commitID, opts.TreePath)
  225. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  226. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  227. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  228. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  229. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  230. })
  231. }
  232. func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {
  233. // setup
  234. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  235. ctx := test.MockContext(t, "user2/repo1")
  236. ctx.SetParams(":id", "1")
  237. test.LoadRepo(t, ctx, 1)
  238. test.LoadRepoCommit(t, ctx)
  239. test.LoadUser(t, ctx, 2)
  240. test.LoadGitRepo(t, ctx)
  241. defer ctx.Repo.GitRepo.Close()
  242. repo := ctx.Repo.Repository
  243. doer := ctx.User
  244. opts := getUpdateRepoFileOptions(repo)
  245. opts.FromTreePath = "README.md"
  246. opts.TreePath = "README_new.md" // new file name, README_new.md
  247. // test
  248. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  249. // asserts
  250. assert.Nil(t, err)
  251. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  252. defer gitRepo.Close()
  253. commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
  254. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.TreePath)
  255. // assert that the old file no longer exists in the last commit of the branch
  256. fromEntry, err := commit.GetTreeEntryByPath(opts.FromTreePath)
  257. toEntry, err := commit.GetTreeEntryByPath(opts.TreePath)
  258. assert.Nil(t, fromEntry) // Should no longer exist here
  259. assert.NotNil(t, toEntry) // Should exist here
  260. // assert SHA has remained the same but paths use the new file name
  261. assert.EqualValues(t, expectedFileResponse.Content.SHA, fileResponse.Content.SHA)
  262. assert.EqualValues(t, expectedFileResponse.Content.Name, fileResponse.Content.Name)
  263. assert.EqualValues(t, expectedFileResponse.Content.Path, fileResponse.Content.Path)
  264. assert.EqualValues(t, expectedFileResponse.Content.URL, fileResponse.Content.URL)
  265. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  266. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  267. })
  268. }
  269. // Test opts with branch names removed, should get same results as above test
  270. func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
  271. // setup
  272. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  273. ctx := test.MockContext(t, "user2/repo1")
  274. ctx.SetParams(":id", "1")
  275. test.LoadRepo(t, ctx, 1)
  276. test.LoadRepoCommit(t, ctx)
  277. test.LoadUser(t, ctx, 2)
  278. test.LoadGitRepo(t, ctx)
  279. defer ctx.Repo.GitRepo.Close()
  280. repo := ctx.Repo.Repository
  281. doer := ctx.User
  282. opts := getUpdateRepoFileOptions(repo)
  283. opts.OldBranch = ""
  284. opts.NewBranch = ""
  285. // test
  286. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  287. // asserts
  288. assert.Nil(t, err)
  289. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  290. defer gitRepo.Close()
  291. commitID, _ := gitRepo.GetBranchCommitID(repo.DefaultBranch)
  292. expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commitID, opts.TreePath)
  293. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  294. })
  295. }
  296. func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
  297. // setup
  298. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  299. ctx := test.MockContext(t, "user2/repo1")
  300. ctx.SetParams(":id", "1")
  301. test.LoadRepo(t, ctx, 1)
  302. test.LoadRepoCommit(t, ctx)
  303. test.LoadUser(t, ctx, 2)
  304. test.LoadGitRepo(t, ctx)
  305. defer ctx.Repo.GitRepo.Close()
  306. repo := ctx.Repo.Repository
  307. doer := ctx.User
  308. t.Run("bad branch", func(t *testing.T) {
  309. opts := getUpdateRepoFileOptions(repo)
  310. opts.OldBranch = "bad_branch"
  311. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  312. assert.Error(t, err)
  313. assert.Nil(t, fileResponse)
  314. expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
  315. assert.EqualError(t, err, expectedError)
  316. })
  317. t.Run("bad SHA", func(t *testing.T) {
  318. opts := getUpdateRepoFileOptions(repo)
  319. origSHA := opts.SHA
  320. opts.SHA = "bad_sha"
  321. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  322. assert.Nil(t, fileResponse)
  323. assert.Error(t, err)
  324. expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
  325. assert.EqualError(t, err, expectedError)
  326. })
  327. t.Run("new branch already exists", func(t *testing.T) {
  328. opts := getUpdateRepoFileOptions(repo)
  329. opts.NewBranch = "develop"
  330. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  331. assert.Nil(t, fileResponse)
  332. assert.Error(t, err)
  333. expectedError := "branch already exists [name: " + opts.NewBranch + "]"
  334. assert.EqualError(t, err, expectedError)
  335. })
  336. t.Run("treePath is empty:", func(t *testing.T) {
  337. opts := getUpdateRepoFileOptions(repo)
  338. opts.TreePath = ""
  339. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  340. assert.Nil(t, fileResponse)
  341. assert.Error(t, err)
  342. expectedError := "path contains a malformed path component [path: ]"
  343. assert.EqualError(t, err, expectedError)
  344. })
  345. t.Run("treePath is a git directory:", func(t *testing.T) {
  346. opts := getUpdateRepoFileOptions(repo)
  347. opts.TreePath = ".git"
  348. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  349. assert.Nil(t, fileResponse)
  350. assert.Error(t, err)
  351. expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
  352. assert.EqualError(t, err, expectedError)
  353. })
  354. t.Run("create file that already exists", func(t *testing.T) {
  355. opts := getCreateRepoFileOptions(repo)
  356. opts.TreePath = "README.md" //already exists
  357. fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
  358. assert.Nil(t, fileResponse)
  359. assert.Error(t, err)
  360. expectedError := "repository file already exists [path: " + opts.TreePath + "]"
  361. assert.EqualError(t, err, expectedError)
  362. })
  363. })
  364. }