Просмотр исходного кода

Fix test by mocking requests.get function

tags/v1.0.0
Krzysztof Klimonda 11 лет назад
Родитель
Сommit
494f7fbd8c
2 измененных файлов: 13 добавлений и 11 удалений
  1. +7
    -6
      pinry/core/tests/api.py
  2. +6
    -5
      pinry/users/tests.py

+ 7
- 6
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'


+ 6
- 5
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'))
self.assertRedirects(response, reverse('core:recent-pins'))

Загрузка…
Отмена
Сохранить