|
| 1 | +package banlist |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestBanlistMissingFile(t *testing.T) { |
| 17 | + bl := newBanlist(tl(t), "not a path") |
| 18 | + require.Error(t, bl.update()) |
| 19 | +} |
| 20 | + |
| 21 | +func TestBanlistInvalidFileContents(t *testing.T) { |
| 22 | + path, err := ioutil.TempFile("", "") |
| 23 | + require.NoError(t, err) |
| 24 | + defer os.Remove(path.Name()) |
| 25 | + |
| 26 | + _, err = path.WriteString("this isn't valid json") |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + bl := newBanlist(tl(t), path.Name()) |
| 30 | + require.Error(t, bl.update()) |
| 31 | +} |
| 32 | + |
| 33 | +func TestBanlistNoPaths(t *testing.T) { |
| 34 | + bl := testList(t, &Config{ |
| 35 | + Domains: []string{"something.com"}, |
| 36 | + }) |
| 37 | + |
| 38 | + assert.Empty(t, bl.urls()) |
| 39 | + domains := bl.domains() |
| 40 | + assert.Len(t, domains, 1) |
| 41 | + _, ok := domains["something.com"] |
| 42 | + assert.True(t, ok) |
| 43 | +} |
| 44 | + |
| 45 | +func TestBanlistNoDomains(t *testing.T) { |
| 46 | + bl := testList(t, &Config{ |
| 47 | + URLs: []string{"something.com/path/to/thing"}, |
| 48 | + }) |
| 49 | + |
| 50 | + urls := bl.urls() |
| 51 | + assert.Len(t, urls, 1) |
| 52 | + _, ok := urls["something.com/path/to/thing"] |
| 53 | + assert.True(t, ok) |
| 54 | + |
| 55 | + assert.Empty(t, bl.domains()) |
| 56 | +} |
| 57 | +func TestBanlistBanning(t *testing.T) { |
| 58 | + bl := testList(t, &Config{ |
| 59 | + URLs: []string{"villians.com/the/joker"}, |
| 60 | + Domains: []string{"sick.com"}, |
| 61 | + }) |
| 62 | + |
| 63 | + tests := []struct { |
| 64 | + url string |
| 65 | + isBanned bool |
| 66 | + name string |
| 67 | + }{ |
| 68 | + {"http://heros.com", false, "completely unbanned"}, |
| 69 | + {"http://sick.com:12345", true, "banned domain with port"}, |
| 70 | + {"http://sick.com", true, "banned domain without port"}, |
| 71 | + {"http://siCK.com", true, "banned domain mixed case"}, |
| 72 | + {"http://villians.com:12354/the/joker", true, "banned path with port"}, |
| 73 | + {"http://villians.com/the/joker", true, "banned path without port"}, |
| 74 | + {"http://villians.com/the/Joker", true, "banned path mixed case"}, |
| 75 | + {"http://villians.com/the/joker?query=param", true, "banned path with query params"}, |
| 76 | + } |
| 77 | + for _, test := range tests { |
| 78 | + t.Run(test.name, func(t *testing.T) { |
| 79 | + req := httptest.NewRequest(http.MethodGet, test.url, nil) |
| 80 | + assert.Equal(t, test.isBanned, bl.CheckRequest(req)) |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func tl(t *testing.T) logrus.FieldLogger { |
| 86 | + l := logrus.New() |
| 87 | + l.SetLevel(logrus.DebugLevel) |
| 88 | + return l.WithField("test", t.Name()) |
| 89 | +} |
| 90 | + |
| 91 | +func testList(t *testing.T, config *Config) *Banlist { |
| 92 | + path, err := ioutil.TempFile("", "") |
| 93 | + require.NoError(t, err) |
| 94 | + defer os.Remove(path.Name()) |
| 95 | + |
| 96 | + require.NoError(t, json.NewEncoder(path).Encode(config)) |
| 97 | + |
| 98 | + bl := newBanlist(tl(t), path.Name()) |
| 99 | + require.NoError(t, bl.update()) |
| 100 | + return bl |
| 101 | +} |
0 commit comments