gitea源码

protected_branch_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/db"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/models/unittest"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestBranchRuleMatch(t *testing.T) {
  12. kases := []struct {
  13. Rule string
  14. BranchName string
  15. ExpectedMatch bool
  16. }{
  17. {
  18. Rule: "release/*",
  19. BranchName: "release/v1.17",
  20. ExpectedMatch: true,
  21. },
  22. {
  23. Rule: "release/**/v1.17",
  24. BranchName: "release/test/v1.17",
  25. ExpectedMatch: true,
  26. },
  27. {
  28. Rule: "release/**/v1.17",
  29. BranchName: "release/test/1/v1.17",
  30. ExpectedMatch: true,
  31. },
  32. {
  33. Rule: "release/*/v1.17",
  34. BranchName: "release/test/1/v1.17",
  35. ExpectedMatch: false,
  36. },
  37. {
  38. Rule: "release/v*",
  39. BranchName: "release/v1.16",
  40. ExpectedMatch: true,
  41. },
  42. {
  43. Rule: "*",
  44. BranchName: "release/v1.16",
  45. ExpectedMatch: false,
  46. },
  47. {
  48. Rule: "**",
  49. BranchName: "release/v1.16",
  50. ExpectedMatch: true,
  51. },
  52. {
  53. Rule: "main",
  54. BranchName: "main",
  55. ExpectedMatch: true,
  56. },
  57. {
  58. Rule: "master",
  59. BranchName: "main",
  60. ExpectedMatch: false,
  61. },
  62. }
  63. for _, kase := range kases {
  64. pb := ProtectedBranch{RuleName: kase.Rule}
  65. var should, infact string
  66. if !kase.ExpectedMatch {
  67. should = " not"
  68. } else {
  69. infact = " not"
  70. }
  71. assert.Equal(t, kase.ExpectedMatch, pb.Match(kase.BranchName),
  72. "%s should%s match %s but it is%s", kase.BranchName, should, kase.Rule, infact,
  73. )
  74. }
  75. }
  76. func TestUpdateProtectBranchPriorities(t *testing.T) {
  77. assert.NoError(t, unittest.PrepareTestDatabase())
  78. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  79. // Create some test protected branches with initial priorities
  80. protectedBranches := []*ProtectedBranch{
  81. {
  82. RepoID: repo.ID,
  83. RuleName: "master",
  84. Priority: 1,
  85. },
  86. {
  87. RepoID: repo.ID,
  88. RuleName: "develop",
  89. Priority: 2,
  90. },
  91. {
  92. RepoID: repo.ID,
  93. RuleName: "feature/*",
  94. Priority: 3,
  95. },
  96. }
  97. for _, pb := range protectedBranches {
  98. _, err := db.GetEngine(t.Context()).Insert(pb)
  99. assert.NoError(t, err)
  100. }
  101. // Test updating priorities
  102. newPriorities := []int64{protectedBranches[2].ID, protectedBranches[0].ID, protectedBranches[1].ID}
  103. err := UpdateProtectBranchPriorities(t.Context(), repo, newPriorities)
  104. assert.NoError(t, err)
  105. // Verify new priorities
  106. pbs, err := FindRepoProtectedBranchRules(t.Context(), repo.ID)
  107. assert.NoError(t, err)
  108. expectedPriorities := map[string]int64{
  109. "feature/*": 1,
  110. "master": 2,
  111. "develop": 3,
  112. }
  113. for _, pb := range pbs {
  114. assert.Equal(t, expectedPriorities[pb.RuleName], pb.Priority)
  115. }
  116. }
  117. func TestNewProtectBranchPriority(t *testing.T) {
  118. assert.NoError(t, unittest.PrepareTestDatabase())
  119. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  120. err := UpdateProtectBranch(t.Context(), repo, &ProtectedBranch{
  121. RepoID: repo.ID,
  122. RuleName: "branch-1",
  123. Priority: 1,
  124. }, WhitelistOptions{})
  125. assert.NoError(t, err)
  126. newPB := &ProtectedBranch{
  127. RepoID: repo.ID,
  128. RuleName: "branch-2",
  129. // Priority intentionally omitted
  130. }
  131. err = UpdateProtectBranch(t.Context(), repo, newPB, WhitelistOptions{})
  132. assert.NoError(t, err)
  133. savedPB2, err := GetFirstMatchProtectedBranchRule(t.Context(), repo.ID, "branch-2")
  134. assert.NoError(t, err)
  135. assert.Equal(t, int64(2), savedPB2.Priority)
  136. }