summaryrefslogtreecommitdiffstats
path: root/tests/integration/view_test.go
blob: ff2f2bdfd0186028ec9167bb8866917fe55df017 (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
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"testing"

	unit_model "code.gitea.io/gitea/models/unit"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/test"
	files_service "code.gitea.io/gitea/services/repository/files"
	"code.gitea.io/gitea/tests"

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

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

	session := loginUser(t, "user2")

	req := NewRequest(t, "GET", "/user2/repo2/src/branch/master/line.svg")
	resp := session.MakeRequest(t, req, http.StatusOK)

	doc := NewHTMLParser(t, resp.Body)
	src, exists := doc.doc.Find(".file-view img").Attr("src")
	assert.True(t, exists, "The SVG image should be in an <img> tag so that scripts in the SVG are not run")
	assert.Equal(t, "/user2/repo2/raw/branch/master/line.svg", src)
}

func TestAmbiguousCharacterDetection(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
		session := loginUser(t, user2.Name)

		// Prepare the environments. File view, commit view (diff), wiki page.
		repo, commitID, f := tests.CreateDeclarativeRepo(t, user2, "",
			[]unit_model.Type{unit_model.TypeCode, unit_model.TypeWiki}, nil,
			[]*files_service.ChangeRepoFile{
				{
					Operation:     "create",
					TreePath:      "test.sh",
					ContentReader: strings.NewReader("Hello there!\nline western"),
				},
			},
		)
		defer f()

		req := NewRequestWithValues(t, "POST", repo.Link()+"/wiki?action=new", map[string]string{
			"_csrf":   GetCSRF(t, session, repo.Link()+"/wiki?action=new"),
			"title":   "Normal",
			"content": "Hello – Hello",
		})
		session.MakeRequest(t, req, http.StatusSeeOther)

		assertCase := func(t *testing.T, fileContext, commitContext, wikiContext bool) {
			t.Helper()

			t.Run("File context", func(t *testing.T) {
				defer tests.PrintCurrentTest(t)()

				req := NewRequest(t, "GET", repo.Link()+"/src/branch/main/test.sh")
				resp := session.MakeRequest(t, req, http.StatusOK)

				htmlDoc := NewHTMLParser(t, resp.Body)
				htmlDoc.AssertElement(t, ".unicode-escape-prompt", fileContext)
			})
			t.Run("Commit context", func(t *testing.T) {
				defer tests.PrintCurrentTest(t)()

				req := NewRequest(t, "GET", repo.Link()+"/commit/"+commitID)
				resp := session.MakeRequest(t, req, http.StatusOK)

				htmlDoc := NewHTMLParser(t, resp.Body)
				htmlDoc.AssertElement(t, ".lines-escape .toggle-escape-button", commitContext)
			})
			t.Run("Wiki context", func(t *testing.T) {
				defer tests.PrintCurrentTest(t)()

				req := NewRequest(t, "GET", repo.Link()+"/wiki/Normal")
				resp := session.MakeRequest(t, req, http.StatusOK)

				htmlDoc := NewHTMLParser(t, resp.Body)
				htmlDoc.AssertElement(t, ".unicode-escape-prompt", wikiContext)
			})
		}

		t.Run("Enabled all context", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{})()

			assertCase(t, true, true, true)
		})

		t.Run("Enabled file context", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{"diff", "wiki"})()

			assertCase(t, true, false, false)
		})

		t.Run("Enabled commit context", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{"file-view", "wiki"})()

			assertCase(t, false, true, false)
		})

		t.Run("Enabled wiki context", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{"file-view", "diff"})()

			assertCase(t, false, false, true)
		})

		t.Run("No context", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{"file-view", "wiki", "diff"})()

			assertCase(t, false, false, false)
		})

		t.Run("Disabled detection", func(t *testing.T) {
			defer test.MockVariableValue(&setting.UI.SkipEscapeContexts, []string{})()
			defer test.MockVariableValue(&setting.UI.AmbiguousUnicodeDetection, false)()

			assertCase(t, false, false, false)
		})
	})
}

func TestInHistoryButton(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
		session := loginUser(t, user2.Name)
		repo, commitID, f := tests.CreateDeclarativeRepo(t, user2, "",
			[]unit_model.Type{unit_model.TypeCode, unit_model.TypeWiki}, nil,
			[]*files_service.ChangeRepoFile{
				{
					Operation:     "create",
					TreePath:      "test.sh",
					ContentReader: strings.NewReader("Hello there!"),
				},
			},
		)
		defer f()

		req := NewRequestWithValues(t, "POST", repo.Link()+"/wiki?action=new", map[string]string{
			"_csrf":   GetCSRF(t, session, repo.Link()+"/wiki?action=new"),
			"title":   "Normal",
			"content": "Hello world!",
		})
		session.MakeRequest(t, req, http.StatusSeeOther)

		t.Run("Wiki revision", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", repo.Link()+"/wiki/Normal?action=_revision")
			resp := session.MakeRequest(t, req, http.StatusOK)
			htmlDoc := NewHTMLParser(t, resp.Body)

			htmlDoc.AssertElement(t, fmt.Sprintf(".commit-list a[href^='/%s/src/commit/']", repo.FullName()), false)
		})

		t.Run("Commit list", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", repo.Link()+"/commits/branch/main")
			resp := session.MakeRequest(t, req, http.StatusOK)
			htmlDoc := NewHTMLParser(t, resp.Body)

			htmlDoc.AssertElement(t, fmt.Sprintf(".commit-list a[href='/%s/src/commit/%s']", repo.FullName(), commitID), true)
		})

		t.Run("File history", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", repo.Link()+"/commits/branch/main/test.sh")
			resp := session.MakeRequest(t, req, http.StatusOK)
			htmlDoc := NewHTMLParser(t, resp.Body)

			htmlDoc.AssertElement(t, fmt.Sprintf(".commit-list a[href='/%s/src/commit/%s/test.sh']", repo.FullName(), commitID), true)
		})
	})
}

func TestTitleDisplayName(t *testing.T) {
	session := emptyTestSession(t)
	title := GetHTMLTitle(t, session, "/")
	assert.Equal(t, "Forgejo: Beyond coding. We Forge.", title)
}

func TestHomeDisplayName(t *testing.T) {
	session := emptyTestSession(t)
	req := NewRequest(t, "GET", "/")
	resp := session.MakeRequest(t, req, http.StatusOK)
	htmlDoc := NewHTMLParser(t, resp.Body)
	assert.Equal(t, "Forgejo: Beyond coding. We Forge.", strings.TrimSpace(htmlDoc.Find("h1.title").Text()))
}

func TestOpenGraphDisplayName(t *testing.T) {
	session := emptyTestSession(t)
	req := NewRequest(t, "GET", "/")
	resp := session.MakeRequest(t, req, http.StatusOK)
	htmlDoc := NewHTMLParser(t, resp.Body)
	ogTitle, _ := htmlDoc.Find("meta[property='og:title']").Attr("content")
	assert.Equal(t, "Forgejo: Beyond coding. We Forge.", ogTitle)
	ogSiteName, _ := htmlDoc.Find("meta[property='og:site_name']").Attr("content")
	assert.Equal(t, "Forgejo: Beyond coding. We Forge.", ogSiteName)
}