def test_image(self):
     res = CloudinaryResource(metadata=self.uploaded)
     image = res.image()
     self.assertRegexpMatches(image, '[^-]src="{url}'.format(url=res.url))
     self.assertNotRegexpMatches(image, 'data-src="{url}'.format(url=res.url))
     image = res.image(responsive=True, width="auto", crop="scale")
     self.assertNotRegexpMatches(image, '[^-]src="{url}'.format(url=res.build_url(width="auto", crop="scale")))
     self.assertRegexpMatches(image, 'data-src="{url}'.format(url=res.build_url(width="auto", crop="scale")))
 def test_image(self):
     res = CloudinaryResource(metadata=self.uploaded)
     image = res.image()
     self.assertRegexpMatches(image, '[^-]src="{url}'.format(url=res.url))
     self.assertNotRegexpMatches(image, 'data-src="{url}'.format(url=res.url))
     image = res.image(responsive=True, width="auto", crop="scale")
     self.assertNotRegexpMatches(image, '[^-]src="{url}'.format(url=res.build_url(width="auto", crop="scale")))
     self.assertRegexpMatches(image, 'data-src="{url}'.format(url=res.build_url(width="auto", crop="scale")))
Esempio n. 3
0
 def test_to_python(self):
     c = CloudinaryField('image')
     res = CloudinaryResource(public_id=API_TEST_ID, format='jpg')
     # Can't compare the objects, so compare url instead
     self.assertEqual(
         c.to_python('{}.jpg'.format(API_TEST_ID)).build_url(),
         res.build_url())
Esempio n. 4
0
def cloudinary_url(context, source, options_dict={}, **options):
    options = dict(options_dict, **options)
    try:
        if context['request'].is_secure() and 'secure' not in options:
            options['secure'] = True
    except KeyError:
        pass
    if not isinstance(source, CloudinaryResource):
        source = CloudinaryResource(source)
    return source.build_url(**options)
Esempio n. 5
0
def cloudinary_url(context, source, options_dict={}, **options):
    options = dict(options_dict, **options)
    try:
        if context['request'].is_secure() and 'secure' not in options:
            options['secure'] = True
    except KeyError:
        pass
    if not isinstance(source, CloudinaryResource):
        source = CloudinaryResource(source)
    return source.build_url(**options)
class TestCloudinaryResource(TestCase):
    mocked_response = http_response_mock('{"breakpoints": [50, 500, 1000]}')
    mocked_breakpoints = [50, 500, 1000]
    expected_transformation = "c_scale,w_auto:breakpoints_50_1000_20_20:json"

    crop_transformation = {'crop': 'crop', 'width': 100}
    crop_transformation_str = 'c_crop,w_100'

    @classmethod
    def setUpClass(cls):
        cloudinary.reset_config()
        cls.uploaded = uploader.upload(TEST_IMAGE,
                                       public_id=TEST_ID,
                                       tags=TEST_TAG)

    @classmethod
    def tearDownClass(cls):
        api.delete_resources_by_tag(TEST_TAG)

    def setUp(self):
        self.res = CloudinaryResource(metadata=self.uploaded)

    def test_empty_class(self):
        """An empty CloudinaryResource"""

        self.shortDescription()
        res = CloudinaryResource()
        self.assertFalse(res, "should be 'False'")
        self.assertEqual(len(res), 0, "should have zero len()")
        self.assertIsNone(res.url, "should have None url")

    def test_validate(self):
        self.assertTrue(self.res.validate())
        self.assertTrue(self.res)
        self.assertGreater(len(self.res), 0)

    def test_get_prep_value(self):
        value = "image/upload/v{version}/{id}.{format}".format(
            version=self.uploaded["version"],
            id=self.uploaded["public_id"],
            format=self.uploaded["format"])

        self.assertEqual(value, self.res.get_prep_value())

    def test_get_presigned(self):
        value = "image/upload/v{version}/{id}.{format}#{signature}".format(
            version=self.uploaded["version"],
            id=self.uploaded["public_id"],
            format=self.uploaded["format"],
            signature=self.uploaded["signature"])

        self.assertEqual(value, self.res.get_presigned())

    def test_url(self):
        self.assertEqual(self.res.url, self.uploaded["url"])

    def test_image(self):
        image = self.res.image()
        self.assertIn(' src="{url}'.format(url=self.res.url), image)
        self.assertNotIn('data-src="{url}'.format(url=self.res.url), image)
        image = self.res.image(responsive=True, width="auto", crop="scale")
        self.assertNotIn(
            ' src="{url}'.format(
                url=self.res.build_url(width="auto", crop="scale")), image)
        self.assertIn(
            'data-src="{url}'.format(
                url=self.res.build_url(width="auto", crop="scale")), image)

    @mock.patch('urllib3.request.RequestMethods.request',
                return_value=mocked_response)
    def test_fetch_breakpoints(self, mocked_request):
        """Should retrieve responsive breakpoints from cloudinary resource (mocked)"""
        actual_breakpoints = self.res._fetch_breakpoints()

        self.assertEqual(self.mocked_breakpoints, actual_breakpoints)

        self.assertIn(self.expected_transformation,
                      get_request_url(mocked_request))

    @mock.patch('urllib3.request.RequestMethods.request',
                return_value=mocked_response)
    def test_fetch_breakpoints_with_transformation(self, mocked_request):
        """Should retrieve responsive breakpoints from cloudinary resource with custom transformation (mocked)"""
        srcset = {"transformation": self.crop_transformation}
        actual_breakpoints = self.res._fetch_breakpoints(srcset)

        self.assertEqual(self.mocked_breakpoints, actual_breakpoints)

        self.assertIn(
            self.crop_transformation_str + "/" + self.expected_transformation,
            get_request_url(mocked_request))

    def test_fetch_breakpoints_real(self):
        """Should retrieve responsive breakpoints from cloudinary resource (real request)"""
        actual_breakpoints = self.res._fetch_breakpoints()

        self.assertIsInstance(actual_breakpoints, list)

        self.assertGreater(len(actual_breakpoints), 0)
 def test_to_python(self):
     c = CloudinaryField('image')
     res = CloudinaryResource(public_id='sample', format='jpg')
     # Can't compare the objects, so compare url instead
     self.assertEqual(
         c.to_python('sample.jpg').build_url(), res.build_url())
