Skip embedly tests if embedly not installed. Split up tests.

This commit is contained in:
Tom Talbot 2014-06-10 11:13:48 +01:00
parent 504bc1c4f6
commit da361bbd01
4 changed files with 128 additions and 118 deletions

View file

@ -0,0 +1,39 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
from wagtail.wagtailcore.models import Page
from wagtail.tests.models import SimplePage
from wagtail.tests.utils import login
class TestChooserBrowse(TestCase):
def setUp(self):
self.root_page = Page.objects.get(id=2)
# Add child page
self.child_page = SimplePage()
self.child_page.title = "foobarbaz"
self.child_page.slug = "foobarbaz"
self.root_page.add_child(instance=self.child_page)
login(self.client)
def get(self, params={}):
return self.client.get(reverse('wagtailadmin_choose_page'), params)
def test_status_code(self):
self.assertEqual(self.get().status_code, 200)
def test_search(self):
response = self.get({'q': "foobarbaz"})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "There is one match")
self.assertContains(response, "foobarbaz")
def test_search_no_results(self):
response = self.get({'q': "quux"})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "There are 0 matches")
def test_get_invalid(self):
response = self.get({'page_type': 'foo.bar'})
self.assertEqual(response.status_code, 404)

View file

@ -46,40 +46,6 @@ class TestSendEmailTask(TestCase):
self.assertEqual(mail.outbox[0].to, ["nobody@email.com"])
class TestChooserBrowse(TestCase):
def setUp(self):
self.root_page = Page.objects.get(id=2)
# Add child page
self.child_page = SimplePage()
self.child_page.title = "foobarbaz"
self.child_page.slug = "foobarbaz"
self.root_page.add_child(instance=self.child_page)
login(self.client)
def get(self, params={}):
return self.client.get(reverse('wagtailadmin_choose_page'), params)
def test_status_code(self):
self.assertEqual(self.get().status_code, 200)
def test_search(self):
response = self.get({'q': "foobarbaz"})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "There is one match")
self.assertContains(response, "foobarbaz")
def test_search_no_results(self):
response = self.get({'q': "quux"})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "There are 0 matches")
def test_get_invalid(self):
response = self.get({'page_type': 'foo.bar'})
self.assertEqual(response.status_code, 404)
class TestChooserBrowseChild(TestCase):
def setUp(self):
self.root_page = Page.objects.get(id=2)

View file

@ -19,11 +19,6 @@ class EmbedNotFoundException(Exception): pass
class EmbedlyException(Exception): pass
class AccessDeniedEmbedlyException(EmbedlyException): pass
# This is here for unit testing. It is used if Embedly is not installed.
class MockEmbedly():
def oembed():
pass
# Pinched from django 1.7 source code.
# TODO: Replace this with "from django.utils.module_loading import import_string" when django 1.7 is released
@ -49,17 +44,14 @@ def import_string(dotted_path):
def embedly(url, max_width=None, key=None):
from embedly import Embedly
# Get embedly client
client = Embedly(key=key)
# Get embedly key
if key is None:
key = settings.EMBEDLY_KEY
try:
from embedly import Embedly
# Get embedly client
client = Embedly(key=key)
except ImportError:
client = MockEmbedly()
# Call embedly
if max_width is not None:
oembed = client.oembed(url, maxwidth=max_width, better=False)

View file

@ -2,23 +2,24 @@ from mock import patch
import urllib2
try:
from embedly import Embedly
patch_me = 'embedly.Embedly.oembed'
import embedly
no_embedly = False
except ImportError:
patch_me = 'wagtail.wagtailembeds.embeds.MockEmbedly.oembed'
no_embedly = True
from django.test import TestCase
from django.test.client import Client
from wagtail.tests.utils import login
from wagtail.tests.utils import unittest
from wagtail.wagtailembeds import get_embed
from wagtail.wagtailembeds.embeds import (
embedly,
EmbedNotFoundException,
EmbedlyException,
AccessDeniedEmbedlyException,
MockEmbedly
)
from wagtail.wagtailembeds.embeds import embedly as wagtail_embedly
from wagtail.wagtailembeds.embeds import oembed as wagtail_oembed
@ -92,84 +93,96 @@ class TestChooser(TestCase):
# TODO: Test submitting
class TestEmbedly(TestCase):
@patch(patch_me)
def test_embedly_oembed_called_with_correct_arguments(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_oembed_called_with_correct_arguments(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
embedly('http://www.example.com', key='foo')
oembed.assert_called_with('http://www.example.com', better=False)
wagtail_embedly('http://www.example.com', key='foo')
oembed.assert_called_with('http://www.example.com', better=False)
embedly('http://www.example.com', max_width=100, key='foo')
oembed.assert_called_with('http://www.example.com', maxwidth=100, better=False)
wagtail_embedly('http://www.example.com', max_width=100, key='foo')
oembed.assert_called_with('http://www.example.com', maxwidth=100, better=False)
@patch(patch_me)
def test_embedly_errors(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com',
'error': True,
'error_code': 401}
self.assertRaises(AccessDeniedEmbedlyException,
embedly, 'http://www.example.com', key='foo')
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_401(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com',
'error': True,
'error_code': 401}
self.assertRaises(AccessDeniedEmbedlyException,
wagtail_embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 403
self.assertRaises(AccessDeniedEmbedlyException,
embedly, 'http://www.example.com', key='foo')
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_403(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value['error_code'] = 403
self.assertRaises(AccessDeniedEmbedlyException,
wagtail_embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 404
self.assertRaises(EmbedNotFoundException,
embedly, 'http://www.example.com', key='foo')
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_404(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value['error_code'] = 404
self.assertRaises(EmbedNotFoundException,
wagtail_embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 999
self.assertRaises(EmbedlyException, embedly,
'http://www.example.com', key='foo')
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_other_error(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value['error_code'] = 999
self.assertRaises(EmbedlyException, wagtail_embedly,
'http://www.example.com', key='foo')
@patch(patch_me)
def test_embedly_html_conversion(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<img src="http://www.example.com" />')
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_html_conversion(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
result = wagtail_embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<img src="http://www.example.com" />')
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<foo>bar</foo>')
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = wagtail_embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<foo>bar</foo>')
@patch(patch_me)
def test_embedly_return_value(self, oembed):
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result, {
'title': '',
'author_name': '',
'provider_name': '',
'type': 'something else',
'thumbnail_url': None,
'width': None,
'height': None,
'html': '<foo>bar</foo>'})
@unittest.skipIf(no_embedly, "Embedly is not installed")
def test_embedly_return_value(self):
with patch('embedly.Embedly.oembed') as oembed:
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = wagtail_embedly('http://www.example.com', key='foo')
self.assertEqual(result, {
'title': '',
'author_name': '',
'provider_name': '',
'type': 'something else',
'thumbnail_url': None,
'width': None,
'height': None,
'html': '<foo>bar</foo>'})
oembed.return_value = {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result, {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'})
oembed.return_value = {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'}
result = wagtail_embedly('http://www.example.com', key='foo')
self.assertEqual(result, {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'})
class TestOembed(TestCase):