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.

79 lines
2.1 KiB

  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. 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. prepareTestEnv(t)
  23. deleteBranch(t)
  24. }
  25. func TestUndoDeleteBranch(t *testing.T) {
  26. prepareTestEnv(t)
  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. func deleteBranch(t *testing.T) {
  35. htmlDoc, name := branchAction(t, ".delete-branch-button")
  36. assert.Contains(t,
  37. htmlDoc.doc.Find(".ui.positive.message").Text(),
  38. i18n.Tr("en", "repo.branch.deletion_success", name),
  39. )
  40. }
  41. func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
  42. session := loginUser(t, "user2")
  43. req := NewRequest(t, "GET", "/user2/repo1/branches")
  44. resp := session.MakeRequest(t, req, http.StatusOK)
  45. htmlDoc := NewHTMLParser(t, resp.Body)
  46. link, exists := htmlDoc.doc.Find(button).Attr("data-url")
  47. assert.True(t, exists, "The template has changed")
  48. req = NewRequestWithValues(t, "POST", link, map[string]string{
  49. "_csrf": getCsrf(t, htmlDoc.doc),
  50. })
  51. session.MakeRequest(t, req, http.StatusOK)
  52. url, err := url.Parse(link)
  53. assert.NoError(t, err)
  54. req = NewRequest(t, "GET", "/user2/repo1/branches")
  55. resp = session.MakeRequest(t, req, http.StatusOK)
  56. return NewHTMLParser(t, resp.Body), url.Query()["name"][0]
  57. }
  58. func getCsrf(t *testing.T, doc *goquery.Document) string {
  59. csrf, exists := doc.Find("meta[name=\"_csrf\"]").Attr("content")
  60. assert.True(t, exists)
  61. return csrf
  62. }