Skip to content

Commit 6397c70

Browse files
committed
Execute tests in the Europe/Brussels timezone (#12)
1 parent f1e5b23 commit 6397c70

File tree

6 files changed

+106
-13
lines changed

6 files changed

+106
-13
lines changed

DMARCReporting/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from lxml import etree as ET
21
from datetime import datetime
32

3+
from lxml import etree as ET
4+
45
xpath_all = ".//record"
56
xpath_failed = (
67
".//record["

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ format: ## Format source and test code using black
3030
pipenv run black --skip-string-normalization tests
3131

3232
test: lint ## Run unit tests
33-
pipenv run pytest -vv
33+
pipenv run pytest -vv -s
3434

3535
dist: clean ## Creates a source distribution and wheel distribution
3636
pipenv run python setup.py sdist bdist_wheel

tests/test_acceptance.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1-
import io
1+
import os
22
import sys
3+
import io
4+
import time
35
from unittest.mock import patch
46

7+
import pytest
8+
59
from .context import DMARCReporting # noqa F401
610
from DMARCReporting.cli import CLI
711

12+
test_tz = 'Europe/Brussels'
13+
env_tz = ''
14+
15+
16+
@pytest.fixture
17+
def setup_timezone():
18+
env_tz = time.tzname[0]
19+
print()
20+
print(f"save current timezone: {env_tz}")
21+
print(f"set timezone for tests to {test_tz}")
22+
os.environ['TZ'] = test_tz
23+
time.tzset()
24+
print(f"timezone is now {time.tzname[0]}")
25+
yield
26+
print()
27+
print(f"reset timezone back to {env_tz}")
28+
os.environ['TZ'] = env_tz
29+
time.tzset()
30+
print(f"timezone is now {time.tzname[0]}")
31+
832

933
@patch('socket.gethostbyaddr')
10-
def test_render(gethostbyaddr_mock):
34+
def test_render(gethostbyaddr_mock, setup_timezone):
1135
gethostbyaddr_mock.side_effect = [
1236
("Unknown host", [], []),
1337
("208-90-221-45.static.flhsi.com", [], []),
@@ -38,7 +62,7 @@ def test_render(gethostbyaddr_mock):
3862

3963

4064
@patch('socket.gethostbyaddr')
41-
def test_render_all(gethostbyaddr_mock):
65+
def test_render_all(gethostbyaddr_mock, setup_timezone):
4266
gethostbyaddr_mock.side_effect = [
4367
("smtp.bellous.com", [], []),
4468
("smtp.bellous.com", [], []),

tests/test_parser_dkim.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1+
import os
12
import io
3+
import time
24

35
import pytest
46

57
from .context import DMARCReporting # noqa F401
68
from DMARCReporting.parser import DMARCRuaParser
79

10+
test_tz = 'Europe/Brussels'
11+
env_tz = ''
12+
13+
14+
@pytest.fixture
15+
def setup_timezone():
16+
env_tz = time.tzname[0]
17+
print()
18+
print(f"save current timezone: {env_tz}")
19+
print(f"set timezone for tests to {test_tz}")
20+
os.environ['TZ'] = test_tz
21+
time.tzset()
22+
print(f"timezone is now {time.tzname[0]}")
23+
yield
24+
print()
25+
print(f"reset timezone back to {env_tz}")
26+
os.environ['TZ'] = env_tz
27+
time.tzset()
28+
print(f"timezone is now {time.tzname[0]}")
29+
830

931
@pytest.fixture
1032
def not_authenticated():
@@ -50,7 +72,7 @@ def reverse_name(self, ipv4):
5072
return "mail.email.com"
5173

5274

53-
def test_when_not_authenticated(not_authenticated):
75+
def test_when_not_authenticated(not_authenticated, setup_timezone):
5476
sut = DMARCRuaParser(DNSStub())
5577
actual = sut.parse(not_authenticated)
5678
expected = [
@@ -116,7 +138,7 @@ def authenticated_two_records():
116138
)
117139

118140

119-
def test_when_not_authenticated_having_two_records(authenticated_two_records):
141+
def test_when_not_authenticated_having_two_records(authenticated_two_records, setup_timezone):
120142
sut = DMARCRuaParser(DNSStub())
121143
actual = sut.parse(authenticated_two_records)
122144
expected = [

tests/test_parser_dmarc.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
import os
12
import io
3+
import time
24

35
import pytest
46

57
from .context import DMARCReporting # noqa F401
68
from DMARCReporting.parser import DMARCRuaParser
79

810

11+
test_tz = 'Europe/Brussels'
12+
env_tz = ''
13+
14+
15+
@pytest.fixture
16+
def setup_timezone():
17+
env_tz = time.tzname[0]
18+
print()
19+
print(f"save current timezone: {env_tz}")
20+
print(f"set timezone for tests to {test_tz}")
21+
os.environ['TZ'] = test_tz
22+
time.tzset()
23+
print(f"timezone is now {time.tzname[0]}")
24+
yield
25+
print()
26+
print(f"reset timezone back to {env_tz}")
27+
os.environ['TZ'] = env_tz
28+
time.tzset()
29+
print(f"timezone is now {time.tzname[0]}")
30+
31+
932
def rua_report(disposition="none", spf_aligned="pass", dkim_aligned="pass"):
1033
return io.StringIO(
1134
"""
@@ -85,7 +108,7 @@ def reverse_name(self, ipv4):
85108
return "mail.email.com"
86109

87110

88-
def test_when_dmarc_disposition_quarantine(rua_report_quarantine):
111+
def test_when_dmarc_disposition_quarantine(rua_report_quarantine, setup_timezone):
89112
sut = DMARCRuaParser(DNSStub())
90113
actual = sut.parse(rua_report_quarantine)
91114
expected = [
@@ -107,13 +130,13 @@ def test_when_dmarc_disposition_quarantine(rua_report_quarantine):
107130
assert expected == actual
108131

109132

110-
def test_when_dmarc_disposition_none(rua_report_none):
133+
def test_when_dmarc_disposition_none(rua_report_none, setup_timezone):
111134
sut = DMARCRuaParser(DNSStub())
112135
actual = sut.parse(rua_report_none)
113136
assert [] == actual
114137

115138

116-
def test_when_dmarc_disposition_reject(rua_report_reject):
139+
def test_when_dmarc_disposition_reject(rua_report_reject, setup_timezone):
117140
sut = DMARCRuaParser(DNSStub())
118141
actual = sut.parse(rua_report_reject)
119142
expected = [
@@ -135,13 +158,13 @@ def test_when_dmarc_disposition_reject(rua_report_reject):
135158
assert expected == actual
136159

137160

138-
def test_when_spf_and_dkim_aligned(rua_report_spf_and_dkim_aligned):
161+
def test_when_spf_and_dkim_aligned(rua_report_spf_and_dkim_aligned, setup_timezone):
139162
sut = DMARCRuaParser(DNSStub())
140163
actual = sut.parse(rua_report_spf_and_dkim_aligned)
141164
assert [] == actual
142165

143166

144-
def test_when_spf_not_aligned(rua_report_spf_not_aligned):
167+
def test_when_spf_not_aligned(rua_report_spf_not_aligned, setup_timezone):
145168
sut = DMARCRuaParser(DNSStub())
146169
actual = sut.parse(rua_report_spf_not_aligned)
147170
expected = [
@@ -163,7 +186,7 @@ def test_when_spf_not_aligned(rua_report_spf_not_aligned):
163186
assert expected == actual
164187

165188

166-
def test_when_dkim_not_aligned(rua_report_dkim_not_aligned):
189+
def test_when_dkim_not_aligned(rua_report_dkim_not_aligned, setup_timezone):
167190
sut = DMARCRuaParser(DNSStub())
168191
actual = sut.parse(rua_report_dkim_not_aligned)
169192
expected = [

tests/test_parser_spf.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
import os
12
import io
3+
import time
24

35
import pytest
46

57
from .context import DMARCReporting # noqa F401
68
from DMARCReporting.parser import DMARCRuaParser
79

810

11+
test_tz = 'Europe/Brussels'
12+
env_tz = ''
13+
14+
15+
@pytest.fixture
16+
def setup_timezone():
17+
env_tz = time.tzname[0]
18+
print()
19+
print(f"save current timezone: {env_tz}")
20+
print(f"set timezone for tests to {test_tz}")
21+
os.environ['TZ'] = test_tz
22+
time.tzset()
23+
print(f"timezone is now {time.tzname[0]}")
24+
yield
25+
print()
26+
print(f"reset timezone back to {env_tz}")
27+
os.environ['TZ'] = env_tz
28+
time.tzset()
29+
print(f"timezone is now {time.tzname[0]}")
30+
31+
932
@pytest.fixture
1033
def not_authenticated():
1134
return io.StringIO(

0 commit comments

Comments
 (0)