2017-12-07 00:00:09 -07:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-07 00:00:09 -07:00
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-08-11 14:05:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-17 05:34:35 -07:00
|
|
|
|
2017-12-07 00:00:09 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoveUsernameParameterSuffix(t *testing.T) {
|
|
|
|
assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar (Foo Bar)"))
|
|
|
|
assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar"))
|
|
|
|
assert.Equal(t, "", RemoveUsernameParameterSuffix(""))
|
|
|
|
}
|
2018-09-10 08:31:08 -06:00
|
|
|
|
2020-08-11 14:05:34 -06:00
|
|
|
func TestIsExternalURL(t *testing.T) {
|
2021-02-19 14:36:43 -07:00
|
|
|
setting.AppURL = "https://try.gitea.io/"
|
2020-08-11 14:05:34 -06:00
|
|
|
type test struct {
|
|
|
|
Expected bool
|
|
|
|
RawURL string
|
|
|
|
}
|
|
|
|
newTest := func(expected bool, rawURL string) test {
|
|
|
|
return test{Expected: expected, RawURL: rawURL}
|
|
|
|
}
|
|
|
|
for _, test := range []test{
|
|
|
|
newTest(false,
|
|
|
|
"https://try.gitea.io"),
|
|
|
|
newTest(true,
|
|
|
|
"https://example.com/"),
|
|
|
|
newTest(true,
|
|
|
|
"//example.com"),
|
|
|
|
newTest(true,
|
|
|
|
"http://example.com"),
|
|
|
|
newTest(false,
|
|
|
|
"a/"),
|
|
|
|
newTest(false,
|
|
|
|
"https://try.gitea.io/test?param=false"),
|
|
|
|
newTest(false,
|
|
|
|
"test?param=false"),
|
|
|
|
newTest(false,
|
|
|
|
"//try.gitea.io/test?param=false"),
|
|
|
|
newTest(false,
|
|
|
|
"/hey/hey/hey#3244"),
|
2021-06-29 15:00:02 -06:00
|
|
|
newTest(true,
|
|
|
|
"://missing protocol scheme"),
|
2020-08-11 14:05:34 -06:00
|
|
|
} {
|
|
|
|
assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 15:00:02 -06:00
|
|
|
|
|
|
|
func TestSanitizeFlashErrorString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
arg string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no error",
|
|
|
|
arg: "",
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normal error",
|
|
|
|
arg: "can not open file: \"abc.exe\"",
|
|
|
|
want: "can not open file: "abc.exe"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "line break error",
|
|
|
|
arg: "some error:\n\nawesome!",
|
|
|
|
want: "some error:<br><br>awesome!",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := SanitizeFlashErrorString(tt.arg); got != tt.want {
|
|
|
|
t.Errorf("SanitizeFlashErrorString() = '%v', want '%v'", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|