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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }