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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. commitT := time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).In(time.Local).Format(time.RFC1123)
  56. assert.EqualValues(t, []file{
  57. {
  58. fileName: "doc",
  59. commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
  60. commitMsg: "init project",
  61. commitTime: commitT,
  62. },
  63. {
  64. fileName: "README.md",
  65. commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
  66. commitMsg: "init project",
  67. commitTime: commitT,
  68. },
  69. }, items)
  70. }
  71. func TestViewRepo2(t *testing.T) {
  72. // no last commit cache
  73. testViewRepo(t)
  74. // enable last commit cache for all repositories
  75. oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
  76. setting.CacheService.LastCommit.CommitsCount = 0
  77. // first view will not hit the cache
  78. testViewRepo(t)
  79. // second view will hit the cache
  80. testViewRepo(t)
  81. setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
  82. }
  83. func TestViewRepo3(t *testing.T) {
  84. defer prepareTestEnv(t)()
  85. req := NewRequest(t, "GET", "/user3/repo3")
  86. session := loginUser(t, "user4")
  87. session.MakeRequest(t, req, http.StatusOK)
  88. }
  89. func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
  90. defer prepareTestEnv(t)()
  91. req := NewRequest(t, "GET", "/user2/repo1")
  92. resp := MakeRequest(t, req, http.StatusOK)
  93. htmlDoc := NewHTMLParser(t, resp.Body)
  94. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  95. assert.True(t, exists, "The template has changed")
  96. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  97. _, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  98. assert.False(t, exists)
  99. }
  100. func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
  101. defer prepareTestEnv(t)()
  102. session := loginUser(t, "user2")
  103. req := NewRequest(t, "GET", "/user2/repo1")
  104. resp := session.MakeRequest(t, req, http.StatusOK)
  105. htmlDoc := NewHTMLParser(t, resp.Body)
  106. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  107. assert.True(t, exists, "The template has changed")
  108. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  109. link, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  110. assert.True(t, exists, "The template has changed")
  111. sshURL := fmt.Sprintf("ssh://%s@%s:%d/user2/repo1.git", setting.SSH.BuiltinServerUser, setting.SSH.Domain, setting.SSH.Port)
  112. assert.Equal(t, sshURL, link)
  113. }
  114. func TestViewRepoWithSymlinks(t *testing.T) {
  115. defer prepareTestEnv(t)()
  116. session := loginUser(t, "user2")
  117. req := NewRequest(t, "GET", "/user2/repo20.git")
  118. resp := session.MakeRequest(t, req, http.StatusOK)
  119. htmlDoc := NewHTMLParser(t, resp.Body)
  120. files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR > TD.name > SPAN")
  121. items := files.Map(func(i int, s *goquery.Selection) string {
  122. cls, _ := s.Find("SVG").Attr("class")
  123. file := strings.Trim(s.Find("A").Text(), " \t\n")
  124. return fmt.Sprintf("%s: %s", file, cls)
  125. })
  126. assert.Equal(t, len(items), 5)
  127. assert.Equal(t, items[0], "a: svg octicon-file-directory")
  128. assert.Equal(t, items[1], "link_b: svg octicon-file-symlink-directory")
  129. assert.Equal(t, items[2], "link_d: svg octicon-file-symlink-file")
  130. assert.Equal(t, items[3], "link_hi: svg octicon-file-symlink-file")
  131. assert.Equal(t, items[4], "link_link: svg octicon-file-symlink-file")
  132. }
  133. // TestViewAsRepoAdmin tests PR #2167
  134. func TestViewAsRepoAdmin(t *testing.T) {
  135. for user, expectedNoDescription := range map[string]bool{
  136. "user2": true,
  137. "user4": false,
  138. } {
  139. defer prepareTestEnv(t)()
  140. session := loginUser(t, user)
  141. req := NewRequest(t, "GET", "/user2/repo1.git")
  142. resp := session.MakeRequest(t, req, http.StatusOK)
  143. htmlDoc := NewHTMLParser(t, resp.Body)
  144. noDescription := htmlDoc.doc.Find("#repo-desc").Children()
  145. assert.Equal(t, expectedNoDescription, noDescription.HasClass("no-description"))
  146. }
  147. }