gitea源码

change_default_branch_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "testing"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/tests"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestChangeDefaultBranch(t *testing.T) {
  17. defer tests.PrepareTestEnv(t)()
  18. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  19. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  20. session := loginUser(t, owner.Name)
  21. branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
  22. csrf := GetUserCSRFToken(t, session)
  23. req := NewRequestWithValues(t, "POST", branchesURL, map[string]string{
  24. "_csrf": csrf,
  25. "action": "default_branch",
  26. "branch": "DefaultBranch",
  27. })
  28. session.MakeRequest(t, req, http.StatusSeeOther)
  29. csrf = GetUserCSRFToken(t, session)
  30. req = NewRequestWithValues(t, "POST", branchesURL, map[string]string{
  31. "_csrf": csrf,
  32. "action": "default_branch",
  33. "branch": "does_not_exist",
  34. })
  35. session.MakeRequest(t, req, http.StatusNotFound)
  36. }
  37. func checkDivergence(t *testing.T, session *TestSession, branchesURL, expectedDefaultBranch string, expectedBranchToDivergence map[string]git.DivergeObject) {
  38. req := NewRequest(t, "GET", branchesURL)
  39. resp := session.MakeRequest(t, req, http.StatusOK)
  40. htmlDoc := NewHTMLParser(t, resp.Body)
  41. branchNodes := htmlDoc.doc.Find(".branch-name").Nodes
  42. branchNames := []string{}
  43. for _, node := range branchNodes {
  44. branchNames = append(branchNames, node.FirstChild.Data)
  45. }
  46. expectBranchCount := len(expectedBranchToDivergence)
  47. assert.Len(t, branchNames, expectBranchCount+1)
  48. assert.Equal(t, expectedDefaultBranch, branchNames[0])
  49. allCountBehindNodes := htmlDoc.doc.Find(".count-behind").Nodes
  50. allCountAheadNodes := htmlDoc.doc.Find(".count-ahead").Nodes
  51. assert.Len(t, allCountAheadNodes, expectBranchCount)
  52. assert.Len(t, allCountBehindNodes, expectBranchCount)
  53. for i := range expectBranchCount {
  54. branchName := branchNames[i+1]
  55. assert.Contains(t, expectedBranchToDivergence, branchName)
  56. expectedCountAhead := expectedBranchToDivergence[branchName].Ahead
  57. expectedCountBehind := expectedBranchToDivergence[branchName].Behind
  58. countAhead, err := strconv.Atoi(allCountAheadNodes[i].FirstChild.Data)
  59. assert.NoError(t, err)
  60. countBehind, err := strconv.Atoi(allCountBehindNodes[i].FirstChild.Data)
  61. assert.NoError(t, err)
  62. assert.Equal(t, expectedCountAhead, countAhead)
  63. assert.Equal(t, expectedCountBehind, countBehind)
  64. }
  65. }
  66. func TestChangeDefaultBranchDivergence(t *testing.T) {
  67. defer tests.PrepareTestEnv(t)()
  68. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16})
  69. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  70. session := loginUser(t, owner.Name)
  71. branchesURL := fmt.Sprintf("/%s/%s/branches", owner.Name, repo.Name)
  72. settingsBranchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
  73. // check branch divergence before switching default branch
  74. expectedBranchToDivergenceBefore := map[string]git.DivergeObject{
  75. "not-signed": {
  76. Ahead: 0,
  77. Behind: 0,
  78. },
  79. "good-sign-not-yet-validated": {
  80. Ahead: 0,
  81. Behind: 1,
  82. },
  83. "good-sign": {
  84. Ahead: 1,
  85. Behind: 3,
  86. },
  87. }
  88. checkDivergence(t, session, branchesURL, "master", expectedBranchToDivergenceBefore)
  89. // switch default branch
  90. newDefaultBranch := "good-sign-not-yet-validated"
  91. csrf := GetUserCSRFToken(t, session)
  92. req := NewRequestWithValues(t, "POST", settingsBranchesURL, map[string]string{
  93. "_csrf": csrf,
  94. "action": "default_branch",
  95. "branch": newDefaultBranch,
  96. })
  97. session.MakeRequest(t, req, http.StatusSeeOther)
  98. // check branch divergence after switching default branch
  99. expectedBranchToDivergenceAfter := map[string]git.DivergeObject{
  100. "master": {
  101. Ahead: 1,
  102. Behind: 0,
  103. },
  104. "not-signed": {
  105. Ahead: 1,
  106. Behind: 0,
  107. },
  108. "good-sign": {
  109. Ahead: 1,
  110. Behind: 2,
  111. },
  112. }
  113. checkDivergence(t, session, branchesURL, newDefaultBranch, expectedBranchToDivergenceAfter)
  114. }