gitea源码

markup_test.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package misc
  4. import (
  5. go_context "context"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path"
  10. "strings"
  11. "testing"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/test"
  18. "code.gitea.io/gitea/modules/web"
  19. context_service "code.gitea.io/gitea/services/context"
  20. "code.gitea.io/gitea/services/contexttest"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. const AppURL = "http://localhost:3000/"
  24. func TestMain(m *testing.M) {
  25. unittest.MainTest(m, &unittest.TestOptions{
  26. FixtureFiles: []string{"repository.yml", "user.yml"},
  27. })
  28. os.Exit(m.Run())
  29. }
  30. func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) {
  31. setting.AppURL = AppURL
  32. defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  33. context := "/user2/repo1"
  34. if !wiki {
  35. context += path.Join("/src/branch/main", path.Dir(filePath))
  36. }
  37. options := api.MarkupOption{
  38. Mode: mode,
  39. Text: text,
  40. Context: context,
  41. Wiki: wiki,
  42. FilePath: filePath,
  43. }
  44. ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
  45. ctx.Repo = &context_service.Repository{}
  46. ctx.Repo.Repository = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  47. web.SetForm(ctx, &options)
  48. Markup(ctx)
  49. assert.Equal(t, expectedBody, resp.Body.String())
  50. assert.Equal(t, expectedCode, resp.Code)
  51. resp.Body.Reset()
  52. }
  53. func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) {
  54. defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  55. setting.AppURL = AppURL
  56. context := "/user2/repo1"
  57. if !wiki {
  58. context += "/src/branch/main"
  59. }
  60. options := api.MarkdownOption{
  61. Mode: mode,
  62. Text: text,
  63. Context: context,
  64. Wiki: wiki,
  65. }
  66. ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
  67. web.SetForm(ctx, &options)
  68. Markdown(ctx)
  69. assert.Equal(t, responseBody, resp.Body.String())
  70. assert.Equal(t, responseCode, resp.Code)
  71. resp.Body.Reset()
  72. }
  73. func TestAPI_RenderGFM(t *testing.T) {
  74. unittest.PrepareTestEnv(t)
  75. markup.Init(&markup.RenderHelperFuncs{
  76. IsUsernameMentionable: func(ctx go_context.Context, username string) bool {
  77. return username == "r-lyeh"
  78. },
  79. })
  80. testCasesWiki := []string{
  81. // dear imgui wiki markdown extract: special wiki syntax
  82. `Wiki! Enjoy :)
  83. - [[Links, Language bindings, Engine bindings|Links]]
  84. - [[Tips]]
  85. - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
  86. // rendered
  87. `<p>Wiki! Enjoy :)</p>
  88. <ul>
  89. <li><a href="http://localhost:3000/user2/repo1/wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  90. <li><a href="http://localhost:3000/user2/repo1/wiki/Tips" rel="nofollow">Tips</a></li>
  91. <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>
  92. </ul>
  93. `,
  94. // Guard wiki sidebar: special syntax
  95. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  96. // rendered
  97. `<p><a href="http://localhost:3000/user2/repo1/wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  98. `,
  99. // special syntax
  100. `[[Name|Link]]`,
  101. // rendered
  102. `<p><a href="http://localhost:3000/user2/repo1/wiki/Link" rel="nofollow">Name</a></p>
  103. `,
  104. // empty
  105. ``,
  106. // rendered
  107. ``,
  108. }
  109. testCasesWikiDocument := []string{
  110. // wine-staging wiki home extract: special wiki syntax, images
  111. `## What is Wine Staging?
  112. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  113. ## Quick Links
  114. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  115. [[Configuration]]
  116. [[images/icon-bug.png]]
  117. `,
  118. // rendered
  119. `<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
  120. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  121. <h2 id="user-content-quick-links">Quick Links</h2>
  122. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  123. <p><a href="http://localhost:3000/user2/repo1/wiki/Configuration" rel="nofollow">Configuration</a>
  124. <a href="http://localhost:3000/user2/repo1/wiki/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>
  125. `,
  126. }
  127. for i := 0; i < len(testCasesWiki); i += 2 {
  128. text := testCasesWiki[i]
  129. response := testCasesWiki[i+1]
  130. testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
  131. testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
  132. testRenderMarkdown(t, "comment", true, text, response, http.StatusOK)
  133. testRenderMarkup(t, "comment", true, "", text, response, http.StatusOK)
  134. testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
  135. }
  136. for i := 0; i < len(testCasesWikiDocument); i += 2 {
  137. text := testCasesWikiDocument[i]
  138. response := testCasesWikiDocument[i+1]
  139. testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
  140. testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
  141. testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
  142. }
  143. input := "[Link](test.md)\n![Image](image.png)"
  144. testRenderMarkdown(t, "gfm", false, input, `<p><a href="http://localhost:3000/user2/repo1/src/branch/main/test.md" rel="nofollow">Link</a>
  145. <a href="http://localhost:3000/user2/repo1/src/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>
  146. `, http.StatusOK)
  147. testRenderMarkdown(t, "gfm", false, input, `<p><a href="http://localhost:3000/user2/repo1/src/branch/main/test.md" rel="nofollow">Link</a>
  148. <a href="http://localhost:3000/user2/repo1/src/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>
  149. `, http.StatusOK)
  150. testRenderMarkup(t, "gfm", false, "", input, `<p><a href="http://localhost:3000/user2/repo1/src/branch/main/test.md" rel="nofollow">Link</a>
  151. <a href="http://localhost:3000/user2/repo1/src/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. `, http.StatusOK)
  153. 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>
  154. <a href="http://localhost:3000/user2/repo1/src/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>
  155. `, http.StatusOK)
  156. testRenderMarkup(t, "file", false, "path/test.unknown", "## Test", "unsupported file to render: \"path/test.unknown\"\n", http.StatusUnprocessableEntity)
  157. testRenderMarkup(t, "unknown", false, "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
  158. }
  159. var simpleCases = []string{
  160. // Guard wiki sidebar: special syntax
  161. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  162. // rendered
  163. `<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
  164. `,
  165. // special syntax
  166. `[[Name|Link]]`,
  167. // rendered
  168. `<p>[[Name|Link]]</p>
  169. `,
  170. // empty
  171. ``,
  172. // rendered
  173. ``,
  174. }
  175. func TestAPI_RenderSimple(t *testing.T) {
  176. setting.AppURL = AppURL
  177. markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true
  178. options := api.MarkdownOption{
  179. Mode: "markdown",
  180. Text: "",
  181. Context: "/user2/repo1",
  182. }
  183. ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
  184. for i := 0; i < len(simpleCases); i += 2 {
  185. options.Text = simpleCases[i]
  186. web.SetForm(ctx, &options)
  187. Markdown(ctx)
  188. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  189. resp.Body.Reset()
  190. }
  191. }
  192. func TestAPI_RenderRaw(t *testing.T) {
  193. setting.AppURL = AppURL
  194. markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true
  195. ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
  196. for i := 0; i < len(simpleCases); i += 2 {
  197. ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
  198. MarkdownRaw(ctx)
  199. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  200. resp.Body.Reset()
  201. }
  202. }