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()
|
2022-04-29 08:36:36 +02:00
|
|
|
assert.Contains(t, config.AllowedWebmentionSources, "mycooldomain.com")
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"allowedWebmentionSources": [
|
|
|
|
"snoopy.be"
|
|
|
|
],
|
2023-03-01 17:06:17 +01:00
|
|
|
"denylist": [
|
2022-04-18 10:18:00 +02:00
|
|
|
"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))
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestSaveAfterAddingANewDenylistEntry(t *testing.T) {
|
2022-04-23 10:00:00 +02:00
|
|
|
t.Cleanup(cleanupConfig)
|
|
|
|
|
2022-04-18 10:18:00 +02:00
|
|
|
config := Configure()
|
2023-03-01 17:06:17 +01:00
|
|
|
config.AddToDenylist("somethingnew.be")
|
2022-04-18 10:18:00 +02:00
|
|
|
config.Save()
|
|
|
|
|
|
|
|
newConfig := Configure()
|
2023-03-01 17:06:17 +01:00
|
|
|
assert.Contains(t, newConfig.Denylist, "somethingnew.be")
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestAllowlist(t *testing.T) {
|
2022-04-23 11:35:53 +02:00
|
|
|
conf := Config{
|
2023-03-01 17:06:17 +01:00
|
|
|
Allowlist: []string{
|
2022-04-23 11:35:53 +02:00
|
|
|
"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")
|
|
|
|
})
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
conf.AddToAllowlist("dinges.be")
|
|
|
|
assert.Contains(t, conf.Allowlist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(conf.Allowlist))
|
2022-04-23 11:35:53 +02:00
|
|
|
|
|
|
|
confFromFile := Configure()
|
2023-03-01 17:06:17 +01:00
|
|
|
assert.Contains(t, confFromFile.Allowlist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(confFromFile.Allowlist))
|
2022-04-23 11:35:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestAddToDenylistNotYetAddsToListAndSaves(t *testing.T) {
|
2022-04-18 10:18:00 +02:00
|
|
|
conf := Config{
|
2023-03-01 17:06:17 +01:00
|
|
|
Denylist: []string{
|
2022-04-18 10:18:00 +02:00
|
|
|
"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
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
conf.AddToDenylist("dinges.be")
|
|
|
|
assert.Contains(t, conf.Denylist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(conf.Denylist))
|
2022-04-23 11:35:53 +02:00
|
|
|
|
|
|
|
confFromFile := Configure()
|
2023-03-01 17:06:17 +01:00
|
|
|
assert.Contains(t, confFromFile.Denylist, "dinges.be")
|
|
|
|
assert.Equal(t, 2, len(confFromFile.Denylist))
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestAddToDenylistAlreadyAddedDoNotAddAgain(t *testing.T) {
|
2022-04-18 10:18:00 +02:00
|
|
|
conf := Config{
|
2023-03-01 17:06:17 +01:00
|
|
|
Denylist: []string{
|
2022-04-18 10:18:00 +02:00
|
|
|
"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
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
conf.AddToDenylist("youtube.com")
|
|
|
|
assert.Contains(t, conf.Denylist, "youtube.com")
|
|
|
|
assert.Equal(t, 1, len(conf.Denylist))
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestIsAllowlisted(t *testing.T) {
|
2022-04-23 11:35:53 +02:00
|
|
|
cases := []struct {
|
|
|
|
label string
|
|
|
|
url string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"do not allowlist if domain is part of relative url",
|
2022-04-23 11:35:53 +02:00
|
|
|
"https://brainbaking.com/post/youtube.com-sucks",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"allowlist if https domain is on the list",
|
2022-04-23 11:35:53 +02:00
|
|
|
"https://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"allowlist if http domain is on the list",
|
2022-04-23 11:35:53 +02:00
|
|
|
"http://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"do not allowlist if relative url",
|
2022-04-23 11:35:53 +02:00
|
|
|
"/youtube.com",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := Config{
|
2023-03-01 17:06:17 +01:00
|
|
|
Allowlist: []string{
|
2022-04-23 11:35:53 +02:00
|
|
|
"youtube.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.label, func(t *testing.T) {
|
2023-03-01 17:06:17 +01:00
|
|
|
assert.Equal(t, tc.expected, conf.IsAllowlisted(tc.url))
|
2022-04-23 11:35:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:17 +01:00
|
|
|
func TestIsDenylisted(t *testing.T) {
|
2021-05-02 11:40:45 +02:00
|
|
|
cases := []struct {
|
|
|
|
label string
|
|
|
|
url string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"do not denylist if domain is part of relative url",
|
2021-05-02 11:40:45 +02:00
|
|
|
"https://brainbaking.com/post/youtube.com-sucks",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"denylist if https domain is on the list",
|
2021-05-02 11:40:45 +02:00
|
|
|
"https://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"denylist if http domain is on the list",
|
2021-05-02 11:40:45 +02:00
|
|
|
"http://youtube.com/stuff",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2023-03-01 17:06:17 +01:00
|
|
|
"do not denylist if relative url",
|
2021-05-02 11:40:45 +02:00
|
|
|
"/youtube.com",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := Config{
|
2023-03-01 17:06:17 +01:00
|
|
|
Denylist: []string{
|
2021-05-02 11:40:45 +02:00
|
|
|
"youtube.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.label, func(t *testing.T) {
|
2023-03-01 17:06:17 +01:00
|
|
|
assert.Equal(t, tc.expected, conf.IsDenylisted(tc.url))
|
2021-05-02 11:40:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|