Browse Source

Added first code and test for HTTP fetcher

master
Johann Schmitz 5 years ago
parent
commit
2f5026fdc5
Signed by: ercpe GPG Key ID: A084064277C501ED
  1. 4
      requirements.txt
  2. 3
      requirements_dev.txt
  3. 14
      src/maxd/fetcher.py
  4. 39
      tests/test_fetcher.py
  5. 2
      tests/test_worker.py

4
requirements.txt

@ -2,4 +2,6 @@
icalendar
pytz
python-dateutil
pymax>=0.2
pymax>=0.2
CacheControl
requests

3
requirements_dev.txt

@ -1,3 +1,4 @@
# Project dependencies for development
pytest
coveralls
coveralls
mock

14
src/maxd/fetcher.py

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
from icalendar import Calendar
from cachecontrol import CacheControl
import requests
from requests.auth import HTTPBasicAuth
class EventFetcher(object):
@ -20,4 +23,13 @@ class LocalCalendarEventFetcher(EventFetcher):
class HTTPCalendarEventFetcher(EventFetcher):
pass
def __init__(self):
self.session = CacheControl(requests.session())
def fetch(self, calendar_config):
req_kwargs = {}
if calendar_config.auth:
req_kwargs['auth'] = HTTPBasicAuth(calendar_config.username, calendar_config.password)
self.session.get(calendar_config.url, **req_kwargs)

39
tests/test_fetcher.py

@ -1,10 +1,18 @@
# -*- coding: utf-8 -*-
from maxd.config import CalendarConfig
from maxd.fetcher import LocalCalendarEventFetcher
from maxd.fetcher import LocalCalendarEventFetcher, HTTPCalendarEventFetcher
import requests
import datetime
import pytz
import sys
from requests.auth import HTTPBasicAuth
class TestFetcher(object):
if sys.version_info.major == 2 or (sys.version_info.major == 3 and sys.version_info.minor <= 2):
from mock import Mock
else:
from unittest.mock import Mock
class TestLocalFetcher(object):
def test_local_fetcher(self):
f = LocalCalendarEventFetcher()
@ -17,3 +25,30 @@ class TestFetcher(object):
assert event['DTSTART'].dt == datetime.datetime(2015, 12, 20, 9, 0, tzinfo=pytz.UTC)
assert event['DTEND'].dt == datetime.datetime(2015, 12, 20, 10, 0, tzinfo=pytz.UTC)
class TestHTTPFetcher(object):
def test_constructor(self):
f = HTTPCalendarEventFetcher()
assert isinstance(f.session, requests.sessions.Session)
def test_fetch_without_auth(self):
f = HTTPCalendarEventFetcher()
f.session = Mock()
f.session.get = Mock(return_value=None)
f.fetch(CalendarConfig(name='test', url='http://example.com/test.ics'))
f.session.get.assert_called_with('http://example.com/test.ics')
def test_fetch_with_auth(self):
f = HTTPCalendarEventFetcher()
f.session = Mock()
def _assert_get(*args, **kwargs): # stupid way to get around the not implemented __eq__ for HttpBasicAuth
assert len(args) == 1 and args[0] == 'http://example.com/test.ics'
assert len(kwargs) == 1 and 'auth' in kwargs and kwargs['auth'].username == 'foo' and kwargs['auth'].password == 'bar'
f.session.get = _assert_get
f.fetch(CalendarConfig(name='test', url='http://example.com/test.ics', username='foo', password='bar'))

2
tests/test_worker.py

@ -10,7 +10,7 @@ try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from maxd.config import Configuration, CalendarConfig
from maxd.config import Configuration
from maxd.worker import Worker, Schedule, _to_utc_datetime