Exemplo n.º 1
0
    def test_namedblobimage_field_serialization_doesnt_choke_on_corrupt_image(self):
        """In Plone >= 5.2 the original image file is not a "scale", so its url
        is returned as is and we need to check it, but the scales should be empty"""
        image_data = b"INVALID IMAGE DATA"
        fn = "test_namedblobimage_field"
        scale_url_uuid = "uuid_1"
        with patch_scale_uuid(scale_url_uuid):
            value = self.serialize(
                fn,
                NamedBlobImage(
                    data=image_data, contentType="image/gif", filename="1024x768.gif"
                ),
            )

        obj_url = self.doc1.absolute_url()
        self.assertEqual(
            {
                "content-type": "image/gif",
                "download": "{}/@@images/{}.{}".format(obj_url, scale_url_uuid, "gif"),
                "filename": "1024x768.gif",
                "height": -1,
                "scales": {},
                "size": 18,
                "width": -1,
            },
            value,
        )
Exemplo n.º 2
0
    def test_get_news_item(self):
        self.portal.invokeFactory("News Item", id="news1", title="News Item 1")
        image_file = os.path.join(os.path.dirname(__file__), "image.png")
        with open(image_file, "rb") as f:
            image_data = f.read()
        self.portal.news1.image = NamedBlobImage(data=image_data,
                                                 contentType="image/png",
                                                 filename="image.png")
        self.portal.news1.image_caption = "This is an image caption."
        transaction.commit()

        scale_url_uuid = "uuid1"
        with patch_scale_uuid(scale_url_uuid):
            response = self.api_session.get(self.portal.news1.absolute_url())

            self.assertEqual(response.status_code, 200)
            self.assertEqual(
                response.headers.get("Content-Type"),
                "application/json",
                "When sending a GET request with Content-Type: application/json "
                +
                "the server should respond with sending back application/json.",  # noqa
            )
            self.assertEqual(
                "News Item",
                response.json().get("@type"),
                "Response should be @type 'News Item', not '{}'".format(
                    response.json().get("@type")),
            )
            self.assertEqual(response.json().get("@id"),
                             self.portal.news1.absolute_url())
            self.assertEqual("News Item 1", response.json().get("title"))
            self.assertEqual("This is an image caption.",
                             response.json()["image_caption"])
            self.assertDictContainsSubset(
                {
                    "download":
                    self.portal_url + f"/news1/@@images/{scale_url_uuid}.png"
                },
                response.json()["image"],
            )
Exemplo n.º 3
0
    def test_serialize_image(self):
        self.portal.invokeFactory("Image", id="image1", title="Image 1")
        image_file = os.path.join(os.path.dirname(__file__), "image.png")
        with open(image_file, "rb") as f:
            image_data = f.read()
        self.portal.image1.image = NamedBlobImage(data=image_data,
                                                  contentType="image/png",
                                                  filename="image.png")

        self.maxDiff = 99999

        scale_url_uuid = "uuid_1"
        with patch_scale_uuid(scale_url_uuid):
            obj_url = self.portal.image1.absolute_url()
            download_url = f"{obj_url}/@@images/{scale_url_uuid}.png"
            scales = {
                "listing": {
                    "download": download_url,
                    "width": 16,
                    "height": 4
                },
                "icon": {
                    "download": download_url,
                    "width": 32,
                    "height": 8
                },
                "tile": {
                    "download": download_url,
                    "width": 64,
                    "height": 16
                },
                "thumb": {
                    "download": download_url,
                    "width": 128,
                    "height": 33
                },
                "mini": {
                    "download": download_url,
                    "width": 200,
                    "height": 52
                },
                "preview": {
                    "download": download_url,
                    "width": 215,
                    "height": 56
                },
                "large": {
                    "download": download_url,
                    "width": 215,
                    "height": 56
                },
            }
            if HAS_PLONE_6:
                # PLIP #3279 amended the image scales
                # https://github.com/plone/Products.CMFPlone/pull/3450
                scales["great"] = {
                    "download": download_url,
                    "height": 56,
                    "width": 215,
                }
                scales["huge"] = {
                    "download": download_url,
                    "height": 56,
                    "width": 215,
                }
                scales["larger"] = {
                    "download": download_url,
                    "height": 56,
                    "width": 215,
                }
                scales["large"] = {
                    "download": download_url,
                    "height": 56,
                    "width": 215,
                }
                scales["teaser"] = {
                    "download": download_url,
                    "height": 56,
                    "width": 215,
                }
            self.assertEqual(
                {
                    "filename": "image.png",
                    "content-type": "image/png",
                    "size": 1185,
                    "download": download_url,
                    "width": 215,
                    "height": 56,
                    "scales": scales,
                },
                self.serialize(self.portal.image1)["image"],
            )
Exemplo n.º 4
0
    def test_namedblobimage_field_serialization_returns_dict_with_original_scale(self):
        """In Plone >= 5.2 the image returned when requesting an image
        scale with the same width and height of the original image is
        the actual original image in its original format"""
        image_file = os.path.join(os.path.dirname(__file__), "1024x768.gif")
        with open(image_file, "rb") as f:
            image_data = f.read()
        fn = "test_namedblobimage_field"
        scale_url_uuid = "uuid_1"
        with patch_scale_uuid(scale_url_uuid):
            value = self.serialize(
                fn,
                NamedBlobImage(
                    data=image_data, contentType="image/gif", filename="1024x768.gif"
                ),
            )
            self.assertTrue(isinstance(value, dict), "Not a <dict>")

            obj_url = self.doc1.absolute_url()

            # Original image is still a "scale"
            # scaled images are converted to PNG in Plone = 5.2
            original_download_url = "{}/@@images/{}.{}".format(
                obj_url, scale_url_uuid, "gif"
            )

            scale_download_url = "{}/@@images/{}.{}".format(
                obj_url, scale_url_uuid, "png"
            )
            scales = {
                "listing": {
                    "download": scale_download_url,
                    "width": 16,
                    "height": 12,
                },
                "icon": {"download": scale_download_url, "width": 32, "height": 24},
                "tile": {"download": scale_download_url, "width": 64, "height": 48},
                "thumb": {
                    "download": scale_download_url,
                    "width": 128,
                    "height": 96,
                },
                "mini": {
                    "download": scale_download_url,
                    "width": 200,
                    "height": 150,
                },
                "preview": {
                    "download": scale_download_url,
                    "width": 400,
                    "height": 300,
                },
                "large": {
                    "download": scale_download_url,
                    "width": 768,
                    "height": 576,
                },
            }
            if HAS_PLONE_6:
                # PLIP #3279 amended the image scales
                # https://github.com/plone/Products.CMFPlone/pull/3450
                scales["great"] = {
                    "download": scale_download_url,
                    "height": 768,
                    "width": 1024,
                }
                scales["huge"] = {
                    "download": scale_download_url,
                    "height": 768,
                    "width": 1024,
                }
                scales["larger"] = {
                    "download": scale_download_url,
                    "height": 750,
                    "width": 1000,
                }
                scales["large"] = {
                    "download": scale_download_url,
                    "height": 600,
                    "width": 800,
                }
                scales["teaser"] = {
                    "download": scale_download_url,
                    "height": 450,
                    "width": 600,
                }
            self.assertEqual(
                {
                    "filename": "1024x768.gif",
                    "content-type": "image/gif",
                    "size": 1514,
                    "download": original_download_url,
                    "width": 1024,
                    "height": 768,
                    "scales": scales,
                },
                value,
            )