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.

129 lines
3.9 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. "fmt"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/test"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/unknwon/i18n"
  14. )
  15. func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title string, preRelease, draft bool) {
  16. req := NewRequest(t, "GET", repoURL+"/releases/new")
  17. resp := session.MakeRequest(t, req, http.StatusOK)
  18. htmlDoc := NewHTMLParser(t, resp.Body)
  19. link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
  20. assert.True(t, exists, "The template has changed")
  21. postData := map[string]string{
  22. "_csrf": htmlDoc.GetCSRF(),
  23. "tag_name": tag,
  24. "tag_target": "master",
  25. "title": title,
  26. "content": "",
  27. }
  28. if preRelease {
  29. postData["prerelease"] = "on"
  30. }
  31. if draft {
  32. postData["draft"] = "Save Draft"
  33. }
  34. req = NewRequestWithValues(t, "POST", link, postData)
  35. resp = session.MakeRequest(t, req, http.StatusFound)
  36. test.RedirectURL(resp) // check that redirect URL exists
  37. }
  38. func checkLatestReleaseAndCount(t *testing.T, session *TestSession, repoURL, version, label string, count int) {
  39. req := NewRequest(t, "GET", repoURL+"/releases")
  40. resp := session.MakeRequest(t, req, http.StatusOK)
  41. htmlDoc := NewHTMLParser(t, resp.Body)
  42. labelText := htmlDoc.doc.Find("#release-list > li .meta .label").First().Text()
  43. assert.EqualValues(t, label, labelText)
  44. titleText := htmlDoc.doc.Find("#release-list > li .detail h3 a").First().Text()
  45. assert.EqualValues(t, version, titleText)
  46. releaseList := htmlDoc.doc.Find("#release-list > li")
  47. assert.EqualValues(t, count, releaseList.Length())
  48. }
  49. func TestViewReleases(t *testing.T) {
  50. defer prepareTestEnv(t)()
  51. session := loginUser(t, "user2")
  52. req := NewRequest(t, "GET", "/user2/repo1/releases")
  53. session.MakeRequest(t, req, http.StatusOK)
  54. // if CI is to slow this test fail, so lets wait a bit
  55. time.Sleep(time.Millisecond * 100)
  56. }
  57. func TestViewReleasesNoLogin(t *testing.T) {
  58. defer prepareTestEnv(t)()
  59. req := NewRequest(t, "GET", "/user2/repo1/releases")
  60. MakeRequest(t, req, http.StatusOK)
  61. }
  62. func TestCreateRelease(t *testing.T) {
  63. defer prepareTestEnv(t)()
  64. session := loginUser(t, "user2")
  65. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
  66. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 2)
  67. }
  68. func TestCreateReleasePreRelease(t *testing.T) {
  69. defer prepareTestEnv(t)()
  70. session := loginUser(t, "user2")
  71. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
  72. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 2)
  73. }
  74. func TestCreateReleaseDraft(t *testing.T) {
  75. defer prepareTestEnv(t)()
  76. session := loginUser(t, "user2")
  77. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
  78. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 2)
  79. }
  80. func TestCreateReleasePaging(t *testing.T) {
  81. defer prepareTestEnv(t)()
  82. oldAPIDefaultNum := setting.API.DefaultPagingNum
  83. defer func() {
  84. setting.API.DefaultPagingNum = oldAPIDefaultNum
  85. }()
  86. setting.API.DefaultPagingNum = 10
  87. session := loginUser(t, "user2")
  88. // Create enaugh releases to have paging
  89. for i := 0; i < 12; i++ {
  90. version := fmt.Sprintf("v0.0.%d", i)
  91. createNewRelease(t, session, "/user2/repo1", version, version, false, false)
  92. }
  93. createNewRelease(t, session, "/user2/repo1", "v0.0.12", "v0.0.12", false, true)
  94. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", i18n.Tr("en", "repo.release.draft"), 10)
  95. // Check that user4 does not see draft and still see 10 latest releases
  96. session2 := loginUser(t, "user4")
  97. checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10)
  98. }