|
@@ -0,0 +1,171 @@
|
|
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
|
+
|
|
5
|
+package repo
|
|
6
|
+
|
|
7
|
+import (
|
|
8
|
+ "io/ioutil"
|
|
9
|
+ "net/http"
|
|
10
|
+ "path/filepath"
|
|
11
|
+ "testing"
|
|
12
|
+
|
|
13
|
+ "code.gitea.io/gitea/models"
|
|
14
|
+ "code.gitea.io/gitea/modules/auth"
|
|
15
|
+ "code.gitea.io/gitea/modules/test"
|
|
16
|
+
|
|
17
|
+ "github.com/Unknwon/com"
|
|
18
|
+ "github.com/stretchr/testify/assert"
|
|
19
|
+)
|
|
20
|
+
|
|
21
|
+const content = "Wiki contents for unit tests"
|
|
22
|
+const message = "Wiki commit message for unit tests"
|
|
23
|
+
|
|
24
|
+func wikiPath(repo *models.Repository, wikiName string) string {
|
|
25
|
+ return filepath.Join(repo.LocalWikiPath(), models.WikiNameToFilename(wikiName))
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string {
|
|
29
|
+ bytes, err := ioutil.ReadFile(wikiPath(repo, wikiName))
|
|
30
|
+ assert.NoError(t, err)
|
|
31
|
+ return string(bytes)
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
|
|
35
|
+ assert.True(t, com.IsExist(wikiPath(repo, wikiName)))
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
|
|
39
|
+ assert.False(t, com.IsExist(wikiPath(repo, wikiName)))
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
|
|
43
|
+ pageMetas, ok := metas.([]PageMeta)
|
|
44
|
+ if !assert.True(t, ok) {
|
|
45
|
+ return
|
|
46
|
+ }
|
|
47
|
+ if !assert.EqualValues(t, len(expectedNames), len(pageMetas)) {
|
|
48
|
+ return
|
|
49
|
+ }
|
|
50
|
+ for i, pageMeta := range pageMetas {
|
|
51
|
+ assert.EqualValues(t, expectedNames[i], pageMeta.Name)
|
|
52
|
+ }
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+func TestWiki(t *testing.T) {
|
|
56
|
+ models.PrepareTestEnv(t)
|
|
57
|
+
|
|
58
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
|
|
59
|
+ ctx.SetParams(":page", "Home")
|
|
60
|
+ test.LoadRepo(t, ctx, 1)
|
|
61
|
+ Wiki(ctx)
|
|
62
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
63
|
+ assert.EqualValues(t, "Home", ctx.Data["Title"])
|
|
64
|
+ assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+func TestWikiPages(t *testing.T) {
|
|
68
|
+ models.PrepareTestEnv(t)
|
|
69
|
+
|
|
70
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
|
|
71
|
+ test.LoadRepo(t, ctx, 1)
|
|
72
|
+ WikiPages(ctx)
|
|
73
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
74
|
+ assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+func TestNewWiki(t *testing.T) {
|
|
78
|
+ models.PrepareTestEnv(t)
|
|
79
|
+
|
|
80
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
|
81
|
+ test.LoadUser(t, ctx, 2)
|
|
82
|
+ test.LoadRepo(t, ctx, 1)
|
|
83
|
+ NewWiki(ctx)
|
|
84
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
85
|
+ assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+func TestNewWikiPost(t *testing.T) {
|
|
89
|
+ for _, title := range []string{
|
|
90
|
+ "New page",
|
|
91
|
+ "&&&&",
|
|
92
|
+ } {
|
|
93
|
+ models.PrepareTestEnv(t)
|
|
94
|
+
|
|
95
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
|
96
|
+ test.LoadUser(t, ctx, 2)
|
|
97
|
+ test.LoadRepo(t, ctx, 1)
|
|
98
|
+ NewWikiPost(ctx, auth.NewWikiForm{
|
|
99
|
+ Title: title,
|
|
100
|
+ Content: content,
|
|
101
|
+ Message: message,
|
|
102
|
+ })
|
|
103
|
+ assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
|
|
104
|
+ assertWikiExists(t, ctx.Repo.Repository, title)
|
|
105
|
+ assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
|
|
106
|
+ }
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+func TestNewWikiPost_ReservedName(t *testing.T) {
|
|
110
|
+ models.PrepareTestEnv(t)
|
|
111
|
+
|
|
112
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_new")
|
|
113
|
+ test.LoadUser(t, ctx, 2)
|
|
114
|
+ test.LoadRepo(t, ctx, 1)
|
|
115
|
+ NewWikiPost(ctx, auth.NewWikiForm{
|
|
116
|
+ Title: "_edit",
|
|
117
|
+ Content: content,
|
|
118
|
+ Message: message,
|
|
119
|
+ })
|
|
120
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
121
|
+ assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
|
|
122
|
+ assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+func TestEditWiki(t *testing.T) {
|
|
126
|
+ models.PrepareTestEnv(t)
|
|
127
|
+
|
|
128
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
|
|
129
|
+ ctx.SetParams(":page", "Home")
|
|
130
|
+ test.LoadUser(t, ctx, 2)
|
|
131
|
+ test.LoadRepo(t, ctx, 1)
|
|
132
|
+ EditWiki(ctx)
|
|
133
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
134
|
+ assert.EqualValues(t, "Home", ctx.Data["Title"])
|
|
135
|
+ assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+func TestEditWikiPost(t *testing.T) {
|
|
139
|
+ for _, title := range []string{
|
|
140
|
+ "Home",
|
|
141
|
+ "New/<page>",
|
|
142
|
+ } {
|
|
143
|
+ models.PrepareTestEnv(t)
|
|
144
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
|
|
145
|
+ ctx.SetParams(":page", "Home")
|
|
146
|
+ test.LoadUser(t, ctx, 2)
|
|
147
|
+ test.LoadRepo(t, ctx, 1)
|
|
148
|
+ EditWikiPost(ctx, auth.NewWikiForm{
|
|
149
|
+ Title: title,
|
|
150
|
+ Content: content,
|
|
151
|
+ Message: message,
|
|
152
|
+ })
|
|
153
|
+ assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
|
|
154
|
+ assertWikiExists(t, ctx.Repo.Repository, title)
|
|
155
|
+ assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
|
|
156
|
+ if title != "Home" {
|
|
157
|
+ assertWikiNotExists(t, ctx.Repo.Repository, "Home")
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+}
|
|
161
|
+
|
|
162
|
+func TestDeleteWikiPagePost(t *testing.T) {
|
|
163
|
+ models.PrepareTestEnv(t)
|
|
164
|
+
|
|
165
|
+ ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
|
|
166
|
+ test.LoadUser(t, ctx, 2)
|
|
167
|
+ test.LoadRepo(t, ctx, 1)
|
|
168
|
+ DeleteWikiPagePost(ctx)
|
|
169
|
+ assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
|
|
170
|
+ assertWikiNotExists(t, ctx.Repo.Repository, "Home")
|
|
171
|
+}
|