11#!/usr/bin/env python3
2- # -*- coding: utf-8 -*-
2+ # -*- coding: utf-8 -*-https://github.com/domainaware/checkdmarc
33
44"""Automated tests"""
55
2020import checkdmarc .spf
2121import checkdmarc .utils
2222
23+ # Detect if running in GitHub Actions to skip DNS lookups
24+ OFFLINE_MODE = os .environ .get ("GITHUB_ACTIONS" , "false" ).lower () == "true"
25+
2326known_good_domains = ["fbi.gov" , "pm.me" , "ssa.gov" ]
2427
2528
2629class Test (unittest .TestCase ):
30+ @unittest .skipIf (OFFLINE_MODE , "No network access in GitHub Actions" )
31+ def testKnownGood (self ):
32+ """Domains with known good, SPF and DMARC records"""
33+
34+ results = checkdmarc .check_domains (known_good_domains )
35+ for result in results :
36+ spf_error = None
37+ dmarc_error = None
38+ if "error" in result ["spf" ]:
39+ spf_error = result ["spf" ]["error" ]
40+ if "error" in result ["dmarc" ]:
41+ dmarc_error = result ["dmarc" ]["error" ]
42+ self .assertEqual (
43+ result ["spf" ]["valid" ],
44+ True ,
45+ "Known good domain {0} failed SPF check:\n \n {1}" .format (
46+ result ["domain" ], spf_error
47+ ),
48+ )
49+ self .assertEqual (
50+ result ["dmarc" ]["valid" ],
51+ True ,
52+ "Known good domain {0} failed DMARC check:\n \n {1}" .format (
53+ result ["domain" ], dmarc_error
54+ ),
55+ )
56+
2757 def testDMARCMixedFormatting (self ):
2858 """DMARC records with extra spaces and mixed case are still valid"""
2959 examples = [
@@ -75,7 +105,7 @@ def testUppercaseSPFMechanism(self):
75105 self .assertEqual (len (results ["warnings" ]), 0 )
76106 self .assertEqual (results ["dns_lookups" ], 0 )
77107
78- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
108+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
79109 def testSplitSPFRecord (self ):
80110 """Split SPF records are parsed properly"""
81111
@@ -85,7 +115,7 @@ def testSplitSPFRecord(self):
85115
86116 self .assertEqual (parsed_record ["parsed" ]["all" ], "fail" )
87117
88- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
118+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
89119 def testJunkAfterAll (self ):
90120 """Ignore any mechanisms after the all mechanism, but warn about it"""
91121 rec = "v=spf1 ip4:213.5.39.110 -all MS=83859DAEBD1978F9A7A67D3"
@@ -97,12 +127,12 @@ def testJunkAfterAll(self):
97127 parsed_record = checkdmarc .spf .parse_spf_record (rec , domain )
98128 self .assertIn (warning , parsed_record ["warnings" ])
99129
100- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
130+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
101131 def testDNSSEC (self ):
102132 """Test known good DNSSEC"""
103133 self .assertEqual (checkdmarc .dnssec .test_dnssec ("fbi.gov" ), True )
104134
105- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
135+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
106136 def testIncludeMissingSPF (self ):
107137 """A warning is included for SPF records that include domains that are missing SPF records"""
108138
@@ -114,7 +144,7 @@ def testIncludeMissingSPF(self):
114144 )
115145 self .assertEqual (results ["dns_lookups" ], 1 )
116146
117- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
147+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
118148 def testTooManySPFDNSLookups (self ):
119149 """SPF records with > 10 SPF mechanisms that cause DNS lookups raise
120150 SPFTooManyDNSLookups"""
@@ -137,7 +167,7 @@ def testTooManySPFDNSLookups(self):
137167 domain ,
138168 )
139169
140- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
170+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
141171 def testTooManySPFVoidDNSLookups (self ):
142172 """SPF records with > 2 void DNS lookups"""
143173
@@ -263,7 +293,7 @@ def testSPFIncludeLoop(self):
263293 domain ,
264294 )
265295
266- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
296+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
267297 def testSPFMissingMXRecord (self ):
268298 """A warning is issued if an SPF record contains a mx mechanism
269299 pointing to a domain that has no MX records"""
@@ -279,7 +309,7 @@ def testSPFMissingMXRecord(self):
279309 )
280310 self .assertEqual (results ["dns_lookups" ], 1 )
281311
282- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
312+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
283313 def testSPFMissingARecord (self ):
284314 """A warning is issued if an SPF record contains an a mechanism
285315 pointing to a domain that has no A records"""
@@ -291,7 +321,7 @@ def testSPFMissingARecord(self):
291321 self .assertTrue (any (snipit in s for s in results ["warnings" ]))
292322 self .assertEqual (results ["dns_lookups" ], 1 )
293323
294- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
324+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
295325 def testSPFMXMechanism (self ):
296326 """Addresses are included in the output for SPF records with an mx lookup"""
297327 spf_record = "v=spf1 mx:proton.me ~all"
@@ -330,7 +360,7 @@ def testSPFAMechanism(self):
330360 self .assertTrue (len (mechanism ["addresses" ]) > 0 )
331361 self .assertEqual (results ["dns_lookups" ], 1 )
332362
333- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
363+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
334364 def testDMARCPctLessThan100Warning (self ):
335365 """A warning is issued if the DMARC pct value is less than 100"""
336366
@@ -550,7 +580,7 @@ def testDMARCbisTreeWalkDiscovery(self):
550580 self .assertEqual (result ["location" ], "example.com" )
551581 self .assertEqual (result ["record" ], "v=DMARC1; p=reject" )
552582
553- @unittest .skipUnless ( os . path . exists ( "/etc/resolv.conf" ) , "no network" )
583+ @unittest .skipIf ( OFFLINE_MODE , "No network access in GitHub Actions " )
554584 def testBIMI (self ):
555585 """Test BIMI checks"""
556586 domain = "chase.com"
0 commit comments