summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/misc/markup_test.go
blob: 5236fd06ae2343b458cb9728a964cca12dd0bbb0 (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
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package misc

import (
	go_context "context"
	"io"
	"net/http"
	"strings"
	"testing"

	"code.gitea.io/gitea/modules/markup"
	"code.gitea.io/gitea/modules/setting"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/web"
	"code.gitea.io/gitea/services/contexttest"

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

const (
	AppURL  = "http://localhost:3000/"
	Repo    = "gogits/gogs"
	FullURL = AppURL + Repo + "/"
)

func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
	setting.AppURL = AppURL
	options := api.MarkupOption{
		Mode:     mode,
		Text:     text,
		Context:  Repo,
		Wiki:     true,
		FilePath: filePath,
	}
	ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
	web.SetForm(ctx, &options)
	Markup(ctx)
	assert.Equal(t, responseBody, resp.Body.String())
	assert.Equal(t, responseCode, resp.Code)
	resp.Body.Reset()
}

func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
	setting.AppURL = AppURL
	options := api.MarkdownOption{
		Mode:    mode,
		Text:    text,
		Context: Repo,
		Wiki:    true,
	}
	ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
	web.SetForm(ctx, &options)
	Markdown(ctx)
	assert.Equal(t, responseBody, resp.Body.String())
	assert.Equal(t, responseCode, resp.Code)
	resp.Body.Reset()
}

func TestAPI_RenderGFM(t *testing.T) {
	markup.Init(&markup.ProcessorHelper{
		IsUsernameMentionable: func(ctx go_context.Context, username string) bool {
			return username == "r-lyeh"
		},
	})

	testCasesCommon := []string{
		// dear imgui wiki markdown extract: special wiki syntax
		`Wiki! Enjoy :)
- [[Links, Language bindings, Engine bindings|Links]]
- [[Tips]]
- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
		// rendered
		`<p>Wiki! Enjoy :)</p>
<ul>
<li><a href="` + FullURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
<li><a href="` + FullURL + `wiki/Tips" rel="nofollow">Tips</a></li>
<li>Bezier widget (by <a href="` + AppURL + `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>
</ul>
`,
		// Guard wiki sidebar: special syntax
		`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
		// rendered
		`<p><a href="` + FullURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
`,
		// special syntax
		`[[Name|Link]]`,
		// rendered
		`<p><a href="` + FullURL + `wiki/Link" rel="nofollow">Name</a></p>
`,
		// empty
		``,
		// rendered
		``,
	}

	testCasesDocument := []string{
		// wine-staging wiki home extract: special wiki syntax, images
		`## What is Wine Staging?
**Wine Staging** on website [wine-staging.com](http://wine-staging.com).

## Quick Links
Here are some links to the most important topics. You can find the full list of pages at the sidebar.

[[Configuration]]
[[images/icon-bug.png]]
`,
		// rendered
		`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
<h2 id="user-content-quick-links">Quick Links</h2>
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
<p><a href="` + FullURL + `wiki/Configuration" rel="nofollow">Configuration</a>
<a href="` + FullURL + `wiki/raw/images/icon-bug.png" rel="nofollow"><img src="` + FullURL + `wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
`,
	}

	for i := 0; i < len(testCasesCommon); i += 2 {
		text := testCasesCommon[i]
		response := testCasesCommon[i+1]
		testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
		testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
		testRenderMarkdown(t, "comment", text, response, http.StatusOK)
		testRenderMarkup(t, "comment", "", text, response, http.StatusOK)
		testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
	}

	for i := 0; i < len(testCasesDocument); i += 2 {
		text := testCasesDocument[i]
		response := testCasesDocument[i+1]
		testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
		testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
		testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
	}

	testRenderMarkup(t, "file", "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity)
	testRenderMarkup(t, "unknown", "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
}

var simpleCases = []string{
	// Guard wiki sidebar: special syntax
	`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
	// rendered
	`<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
`,
	// special syntax
	`[[Name|Link]]`,
	// rendered
	`<p>[[Name|Link]]</p>
`,
	// empty
	``,
	// rendered
	``,
}

func TestAPI_RenderSimple(t *testing.T) {
	setting.AppURL = AppURL
	options := api.MarkdownOption{
		Mode:    "markdown",
		Text:    "",
		Context: Repo,
	}
	ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
	for i := 0; i < len(simpleCases); i += 2 {
		options.Text = simpleCases[i]
		web.SetForm(ctx, &options)
		Markdown(ctx)
		assert.Equal(t, simpleCases[i+1], resp.Body.String())
		resp.Body.Reset()
	}
}

func TestAPI_RenderRaw(t *testing.T) {
	setting.AppURL = AppURL
	ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
	for i := 0; i < len(simpleCases); i += 2 {
		ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
		MarkdownRaw(ctx)
		assert.Equal(t, simpleCases[i+1], resp.Body.String())
		resp.Body.Reset()
	}
}