go-jamming/common/config_test.go

198 lines
4.2 KiB
Go

package common
import (
"github.com/stretchr/testify/assert"
"io/fs"
"io/ioutil"
"os"
"testing"
)
func cleanupConfig() {
os.Remove("config.json")
}
func TestReadFromJsonMalformedReversToDefaults(t *testing.T) {
ioutil.WriteFile("config.json", []byte("dinges"), fs.ModePerm)
t.Cleanup(cleanupConfig)
config := Configure()
assert.Contains(t, config.AllowedWebmentionSources, "mycooldomain.com")
}
func TestReadFromJsonWithCorrectJsonData(t *testing.T) {
confString := `{
"port": 1337,
"host": "localhost",
"baseURL": "https://jam.brainbaking.com/",
"token": "miauwkes",
"allowedWebmentionSources": [
"snoopy.be"
],
"denylist": [
"youtube.com"
]
}`
ioutil.WriteFile("config.json", []byte(confString), fs.ModePerm)
t.Cleanup(cleanupConfig)
config := Configure()
assert.Contains(t, config.AllowedWebmentionSources, "snoopy.be")
assert.Equal(t, 1, len(config.AllowedWebmentionSources))
}
func TestSaveAfterAddingANewDenylistEntry(t *testing.T) {
t.Cleanup(cleanupConfig)
config := Configure()
config.AddToDenylist("somethingnew.be")
config.Save()
newConfig := Configure()
assert.Contains(t, newConfig.Denylist, "somethingnew.be")
}
func TestAllowlist(t *testing.T) {
conf := Config{
Allowlist: []string{
"youtube.com",
},
BaseURL: "https://jam.brainbaking.com/",
Port: 123,
Token: "token",
AllowedWebmentionSources: []string{"blah.com"},
}
t.Cleanup(func() {
os.Remove("config.json")
})
conf.AddToAllowlist("dinges.be")
assert.Contains(t, conf.Allowlist, "dinges.be")
assert.Equal(t, 2, len(conf.Allowlist))
confFromFile := Configure()
assert.Contains(t, confFromFile.Allowlist, "dinges.be")
assert.Equal(t, 2, len(confFromFile.Allowlist))
}
func TestAddToDenylistNotYetAddsToListAndSaves(t *testing.T) {
conf := Config{
Denylist: []string{
"youtube.com",
},
BaseURL: "https://jam.brainbaking.com/",
Port: 123,
Token: "token",
AllowedWebmentionSources: []string{"blah.com"},
}
t.Cleanup(func() {
os.Remove("config.json")
})
conf.AddToDenylist("dinges.be")
assert.Contains(t, conf.Denylist, "dinges.be")
assert.Equal(t, 2, len(conf.Denylist))
confFromFile := Configure()
assert.Contains(t, confFromFile.Denylist, "dinges.be")
assert.Equal(t, 2, len(confFromFile.Denylist))
}
func TestAddToDenylistAlreadyAddedDoNotAddAgain(t *testing.T) {
conf := Config{
Denylist: []string{
"youtube.com",
},
Port: 123,
Token: "token",
AllowedWebmentionSources: []string{"blah.com"},
}
t.Cleanup(func() {
os.Remove("config.json")
})
conf.AddToDenylist("youtube.com")
assert.Contains(t, conf.Denylist, "youtube.com")
assert.Equal(t, 1, len(conf.Denylist))
}
func TestIsAllowlisted(t *testing.T) {
cases := []struct {
label string
url string
expected bool
}{
{
"do not allowlist if domain is part of relative url",
"https://brainbaking.com/post/youtube.com-sucks",
false,
},
{
"allowlist if https domain is on the list",
"https://youtube.com/stuff",
true,
},
{
"allowlist if http domain is on the list",
"http://youtube.com/stuff",
true,
},
{
"do not allowlist if relative url",
"/youtube.com",
false,
},
}
conf := Config{
Allowlist: []string{
"youtube.com",
},
}
for _, tc := range cases {
t.Run(tc.label, func(t *testing.T) {
assert.Equal(t, tc.expected, conf.IsAllowlisted(tc.url))
})
}
}
func TestIsDenylisted(t *testing.T) {
cases := []struct {
label string
url string
expected bool
}{
{
"do not denylist if domain is part of relative url",
"https://brainbaking.com/post/youtube.com-sucks",
false,
},
{
"denylist if https domain is on the list",
"https://youtube.com/stuff",
true,
},
{
"denylist if http domain is on the list",
"http://youtube.com/stuff",
true,
},
{
"do not denylist if relative url",
"/youtube.com",
false,
},
}
conf := Config{
Denylist: []string{
"youtube.com",
},
}
for _, tc := range cases {
t.Run(tc.label, func(t *testing.T) {
assert.Equal(t, tc.expected, conf.IsDenylisted(tc.url))
})
}
}