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/Unknwon/i18n"
  11. "github.com/stretchr/testify/assert"
  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. htmlDoc = NewHTMLParser(t, resp.Body)
  49. req = NewRequestWithValues(t, "POST", link, map[string]string{
  50. "_csrf": getCsrf(htmlDoc.doc),
  51. })
  52. resp = 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(doc *goquery.Document) string {
  60. csrf, _ := doc.Find("meta[name=\"_csrf\"]").Attr("content")
  61. return csrf
  62. }