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.

branches_test.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "net/http"
  7. "net/url"
  8. "testing"
  9. "github.com/PuerkitoBio/goquery"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/unknwon/i18n"
  12. )
  13. func TestViewBranches(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. req := NewRequest(t, "GET", "/user2/repo1/branches")
  16. resp := MakeRequest(t, req, http.StatusOK)
  17. htmlDoc := NewHTMLParser(t, resp.Body)
  18. _, exists := htmlDoc.doc.Find(".delete-branch-button").Attr("data-url")
  19. assert.False(t, exists, "The template has changed")
  20. }
  21. func TestDeleteBranch(t *testing.T) {
  22. defer prepareTestEnv(t)()
  23. deleteBranch(t)
  24. }
  25. func TestUndoDeleteBranch(t *testing.T) {
  26. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  27. deleteBranch(t)
  28. htmlDoc, name := branchAction(t, ".undo-button")
  29. assert.Contains(t,
  30. htmlDoc.doc.Find(".ui.positive.message").Text(),
  31. i18n.Tr("en", "repo.branch.restore_success", name),
  32. )
  33. })
  34. }
  35. func deleteBranch(t *testing.T) {
  36. htmlDoc, name := branchAction(t, ".delete-branch-button")
  37. assert.Contains(t,
  38. htmlDoc.doc.Find(".ui.positive.message").Text(),
  39. i18n.Tr("en", "repo.branch.deletion_success", name),
  40. )
  41. }
  42. func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
  43. session := loginUser(t, "user2")
  44. req := NewRequest(t, "GET", "/user2/repo1/branches")
  45. resp := session.MakeRequest(t, req, http.StatusOK)
  46. htmlDoc := NewHTMLParser(t, resp.Body)
  47. link, exists := htmlDoc.doc.Find(button).Attr("data-url")
  48. assert.True(t, exists, "The template has changed")
  49. req = NewRequestWithValues(t, "POST", link, map[string]string{
  50. "_csrf": getCsrf(t, htmlDoc.doc),
  51. })
  52. session.MakeRequest(t, req, http.StatusOK)
  53. url, err := url.Parse(link)
  54. assert.NoError(t, err)
  55. req = NewRequest(t, "GET", "/user2/repo1/branches")
  56. resp = session.MakeRequest(t, req, http.StatusOK)
  57. return NewHTMLParser(t, resp.Body), url.Query()["name"][0]
  58. }
  59. func getCsrf(t *testing.T, doc *goquery.Document) string {
  60. csrf, exists := doc.Find("meta[name=\"_csrf\"]").Attr("content")
  61. assert.True(t, exists)
  62. return csrf
  63. }