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.

150 lines
4.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. "path"
  9. "testing"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/test"
  12. api "code.gitea.io/sdk/gitea"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestLinksNoLogin(t *testing.T) {
  16. prepareTestEnv(t)
  17. var links = []string{
  18. "/explore/repos",
  19. "/explore/repos?q=test&tab=",
  20. "/explore/users",
  21. "/explore/users?q=test&tab=",
  22. "/explore/organizations",
  23. "/explore/organizations?q=test&tab=",
  24. "/",
  25. "/user/sign_up",
  26. "/user/login",
  27. "/user/forgot_password",
  28. "/api/swagger",
  29. "/api/v1/swagger",
  30. // TODO: follow this page and test every link
  31. "/vendor/librejs.html",
  32. }
  33. for _, link := range links {
  34. req := NewRequest(t, "GET", link)
  35. MakeRequest(t, req, http.StatusOK)
  36. }
  37. }
  38. func TestRedirectsNoLogin(t *testing.T) {
  39. prepareTestEnv(t)
  40. var redirects = map[string]string{
  41. "/user2/repo1/commits/master": "/user2/repo1/commits/branch/master",
  42. "/user2/repo1/src/master": "/user2/repo1/src/branch/master",
  43. "/user2/repo1/src/master/file.txt": "/user2/repo1/src/branch/master/file.txt",
  44. "/user2/repo1/src/master/directory/file.txt": "/user2/repo1/src/branch/master/directory/file.txt",
  45. }
  46. for link, redirectLink := range redirects {
  47. req := NewRequest(t, "GET", link)
  48. resp := MakeRequest(t, req, http.StatusFound)
  49. assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
  50. }
  51. }
  52. func testLinksAsUser(userName string, t *testing.T) {
  53. var links = []string{
  54. "/explore/repos",
  55. "/explore/repos?q=test&tab=",
  56. "/explore/users",
  57. "/explore/users?q=test&tab=",
  58. "/explore/organizations",
  59. "/explore/organizations?q=test&tab=",
  60. "/",
  61. "/user/forgot_password",
  62. "/api/swagger",
  63. "/api/v1/swagger",
  64. "/issues",
  65. "/issues?type=your_repositories&repo=0&sort=&state=open",
  66. "/issues?type=assigned&repo=0&sort=&state=open",
  67. "/issues?type=created_by&repo=0&sort=&state=open",
  68. "/issues?type=your_repositories&repo=0&sort=&state=closed",
  69. "/issues?type=assigned&repo=0&sort=&state=closed",
  70. "/issues?type=created_by&repo=0&sort=&state=closed",
  71. "/pulls",
  72. "/pulls?type=your_repositories&repo=0&sort=&state=open",
  73. "/pulls?type=assigned&repo=0&sort=&state=open",
  74. "/pulls?type=created_by&repo=0&sort=&state=open",
  75. "/pulls?type=your_repositories&repo=0&sort=&state=closed",
  76. "/pulls?type=assigned&repo=0&sort=&state=closed",
  77. "/pulls?type=created_by&repo=0&sort=&state=closed",
  78. "/notifications",
  79. "/repo/create",
  80. "/repo/migrate",
  81. "/org/create",
  82. "/user2",
  83. "/user2?tab=stars",
  84. "/user2?tab=activity",
  85. "/user/settings",
  86. "/user/settings/account",
  87. "/user/settings/security",
  88. "/user/settings/security/two_factor/enroll",
  89. "/user/settings/keys",
  90. "/user/settings/organization",
  91. "/user/settings/repos",
  92. }
  93. session := loginUser(t, userName)
  94. for _, link := range links {
  95. req := NewRequest(t, "GET", link)
  96. session.MakeRequest(t, req, http.StatusOK)
  97. }
  98. reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
  99. respAPI := MakeRequest(t, reqAPI, http.StatusOK)
  100. var apiRepos []api.Repository
  101. DecodeJSON(t, respAPI, &apiRepos)
  102. var repoLinks = []string{
  103. "",
  104. "/issues",
  105. "/pulls",
  106. "/commits/branch/master",
  107. "/graph",
  108. "/settings",
  109. "/settings/collaboration",
  110. "/settings/branches",
  111. "/settings/hooks",
  112. // FIXME: below links should return 200 but 404 ??
  113. //"/settings/hooks/git",
  114. //"/settings/hooks/git/pre-receive",
  115. //"/settings/hooks/git/update",
  116. //"/settings/hooks/git/post-receive",
  117. "/settings/keys",
  118. "/releases",
  119. "/releases/new",
  120. //"/wiki/_pages",
  121. "/wiki/_new",
  122. }
  123. for _, repo := range apiRepos {
  124. for _, link := range repoLinks {
  125. req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s%s", userName, repo.Name, link))
  126. session.MakeRequest(t, req, http.StatusOK)
  127. }
  128. }
  129. }
  130. func TestLinksLogin(t *testing.T) {
  131. prepareTestEnv(t)
  132. testLinksAsUser("user2", t)
  133. }