소스 검색

[GIN] Test email filtering: Expect function

Utility function for more concisely defining block expectation in tests.
Achilleas Koutsou 4 년 전
부모
커밋
00d310743e
1개의 변경된 파일16개의 추가작업 그리고 36개의 파일을 삭제
  1. 16 36
      internal/db/db_gin_test.go

+ 16 - 36
internal/db/db_gin_test.go

@@ -39,6 +39,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 in the specified custom directory. This file needs to
 // Writes the filters to a file in the specified custom directory. This file needs to
 // be cleaned up afterwards. Returns the full path to the written file as string.
 // be cleaned up afterwards. Returns the full path to the written file as string.
 func writeCustomDirFilterFile(t *testing.T, filters []string) string {
 func writeCustomDirFilterFile(t *testing.T, filters []string) string {
@@ -78,18 +84,11 @@ func TestAllowGNodeFilter(t *testing.T) {
 	defer os.Remove(ffile)
 	defer os.Remove(ffile)
 
 
 	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) {
@@ -104,20 +103,14 @@ 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)
 	}
 	}
 
 
 	ffile = writeCustomDirFilterFile(t, blockEverythingFilter)
 	ffile = writeCustomDirFilterFile(t, blockEverythingFilter)
 	defer os.Remove(ffile)
 	defer os.Remove(ffile)
 
 
 	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)
 	}
 	}
 }
 }
 
 
@@ -131,25 +124,12 @@ func TestBlockDomainFilter(t *testing.T) {
 	defer os.Remove(ffile)
 	defer os.Remove(ffile)
 
 
 	// 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) {
-	if address := emails[3]; !isAddressAllowed(address) {
-		t.Fatalf("Address %q should be allowed but was blocked", address)
-	}
+	filterExpect(t, emails[3], true)
 }
 }