class TestCloudinaryResource(TestCase):
    mocked_response = http_response_mock('{"breakpoints": [50, 500, 1000]}')
    mocked_breakpoints = [50, 500, 1000]
    expected_transformation = "c_scale,w_auto:breakpoints_50_1000_20_20:json"

    crop_transformation = {'crop': 'crop', 'width': 100}
    crop_transformation_str = 'c_crop,w_100'

    @classmethod
    def setUpClass(cls):
        cloudinary.reset_config()
        cls.uploaded = uploader.upload(TEST_IMAGE, public_id=TEST_ID, tags=TEST_TAG)

    @classmethod
    def tearDownClass(cls):
        cleanup_test_resources_by_tag([(TEST_TAG,)])

    def setUp(self):
        self.res = CloudinaryResource(metadata=self.uploaded)

    def test_empty_class(self):
        """An empty CloudinaryResource"""

        self.shortDescription()
        res = CloudinaryResource()
        self.assertFalse(res, "should be 'False'")
        self.assertEqual(len(res), 0, "should have zero len()")
        self.assertIsNone(res.url, "should have None url")

    def test_validate(self):
        self.assertTrue(self.res.validate())
        self.assertTrue(self.res)
        self.assertGreater(len(self.res), 0)

    def test_get_prep_value(self):
        value = "image/upload/v{version}/{id}.{format}".format(
            version=self.uploaded["version"],
            id=self.uploaded["public_id"],
            format=self.uploaded["format"])

        self.assertEqual(value, self.res.get_prep_value())

    def test_get_presigned(self):
        value = "image/upload/v{version}/{id}.{format}#{signature}".format(
            version=self.uploaded["version"],
            id=self.uploaded["public_id"],
            format=self.uploaded["format"],
            signature=self.uploaded["signature"])

        self.assertEqual(value, self.res.get_presigned())

    def test_url(self):
        self.assertEqual(self.res.url, self.uploaded["url"])

    def test_image(self):
        image = self.res.image()
        self.assertIn(' src="{url}'.format(url=self.res.url), image)
        self.assertNotIn('data-src="{url}'.format(url=self.res.url), image)
        image = self.res.image(responsive=True, width="auto", crop="scale")
        self.assertNotIn(' src="{url}'.format(url=self.res.build_url(width="auto", crop="scale")), image)
        self.assertIn('data-src="{url}'.format(url=self.res.build_url(width="auto", crop="scale")), image)

    @mock.patch('urllib3.request.RequestMethods.request', return_value=mocked_response)
    def test_fetch_breakpoints(self, mocked_request):
        """Should retrieve responsive breakpoints from cloudinary resource (mocked)"""
        actual_breakpoints = self.res._fetch_breakpoints()

        self.assertEqual(self.mocked_breakpoints, actual_breakpoints)

        self.assertIn(self.expected_transformation, get_request_url(mocked_request))

    @mock.patch('urllib3.request.RequestMethods.request', return_value=mocked_response)
    def test_fetch_breakpoints_with_transformation(self, mocked_request):
        """Should retrieve responsive breakpoints from cloudinary resource with custom transformation (mocked)"""
        srcset = {"transformation": self.crop_transformation}
        actual_breakpoints = self.res._fetch_breakpoints(srcset)

        self.assertEqual(self.mocked_breakpoints, actual_breakpoints)

        self.assertIn(self.crop_transformation_str + "/" + self.expected_transformation,
                      get_request_url(mocked_request))

    def test_fetch_breakpoints_real(self):
        """Should retrieve responsive breakpoints from cloudinary resource (real request)"""
        actual_breakpoints = self.res._fetch_breakpoints()

        self.assertIsInstance(actual_breakpoints, list)

        self.assertGreater(len(actual_breakpoints), 0)
Esempio n. 9
0
 def test_to_python(self):
     c = CloudinaryField('image')
     res = CloudinaryResource(public_id='sample', format='jpg')
     # Can't compare the objects, so compare url instead
     self.assertEqual(c.to_python('sample.jpg').build_url(), res.build_url())
 def test_to_python(self):
     c = CloudinaryField('image')
     res = CloudinaryResource(public_id=API_TEST_ID, format='jpg')
     # Can't compare the objects, so compare url instead
     self.assertEqual(c.to_python('{}.jpg'.format(API_TEST_ID)).build_url(), res.build_url())