Example #1
0
    def test_direct_call(self, get_embed):
        get_embed.return_value = Embed(
            html='<img src="http://www.example.com" />')

        result = embed_tag('http://www.youtube.com/watch/')

        self.assertEqual(result, '<img src="http://www.example.com" />')
Example #2
0
    def test_expand_db_html_with_embed(self, get_embed):
        from wagtail.embeds.models import Embed

        get_embed.return_value = Embed(html="test html")
        html = '<embed embedtype="media" url="http://www.youtube.com/watch" />'
        result = expand_db_html(html)
        self.assertIn("test html", result)
Example #3
0
    def test_render_within_structblock(self, get_embed):
        """
        When rendering the value of an EmbedBlock directly in a template
        (as happens when accessing it as a child of a StructBlock), the
        proper embed output should be rendered, not the URL.
        """
        get_embed.return_value = Embed(html="<h1>Hello world!</h1>")

        block = blocks.StructBlock([
            ("title", blocks.CharBlock()),
            ("embed", EmbedBlock()),
        ])

        block_val = block.to_python({
            "title": "A test",
            "embed": "http://www.example.com/foo"
        })

        temp = template.Template("embed: {{ self.embed }}")
        context = template.Context({"self": block_val})
        result = temp.render(context)

        self.assertIn("<h1>Hello world!</h1>", result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call("http://www.example.com/foo", None, None)
Example #4
0
    def test_expand_db_attributes_for_editor(self, get_embed):
        get_embed.return_value = Embed(
            url='http://www.youtube.com/watch/',
            max_width=None,
            type='video',
            html='test html',
            title='test title',
            author_name='test author name',
            provider_name='test provider name',
            thumbnail_url='http://test/thumbnail.url',
            width=1000,
            height=1000,
        )

        result = MediaEmbedHandler.expand_db_attributes(
            {'url': 'http://www.youtube.com/watch/'}, True)
        self.assertIn((
            '<div class="embed-placeholder" contenteditable="false" data-embedtype="media"'
            ' data-url="http://www.youtube.com/watch/">'), result)
        self.assertIn('<h3>test title</h3>', result)
        self.assertIn('<p>URL: http://www.youtube.com/watch/</p>', result)
        self.assertIn('<p>Provider: test provider name</p>', result)
        self.assertIn('<p>Author: test author name</p>', result)
        self.assertIn('<img src="http://test/thumbnail.url" alt="test title">',
                      result)
Example #5
0
    def test_call_from_template(self, get_embed):
        get_embed.return_value = Embed(html='<img src="http://www.example.com" />')

        temp = template.Template('{% load wagtailembeds_tags %}{% embed "http://www.youtube.com/watch/" %}')
        result = temp.render(template.Context())

        self.assertEqual(result, '<img src="http://www.example.com" />')
Example #6
0
    def test_render_within_structblock(self, get_embed):
        """
        When rendering the value of an EmbedBlock directly in a template
        (as happens when accessing it as a child of a StructBlock), the
        proper embed output should be rendered, not the URL.
        """
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = blocks.StructBlock([
            ('title', blocks.CharBlock()),
            ('embed', EmbedBlock()),
        ])

        block_val = block.to_python({
            'title': 'A test',
            'embed': 'http://www.example.com/foo'
        })

        temp = template.Template('embed: {{ self.embed }}')
        context = template.Context({'self': block_val})
        result = temp.render(context)

        self.assertIn('<h1>Hello world!</h1>', result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call('http://www.example.com/foo')
Example #7
0
    def test_submit_valid_embed(self, get_embed):
        get_embed.return_value = Embed(
            html='<img src="http://www.example.com" />',
            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""")
Example #8
0
    def test_submit_valid_embed(self, get_embed):
        get_embed.return_value = Embed(html='<img src="http://www.example.com" />', title="An example embed")

        response = self.client.post(reverse('wagtailembeds:chooser_upload'), {
            'url': 'http://www.example.com/'
        })
        self.assertEqual(response.status_code, 200)
        response_json = json.loads(response.content.decode())
        self.assertEqual(response_json['step'], 'embed_chosen')
        self.assertEqual(response_json['embed_data']['title'], "An example embed")
    def test_add_spacer_paras_between_media_embeds(self, get_embed):
        get_embed.return_value = Embed(
            url='https://www.youtube.com/watch?v=Kh0Y2hVe_bw',
            max_width=None,
            type='video',
            html='test html',
            title='what are birds',
            author_name='look around you',
            provider_name='YouTube',
            thumbnail_url='http://test/thumbnail.url',
            width=1000,
            height=1000,
        )

        converter = ContentstateConverter(features=['embed'])
        result = json.loads(converter.from_database_format(
            '''
            <embed embedtype="media" url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw" />
            <embed embedtype="media" url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw" />
            '''
        ))
        self.assertContentStateEqual(result, {
            'blocks': [
                {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': '', 'type': 'unstyled'},
                {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
                {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': '', 'type': 'unstyled'},
                {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 1, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
                {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': '', 'type': 'unstyled'},
            ],
            'entityMap': {
                '0': {
                    'data': {
                        'thumbnail': 'http://test/thumbnail.url',
                        'embedType': 'video',
                        'providerName': 'YouTube',
                        'title': 'what are birds',
                        'authorName': 'look around you',
                        'url': 'https://www.youtube.com/watch?v=Kh0Y2hVe_bw'
                    },
                    'mutability': 'IMMUTABLE', 'type': 'EMBED'
                },
                '1': {
                    'data': {
                        'thumbnail': 'http://test/thumbnail.url',
                        'embedType': 'video',
                        'providerName': 'YouTube',
                        'title': 'what are birds',
                        'authorName': 'look around you',
                        'url': 'https://www.youtube.com/watch?v=Kh0Y2hVe_bw'
                    },
                    'mutability': 'IMMUTABLE', 'type': 'EMBED'
                },
            }
        })
Example #10
0
    def test_clean_required(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock()

        cleaned_value = block.clean(
            EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw'))
        self.assertIsInstance(cleaned_value, EmbedValue)
        self.assertEqual(cleaned_value.url,
                         'https://www.youtube.com/watch?v=_U79Wc965vw')

        with self.assertRaisesMessage(ValidationError, ''):
            block.clean(None)
Example #11
0
    def test_clean_non_required(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock(required=False)

        cleaned_value = block.clean(
            EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw'))
        self.assertIsInstance(cleaned_value, EmbedValue)
        self.assertEqual(cleaned_value.url,
                         'https://www.youtube.com/watch?v=_U79Wc965vw')

        cleaned_value = block.clean(None)
        self.assertIsNone(cleaned_value)
Example #12
0
    def test_submit_valid_embed(self, get_embed):
        get_embed.return_value = Embed(
            html='<img src="http://www.example.com" />',
            title="An example embed")

        response = self.client.post(
            reverse("wagtailembeds:chooser_upload"),
            {"embed-chooser-url": "http://www.example.com/"},
        )
        self.assertEqual(response.status_code, 200)
        response_json = json.loads(response.content.decode())
        self.assertEqual(response_json["step"], "embed_chosen")
        self.assertEqual(response_json["embed_data"]["title"],
                         "An example embed")
Example #13
0
    def test_render(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock()
        block_val = block.to_python('http://www.example.com/foo')

        temp = template.Template('embed: {{ embed }}')
        context = template.Context({'embed': block_val})
        result = temp.render(context)

        # Check that the embed was in the returned HTML
        self.assertIn('<h1>Hello world!</h1>', result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call('http://www.example.com/foo')
Example #14
0
    def test_render(self, get_embed):
        get_embed.return_value = Embed(html="<h1>Hello world!</h1>")

        block = EmbedBlock()
        block_val = block.to_python("http://www.example.com/foo")

        temp = template.Template("embed: {{ embed }}")
        context = template.Context({"embed": block_val})
        result = temp.render(context)

        # Check that the embed was in the returned HTML
        self.assertIn("<h1>Hello world!</h1>", result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call("http://www.example.com/foo", None, None)
Example #15
0
    def test_expand_html_escaping_end_to_end(self, get_embed):
        get_embed.return_value = Embed(
            url='http://www.youtube.com/watch/',
            max_width=None,
            type='video',
            html='test html',
            title='test title',
            author_name='test author name',
            provider_name='test provider name',
            thumbnail_url='htto://test/thumbnail.url',
            width=1000,
            height=1000,
        )

        result = expand_db_html('<p>1 2 <embed embedtype="media" url="https://www.youtube.com/watch?v=O7D-1RG-VRk&amp;t=25" /> 3 4</p>')
        self.assertIn('test html', result)
        get_embed.assert_called_with('https://www.youtube.com/watch?v=O7D-1RG-VRk&t=25')
Example #16
0
    def test_expand_db_attributes(self, get_embed):
        get_embed.return_value = Embed(
            url='http://www.youtube.com/watch/',
            max_width=None,
            type='video',
            html='test html',
            title='test title',
            author_name='test author name',
            provider_name='test provider name',
            thumbnail_url='htto://test/thumbnail.url',
            width=1000,
            height=1000,
        )

        result = MediaEmbedHandler.expand_db_attributes(
            {'url': 'http://www.youtube.com/watch/'}, False)
        self.assertIn('test html', result)
    def test_expand_db_attributes_for_frontend_nonresponsive(self, get_embed):
        get_embed.return_value = Embed(
            url='http://www.youtube.com/watch/',
            max_width=None,
            type='video',
            html='test html',
            title='test title',
            author_name='test author name',
            provider_name='test provider name',
            thumbnail_url='http://test/thumbnail.url',
            width=1000,
            height=1000,
        )

        result = FrontendMediaEmbedHandler.expand_db_attributes({
            'url': 'http://www.youtube.com/watch/',
        })
        self.assertIn('test html', result)
        self.assertNotIn('class="responsive-object"', result)
Example #18
0
    def test_expand_db_attributes_for_frontend_responsive(self, get_embed):
        get_embed.return_value = Embed(
            url="http://www.youtube.com/watch/",
            max_width=None,
            type="video",
            html="test html",
            title="test title",
            author_name="test author name",
            provider_name="test provider name",
            thumbnail_url="http://test/thumbnail.url",
            width=1000,
            height=1000,
        )

        result = FrontendMediaEmbedHandler.expand_db_attributes({
            "url":
            "http://www.youtube.com/watch/",
        })
        self.assertIn("test html", result)
        self.assertIn('class="responsive-object"', result)