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.

repo_test.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "path"
  9. "strings"
  10. "testing"
  11. "time"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/PuerkitoBio/goquery"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestViewRepo(t *testing.T) {
  17. defer prepareTestEnv(t)()
  18. req := NewRequest(t, "GET", "/user2/repo1")
  19. MakeRequest(t, req, http.StatusOK)
  20. req = NewRequest(t, "GET", "/user3/repo3")
  21. MakeRequest(t, req, http.StatusNotFound)
  22. session := loginUser(t, "user1")
  23. session.MakeRequest(t, req, http.StatusNotFound)
  24. }
  25. func testViewRepo(t *testing.T) {
  26. defer prepareTestEnv(t)()
  27. req := NewRequest(t, "GET", "/user3/repo3")
  28. session := loginUser(t, "user2")
  29. resp := session.MakeRequest(t, req, http.StatusOK)
  30. htmlDoc := NewHTMLParser(t, resp.Body)
  31. files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR")
  32. type file struct {
  33. fileName string
  34. commitID string
  35. commitMsg string
  36. commitTime string
  37. }
  38. var items []file
  39. files.Each(func(i int, s *goquery.Selection) {
  40. tds := s.Find("td")
  41. var f file
  42. tds.Each(func(i int, s *goquery.Selection) {
  43. if i == 0 {
  44. f.fileName = strings.TrimSpace(s.Text())
  45. } else if i == 1 {
  46. a := s.Find("a")
  47. f.commitMsg = strings.TrimSpace(a.Text())
  48. l, _ := a.Attr("href")
  49. f.commitID = path.Base(l)
  50. }
  51. })
  52. f.commitTime, _ = s.Find("span.time-since").Attr("title")
  53. items = append(items, f)
  54. })
  55. assert.EqualValues(t, []file{
  56. {
  57. fileName: "doc",
  58. commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
  59. commitMsg: "init project",
  60. commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
  61. },
  62. {
  63. fileName: "README.md",
  64. commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
  65. commitMsg: "init project",
  66. commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
  67. },
  68. }, items)
  69. }
  70. func TestViewRepo2(t *testing.T) {
  71. // no last commit cache
  72. testViewRepo(t)
  73. // enable last commit cache for all repositories
  74. oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
  75. setting.CacheService.LastCommit.CommitsCount = 0
  76. // first view will not hit the cache
  77. testViewRepo(t)
  78. // second view will hit the cache
  79. testViewRepo(t)
  80. setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
  81. }
  82. func TestViewRepo3(t *testing.T) {
  83. defer prepareTestEnv(t)()
  84. req := NewRequest(t, "GET", "/user3/repo3")
  85. session := loginUser(t, "user4")
  86. session.MakeRequest(t, req, http.StatusOK)
  87. }
  88. func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
  89. defer prepareTestEnv(t)()
  90. req := NewRequest(t, "GET", "/user2/repo1")
  91. resp := MakeRequest(t, req, http.StatusOK)
  92. htmlDoc := NewHTMLParser(t, resp.Body)
  93. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  94. assert.True(t, exists, "The template has changed")
  95. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  96. _, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  97. assert.False(t, exists)
  98. }
  99. func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
  100. defer prepareTestEnv(t)()
  101. session := loginUser(t, "user2")
  102. req := NewRequest(t, "GET", "/user2/repo1")
  103. resp := session.MakeRequest(t, req, http.StatusOK)
  104. htmlDoc := NewHTMLParser(t, resp.Body)
  105. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  106. assert.True(t, exists, "The template has changed")
  107. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  108. link, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  109. assert.True(t, exists, "The template has changed")
  110. sshURL := fmt.Sprintf("ssh://%s@%s:%d/user2/repo1.git", setting.SSH.BuiltinServerUser, setting.SSH.Domain, setting.SSH.Port)
  111. assert.Equal(t, sshURL, link)
  112. }
  113. func TestViewRepoWithSymlinks(t *testing.T) {
  114. defer prepareTestEnv(t)()
  115. session := loginUser(t, "user2")
  116. req := NewRequest(t, "GET", "/user2/repo20.git")
  117. resp := session.MakeRequest(t, req, http.StatusOK)
  118. htmlDoc := NewHTMLParser(t, resp.Body)
  119. files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR > TD.name > SPAN")
  120. items := files.Map(func(i int, s *goquery.Selection) string {
  121. cls, _ := s.Find("SVG").Attr("class")
  122. file := strings.Trim(s.Find("A").Text(), " \t\n")
  123. return fmt.Sprintf("%s: %s", file, cls)
  124. })
  125. assert.Equal(t, len(items), 5)
  126. assert.Equal(t, items[0], "a: svg octicon-file-directory")
  127. assert.Equal(t, items[1], "link_b: svg octicon-file-symlink-directory")
  128. assert.Equal(t, items[2], "link_d: svg octicon-file-symlink-file")
  129. assert.Equal(t, items[3], "link_hi: svg octicon-file-symlink-file")
  130. assert.Equal(t, items[4], "link_link: svg octicon-file-symlink-file")
  131. }
  132. // TestViewAsRepoAdmin tests PR #2167
  133. func TestViewAsRepoAdmin(t *testing.T) {
  134. for user, expectedNoDescription := range map[string]bool{
  135. "user2": true,
  136. "user4": false,
  137. } {
  138. defer prepareTestEnv(t)()
  139. session := loginUser(t, user)
  140. req := NewRequest(t, "GET", "/user2/repo1.git")
  141. resp := session.MakeRequest(t, req, http.StatusOK)
  142. htmlDoc := NewHTMLParser(t, resp.Body)
  143. noDescription := htmlDoc.doc.Find("#repo-desc").Children()
  144. assert.Equal(t, expectedNoDescription, noDescription.HasClass("no-description"))
  145. }
  146. }