Repository: wagtail/wagtail
File: wagtail/embeds/tests/test_embeds.py (Python)

Code before:
        block_val = EmbedValue("http://www.example.com/foo")
        serialized_val = block.get_prep_value(block_val)
        self.assertEqual(serialized_val, "http://www.example.com/foo")

        serialized_empty_val = block.get_prep_value(None)
        self.assertEqual(serialized_empty_val, "")

    @patch("wagtail.embeds.embeds.get_embed")
    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)

    @patch("wagtail.embeds.embeds.get_embed")
    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

Review comment (suggestion):
These tests have been updated to use the `responses` library to simulate HTTP responses rather than mocking `get_embed`, as the latter was masking the error that occurs when the embed finder attempts to retrieve an embed at an empty URL.

Code after:
    def test_deserialize(self):
        """
        Deserialising the JSONish value of an EmbedBlock (a URL) should give us an EmbedValue
        for that URL
        """
        block = EmbedBlock(required=False)

        block_val = block.to_python("http://www.example.com/foo")
        self.assertIsInstance(block_val, EmbedValue)
        self.assertEqual(block_val.url, "http://www.example.com/foo")

        # empty values should yield None
        empty_block_val = block.to_python("")
        self.assertIsNone(empty_block_val)

    def test_serialize(self):
        block = EmbedBlock(required=False)

        block_val = EmbedValue("http://www.example.com/foo")
        serialized_val = block.get_prep_value(block_val)
        self.assertEqual(serialized_val, "http://www.example.com/foo")

        serialized_empty_val = block.get_prep_value(None)
        self.assertEqual(serialized_empty_val, "")

    @responses.activate
    def test_render(self):
        self.set_up_embed_response()

        block = EmbedBlock()
        block_val = block.to_python("https://www.youtube.com/watch/")

        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)

    @responses.activate
    def test_render_within_structblock(self):
        """
        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.
        """
        self.set_up_embed_response()

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