diff --git a/wagtail/wagtailembeds/format.py b/wagtail/wagtailembeds/format.py index a2cf72c9d9..f5140af7ce 100644 --- a/wagtail/wagtailembeds/format.py +++ b/wagtail/wagtailembeds/format.py @@ -21,17 +21,15 @@ def embed_to_frontend_html(url): 'ratio': ratio, }) except embeds.EmbedException: + # silently ignore failed embeds, rather than letting them crash the page return '' def embed_to_editor_html(url): - try: - embed = embeds.get_embed(url) + embed = embeds.get_embed(url) + # catching EmbedException is the responsibility of the caller - # Render template - return render_to_string('wagtailembeds/embed_editor.html', { - 'embed': embed, - }) - except embeds.EmbedException: - # Could be replaced with a nice error message - return '' + # Render template + return render_to_string('wagtailembeds/embed_editor.html', { + 'embed': embed, + }) diff --git a/wagtail/wagtailembeds/rich_text.py b/wagtail/wagtailembeds/rich_text.py index 763693c40f..1d52ca48be 100644 --- a/wagtail/wagtailembeds/rich_text.py +++ b/wagtail/wagtailembeds/rich_text.py @@ -1,4 +1,4 @@ -from wagtail.wagtailembeds import format +from wagtail.wagtailembeds import format, embeds class MediaEmbedHandler(object): @@ -26,6 +26,10 @@ class MediaEmbedHandler(object): representation. """ if for_editor: - return format.embed_to_editor_html(attrs['url']) + try: + return format.embed_to_editor_html(attrs['url']) + except embeds.EmbedException: + # Could be replaced with a nice error message + return '' else: return format.embed_to_frontend_html(attrs['url']) diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py index 59a8fd0f09..dc199026f3 100644 --- a/wagtail/wagtailembeds/tests.py +++ b/wagtail/wagtailembeds/tests.py @@ -12,6 +12,7 @@ import django.utils.six.moves.urllib.request from django import template from django.test import TestCase from django.core.exceptions import ValidationError +from django.core.urlresolvers import reverse from django.utils.six.moves.urllib.error import URLError from wagtail.wagtailcore import blocks @@ -112,7 +113,28 @@ class TestChooser(TestCase, WagtailTestUtils): r = self.client.get('/admin/embeds/chooser/') self.assertEqual(r.status_code, 200) - # TODO: Test submitting + @patch('wagtail.wagtailembeds.embeds.get_embed') + def test_submit_valid_embed(self, get_embed): + get_embed.return_value = Embed(html='', title="An example embed") + + response = self.client.post(reverse('wagtailembeds:chooser_upload'), { + 'url': 'http://www.example.com/' + }) + self.assertEqual(response.status_code, 200) + self.assertContains(response, """modal.respond('embedChosen'""") + self.assertContains(response, """An example embed""") + + @patch('wagtail.wagtailembeds.embeds.get_embed') + def test_submit_unrecognised_embed(self, get_embed): + get_embed.side_effect = EmbedNotFoundException + + response = self.client.post(reverse('wagtailembeds:chooser_upload'), { + 'url': 'http://www.example.com/' + }) + self.assertEqual(response.status_code, 200) + self.assertNotContains(response, """modal.respond('embedChosen'""") + self.assertContains(response, """Cannot find an embed for this URL.""") + class TestEmbedly(TestCase): @unittest.skipIf(no_embedly, "Embedly is not installed") diff --git a/wagtail/wagtailembeds/views/chooser.py b/wagtail/wagtailembeds/views/chooser.py index da10e802e9..534427905a 100644 --- a/wagtail/wagtailembeds/views/chooser.py +++ b/wagtail/wagtailembeds/views/chooser.py @@ -8,7 +8,6 @@ from wagtail.wagtailembeds.format import embed_to_editor_html from wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException - def chooser(request): form = EmbedForm()