summaryrefslogtreecommitdiffstats
path: root/tests/integration/links_test.go
blob: e9ad933b24937b9232c73ed25ba24eef0e8dbb42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"path"
	"testing"

	"code.gitea.io/gitea/modules/setting"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/test"
	forgejo_context "code.gitea.io/gitea/services/context"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/assert"
)

func TestLinksNoLogin(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	links := []string{
		"/explore/repos",
		"/explore/repos?q=test",
		"/explore/users",
		"/explore/users?q=test",
		"/explore/organizations",
		"/explore/organizations?q=test",
		"/",
		"/user/sign_up",
		"/user/login",
		"/user/forgot_password",
		"/api/swagger",
		"/user2/repo1",
		"/user2/repo1/",
		"/user2/repo1/projects",
		"/user2/repo1/projects/1",
		"/.well-known/security.txt",
	}

	for _, link := range links {
		req := NewRequest(t, "GET", link)
		MakeRequest(t, req, http.StatusOK)
	}
}

func TestRedirectsNoLogin(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	redirects := map[string]string{
		"/user2/repo1/commits/master":                "/user2/repo1/commits/branch/master",
		"/user2/repo1/src/master":                    "/user2/repo1/src/branch/master",
		"/user2/repo1/src/master/file.txt":           "/user2/repo1/src/branch/master/file.txt",
		"/user2/repo1/src/master/directory/file.txt": "/user2/repo1/src/branch/master/directory/file.txt",
		"/user/avatar/Ghost/-1":                      "/assets/img/avatar_default.png",
		"/api/v1/swagger":                            "/api/swagger",
	}
	for link, redirectLink := range redirects {
		req := NewRequest(t, "GET", link)
		resp := MakeRequest(t, req, http.StatusSeeOther)
		assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
	}
}

func TestNoLoginNotExist(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	links := []string{
		"/user5/repo4/projects",
		"/user5/repo4/projects/3",
	}

	for _, link := range links {
		req := NewRequest(t, "GET", link)
		MakeRequest(t, req, http.StatusNotFound)
	}
}

func testLinksAsUser(userName string, t *testing.T) {
	links := []string{
		"/explore/repos",
		"/explore/repos?q=test",
		"/explore/users",
		"/explore/users?q=test",
		"/explore/organizations",
		"/explore/organizations?q=test",
		"/",
		"/user/forgot_password",
		"/api/swagger",
		"/issues",
		"/issues?type=your_repositories&repos=[0]&sort=&state=open",
		"/issues?type=assigned&repos=[0]&sort=&state=open",
		"/issues?type=your_repositories&repos=[0]&sort=&state=closed",
		"/issues?type=assigned&repos=[]&sort=&state=closed",
		"/issues?type=assigned&sort=&state=open",
		"/issues?type=created_by&repos=[1,2]&sort=&state=closed",
		"/issues?type=created_by&repos=[1,2]&sort=&state=open",
		"/pulls",
		"/pulls?type=your_repositories&repos=[2]&sort=&state=open",
		"/pulls?type=assigned&repos=[]&sort=&state=open",
		"/pulls?type=created_by&repos=[0]&sort=&state=open",
		"/pulls?type=your_repositories&repos=[0]&sort=&state=closed",
		"/pulls?type=assigned&repos=[0]&sort=&state=closed",
		"/pulls?type=created_by&repos=[0]&sort=&state=closed",
		"/milestones",
		"/milestones?sort=mostcomplete&state=closed",
		"/milestones?type=your_repositories&sort=mostcomplete&state=closed",
		"/milestones?sort=&repos=[1]&state=closed",
		"/milestones?sort=&repos=[1]&state=open",
		"/milestones?repos=[0]&sort=mostissues&state=open",
		"/notifications",
		"/repo/create",
		"/repo/migrate",
		"/org/create",
		"/user2",
		"/user2?tab=stars",
		"/user2?tab=activity",
		"/user/settings",
		"/user/settings/account",
		"/user/settings/security",
		"/user/settings/security/two_factor/enroll",
		"/user/settings/keys",
		"/user/settings/organization",
		"/user/settings/repos",
	}

	session := loginUser(t, userName)
	for _, link := range links {
		req := NewRequest(t, "GET", link)
		session.MakeRequest(t, req, http.StatusOK)
	}

	reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
	respAPI := MakeRequest(t, reqAPI, http.StatusOK)

	var apiRepos []*api.Repository
	DecodeJSON(t, respAPI, &apiRepos)

	repoLinks := []string{
		"",
		"/issues",
		"/pulls",
		"/commits/branch/master",
		"/graph",
		"/settings",
		"/settings/collaboration",
		"/settings/branches",
		"/settings/hooks",
		// FIXME: below links should return 200 but 404 ??
		//"/settings/hooks/git",
		//"/settings/hooks/git/pre-receive",
		//"/settings/hooks/git/update",
		//"/settings/hooks/git/post-receive",
		"/settings/keys",
		"/releases",
		"/releases/new",
		//"/wiki/_pages",
		"/wiki/?action=_new",
		"/activity",
	}

	for _, repo := range apiRepos {
		for _, link := range repoLinks {
			req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s%s", userName, repo.Name, link))
			session.MakeRequest(t, req, http.StatusOK)
		}
	}
}

