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.

122 lines
3.8 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. "github.com/Unknwon/i18n"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title string, preRelease, draft bool) {
  13. req := NewRequest(t, "GET", repoURL+"/releases/new")
  14. resp := session.MakeRequest(t, req)
  15. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  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)
  34. assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
  35. redirectedURL := resp.Headers["Location"]
  36. assert.NotEmpty(t, redirectedURL, "Redirected URL is not found")
  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)
  41. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  42. htmlDoc := NewHTMLParser(t, resp.Body)
  43. labelText := htmlDoc.doc.Find("#release-list > li .meta .label").First().Text()
  44. assert.EqualValues(t, label, labelText)
  45. titleText := htmlDoc.doc.Find("#release-list > li .detail h3 a").First().Text()
  46. assert.EqualValues(t, version, titleText)
  47. releaseList := htmlDoc.doc.Find("#release-list > li")
  48. assert.EqualValues(t, count, releaseList.Length())
  49. }
  50. func TestViewReleases(t *testing.T) {
  51. prepareTestEnv(t)
  52. session := loginUser(t, "user2")
  53. req := NewRequest(t, "GET", "/user2/repo1/releases")
  54. resp := session.MakeRequest(t, req)
  55. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  56. }
  57. func TestViewReleasesNoLogin(t *testing.T) {
  58. prepareTestEnv(t)
  59. req := NewRequest(t, "GET", "/user2/repo1/releases")
  60. resp := MakeRequest(req)
  61. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  62. }
  63. func TestCreateRelease(t *testing.T) {
  64. prepareTestEnv(t)
  65. session := loginUser(t, "user2")
  66. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
  67. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 1)
  68. }
  69. func TestCreateReleasePreRelease(t *testing.T) {
  70. prepareTestEnv(t)
  71. session := loginUser(t, "user2")
  72. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
  73. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 1)
  74. }
  75. func TestCreateReleaseDraft(t *testing.T) {
  76. prepareTestEnv(t)
  77. session := loginUser(t, "user2")
  78. createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
  79. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 1)
  80. }
  81. func TestCreateReleasePaging(t *testing.T) {
  82. prepareTestEnv(t)
  83. session := loginUser(t, "user2")
  84. // Create enaugh releases to have paging
  85. for i := 0; i < 12; i++ {
  86. version := fmt.Sprintf("v0.0.%d", i)
  87. createNewRelease(t, session, "/user2/repo1", version, version, false, false)
  88. }
  89. createNewRelease(t, session, "/user2/repo1", "v0.0.12", "v0.0.12", false, true)
  90. checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", i18n.Tr("en", "repo.release.draft"), 10)
  91. // Check that user3 does not see draft and still see 10 latest releases
  92. session2 := loginUser(t, "user3")
  93. checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10)
  94. }