aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/markup_external_test.go
blob: 2d713b0eb9d5abbee5feec54916a4740ef60a875 (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
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"bytes"
	"io"
	"net/http"
	"strings"
	"testing"

	"code.gitea.io/gitea/modules/markup"
	"code.gitea.io/gitea/modules/markup/external"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/tests"

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

func TestExternalMarkupRenderer(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	if !setting.Database.Type.IsSQLite3() {
		t.Skip()
		return
	}

	req := NewRequest(t, "GET", "/user30/renderer/src/branch/master/README.html")
	resp := MakeRequest(t, req, http.StatusOK)
	assert.EqualValues(t, "text/html; charset=utf-8", resp.Header().Get("Content-Type"))

	bs, err := io.ReadAll(resp.Body)
	assert.NoError(t, err)

	doc := NewHTMLParser(t, bytes.NewBuffer(bs))
	div := doc.Find("div.file-view")
	data, err := div.Html()
	assert.NoError(t, err)
	assert.EqualValues(t, "<div>\n\ttest external renderer\n</div>", strings.TrimSpace(data))

	r := markup.GetRendererByFileName("a.html").(*external.Renderer)
	r.RenderContentMode = setting.RenderContentModeIframe

	req = NewRequest(t, "GET", "/user30/renderer/src/branch/master/README.html")
	resp = MakeRequest(t, req, http.StatusOK)
	assert.EqualValues(t, "text/html; charset=utf-8", resp.Header().Get("Content-Type"))
	bs, err = io.ReadAll(resp.Body)
	assert.NoError(t, err)
	doc = NewHTMLParser(t, bytes.NewBuffer(bs))
	iframe := doc.Find("iframe")
	assert.EqualValues(t, "/user30/renderer/render/branch/master/README.html", iframe.AttrOr("src", ""))

	req = NewRequest(t, "GET", "/user30/renderer/render/branch/master/README.html")
	resp = MakeRequest(t, req, http.StatusOK)
	assert.EqualValues(t, "text/html; charset=utf-8", resp.Header().Get("Content-Type"))
	bs, err = io.ReadAll(resp.Body)
	assert.NoError(t, err)
	assert.EqualValues(t, "frame-src 'self'; sandbox allow-scripts", resp.Header().Get("Content-Security-Policy"))
	assert.EqualValues(t, "<div>\n\ttest external renderer\n</div>\n", string(bs))
}