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
5package integrations
6
7import (
8	"fmt"
9	"net/http"
10	"path"
11	"testing"
12
13	"code.gitea.io/gitea/modules/setting"
14	api "code.gitea.io/gitea/modules/structs"
15	"code.gitea.io/gitea/modules/test"
16
17	"github.com/stretchr/testify/assert"
18)
19
20func TestLinksNoLogin(t *testing.T) {
21	defer prepareTestEnv(t)()
22
23	var links = []string{
24		"/explore/repos",
25		"/explore/repos?q=test&tab=",
26		"/explore/users",
27		"/explore/users?q=test&tab=",
28		"/explore/organizations",
29		"/explore/organizations?q=test&tab=",
30		"/",
31		"/user/sign_up",
32		"/user/login",
33		"/user/forgot_password",
34		"/api/swagger",
35		"/user2/repo1",
36		"/user2/repo1/",
37		"/user2/repo1/projects",
38		"/user2/repo1/projects/1",
39		"/assets/img/404.png",
40		"/assets/img/500.png",
41	}
42
43	for _, link := range links {
44		req := NewRequest(t, "GET", link)
45		MakeRequest(t, req, http.StatusOK)
46	}
47}
48
49func TestRedirectsNoLogin(t *testing.T) {
50	defer prepareTestEnv(t)()
51
52	var redirects = map[string]string{
53		"/user2/repo1/commits/master":                "/user2/repo1/commits/branch/master",
54		"/user2/repo1/src/master":                    "/user2/repo1/src/branch/master",
55		"/user2/repo1/src/master/file.txt":           "/user2/repo1/src/branch/master/file.txt",
56		"/user2/repo1/src/master/directory/file.txt": "/user2/repo1/src/branch/master/directory/file.txt",
57		"/user/avatar/Ghost/-1":                      "/assets/img/avatar_default.png",
58		"/api/v1/swagger":                            "/api/swagger",
59	}
60	for link, redirectLink := range redirects {
61		req := NewRequest(t, "GET", link)
62		resp := MakeRequest(t, req, http.StatusFound)
63		assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
64	}
65}
66
67func TestNoLoginNotExist(t *testing.T) {
68	defer prepareTestEnv(t)()
69
70	var links = []string{
71		"/user5/repo4/projects",
72		"/user5/repo4/projects/3",
73	}
74
75	for _, link := range links {
76		req := NewRequest(t, "GET", link)
77		MakeRequest(t, req, http.StatusNotFound)
78	}
79}
80
81func testLinksAsUser(userName string, t *testing.T) {
82	var links = []string{
83		"/explore/repos",
84		"/explore/repos?q=test&tab=",
85		"/explore/users",
86		"/explore/users?q=test&tab=",
87		"/explore/organizations",
88		"/explore/organizations?q=test&tab=",
89		"/",
90		"/user/forgot_password",
91		"/api/swagger",
92		"/issues",
93		"/issues?type=your_repositories&repos=[0]&sort=&state=open",
94		"/issues?type=assigned&repos=[0]&sort=&state=open",
95		"/issues?type=your_repositories&repos=[0]&sort=&state=closed",
96		"/issues?type=assigned&repos=[]&sort=&state=closed",
97		"/issues?type=assigned&sort=&state=open",
98		"/issues?type=created_by&repos=[1,2]&sort=&state=closed",
99		"/issues?type=created_by&repos=[1,2]&sort=&state=open",
100		"/pulls",
101		"/pulls?type=your_repositories&repos=[2]&sort=&state=open",
102		"/pulls?type=assigned&repos=[]&sort=&state=open",
103		"/pulls?type=created_by&repos=[0]&sort=&state=open",
104		"/pulls?type=your_repositories&repos=[0]&sort=&state=closed",
105		"/pulls?type=assigned&repos=[0]&sort=&state=closed",
106		"/pulls?type=created_by&repos=[0]&sort=&state=closed",
107		"/milestones",
108		"/milestones?sort=mostcomplete&state=closed",
109		"/milestones?type=your_repositories&sort=mostcomplete&state=closed",
110		"/milestones?sort=&repos=[1]&state=closed",
111		"/milestones?sort=&repos=[1]&state=open",
112		"/milestones?repos=[0]&sort=mostissues&state=open",
113		"/notifications",
114		"/repo/create",
115		"/repo/migrate",
116		"/org/create",
117		"/user2",
118		"/user2?tab=stars",
119		"/user2?tab=activity",
120		"/user/settings",
121		"/user/settings/account",
122		"/user/settings/security",
123		"/user/settings/security/two_factor/enroll",
124		"/user/settings/keys",
125		"/user/settings/organization",
126		"/user/settings/repos",
127	}
128
129	session := loginUser(t, userName)
130	for _, link := range links {
131		req := NewRequest(t, "GET", link)
132		session.MakeRequest(t, req, http.StatusOK)
133	}
134
135	reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
136	respAPI := MakeRequest(t, reqAPI, http.StatusOK)
137
138	var apiRepos []*api.Repository
139	DecodeJSON(t, respAPI, &apiRepos)
140
141	var repoLinks = []string{
142		"",
143		"/issues",
144		"/pulls",
145		"/commits/branch/master",
146		"/graph",
147		"/settings",
148		"/settings/collaboration",
149		"/settings/branches",
150		"/settings/hooks",
151		// FIXME: below links should return 200 but 404 ??
152		//"/settings/hooks/git",
153		//"/settings/hooks/git/pre-receive",
154		//"/settings/hooks/git/update",
155		//"/settings/hooks/git/post-receive",
156		"/settings/keys",
157		"/releases",
158		"/releases/new",
159		//"/wiki/_pages",
160		"/wiki/?action=_new",
161	}
162
163	for _, repo := range apiRepos {
164		for _, link := range repoLinks {
165			req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s%s", userName, repo.Name, link))
166			session.MakeRequest(t, req, http.StatusOK)
167		}
168	}
169}
170
171func TestLinksLogin(t *testing.T) {
172	defer prepareTestEnv(t)()
173
174	testLinksAsUser("user2", t)
175}
176