@@ -7,26 +7,37 @@ import (
7
7
go_context "context"
8
8
"io"
9
9
"net/http"
10
+ "os"
10
11
"path"
11
12
"strings"
12
13
"testing"
13
14
15
+ repo_model "code.gitea.io/gitea/models/repo"
16
+ "code.gitea.io/gitea/models/unittest"
14
17
"code.gitea.io/gitea/modules/markup"
15
18
"code.gitea.io/gitea/modules/setting"
16
19
api "code.gitea.io/gitea/modules/structs"
17
20
"code.gitea.io/gitea/modules/test"
18
21
"code.gitea.io/gitea/modules/web"
22
+ context_service "code.gitea.io/gitea/services/context"
19
23
"code.gitea.io/gitea/services/contexttest"
20
24
21
25
"github.com/stretchr/testify/assert"
22
26
)
23
27
24
28
const AppURL = "http://localhost:3000/"
25
29
30
+ func TestMain (m * testing.M ) {
31
+ unittest .MainTest (m , & unittest.TestOptions {
32
+ FixtureFiles : []string {"repository.yml" , "user.yml" },
33
+ })
34
+ os .Exit (m .Run ())
35
+ }
36
+
26
37
func testRenderMarkup (t * testing.T , mode string , wiki bool , filePath , text , expectedBody string , expectedCode int ) {
27
38
setting .AppURL = AppURL
28
39
defer test .MockVariableValue (& markup .RenderBehaviorForTesting .DisableAdditionalAttributes , true )()
29
- context := "/gogits/gogs "
40
+ context := "/user2/repo1 "
30
41
if ! wiki {
31
42
context += path .Join ("/src/branch/main" , path .Dir (filePath ))
32
43
}
@@ -38,6 +49,8 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe
38
49
FilePath : filePath ,
39
50
}
40
51
ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markup" )
52
+ ctx .Repo = & context_service.Repository {}
53
+ ctx .Repo .Repository = unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
41
54
web .SetForm (ctx , & options )
42
55
Markup (ctx )
43
56
assert .Equal (t , expectedBody , resp .Body .String ())
@@ -48,7 +61,7 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe
48
61
func testRenderMarkdown (t * testing.T , mode string , wiki bool , text , responseBody string , responseCode int ) {
49
62
defer test .MockVariableValue (& markup .RenderBehaviorForTesting .DisableAdditionalAttributes , true )()
50
63
setting .AppURL = AppURL
51
- context := "/gogits/gogs "
64
+ context := "/user2/repo1 "
52
65
if ! wiki {
53
66
context += "/src/branch/main"
54
67
}
@@ -67,6 +80,7 @@ func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody
67
80
}
68
81
69
82
func TestAPI_RenderGFM (t * testing.T ) {
83
+ unittest .PrepareTestEnv (t )
70
84
markup .Init (& markup.RenderHelperFuncs {
71
85
IsUsernameMentionable : func (ctx go_context.Context , username string ) bool {
72
86
return username == "r-lyeh"
@@ -82,20 +96,20 @@ func TestAPI_RenderGFM(t *testing.T) {
82
96
// rendered
83
97
`<p>Wiki! Enjoy :)</p>
84
98
<ul>
85
- <li><a href="http://localhost:3000/gogits/gogs /wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
86
- <li><a href="http://localhost:3000/gogits/gogs /wiki/Tips" rel="nofollow">Tips</a></li>
99
+ <li><a href="http://localhost:3000/user2/repo1 /wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
100
+ <li><a href="http://localhost:3000/user2/repo1 /wiki/Tips" rel="nofollow">Tips</a></li>
87
101
<li>Bezier widget (by <a href="http://localhost:3000/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
88
102
</ul>
89
103
` ,
90
104
// Guard wiki sidebar: special syntax
91
105
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]` ,
92
106
// rendered
93
- `<p><a href="http://localhost:3000/gogits/gogs /wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
107
+ `<p><a href="http://localhost:3000/user2/repo1 /wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
94
108
` ,
95
109
// special syntax
96
110
`[[Name|Link]]` ,
97
111
// rendered
98
- `<p><a href="http://localhost:3000/gogits/gogs /wiki/Link" rel="nofollow">Name</a></p>
112
+ `<p><a href="http://localhost:3000/user2/repo1 /wiki/Link" rel="nofollow">Name</a></p>
99
113
` ,
100
114
// empty
101
115
`` ,
@@ -119,8 +133,8 @@ Here are some links to the most important topics. You can find the full list of
119
133
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
120
134
<h2 id="user-content-quick-links">Quick Links</h2>
121
135
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
122
- <p><a href="http://localhost:3000/gogits/gogs /wiki/Configuration" rel="nofollow">Configuration</a>
123
- <a href="http://localhost:3000/gogits/gogs /wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/gogits/gogs /wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
136
+ <p><a href="http://localhost:3000/user2/repo1 /wiki/Configuration" rel="nofollow">Configuration</a>
137
+ <a href="http://localhost:3000/user2/repo1 /wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/user2/repo1 /wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
124
138
` ,
125
139
}
126
140
@@ -143,20 +157,20 @@ Here are some links to the most important topics. You can find the full list of
143
157
}
144
158
145
159
input := "[Link](test.md)\n "
146
- testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
147
- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
160
+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
161
+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
148
162
` , http .StatusOK )
149
163
150
- testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
151
- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
164
+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
165
+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
152
166
` , http .StatusOK )
153
167
154
- testRenderMarkup (t , "gfm" , false , "" , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
155
- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
168
+ testRenderMarkup (t , "gfm" , false , "" , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
169
+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
156
170
` , http .StatusOK )
157
171
158
- testRenderMarkup (t , "file" , false , "path/new-file.md" , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/path/test.md" rel="nofollow">Link</a>
159
- <a href="http://localhost:3000/gogits/gogs /media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/path/image.png" alt="Image"/></a></p>
172
+ testRenderMarkup (t , "file" , false , "path/new-file.md" , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/path/test.md" rel="nofollow">Link</a>
173
+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/path/image.png" alt="Image"/></a></p>
160
174
` , http .StatusOK )
161
175
162
176
testRenderMarkup (t , "file" , false , "path/test.unknown" , "## Test" , "unsupported file to render: \" path/test.unknown\" \n " , http .StatusUnprocessableEntity )
@@ -186,7 +200,7 @@ func TestAPI_RenderSimple(t *testing.T) {
186
200
options := api.MarkdownOption {
187
201
Mode : "markdown" ,
188
202
Text : "" ,
189
- Context : "/gogits/gogs " ,
203
+ Context : "/user2/repo1 " ,
190
204
}
191
205
ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
192
206
for i := 0 ; i < len (simpleCases ); i += 2 {
0 commit comments