domain_test.go (2291B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package domain_test 19 20 import ( 21 "errors" 22 "testing" 23 24 "github.com/superseriousbusiness/gotosocial/internal/cache/domain" 25 ) 26 27 func TestBlockCache(t *testing.T) { 28 c := new(domain.BlockCache) 29 30 blocks := []string{ 31 "google.com", 32 "google.co.uk", 33 "pleroma.bad.host", 34 } 35 36 loader := func() ([]string, error) { 37 t.Log("load: returning blocked domains") 38 return blocks, nil 39 } 40 41 // Check a list of known blocked domains. 42 for _, domain := range []string{ 43 "google.com", 44 "mail.google.com", 45 "google.co.uk", 46 "mail.google.co.uk", 47 "pleroma.bad.host", 48 "dev.pleroma.bad.host", 49 } { 50 t.Logf("checking domain is blocked: %s", domain) 51 if b, _ := c.IsBlocked(domain, loader); !b { 52 t.Errorf("domain should be blocked: %s", domain) 53 } 54 } 55 56 // Check a list of known unblocked domains. 57 for _, domain := range []string{ 58 "askjeeves.com", 59 "ask-kim.co.uk", 60 "google.ie", 61 "mail.google.ie", 62 "gts.bad.host", 63 "mastodon.bad.host", 64 } { 65 t.Logf("checking domain isn't blocked: %s", domain) 66 if b, _ := c.IsBlocked(domain, loader); b { 67 t.Errorf("domain should not be blocked: %s", domain) 68 } 69 } 70 71 // Clear the cache 72 t.Logf("%+v\n", c) 73 c.Clear() 74 t.Logf("%+v\n", c) 75 76 knownErr := errors.New("known error") 77 78 // Check that reload is actually performed and returns our error 79 if _, err := c.IsBlocked("", func() ([]string, error) { 80 t.Log("load: returning known error") 81 return nil, knownErr 82 }); !errors.Is(err, knownErr) { 83 t.Errorf("is blocked did not return expected error: %v", err) 84 } 85 }