Skip to content

Commit

Permalink
Merge pull request #43 from imapautofiler/fix-time-limit-pr-32
Browse files Browse the repository at this point in the history
Fix comparison with TZ aware datetime in TimeLimit rule
  • Loading branch information
dhellmann committed Sep 7, 2019
2 parents 6f40f7f + 1017b0c commit b3c9c0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
11 changes: 9 additions & 2 deletions imapautofiler/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import logging
import re
from email.utils import parsedate_to_datetime
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from imapautofiler import i18n
from imapautofiler import lookup

Expand Down Expand Up @@ -268,8 +268,15 @@ def __init__(self, rule_data, cfg, ):

def check(self, message):
date = parsedate_to_datetime(i18n.get_header_value(message, 'date'))

# RFC2822 dates ending with '-0000' create timezone naive datetimes
# so we need to manually set their timezone so we can compare them
# with TZ aware datetimes.
if date.tzinfo is None:
date = date.replace(tzinfo=timezone.utc)

if self._age:
time_limit = datetime.now() - timedelta(days=self._age)
time_limit = datetime.now(timezone.utc) - timedelta(days=self._age)
if date <= time_limit:
return True
else:
Expand Down
23 changes: 19 additions & 4 deletions imapautofiler/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import unittest

import fixtures
from datetime import datetime, timezone, timedelta


def construct_message(headers):
Expand All @@ -31,9 +32,9 @@ def construct_message(headers):
return msg.as_string()


date = format_datetime(datetime.datetime.now())
date = format_datetime(datetime.now(timezone.utc))
past_date = format_datetime(
datetime.datetime.now() - datetime.timedelta(days=90)
datetime.now(timezone.utc) - timedelta(days=90)
)
MESSAGE = {
'From': 'Sender Name <sender@example.com>',
Expand All @@ -44,7 +45,7 @@ def construct_message(headers):
'Mime-Version': r'1.0 (Mac OS X Mail 9.2 \(3112\))',
'X-Smtp-Server': 'AE35BF63-D70A-4AB0-9FAA-3F18EB9802A9',
'Subject': 'Re: reply to previous message',
'Date': '{}'.format(past_date),
'Date': past_date,
'X-Universally-Unique-Identifier': 'CC844EE1-C406-4ABA-9DA5-685759BBC15A',
'References': '<33509d2c-e2a7-48c0-8bf3-73b4ba352b2f@example.com>',
'To': 'recipient1@example.com',
Expand All @@ -62,14 +63,20 @@ def construct_message(headers):

RECENT_MESSAGE = MESSAGE.copy()
RECENT_MESSAGE.update({
'Date': '{}'.format(date),
'Date': date,
})

WITHOUT_OFFSET_MESSAGE = MESSAGE.copy()
WITHOUT_OFFSET_MESSAGE.update({
'Date': 'Thu, 07 Sep 2000 20:57:30 -0000',
})


class TestCase(unittest.TestCase):
_msg = None
_recent_msg = None
_i18n_msg = None
_without_offset_msg = None

def setUp(self):
super().setUp()
Expand Down Expand Up @@ -108,6 +115,14 @@ def recent_msg(self):
)
return self._recent_msg

@property
def without_offset_msg(self):
if self._without_offset_msg is None:
self._without_offset_msg = email.parser.Parser().parsestr(
construct_message(WITHOUT_OFFSET_MESSAGE)
)
return self._without_offset_msg


def pytest_generate_tests(metafunc):
# from https://docs.pytest.org/en/latest/example/parametrize.html#a-quick-port-of-testscenarios # noqa
Expand Down
4 changes: 4 additions & 0 deletions imapautofiler/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,7 @@ def test_time_limit_expired(self):
def test_time_limit_current(self):
r = rules.TimeLimit(self.get_def(), {})
self.assertEqual(r.check(self.recent_msg), 0)

def test_time_limit_expired_without_offset(self):
r = rules.TimeLimit(self.get_def(), {})
self.assertTrue(r.check(self.without_offset_msg))

0 comments on commit b3c9c0a

Please sign in to comment.