aboutsummaryrefslogtreecommitdiffstats
path: root/services/wiki/wiki_test.go
blob: f126224244a1ca6b226accec0f0e32487fe6442e (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package wiki

import (
	"math/rand"
	"path/filepath"
	"strings"
	"testing"

	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/git"

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

func TestMain(m *testing.M) {
	unittest.MainTest(m, &unittest.TestOptions{
		GiteaRootPath: filepath.Join("..", ".."),
	})
}

func TestWebPathSegments(t *testing.T) {
	a := WebPathSegments("a%2Fa/b+c/d-e/f-g.-")
	assert.EqualValues(t, []string{"a/a", "b c", "d e", "f-g"}, a)
}

func TestUserTitleToWebPath(t *testing.T) {
	type test struct {
		Expected  string
		UserTitle string
	}
	for _, test := range []test{
		{"unnamed", ""},
		{"unnamed", "."},
		{"unnamed", ".."},
		{"wiki-name", "wiki name"},
		{"title.md.-", "title.md"},
		{"wiki-name.-", "wiki-name"},
		{"the+wiki-name.-", "the wiki-name"},
		{"a%2Fb", "a/b"},
		{"a%25b", "a%b"},
	} {
		assert.EqualValues(t, test.Expected, UserTitleToWebPath("", test.UserTitle))
	}
}

func TestWebPathToDisplayName(t *testing.T) {
	type test struct {
		Expected string
		WebPath  WebPath
	}
	for _, test := range []test{
		{"wiki name", "wiki-name"},
		{"wiki-name", "wiki-name.-"},
		{"name with / slash", "name-with %2F slash"},
		{"name with % percent", "name-with %25 percent"},
		{"2000-01-02 meeting", "2000-01-02+meeting.-.md"},
	} {
		_, displayName := WebPathToUserTitle(test.WebPath)
		assert.EqualValues(t, test.Expected, displayName)
	}
}

func TestWebPathToGitPath(t *testing.T) {
	type test struct {
		Expected string
		WikiName WebPath
	}
	for _, test := range []test{
		{"wiki-name.md", "wiki%20name"},
		{"wiki-name.md", "wiki+name"},
		{"wiki%20name.md", "wiki%20name.md"},
		{"2000-01-02-meeting.md", "2000-01-02+meeting"},
		{"2000-01-02 meeting.-.md", "2000-01-02%20meeting.-"},
	} {
		assert.EqualValues(t, test.Expected, WebPathToGitPath(test.WikiName))
	}
}

func TestGitPathToWebPath(t *testing.T) {
	type test struct {
		Expected string
		Filename string
	}
	for _, test := range []test{
		{"hello-world", "hello-world.md"}, // this shouldn't happen, because it should always have a ".-" suffix
		{"hello-world", "hello world.md"},
		{"hello-world.-", "hello-world.-.md"},
		{"hello+world.-", "hello world.-.md"},
		{"symbols-%2F", "symbols %2F.md"},
	} {
		name, err := GitPathToWebPath(test.Filename)
		assert.NoError(t, err)
		assert.EqualValues(t, test.Expected, name)
	}
	for _, badFilename := range []string{
		"nofileextension",
		"wrongfileextension.txt",
	} {
		_, err := GitPathToWebPath(badFilename)
		assert.Error(t, err)
		assert.True(t, repo_model.IsErrWikiInvalidFileName(err))
	}
	_, err := GitPathToWebPath("badescaping%%.md")
	assert.Error(t, err)
	assert.False(t, repo_model.IsErrWikiInvalidFileName(err))
}

func TestUserWebGitPathConsistency(t *testing.T) {
	maxLen := 20
	b := make([]byte, maxLen)
	for i := 0; i < 1000; i++ {
		l := rand.Intn(maxLen)
		for j := 0; j < l; j++ {
			r := rand.Intn(0x80-0x20) + 0x20
			b[j] = byte(r)
		}

		userTitle := strings.TrimSpace(string(b[:l]))
		if userTitle == "" || userTitle == "." || userTitle == ".." {
			continue
		}
		webPath := UserTitleToWebPath("", userTitle)
		gitPath := WebPathToGitPath(webPath)

		webPath1, _ := GitPathToWebPath(gitPath)
		_, userTitle1 := WebPathToUserTitle(webPath1)
		gitPath1 := WebPathToGitPath(webPath1)

		assert.EqualValues(t, userTitle, userTitle1, "UserTitle for userTitle: %q", userTitle)
		assert.EqualValues(t, webPath, webPath1, "WebPath for userTitle: %q", userTitle)
		assert.EqualValues(t, gitPath, gitPath1, "GitPath for userTitle: %q", userTitle)
	}
}

func TestRepository_InitWiki(t *testing.T) {
	unittest.PrepareTestEnv(t)
	// repo1 already has a wiki
	repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	assert.NoError(t, InitWiki(git.DefaultContext, repo1))

	// repo2 does not already have a wiki
	repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
	assert.NoError(t, InitWiki(git.DefaultContext, repo2))
	assert.True(t, repo2.HasWiki())
}

func TestRepository_AddWikiPage(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())
	const wikiContent = "This is the wiki content"
	const commitMsg = "Commit message"
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	for _, userTitle := range []string{
		"Another page",
		"Here's a <tag> and a/slash",
	} {
		t.Run("test wiki exist: "+userTitle, func(t *testing.T) {
			webPath := UserTitleToWebPath("", userTitle)
			assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, webPath, wikiContent, commitMsg))
			// Now need to show that the page has been added:
			gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
			assert.NoError(t, err)
			defer gitRepo.Close()
			masterTree, err := gitRepo.GetTree(DefaultBranch)
			assert.NoError(t, err)
			gitPath := WebPathToGitPath(webPath)
			entry, err := masterTree.GetTreeEntryByPath(gitPath)
			assert.NoError(t, err)
			assert.EqualValues(t, gitPath, entry.Name(), "%s not added correctly", userTitle)
		})
	}

	t.Run("check wiki already exist", func(t *testing.T) {
		t.Parallel()
		// test for already-existing wiki name
		err := AddWikiPage(git.DefaultContext, doer, repo, "Home", wikiContent, commitMsg)
		assert.Error(t, err)
		assert.True(t, repo_model.IsErrWikiAlreadyExist(err))
	})

	t.Run("check wiki reserved name", func(t *testing.T) {
		t.Parallel()
		// test for reserved wiki name
		err := AddWikiPage(git.DefaultContext, doer, repo, "_edit", wikiContent, commitMsg)
		assert.Error(t, err)
		assert.True(t, repo_model.IsErrWikiReservedName(err))
	})
}

