diff --git a/src/maxd/fetcher.py b/src/maxd/fetcher.py index 870fb02..c1b95ae 100644 --- a/src/maxd/fetcher.py +++ b/src/maxd/fetcher.py @@ -32,4 +32,11 @@ def fetch(self, calendar_config): if calendar_config.auth: req_kwargs['auth'] = HTTPBasicAuth(calendar_config.username, calendar_config.password) - self.session.get(calendar_config.url, **req_kwargs) \ No newline at end of file + response = self.session.get(calendar_config.url, **req_kwargs) + response.raise_for_status() + calendar = Calendar.from_ical(response.content) + for item in calendar.walk(): + if item.name != "VEVENT": + continue + + yield item diff --git a/tests/test_fetcher.py b/tests/test_fetcher.py index c001e9e..65d07c1 100644 --- a/tests/test_fetcher.py +++ b/tests/test_fetcher.py @@ -5,12 +5,11 @@ import datetime import pytz import sys -from requests.auth import HTTPBasicAuth if sys.version_info.major == 2 or (sys.version_info.major == 3 and sys.version_info.minor <= 2): - from mock import Mock + from mock import Mock, patch else: - from unittest.mock import Mock + from unittest.mock import Mock, patch class TestLocalFetcher(object): @@ -33,22 +32,32 @@ def test_constructor(self): assert isinstance(f.session, requests.sessions.Session) def test_fetch_without_auth(self): + response_mock = Mock() + with open('tests/fixtures/calendars/single_event.ics', 'r') as f: + response_mock.content = f.read() + 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 = Mock(return_value=response_mock) + response = list(f.fetch(CalendarConfig(name='test', url='http://example.com/test.ics'))) f.session.get.assert_called_with('http://example.com/test.ics') + assert len(response) == 1 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 + def get_mock(*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')) + assert len(kwargs) == 1 and 'auth' in kwargs and kwargs['auth'].username == 'foo' and kwargs['auth'].password == 'bar', \ + "A HTTPBasicAuth instance should be passed to requests" + response_mock = Mock() + with open('tests/fixtures/calendars/single_event.ics', 'r') as f: + response_mock.content = f.read() + return response_mock + + f.session.get = Mock(side_effect=get_mock) + response = list(f.fetch(CalendarConfig(name='test', url='http://example.com/test.ics', username='foo', password='bar'))) + assert f.session.get.called + assert len(response) == 1