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.

change_default_branch_test.go 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2017 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 integrations
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestChangeDefaultBranch(t *testing.T) {
  15. prepareTestEnv(t)
  16. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  17. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  18. session := loginUser(t, owner.Name, "password")
  19. branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
  20. req := NewRequest(t, "GET", branchesURL)
  21. resp := session.MakeRequest(t, req)
  22. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  23. doc, err := NewHtmlParser(resp.Body)
  24. assert.NoError(t, err)
  25. req = NewRequestBody(t, "POST", branchesURL,
  26. bytes.NewBufferString(url.Values{
  27. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  28. "action": []string{"default_branch"},
  29. "branch": []string{"DefaultBranch"},
  30. }.Encode()))
  31. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  32. resp = session.MakeRequest(t, req)
  33. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  34. req = NewRequest(t, "GET", branchesURL)
  35. resp = session.MakeRequest(t, req)
  36. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  37. doc, err = NewHtmlParser(resp.Body)
  38. assert.NoError(t, err)
  39. req = NewRequestBody(t, "POST", branchesURL,
  40. bytes.NewBufferString(url.Values{
  41. "_csrf": []string{doc.GetInputValueByName("_csrf")},
  42. "action": []string{"default_branch"},
  43. "branch": []string{"does_not_exist"},
  44. }.Encode()))
  45. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  46. resp = session.MakeRequest(t, req)
  47. assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode)
  48. }