Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Commit

Permalink
More tests for HTTP fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ercpe committed Dec 29, 2015
1 parent 2f5026f commit a0b1cf1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/maxd/fetcher.py
Expand Up @@ -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)
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
33 changes: 21 additions & 12 deletions tests/test_fetcher.py
Expand Up @@ -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):

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

0 comments on commit a0b1cf1

Please sign in to comment.