func TestRepository_EditWikiPage(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())

	const newWikiContent = "This is the new content"
	const commitMsg = "Commit message"
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	for _, newWikiName := range []string{
		"Home", // same name as before
		"New home",
		"New/name/with/slashes",
	} {
		webPath := UserTitleToWebPath("", newWikiName)
		unittest.PrepareTestEnv(t)
		assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", webPath, newWikiContent, commitMsg))

		// Now need to show that the page has been added:
		gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
		assert.NoError(t, err)
		masterTree, err := gitRepo.GetTree(DefaultBranch)
		assert.NoError(t, err)
		gitPath := WebPathToGitPath(webPath)
		entry, err := masterTree.GetTreeEntryByPath(gitPath)
		assert.NoError(t, err)
		assert.EqualValues(t, gitPath, entry.Name(), "%s not edited correctly", newWikiName)

		if newWikiName != "Home" {
			_, err := masterTree.GetTreeEntryByPath("Home.md")
			assert.Error(t, err)
		}
		gitRepo.Close()
	}
}

func TestRepository_DeleteWikiPage(t *testing.T) {
	unittest.PrepareTestEnv(t)
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home"))

	// Now need to show that the page has been added:
	gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
	assert.NoError(t, err)
	defer gitRepo.Close()
	masterTree, err := gitRepo.GetTree(DefaultBranch)
	assert.NoError(t, err)
	gitPath := WebPathToGitPath("Home")
	_, err = masterTree.GetTreeEntryByPath(gitPath)
	assert.Error(t, err)
}

func TestPrepareWikiFileName(t *testing.T) {
	unittest.PrepareTestEnv(t)
	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
	gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
	defer gitRepo.Close()
	assert.NoError(t, err)

	tests := []struct {
		name      string
		arg       string
		existence bool
		wikiPath  string
		wantErr   bool
	}{{
		name:      "add suffix",
		arg:       "Home",
		existence: true,
		wikiPath:  "Home.md",
		wantErr:   false,
	}, {
		name:      "test special chars",
		arg:       "home of and & or wiki page!",
		existence: false,
		wikiPath:  "home-of-and-%26-or-wiki-page%21.md",
		wantErr:   false,
	}}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			webPath := UserTitleToWebPath("", tt.arg)
			existence, newWikiPath, err := prepareGitPath(gitRepo, webPath)
			if (err != nil) != tt.wantErr {
				assert.NoError(t, err)
				return
			}
			if existence != tt.existence {
				if existence {
					t.Errorf("expect to find no escaped file but we detect one")
				} else {
					t.Errorf("expect to find an escaped file but we could not detect one")
				}
			}
			assert.EqualValues(t, tt.wikiPath, newWikiPath)
		})
	}
}

func TestPrepareWikiFileName_FirstPage(t *testing.T) {
	unittest.PrepareTestEnv(t)

	// Now create a temporaryDirectory
	tmpDir := t.TempDir()

	err := git.InitRepository(git.DefaultContext, tmpDir, true)
	assert.NoError(t, err)

	gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir)
	defer gitRepo.Close()
	assert.NoError(t, err)

	existence, newWikiPath, err := prepareGitPath(gitRepo, "Home")
	assert.False(t, existence)
	assert.NoError(t, err)
	assert.EqualValues(t, "Home.md", newWikiPath)
}

func TestWebPathConversion(t *testing.T) {
	assert.Equal(t, "path/wiki", WebPathToURLPath(WebPath("path/wiki")))
	assert.Equal(t, "wiki", WebPathToURLPath(WebPath("wiki")))
	assert.Equal(t, "", WebPathToURLPath(WebPath("")))
}

func TestWebPathFromRequest(t *testing.T) {
	assert.Equal(t, WebPath("a%2Fb"), WebPathFromRequest("a/b"))
	assert.Equal(t, WebPath("a"), WebPathFromRequest("a"))
	assert.Equal(t, WebPath("b"), WebPathFromRequest("a/../b"))
}