2021-05-02 11:40:45 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-18 10:18:00 +02:00
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-05-02 11:40:45 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-04-23 10:00:00 +02:00
|
|
|
func cleanupConfig() {
|
|
|
|
os.Remove("config.json")
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:18:00 +02:00
|
|
|
func TestReadFromJsonMalformedReversToDefaults(t *testing.T) {
|
2022-04-23 10:00:00 +02:00
|
|
|
ioutil.WriteFile("config.json", []byte("dinges"), fs.ModePerm)
|
|
|
|
t.Cleanup(cleanupConfig)
|
2022-04-18 10:18:00 +02:00
|
|
|
|
|
|
|
config := Configure()
|
|
|
|
assert.Contains(t, config.AllowedWebmentionSources, "brainbaking.com")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadFromJsonWithCorrectJsonData(t *testing.T) {
|
|
|
|
confString := `{
|
|
|
|
"port": 1337,
|
|
|
|
"host": "localhost",
|
2022-04-23 21:27:43 +02:00
|
|
|
"baseURL": "https://jam.brainbaking.com/",
|
2022-04-18 10:18:00 +02:00
|
|
|
"token": "miauwkes",
|
|
|
|
"utcOffset": 60,
|
|
|
|
"allowedWebmentionSources": [
|
|
|
|
"snoopy.be"
|
|
|
|
],
|
|
|
|
"blacklist": [
|
|
|
|
"youtube.com"
|
|
|
|
]
|
|
|
|
}`
|
2022-04-23 10:00:00 +02:00
|
|
|
ioutil.WriteFile("config.json", []byte(confString), fs.ModePerm)
|
|
|
|
t.Cleanup(cleanupConfig)
|
2022-04-18 10:18:00 +02:00
|
|
|
|
|
|
|
config := Configure()
|
|
|
|
assert.Contains(t, config.AllowedWebmentionSources, "snoopy.be")
|
|
|
|
assert.Equal(t, 1, len(config.AllowedWebmentionSources))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveAfterAddingANewBlacklistEntry(t *testing.T) {
|
2022-04-23 10:00:00 +02:00
|
|
|
t.Cleanup(cleanupConfig)
|
|
|
|
|
2022-04-18 10:18:00 +02:00
|
|
|
config := Configure()
|
|
|
|
config.AddToBlacklist("somethingnew.be")
|
|
|
|
config.Save()
|
|
|
|
|
|
|
|
newConfig := Configure()
|
|
|
|
assert.Contains(t, newConfig.Blacklist, "somethingnew.be")
|
|
|
|
}
|
|
|
|
|
2022-04-23 11:35:53 +02:00
|
|
|
func TestWhitelist(t *testing.T) {
|
|
|
|
conf := Config{
|
|
|
|
Whitelist: []string{
|
|
|
|
"youtube.com",
|
|
|
|
},
|
2022-04-23 21:27:43 +02:00
|
|
|
BaseURL: "https://jam.brainbaking.com/",
|
2022-04-23 11:35:53 +02:00
|
|
|
Port: 123,
|
|
|
|
Token: "token",
|
|
|
|
AllowedWebmentionSources: []string{"blah.com"},
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove("config.json")
|
|
|
|
})
|
|
|
|
|
|
|
|
conf.AddToWhitelist("dinges.be")
|
|
|
|
assert.Contains(t, conf.Whitelist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(conf.Whitelist))
|
|
|
|
|
|
|
|
confFromFile := Configure()
|
|
|
|
assert.Contains(t, confFromFile.Whitelist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(confFromFile.Whitelist))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddToBlacklistNotYetAddsToListAndSaves(t *testing.T) {
|
2022-04-18 10:18:00 +02:00
|
|
|
conf := Config{
|
|
|
|
Blacklist: []string{
|
|
|
|
"youtube.com",
|
|
|
|
},
|
2022-04-23 21:27:43 +02:00
|
|
|
BaseURL: "https://jam.brainbaking.com/",
|
2022-04-23 11:35:53 +02:00
|
|
|
Port: 123,
|
|
|
|
Token: "token",
|
|
|
|
AllowedWebmentionSources: []string{"blah.com"},
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
2022-04-23 11:35:53 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove("config.json")
|
|
|
|
})
|
2022-04-18 10:18:00 +02:00
|
|
|
|
|
|
|
conf.AddToBlacklist("dinges.be")
|
|
|
|
assert.Contains(t, conf.Blacklist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(conf.Blacklist))
|
2022-04-23 11:35:53 +02:00
|
|
|
|
|
|
|
confFromFile := Configure()
|
|
|
|
assert.Contains(t, confFromFile.Blacklist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(confFromFile.Blacklist))
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddToBlacklistAlreadyAddedDoNotAddAgain(t *testing.T) {
|
|
|
|
conf := Config{
|
|
|
|
Blacklist: []string{
|
|
|
|
"youtube.com",
|
|
|
|
},
|
2022-04-23 11:35:53 +02:00
|
|
|
Port: 123,
|
|
|
|
Token: "token",
|
|
|
|
AllowedWebmentionSources: []string{"blah.com"},
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
2022-04-23 11:35:53 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove("config.json")
|
|
|
|
})
|
2022-04-18 10:18:00 +02:00
|
|
|
|
|
|
|
conf.AddToBlacklist("youtube.com")
|
|
|
|
assert.Contains(t, conf.Blacklist, "youtube.com")
|
|
|
|
assert.Equal(t, 1, len(conf.Blacklist))
|
|
|
|
}
|
|
|
|
|
2022-04-23 11:35:53 +02:00
|
|
|
func TestIsWhitelisted(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
label string
|
|
|
|
url string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"do not whitelist if domain is part of relative url",
|
|
|
|
"https://brainbaking.com/post/youtube.com-sucks",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"whitelist if https domain is on the list",
|
|
|
|
"https://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"whitelist if http domain is on the list",
|
|
|
|
"http://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"do not whitelist if relative url",
|
|
|
|
"/youtube.com",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := Config{
|
|
|
|
Whitelist: []string{
|
|
|
|
"youtube.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.label, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.expected, conf.IsWhitelisted(tc.url))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 11:40:45 +02:00
|
|
|
func TestIsBlacklisted(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
label string
|
|
|
|
url string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"do not blacklist if domain is part of relative url",
|
|
|
|
"https://brainbaking.com/post/youtube.com-sucks",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"blacklist if https domain is on the list",
|
|
|
|
"https://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"blacklist if http domain is on the list",
|
|
|
|
"http://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"do not blacklist if relative url",
|
|
|
|
"/youtube.com",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := Config{
|
|
|
|
Blacklist: []string{
|
|
|
|
"youtube.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.label, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.expected, conf.IsBlacklisted(tc.url))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|