func TestLinksLogin(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	testLinksAsUser("user2", t)
}

func TestRedirectsWebhooks(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	//
	// A redirect means the route exists but not if it performs as intended.
	//
	for _, kind := range []string{"forgejo", "gitea"} {
		redirects := []struct {
			from string
			to   string
			verb string
		}{
			{from: "/user2/repo1/settings/hooks/" + kind + "/new", to: "/user/login", verb: "GET"},
			{from: "/user/settings/hooks/" + kind + "/new", to: "/user/login", verb: "GET"},
			{from: "/admin/system-hooks/" + kind + "/new", to: "/user/login", verb: "GET"},
			{from: "/admin/default-hooks/" + kind + "/new", to: "/user/login", verb: "GET"},
		}
		for _, info := range redirects {
			req := NewRequest(t, info.verb, info.from)
			resp := MakeRequest(t, req, http.StatusSeeOther)
			assert.EqualValues(t, path.Join(setting.AppSubURL, info.to), test.RedirectURL(resp), info.from)
		}
	}

	for _, kind := range []string{"forgejo", "gitea"} {
		csrf := []struct {
			from string
			verb string
		}{
			{from: "/user2/repo1/settings/hooks/" + kind + "/new", verb: "POST"},
			{from: "/admin/hooks/1", verb: "POST"},
			{from: "/admin/system-hooks/" + kind + "/new", verb: "POST"},
			{from: "/admin/default-hooks/" + kind + "/new", verb: "POST"},
			{from: "/user2/repo1/settings/hooks/1", verb: "POST"},
		}
		for _, info := range csrf {
			req := NewRequest(t, info.verb, info.from)
			resp := MakeRequest(t, req, http.StatusBadRequest)
			assert.Contains(t, resp.Body.String(), forgejo_context.CsrfErrorString)
		}
	}
}

func TestRepoLinks(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	// repo1 has enabled almost features, so we can test most links
	repoLink := "/user2/repo1"
	links := []string{
		"/actions",
		"/packages",
		"/projects",
	}

	// anonymous user
	for _, link := range links {
		req := NewRequest(t, "GET", repoLink+link)
		MakeRequest(t, req, http.StatusOK)
	}

	// admin/owner user
	session := loginUser(t, "user1")
	for _, link := range links {
		req := NewRequest(t, "GET", repoLink+link)
		session.MakeRequest(t, req, http.StatusOK)
	}

	// non-admin non-owner user
	session = loginUser(t, "user2")
	for _, link := range links {
		req := NewRequest(t, "GET", repoLink+link)
		session.MakeRequest(t, req, http.StatusOK)
	}
}