2017-12-07 15:00:09 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-08-12 04:05:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-12-07 15:00:09 +08: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 22:31:08 +08:00
|
|
|
|
|
|
|
func TestIsValidSlackChannel(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
channelName string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"gitea", true},
|
|
|
|
{" ", false},
|
|
|
|
{"#", false},
|
|
|
|
{"gitea ", true},
|
|
|
|
{" gitea", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range tt {
|
|
|
|
assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 04:05:34 +08:00
|
|
|
|
|
|
|
func TestIsExternalURL(t *testing.T) {
|
2021-02-20 05:36:43 +08:00
|
|
|
setting.AppURL = "https://try.gitea.io/"
|
2020-08-12 04:05:34 +08: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"),
|
|
|
|
} {
|
|
|
|
assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
|
|
|
|
}
|
|
|
|
}
|