diff --git a/wagtail/wagtailadmin/tests/test_page_chooser.py b/wagtail/wagtailadmin/tests/test_page_chooser.py
new file mode 100644
index 000000000..036619e2c
--- /dev/null
+++ b/wagtail/wagtailadmin/tests/test_page_chooser.py
@@ -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)
diff --git a/wagtail/wagtailadmin/tests/tests.py b/wagtail/wagtailadmin/tests/tests.py
index 2a0c5e886..cb8e0ec29 100644
--- a/wagtail/wagtailadmin/tests/tests.py
+++ b/wagtail/wagtailadmin/tests/tests.py
@@ -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)
diff --git a/wagtail/wagtailembeds/embeds.py b/wagtail/wagtailembeds/embeds.py
index 312f22f2e..38c8ee50a 100644
--- a/wagtail/wagtailembeds/embeds.py
+++ b/wagtail/wagtailembeds/embeds.py
@@ -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)
diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py
index c942b158d..abb7bb049 100644
--- a/wagtail/wagtailembeds/tests.py
+++ b/wagtail/wagtailembeds/tests.py
@@ -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'], '
')
+ @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'], '
')
- oembed.return_value = {'type': 'something else',
- 'html': 'bar'}
- result = embedly('http://www.example.com', key='foo')
- self.assertEqual(result['html'], 'bar')
+ oembed.return_value = {'type': 'something else',
+ 'html': 'bar'}
+ result = wagtail_embedly('http://www.example.com', key='foo')
+ self.assertEqual(result['html'], 'bar')
- @patch(patch_me)
- def test_embedly_return_value(self, oembed):
- oembed.return_value = {'type': 'something else',
- 'html': 'bar'}
- 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': 'bar'})
+ @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': 'bar'}
+ 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': 'bar'})
- 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': 'bar'}
- 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': 'bar'})
+ 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': 'bar'}
+ 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': 'bar'})
class TestOembed(TestCase):