diff --git a/drop_service/views.py b/drop_service/views.py index f718836..4c05264 100644 --- a/drop_service/views.py +++ b/drop_service/views.py @@ -1,5 +1,6 @@ import datetime import json +import logging import uuid from email.utils import formatdate from time import mktime @@ -16,6 +17,8 @@ from .notify import get_notificators from .util import CsrfExemptView, check_drop_id, set_last_modified, utc_timestamp +logger = logging.getLogger(__name__) + def error(msg, status=status.HTTP_400_BAD_REQUEST): return HttpResponse(json.dumps({'error': msg}), status=status) @@ -37,7 +40,8 @@ def _get_drops(self, drop_id): try: have_since, since = self.get_if_modified_since() except ValueError as value_error: - return error(str(value_error)), None + logger.warning('Could not parse modified-since header pack: %s', value_error) + have_since, since = False, None if have_since: drops = drops.filter(created_at__gt=since) @@ -88,7 +92,10 @@ def get_if_modified_since(self): if coarse_since and finest_since: raise ValueError('Specify only one of X-Qabel-New-Since, If-Modified-Since') if coarse_since: - return True, dateparser.parse(coarse_since) + since = dateparser.parse(coarse_since) + if not since: + raise ValueError('Unable to parse If-Modified-Since') + return True, since elif finest_since: return True, datetime.datetime.fromtimestamp(float(finest_since), datetime.timezone.utc) else: diff --git a/tests/test_rest.py b/tests/test_rest.py index f0ed549..5afa53c 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -57,7 +57,18 @@ def test_get_messages_posted_since(self): HTTP_IF_MODIFIED_SINCE=format_datetime(dt, usegmt=True)) assert response.status_code == status.HTTP_200_OK - assert 'Hello World' in response.content.decode() + body = response.content.decode() + assert 'Hello World' in body + assert 'Bar' not in body + + def test_get_messages_posted_since_invalid(self): + response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo', + HTTP_IF_MODIFIED_SINCE='warghabl') + + assert response.status_code == status.HTTP_200_OK + body = response.content.decode() + assert 'Hello World' in body + assert 'Bar' not in body def test_get_messages_posted_since_qabel(self): response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo', @@ -68,6 +79,15 @@ def test_get_messages_posted_since_qabel(self): assert 'Hello World' in body assert 'Bar' not in body + def test_get_messages_posted_since_qabel_invalid(self): + response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo', + HTTP_X_QABEL_NEW_SINCE='warghabl') + + assert response.status_code == status.HTTP_200_OK + body = response.content.decode() + assert 'Hello World' in body + assert 'Bar' not in body + def test_get_qabel_round_trip(self): response = self.app.get('/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopo') assert response.status_code == status.HTTP_200_OK