Browse Source

Added first code and test for HTTP fetcher

tags/0.1
Johann Schmitz 4 years ago
parent
commit
2f5026fdc5
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
5 changed files with 56 additions and 6 deletions
  1. +3
    -1
      requirements.txt
  2. +2
    -1
      requirements_dev.txt
  3. +13
    -1
      src/maxd/fetcher.py
  4. +37
    -2
      tests/test_fetcher.py
  5. +1
    -1
      tests/test_worker.py

+ 3
- 1
requirements.txt View File

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

+ 2
- 1
requirements_dev.txt View File

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

+ 13
- 1
src/maxd/fetcher.py View File

@@ -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)

+ 37
- 2
tests/test_fetcher.py View File

@@ -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'))

+ 1
- 1
tests/test_worker.py View File

@@ -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




Loading…
Cancel
Save