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

action_test.go 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 repofiles
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/repository"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testCorrectRepoAction(t *testing.T, opts *CommitRepoActionOptions, actionBean *models.Action) {
  13. models.AssertNotExistsBean(t, actionBean)
  14. assert.NoError(t, CommitRepoAction(opts))
  15. models.AssertExistsAndLoadBean(t, actionBean)
  16. models.CheckConsistencyFor(t, &models.Action{})
  17. }
  18. func TestCommitRepoAction(t *testing.T) {
  19. samples := []struct {
  20. userID int64
  21. repositoryID int64
  22. commitRepoActionOptions CommitRepoActionOptions
  23. action models.Action
  24. }{
  25. {
  26. userID: 2,
  27. repositoryID: 16,
  28. commitRepoActionOptions: CommitRepoActionOptions{
  29. RefFullName: "refName",
  30. OldCommitID: "oldCommitID",
  31. NewCommitID: "newCommitID",
  32. Commits: &repository.PushCommits{
  33. Commits: []*repository.PushCommit{
  34. {
  35. Sha1: "69554a6",
  36. CommitterEmail: "user2@example.com",
  37. CommitterName: "User2",
  38. AuthorEmail: "user2@example.com",
  39. AuthorName: "User2",
  40. Message: "not signed commit",
  41. },
  42. {
  43. Sha1: "27566bd",
  44. CommitterEmail: "user2@example.com",
  45. CommitterName: "User2",
  46. AuthorEmail: "user2@example.com",
  47. AuthorName: "User2",
  48. Message: "good signed commit (with not yet validated email)",
  49. },
  50. },
  51. Len: 2,
  52. },
  53. },
  54. action: models.Action{
  55. OpType: models.ActionCommitRepo,
  56. RefName: "refName",
  57. },
  58. },
  59. {
  60. userID: 2,
  61. repositoryID: 1,
  62. commitRepoActionOptions: CommitRepoActionOptions{
  63. RefFullName: git.TagPrefix + "v1.1",
  64. OldCommitID: git.EmptySHA,
  65. NewCommitID: "newCommitID",
  66. Commits: &repository.PushCommits{},
  67. },
  68. action: models.Action{
  69. OpType: models.ActionPushTag,
  70. RefName: "v1.1",
  71. },
  72. },
  73. {
  74. userID: 2,
  75. repositoryID: 1,
  76. commitRepoActionOptions: CommitRepoActionOptions{
  77. RefFullName: git.TagPrefix + "v1.1",
  78. OldCommitID: "oldCommitID",
  79. NewCommitID: git.EmptySHA,
  80. Commits: &repository.PushCommits{},
  81. },
  82. action: models.Action{
  83. OpType: models.ActionDeleteTag,
  84. RefName: "v1.1",
  85. },
  86. },
  87. {
  88. userID: 2,
  89. repositoryID: 1,
  90. commitRepoActionOptions: CommitRepoActionOptions{
  91. RefFullName: git.BranchPrefix + "feature/1",
  92. OldCommitID: "oldCommitID",
  93. NewCommitID: git.EmptySHA,
  94. Commits: &repository.PushCommits{},
  95. },
  96. action: models.Action{
  97. OpType: models.ActionDeleteBranch,
  98. RefName: "feature/1",
  99. },
  100. },
  101. }
  102. for _, s := range samples {
  103. models.PrepareTestEnv(t)
  104. user := models.AssertExistsAndLoadBean(t, &models.User{ID: s.userID}).(*models.User)
  105. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: s.repositoryID, OwnerID: user.ID}).(*models.Repository)
  106. repo.Owner = user
  107. s.commitRepoActionOptions.PusherName = user.Name
  108. s.commitRepoActionOptions.RepoOwnerID = user.ID
  109. s.commitRepoActionOptions.RepoName = repo.Name
  110. s.action.ActUserID = user.ID
  111. s.action.RepoID = repo.ID
  112. s.action.Repo = repo
  113. s.action.IsPrivate = repo.IsPrivate
  114. testCorrectRepoAction(t, &s.commitRepoActionOptions, &s.action)
  115. }
  116. }
  117. func TestUpdateIssuesCommit(t *testing.T) {
  118. assert.NoError(t, models.PrepareTestDatabase())
  119. pushCommits := []*repository.PushCommit{
  120. {
  121. Sha1: "abcdef1",
  122. CommitterEmail: "user2@example.com",
  123. CommitterName: "User Two",
  124. AuthorEmail: "user4@example.com",
  125. AuthorName: "User Four",
  126. Message: "start working on #FST-1, #1",
  127. },
  128. {
  129. Sha1: "abcdef2",
  130. CommitterEmail: "user2@example.com",
  131. CommitterName: "User Two",
  132. AuthorEmail: "user2@example.com",
  133. AuthorName: "User Two",
  134. Message: "a plain message",
  135. },
  136. {
  137. Sha1: "abcdef2",
  138. CommitterEmail: "user2@example.com",
  139. CommitterName: "User Two",
  140. AuthorEmail: "user2@example.com",
  141. AuthorName: "User Two",
  142. Message: "close #2",
  143. },
  144. }
  145. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  146. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  147. repo.Owner = user
  148. commentBean := &models.Comment{
  149. Type: models.CommentTypeCommitRef,
  150. CommitSHA: "abcdef1",
  151. PosterID: user.ID,
  152. IssueID: 1,
  153. }
  154. issueBean := &models.Issue{RepoID: repo.ID, Index: 4}
  155. models.AssertNotExistsBean(t, commentBean)
  156. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  157. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  158. models.AssertExistsAndLoadBean(t, commentBean)
  159. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  160. models.CheckConsistencyFor(t, &models.Action{})
  161. // Test that push to a non-default branch closes no issue.
  162. pushCommits = []*repository.PushCommit{
  163. {
  164. Sha1: "abcdef1",
  165. CommitterEmail: "user2@example.com",
  166. CommitterName: "User Two",
  167. AuthorEmail: "user4@example.com",
  168. AuthorName: "User Four",
  169. Message: "close #1",
  170. },
  171. }
  172. repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
  173. commentBean = &models.Comment{
  174. Type: models.CommentTypeCommitRef,
  175. CommitSHA: "abcdef1",
  176. PosterID: user.ID,
  177. IssueID: 6,
  178. }
  179. issueBean = &models.Issue{RepoID: repo.ID, Index: 1}
  180. models.AssertNotExistsBean(t, commentBean)
  181. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  182. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  183. models.AssertExistsAndLoadBean(t, commentBean)
  184. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  185. models.CheckConsistencyFor(t, &models.Action{})
  186. }
  187. func TestUpdateIssuesCommit_Colon(t *testing.T) {
  188. assert.NoError(t, models.PrepareTestDatabase())
  189. pushCommits := []*repository.PushCommit{
  190. {
  191. Sha1: "abcdef2",
  192. CommitterEmail: "user2@example.com",
  193. CommitterName: "User Two",
  194. AuthorEmail: "user2@example.com",
  195. AuthorName: "User Two",
  196. Message: "close: #2",
  197. },
  198. }
  199. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  200. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  201. repo.Owner = user
  202. issueBean := &models.Issue{RepoID: repo.ID, Index: 4}
  203. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  204. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  205. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  206. models.CheckConsistencyFor(t, &models.Action{})
  207. }
  208. func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  209. assert.NoError(t, models.PrepareTestDatabase())
  210. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  211. // Test that push to a non-default branch closes an issue.
  212. pushCommits := []*repository.PushCommit{
  213. {
  214. Sha1: "abcdef1",
  215. CommitterEmail: "user2@example.com",
  216. CommitterName: "User Two",
  217. AuthorEmail: "user4@example.com",
  218. AuthorName: "User Four",
  219. Message: "close #2",
  220. },
  221. }
  222. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  223. commentBean := &models.Comment{
  224. Type: models.CommentTypeCommitRef,
  225. CommitSHA: "abcdef1",
  226. PosterID: user.ID,
  227. IssueID: 7,
  228. }
  229. issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7}
  230. models.AssertNotExistsBean(t, commentBean)
  231. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  232. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  233. models.AssertExistsAndLoadBean(t, commentBean)
  234. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  235. models.CheckConsistencyFor(t, &models.Action{})
  236. }
  237. func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
  238. assert.NoError(t, models.PrepareTestDatabase())
  239. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  240. // Test that a push to default branch closes issue in another repo
  241. // If the user also has push permissions to that repo
  242. pushCommits := []*repository.PushCommit{
  243. {
  244. Sha1: "abcdef1",
  245. CommitterEmail: "user2@example.com",
  246. CommitterName: "User Two",
  247. AuthorEmail: "user2@example.com",
  248. AuthorName: "User Two",
  249. Message: "close user2/repo1#1",
  250. },
  251. }
  252. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  253. commentBean := &models.Comment{
  254. Type: models.CommentTypeCommitRef,
  255. CommitSHA: "abcdef1",
  256. PosterID: user.ID,
  257. IssueID: 1,
  258. }
  259. issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1}
  260. models.AssertNotExistsBean(t, commentBean)
  261. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  262. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  263. models.AssertExistsAndLoadBean(t, commentBean)
  264. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  265. models.CheckConsistencyFor(t, &models.Action{})
  266. }
  267. func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
  268. assert.NoError(t, models.PrepareTestDatabase())
  269. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User)
  270. // Test that a push with close reference *can not* close issue
  271. // If the commiter doesn't have push rights in that repo
  272. pushCommits := []*repository.PushCommit{
  273. {
  274. Sha1: "abcdef3",
  275. CommitterEmail: "user10@example.com",
  276. CommitterName: "User Ten",
  277. AuthorEmail: "user10@example.com",
  278. AuthorName: "User Ten",
  279. Message: "close user3/repo3#1",
  280. },
  281. }
  282. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository)
  283. commentBean := &models.Comment{
  284. Type: models.CommentTypeCommitRef,
  285. CommitSHA: "abcdef3",
  286. PosterID: user.ID,
  287. IssueID: 6,
  288. }
  289. issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6}
  290. models.AssertNotExistsBean(t, commentBean)
  291. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  292. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  293. models.AssertNotExistsBean(t, commentBean)
  294. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  295. models.CheckConsistencyFor(t, &models.Action{})
  296. }