2019-11-07 21:34:28 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-07 21:34:28 +08:00
|
|
|
|
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2024-02-25 18:45:56 +08:00
|
|
|
"html/template"
|
2019-11-07 21:34:28 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSubjectBodySeparator(t *testing.T) {
|
|
|
|
test := func(input, subject, body string) {
|
|
|
|
loc := mailSubjectSplit.FindIndex([]byte(input))
|
|
|
|
if loc == nil {
|
|
|
|
assert.Empty(t, subject, "no subject found, but one expected")
|
|
|
|
assert.Equal(t, body, input)
|
|
|
|
} else {
|
2022-06-20 18:02:49 +08:00
|
|
|
assert.Equal(t, subject, input[0:loc[0]])
|
|
|
|
assert.Equal(t, body, input[loc[1]:])
|
2019-11-07 21:34:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test("Simple\n---------------\nCase",
|
|
|
|
"Simple\n",
|
|
|
|
"\nCase")
|
|
|
|
test("Only\nBody",
|
|
|
|
"",
|
|
|
|
"Only\nBody")
|
|
|
|
test("Minimal\n---\nseparator",
|
|
|
|
"Minimal\n",
|
|
|
|
"\nseparator")
|
|
|
|
test("False --- separator",
|
|
|
|
"",
|
|
|
|
"False --- separator")
|
|
|
|
test("False\n--- separator",
|
|
|
|
"",
|
|
|
|
"False\n--- separator")
|
|
|
|
test("False ---\nseparator",
|
|
|
|
"",
|
|
|
|
"False ---\nseparator")
|
|
|
|
test("With extra spaces\n----- \t \nBody",
|
|
|
|
"With extra spaces\n",
|
|
|
|
"\nBody")
|
|
|
|
test("With leading spaces\n -------\nOnly body",
|
|
|
|
"",
|
|
|
|
"With leading spaces\n -------\nOnly body")
|
|
|
|
test("Multiple\n---\n-------\n---\nSeparators",
|
|
|
|
"Multiple\n",
|
|
|
|
"\n-------\n---\nSeparators")
|
2024-04-27 16:03:49 +08:00
|
|
|
test("Insufficient\n--\nSeparators",
|
2019-11-07 21:34:28 +08:00
|
|
|
"",
|
2024-04-27 16:03:49 +08:00
|
|
|
"Insufficient\n--\nSeparators")
|
2019-11-07 21:34:28 +08:00
|
|
|
}
|
2024-02-18 17:52:02 +08:00
|
|
|
|
|
|
|
func TestJSEscapeSafe(t *testing.T) {
|
|
|
|
assert.EqualValues(t, `\u0026\u003C\u003E\'\"`, JSEscapeSafe(`&<>'"`))
|
|
|
|
}
|
2024-02-25 18:45:56 +08:00
|
|
|
|
|
|
|
func TestHTMLFormat(t *testing.T) {
|
|
|
|
assert.Equal(t, template.HTML("<a>< < 1</a>"), HTMLFormat("<a>%s %s %d</a>", "<", template.HTML("<"), 1))
|
|
|
|
}
|
2024-03-01 18:16:19 +08:00
|
|
|
|
|
|
|
func TestSanitizeHTML(t *testing.T) {
|
|
|
|
assert.Equal(t, template.HTML(`<a href="/" rel="nofollow">link</a> xss <div>inline</div>`), SanitizeHTML(`<a href="/">link</a> <a href="javascript:">xss</a> <div style="dangerous">inline</div>`))
|
|
|
|
}
|
2024-06-11 21:07:10 +08:00
|
|
|
|
|
|
|
func TestIsTruthy(t *testing.T) {
|
|
|
|
var test any
|
|
|
|
assert.Equal(t, false, IsTruthy(test))
|
|
|
|
assert.Equal(t, false, IsTruthy(nil))
|
|
|
|
assert.Equal(t, false, IsTruthy(""))
|
|
|
|
assert.Equal(t, true, IsTruthy("non-empty"))
|
|
|
|
assert.Equal(t, true, IsTruthy(-1))
|
|
|
|
assert.Equal(t, false, IsTruthy(0))
|
|
|
|
assert.Equal(t, true, IsTruthy(42))
|
|
|
|
assert.Equal(t, false, IsTruthy(0.0))
|
|
|
|
assert.Equal(t, true, IsTruthy(3.14))
|
|
|
|
assert.Equal(t, false, IsTruthy([]int{}))
|
|
|
|
assert.Equal(t, true, IsTruthy([]int{1}))
|
|
|
|
}
|