code.gitea.io/gitea@v1.21.7/tests/integration/release_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  	"time"
    11  
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  	"code.gitea.io/gitea/models/unittest"
    14  	"code.gitea.io/gitea/modules/setting"
    15  	"code.gitea.io/gitea/modules/test"
    16  	"code.gitea.io/gitea/modules/translation"
    17  	"code.gitea.io/gitea/tests"
    18  
    19  	"github.com/PuerkitoBio/goquery"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func createNewRelease(t *testing.T, session *TestSession, repoURL, tag, title string, preRelease, draft bool) {
    24  	req := NewRequest(t, "GET", repoURL+"/releases/new")
    25  	resp := session.MakeRequest(t, req, http.StatusOK)
    26  	htmlDoc := NewHTMLParser(t, resp.Body)
    27  
    28  	link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
    29  	assert.True(t, exists, "The template has changed")
    30  
    31  	postData := map[string]string{
    32  		"_csrf":      htmlDoc.GetCSRF(),
    33  		"tag_name":   tag,
    34  		"tag_target": "master",
    35  		"title":      title,
    36  		"content":    "",
    37  	}
    38  	if preRelease {
    39  		postData["prerelease"] = "on"
    40  	}
    41  	if draft {
    42  		postData["draft"] = "Save Draft"
    43  	}
    44  	req = NewRequestWithValues(t, "POST", link, postData)
    45  
    46  	resp = session.MakeRequest(t, req, http.StatusSeeOther)
    47  
    48  	test.RedirectURL(resp) // check that redirect URL exists
    49  }
    50  
    51  func checkLatestReleaseAndCount(t *testing.T, session *TestSession, repoURL, version, label string, count int) {
    52  	req := NewRequest(t, "GET", repoURL+"/releases")
    53  	resp := session.MakeRequest(t, req, http.StatusOK)
    54  
    55  	htmlDoc := NewHTMLParser(t, resp.Body)
    56  	labelText := htmlDoc.doc.Find("#release-list > li .detail .label").First().Text()
    57  	assert.EqualValues(t, label, labelText)
    58  	titleText := htmlDoc.doc.Find("#release-list > li .detail h4 a").First().Text()
    59  	assert.EqualValues(t, version, titleText)
    60  
    61  	releaseList := htmlDoc.doc.Find("#release-list > li")
    62  	assert.EqualValues(t, count, releaseList.Length())
    63  }
    64  
    65  func TestViewReleases(t *testing.T) {
    66  	defer tests.PrepareTestEnv(t)()
    67  
    68  	session := loginUser(t, "user2")
    69  	req := NewRequest(t, "GET", "/user2/repo1/releases")
    70  	session.MakeRequest(t, req, http.StatusOK)
    71  
    72  	// if CI is to slow this test fail, so lets wait a bit
    73  	time.Sleep(time.Millisecond * 100)
    74  }
    75  
    76  func TestViewReleasesNoLogin(t *testing.T) {
    77  	defer tests.PrepareTestEnv(t)()
    78  
    79  	req := NewRequest(t, "GET", "/user2/repo1/releases")
    80  	MakeRequest(t, req, http.StatusOK)
    81  }
    82  
    83  func TestCreateRelease(t *testing.T) {
    84  	defer tests.PrepareTestEnv(t)()
    85  
    86  	session := loginUser(t, "user2")
    87  	createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
    88  
    89  	checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.stable"), 4)
    90  }
    91  
    92  func TestCreateReleasePreRelease(t *testing.T) {
    93  	defer tests.PrepareTestEnv(t)()
    94  
    95  	session := loginUser(t, "user2")
    96  	createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
    97  
    98  	checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.prerelease"), 4)
    99  }
   100  
   101  func TestCreateReleaseDraft(t *testing.T) {
   102  	defer tests.PrepareTestEnv(t)()
   103  
   104  	session := loginUser(t, "user2")
   105  	createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
   106  
   107  	checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", translation.NewLocale("en-US").Tr("repo.release.draft"), 4)
   108  }
   109  
   110  func TestCreateReleasePaging(t *testing.T) {
   111  	defer tests.PrepareTestEnv(t)()
   112  
   113  	oldAPIDefaultNum := setting.API.DefaultPagingNum
   114  	defer func() {
   115  		setting.API.DefaultPagingNum = oldAPIDefaultNum
   116  	}()
   117  	setting.API.DefaultPagingNum = 10
   118  
   119  	session := loginUser(t, "user2")
   120  	// Create enough releases to have paging
   121  	for i := 0; i < 12; i++ {
   122  		version := fmt.Sprintf("v0.0.%d", i)
   123  		createNewRelease(t, session, "/user2/repo1", version, version, false, false)
   124  	}
   125  	createNewRelease(t, session, "/user2/repo1", "v0.0.12", "v0.0.12", false, true)
   126  
   127  	checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", translation.NewLocale("en-US").Tr("repo.release.draft"), 10)
   128  
   129  	// Check that user4 does not see draft and still see 10 latest releases
   130  	session2 := loginUser(t, "user4")
   131  	checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", translation.NewLocale("en-US").Tr("repo.release.stable"), 10)
   132  }
   133  
   134  func TestViewReleaseListNoLogin(t *testing.T) {
   135  	defer tests.PrepareTestEnv(t)()
   136  
   137  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 57, OwnerName: "user2", LowerName: "repo-release"})
   138  
   139  	link := repo.Link() + "/releases"
   140  
   141  	req := NewRequest(t, "GET", link)
   142  	rsp := MakeRequest(t, req, http.StatusOK)
   143  
   144  	htmlDoc := NewHTMLParser(t, rsp.Body)
   145  	releases := htmlDoc.Find("#release-list li.ui.grid")
   146  	assert.Equal(t, 5, releases.Length())
   147  
   148  	links := make([]string, 0, 5)
   149  	commitsToMain := make([]string, 0, 5)
   150  	releases.Each(func(i int, s *goquery.Selection) {
   151  		link, exist := s.Find(".release-list-title a").Attr("href")
   152  		if !exist {
   153  			return
   154  		}
   155  		links = append(links, link)
   156  
   157  		commitsToMain = append(commitsToMain, s.Find(".ahead > a").Text())
   158  	})
   159  
   160  	assert.EqualValues(t, []string{
   161  		"/user2/repo-release/releases/tag/empty-target-branch",
   162  		"/user2/repo-release/releases/tag/non-existing-target-branch",
   163  		"/user2/repo-release/releases/tag/v2.0",
   164  		"/user2/repo-release/releases/tag/v1.1",
   165  		"/user2/repo-release/releases/tag/v1.0",
   166  	}, links)
   167  	assert.EqualValues(t, []string{
   168  		"1 commits", // like v1.1
   169  		"1 commits", // like v1.1
   170  		"0 commits",
   171  		"1 commits", // should be 3 commits ahead and 2 commits behind, but not implemented yet
   172  		"3 commits",
   173  	}, commitsToMain)
   174  }
   175  
   176  func TestViewSingleReleaseNoLogin(t *testing.T) {
   177  	defer tests.PrepareTestEnv(t)()
   178  
   179  	req := NewRequest(t, "GET", "/user2/repo-release/releases/tag/v1.0")
   180  	resp := MakeRequest(t, req, http.StatusOK)
   181  
   182  	htmlDoc := NewHTMLParser(t, resp.Body)
   183  	// check the "number of commits to main since this release"
   184  	releaseList := htmlDoc.doc.Find("#release-list .ahead > a")
   185  	assert.EqualValues(t, 1, releaseList.Length())
   186  	assert.EqualValues(t, "3 commits", releaseList.First().Text())
   187  }
   188  
   189  func TestViewReleaseListLogin(t *testing.T) {
   190  	defer tests.PrepareTestEnv(t)()
   191  
   192  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   193  
   194  	link := repo.Link() + "/releases"
   195  
   196  	session := loginUser(t, "user1")
   197  	req := NewRequest(t, "GET", link)
   198  	rsp := session.MakeRequest(t, req, http.StatusOK)
   199  
   200  	htmlDoc := NewHTMLParser(t, rsp.Body)
   201  	releases := htmlDoc.Find("#release-list li.ui.grid")
   202  	assert.Equal(t, 3, releases.Length())
   203  
   204  	links := make([]string, 0, 5)
   205  	releases.Each(func(i int, s *goquery.Selection) {
   206  		link, exist := s.Find(".release-list-title a").Attr("href")
   207  		if !exist {
   208  			return
   209  		}
   210  		links = append(links, link)
   211  	})
   212  
   213  	assert.EqualValues(t, []string{
   214  		"/user2/repo1/releases/tag/draft-release",
   215  		"/user2/repo1/releases/tag/v1.0",
   216  		"/user2/repo1/releases/tag/v1.1",
   217  	}, links)
   218  }
   219  
   220  func TestViewTagsList(t *testing.T) {
   221  	defer tests.PrepareTestEnv(t)()
   222  
   223  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   224  
   225  	link := repo.Link() + "/tags"
   226  
   227  	session := loginUser(t, "user1")
   228  	req := NewRequest(t, "GET", link)
   229  	rsp := session.MakeRequest(t, req, http.StatusOK)
   230  
   231  	htmlDoc := NewHTMLParser(t, rsp.Body)
   232  	tags := htmlDoc.Find(".tag-list tr")
   233  	assert.Equal(t, 3, tags.Length())
   234  
   235  	tagNames := make([]string, 0, 5)
   236  	tags.Each(func(i int, s *goquery.Selection) {
   237  		tagNames = append(tagNames, s.Find(".tag a.gt-df.gt-ac").Text())
   238  	})
   239  
   240  	assert.EqualValues(t, []string{"v1.0", "delete-tag", "v1.1"}, tagNames)
   241  }
   242  
   243  func TestDownloadReleaseAttachment(t *testing.T) {
   244  	defer tests.PrepareTestEnv(t)()
   245  
   246  	tests.PrepareAttachmentsStorage(t)
   247  
   248  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
   249  
   250  	url := repo.Link() + "/releases/download/v1.1/README.md"
   251  
   252  	req := NewRequest(t, "GET", url)
   253  	MakeRequest(t, req, http.StatusNotFound)
   254  
   255  	req = NewRequest(t, "GET", url)
   256  	session := loginUser(t, "user2")
   257  	session.MakeRequest(t, req, http.StatusOK)
   258  }