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.

101 lines
3.0 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. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/PuerkitoBio/goquery"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestViewRepo(t *testing.T) {
  15. prepareTestEnv(t)
  16. req := NewRequest(t, "GET", "/user2/repo1")
  17. MakeRequest(t, req, http.StatusOK)
  18. req = NewRequest(t, "GET", "/user3/repo3")
  19. MakeRequest(t, req, http.StatusNotFound)
  20. session := loginUser(t, "user1")
  21. session.MakeRequest(t, req, http.StatusNotFound)
  22. }
  23. func TestViewRepo2(t *testing.T) {
  24. prepareTestEnv(t)
  25. req := NewRequest(t, "GET", "/user3/repo3")
  26. session := loginUser(t, "user2")
  27. session.MakeRequest(t, req, http.StatusOK)
  28. }
  29. func TestViewRepo3(t *testing.T) {
  30. prepareTestEnv(t)
  31. req := NewRequest(t, "GET", "/user3/repo3")
  32. session := loginUser(t, "user4")
  33. session.MakeRequest(t, req, http.StatusOK)
  34. }
  35. func TestViewRepo1CloneLinkAnonymous(t *testing.T) {
  36. prepareTestEnv(t)
  37. req := NewRequest(t, "GET", "/user2/repo1")
  38. resp := MakeRequest(t, req, http.StatusOK)
  39. htmlDoc := NewHTMLParser(t, resp.Body)
  40. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  41. assert.True(t, exists, "The template has changed")
  42. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  43. _, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  44. assert.False(t, exists)
  45. }
  46. func TestViewRepo1CloneLinkAuthorized(t *testing.T) {
  47. prepareTestEnv(t)
  48. session := loginUser(t, "user2")
  49. req := NewRequest(t, "GET", "/user2/repo1")
  50. resp := session.MakeRequest(t, req, http.StatusOK)
  51. htmlDoc := NewHTMLParser(t, resp.Body)
  52. link, exists := htmlDoc.doc.Find("#repo-clone-https").Attr("data-link")
  53. assert.True(t, exists, "The template has changed")
  54. assert.Equal(t, setting.AppURL+"user2/repo1.git", link)
  55. link, exists = htmlDoc.doc.Find("#repo-clone-ssh").Attr("data-link")
  56. assert.True(t, exists, "The template has changed")
  57. sshURL := fmt.Sprintf("ssh://%s@%s:%d/user2/repo1.git", setting.RunUser, setting.SSH.Domain, setting.SSH.Port)
  58. assert.Equal(t, sshURL, link)
  59. }
  60. func TestViewRepoWithSymlinks(t *testing.T) {
  61. prepareTestEnv(t)
  62. session := loginUser(t, "user2")
  63. req := NewRequest(t, "GET", "/user2/repo20.git")
  64. resp := session.MakeRequest(t, req, http.StatusOK)
  65. htmlDoc := NewHTMLParser(t, resp.Body)
  66. files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR > TD.name")
  67. items := files.Map(func(i int, s *goquery.Selection) string {
  68. cls, _ := s.Find("SPAN").Attr("class")
  69. file := strings.Trim(s.Find("A").Text(), " \t\n")
  70. return fmt.Sprintf("%s: %s", file, cls)
  71. })
  72. assert.Equal(t, len(items), 5)
  73. assert.Equal(t, items[0], "a: octicon octicon-file-directory")
  74. assert.Equal(t, items[1], "link_b: octicon octicon-file-symlink-directory")
  75. assert.Equal(t, items[2], "link_d: octicon octicon-file-symlink-file")
  76. assert.Equal(t, items[3], "link_hi: octicon octicon-file-symlink-file")
  77. assert.Equal(t, items[4], "link_link: octicon octicon-file-symlink-file")
  78. }