Pārlūkot izejas kodu

[GIN] Test email filtering: Expect function

Utility function for more concisely defining block expectation in tests.
Achilleas Koutsou 4 gadi atpakaļ
vecāks
revīzija
a4aa034707
1 mainītis faili ar 16 papildinājumiem un 36 dzēšanām
  1. 16 36
      internal/db/db_gin_test.go

+ 16 - 36
internal/db/db_gin_test.go

@@ -38,6 +38,12 @@ var blockMalicious = []string{
 	"- spammer@",
 	"- spammer@",
 }
 }
 
 
+func filterExpect(t *testing.T, address string, expect bool) {
+	if isAddressAllowed(address) != expect {
+		t.Fatalf("Address %q block: expected %t got %t", address, expect, !expect)
+	}
+}
+
 // Writes the filters to a file and returns the directory that contains the
 // Writes the filters to a file and returns the directory that contains the
 // filter file (to be set as setting.CustomPath)
 // filter file (to be set as setting.CustomPath)
 func writeFilterFile(t *testing.T, filters []string) string {
 func writeFilterFile(t *testing.T, filters []string) string {
@@ -72,18 +78,11 @@ func TestAllowGNodeFilter(t *testing.T) {
 	setting.CustomPath = writeFilterFile(t, allowGNodeFilter)
 	setting.CustomPath = writeFilterFile(t, allowGNodeFilter)
 
 
 	for _, address := range emails {
 	for _, address := range emails {
-		if isAddressAllowed(address) {
-			t.Fatalf("Address %q should be blocked but was allowed", address)
-		}
+		filterExpect(t, address, false)
 	}
 	}
 
 
-	if !isAddressAllowed("me@g-node.org") {
-		t.Fatalf("G-Node address blocked but should be allowed")
-	}
-
-	if isAddressAllowed("malicious@g-node.org@piracy.tk") {
-		t.Fatalf("Malicious address should be blocked but was allowed")
-	}
+	filterExpect(t, "me@g-node.org", true)
+	filterExpect(t, "malicious@g-node.org@piracy.tk", false)
 }
 }
 
 
 func TestEverythingFilters(t *testing.T) {
 func TestEverythingFilters(t *testing.T) {
@@ -91,19 +90,13 @@ func TestEverythingFilters(t *testing.T) {
 	rand.Seed(time.Now().UnixNano())
 	rand.Seed(time.Now().UnixNano())
 
 
 	for idx := 0; idx < 100; idx++ {
 	for idx := 0; idx < 100; idx++ {
-		randress := randAddress()
-		if !isAddressAllowed(randAddress()) {
-			t.Fatalf("Address %q should be allowed but was blocked", randress)
-		}
+		filterExpect(t, randAddress(), true)
 	}
 	}
 
 
 	setting.CustomPath = writeFilterFile(t, blockEverythingFilter)
 	setting.CustomPath = writeFilterFile(t, blockEverythingFilter)
 
 
 	for idx := 0; idx < 100; idx++ {
 	for idx := 0; idx < 100; idx++ {
-		randress := randAddress()
-		if isAddressAllowed(randAddress()) {
-			t.Fatalf("Address %q should be blocked but was allowed", randress)
-		}
+		filterExpect(t, randAddress(), false)
 	}
 	}
 }
 }
 
 
@@ -111,26 +104,13 @@ func TestBlockDomainFilter(t *testing.T) {
 	setting.CustomPath = writeFilterFile(t, blockMalicious)
 	setting.CustomPath = writeFilterFile(t, blockMalicious)
 
 
 	// 0, 3 should be allowed; 1, 2 should be blocked
 	// 0, 3 should be allowed; 1, 2 should be blocked
-	if address := emails[0]; !isAddressAllowed(address) {
-		t.Fatalf("Address %q should be allowed but was blocked", address)
-	}
-
-	if address := emails[1]; isAddressAllowed(address) {
-		t.Fatalf("Address %q should be blocked but was allowed", address)
-	}
-
-	if address := emails[2]; isAddressAllowed(address) {
-		t.Fatalf("Address %q should be blocked but was allowed", address)
-	}
-
-	if address := emails[3]; !isAddressAllowed(address) {
-		t.Fatalf("Address %q should be allowed but was blocked", address)
-	}
+	filterExpect(t, emails[0], true)
+	filterExpect(t, emails[1], false)
+	filterExpect(t, emails[2], false)
+	filterExpect(t, emails[3], true)
 }
 }
 
 
 func TestFiltersNone(t *testing.T) {
 func TestFiltersNone(t *testing.T) {
 	setting.CustomPath = filepath.Join(t.TempDir(), "does", "not", "exist")
 	setting.CustomPath = filepath.Join(t.TempDir(), "does", "not", "exist")
-	if address := emails[3]; !isAddressAllowed(address) {
-		t.Fatalf("Address %q should be allowed but was blocked", address)
-	}
+	filterExpect(t, emails[3], true)
 }
 }