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.

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