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.

action_test.go 10KB

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