code.gitea.io/gitea@v1.21.7/routers/api/v1/misc/markup_test.go (about)

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