From 494f7fbd8cfa90f5ba81d88b9435ee4240a1bd07 Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Tue, 19 Mar 2013 15:41:55 +0100 Subject: [PATCH] Fix test by mocking requests.get function --- pinry/core/tests/api.py | 13 +++++++------ pinry/users/tests.py | 11 ++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pinry/core/tests/api.py b/pinry/core/tests/api.py index a05069a..ac6ffb3 100644 --- a/pinry/core/tests/api.py +++ b/pinry/core/tests/api.py @@ -18,8 +18,9 @@ def filter_generator_for(size): return wrapped_func -def mock_urlopen(url): - return open('logo.png') +def mock_requests_get(url): + response = mock.Mock(content=open('logo.png').read()) + return response class ImageResourceTest(ResourceTestCase): @@ -62,7 +63,7 @@ class PinResourceTest(ResourceTestCase): self.user = UserFactory(password='password') self.api_client.client.login(username=self.user.username, password='password') - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_post_create_url(self): post_data = { 'submitter': '/api/v1/user/1/', @@ -74,7 +75,7 @@ class PinResourceTest(ResourceTestCase): self.assertEqual(Pin.objects.count(), 1) self.assertEqual(Image.objects.count(), 1) - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_post_create_url_with_empty_tags(self): url = 'http://testserver/mocked/logo.png' post_data = { @@ -90,7 +91,7 @@ class PinResourceTest(ResourceTestCase): pin = Pin.objects.get(url=url) self.assertEqual(pin.tags.count(), 0) - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_post_create_url_with_empty_origin(self): url = 'http://testserver/mocked/logo.png' post_data = { @@ -105,7 +106,7 @@ class PinResourceTest(ResourceTestCase): self.assertEqual(Image.objects.count(), 1) self.assertEqual(Pin.objects.get(url=url).origin, None) - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_post_create_url_with_origin(self): origin = 'http://testserver/mocked/' url = origin + 'logo.png' diff --git a/pinry/users/tests.py b/pinry/users/tests.py index 97fa15d..10b3ba6 100644 --- a/pinry/users/tests.py +++ b/pinry/users/tests.py @@ -9,8 +9,9 @@ from ..core.models import Image, Pin from .models import User -def mock_urlopen(url): - return open('logo.png') +def mock_requests_get(url): + response = mock.Mock(content=open('logo.png').read()) + return response class CombinedAuthBackendTest(TestCase): @@ -33,14 +34,14 @@ class CombinedAuthBackendTest(TestCase): def test_authenticate_unknown_user(self): self.assertIsNone(self.backend.authenticate(username='wrong-username', password='wrong-password')) - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_has_perm_on_pin(self): image = Image.objects.create_for_url('http://testserver/mocked/screenshot.png') user = User.objects.get(pk=1) pin = Pin.objects.create(submitter=user, image=image) self.assertTrue(self.backend.has_perm(user, 'add_pin', pin)) - @mock.patch('urllib2.urlopen', mock_urlopen) + @mock.patch('requests.get', mock_requests_get) def test_has_perm_on_pin_unauthorized(self): image = Image.objects.create_for_url('http://testserver/mocked/screenshot.png') user = User.objects.get(pk=1) @@ -73,4 +74,4 @@ class LogoutViewTest(TestCase): def test_logout_view(self): response = self.client.get(reverse('users:logout')) - self.assertRedirects(response, reverse('core:recent-pins')) \ No newline at end of file + self.assertRedirects(response, reverse('core:recent-pins'))