Beispiel #1
0
    def test_extensionless_gif(self):
        """If the image file is a GIF without an extension, we can produce
        a valid thumbnail for it."""

        # Note: this test image file was breaking thumbnails if lazythumbs didn't
        # see the .gif extension.  I tried creating a gif on the fly using
        # PIL but didn't hit the same problem, so it might be something about
        # this image that's special, maybe that it has a transparent background.
        # (The error was "cannot write mode P as JPEG"; the symptom was a 404
        # response.)

        MEDIA_ROOT = tempfile.gettempdir()

        # Need to override MEDIA_ROOT in both django file storage and lazythumbs views
        # and Django doesn't provide override_settings until 1.4
        with patch('django.core.files.storage.settings') as settings1:
            settings1.MEDIA_ROOT = MEDIA_ROOT

            with patch('lazythumbs.views.settings') as settings2:
                settings2.MEDIA_ROOT = MEDIA_ROOT

                testfile = TEST_IMG_GIF
                filename = None
                try:
                    filename = os.path.join(MEDIA_ROOT, "gif_without_extension")
                    shutil.copy(testfile, filename)
                    # Now we have a gif file in a filename that doesn't end in .gif

                    renderer = LazyThumbRenderer()
                    source_path = os.path.relpath(filename, MEDIA_ROOT)
                    rsp = renderer.get(
                        request=Mock(path="/thumbnail/x50/" + source_path),
                        action="thumbnail",
                        geometry="x50",
                        source_path=source_path
                        )
                    # if you get 404, jpeg encoder is probably missing for Pillow
                    self.assertEqual(200, rsp.status_code)
                finally:
                    if filename:
                        os.remove(filename)
Beispiel #2
0
class GetViewTest(TestCase):
    """ Test behavior of LazyThumbRenderer.get """
    def mc_factory(self, was_404):
        """
        churn out mocked caches with a preset .get(). Also a rapper?
        """
        mc = Mock()
        ret = was_404
        mc.get = Mock(return_value=ret)
        mc.set = Mock()
        return mc

    def setUp(self):
        self.renderer = LazyThumbRenderer()
        self.mock_Image = Mock()
        self.mock_img = Mock()
        self.mock_Image.open = Mock(return_value=self.mock_img)
        self.mock_img.size = [1, 1]

    def test_img_404_warm_cache(self):
        """
        Ensure we go straight to a 404 response without setting anything new in
        cache or touching filesystem if we encounter a cached 404.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer._render_and_save = Mock()
        with patch('lazythumbs.views.cache', self.mc_factory(1)) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertFalse(mc.set.called)

    def test_img_404_cold_cache(self):
        """
        Basic 404: requested image is not found. Make sure proper response
        headers are set and the 404 was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        with patch('lazythumbs.views.cache', MockCache()) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(len(mc.cache.keys()), 1)
        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, 1)

    def test_img_200_cold_cache(self):
        """
        Pretend we found the requested rendered image on the filesystem. Ensure
        proper response headers are set and the rendered path was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer.fs.save = Mock()
        with patch('lazythumbs.views.Image', self.mock_Image):
            with patch('lazythumbs.views.cache', MockCache()) as mc:
                resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(resp.content, '')  # empty buffer means raw_data is ''
        self.assertEqual(len(mc.cache.keys()), 1)

        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, False)
Beispiel #3
0
class GetViewTest(TestCase):
    """ Test behavior of LazyThumbRenderer.get """

    def mc_factory(self, was_404):
        """
        churn out mocked caches with a preset .get(). Also a rapper?
        """
        mc = Mock()
        ret = was_404
        mc.get = Mock(return_value=ret)
        mc.set = Mock()
        return mc

    def setUp(self):
        self.renderer = LazyThumbRenderer()
        self.mock_Image = Mock()
        self.mock_img = Mock()
        self.mock_Image.open = Mock(return_value=self.mock_img)
        self.mock_img.size = [1,1]

    def test_img_404_warm_cache(self):
        """
        Ensure we go straight to a 404 response without setting anything new in
        cache or touching filesystem if we encounter a cached 404.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer._render_and_save = Mock()
        with patch('lazythumbs.views.cache', self.mc_factory(1)) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertFalse(mc.set.called)

    def test_img_404_cold_cache(self):
        """
        Basic 404: requested image is not found. Make sure proper response
        headers are set and the 404 was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        with patch('lazythumbs.views.cache', MockCache()) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(len(mc.cache.keys()), 1)
        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, 1)

    def test_img_200_cold_cache(self):
        """
        Pretend we found the requested rendered image on the filesystem. Ensure
        proper response headers are set and the rendered path was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer.fs.save = Mock()
        with patch('lazythumbs.views.Image', self.mock_Image):
            with patch('lazythumbs.views.cache', MockCache()) as mc:
                resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(resp.content, '') # empty buffer means raw_data is ''
        self.assertEqual(len(mc.cache.keys()), 1)

        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, False)

    def test_no_img_should_404(self):
        """
        When save fails with EEXIST error, it will try to read the file again
        But if it still can't be read, make sure it returns a 404 instead of 0-byte image.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer.fs.save = Mock()
        err = OSError()
        err.errno = errno.EEXIST
        self.renderer.fs.save.side_effect = err
        with patch('lazythumbs.views.Image', self.mock_Image):
            with patch('lazythumbs.views.cache', MockCache()):
                resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)

    def test_naughty_paths_root(self):
        resp = self.renderer.get(None, 'thumbnail', '48', '/')
        self.assertEqual(resp.status_code, 404)

    def test_naughty_paths_traverse(self):
        resp = self.renderer.get(None, 'thumbnail', '48', '../')
        self.assertEqual(resp.status_code, 404)

    def test_invalid_action(self):
        resp = self.renderer.get(None, 'invalid_action', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)

    def test_invalid_geometry(self):
        resp = self.renderer.get(None, 'thumbnail', '48x48x48', 'i/p')
        self.assertEqual(resp.status_code, 404)
Beispiel #4
0
class GetViewTest(TestCase):
    """ Test behavior of LazyThumbRenderer.get """

    def mc_factory(self, was_404):
        """
        churn out mocked caches with a preset .get(). Also a rapper?
        """
        mc = Mock()
        ret = was_404
        mc.get = Mock(return_value=ret)
        mc.set = Mock()
        return mc

    def setUp(self):
        self.renderer = LazyThumbRenderer()
        self.mock_Image = Mock()
        self.mock_img = Mock()
        self.mock_Image.open = Mock(return_value=self.mock_img)
        self.mock_img.size = [1,1]

    def test_img_404_warm_cache(self):
        """
        Ensure we go straight to a 404 response without setting anything new in
        cache or touching filesystem if we encounter a cached 404.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer._render_and_save = Mock()
        with patch('lazythumbs.views.cache', self.mc_factory(1)) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertFalse(mc.set.called)

    def test_img_404_cold_cache(self):
        """
        Basic 404: requested image is not found. Make sure proper response
        headers are set and the 404 was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        with patch('lazythumbs.views.cache', MockCache()) as mc:
            resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')
        self.assertEqual(resp.status_code, 404)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(len(mc.cache.keys()), 1)
        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, 1)

    def test_img_200_cold_cache(self):
        """
        Pretend we found the requested rendered image on the filesystem. Ensure
        proper response headers are set and the rendered path was cached.
        """
        req = Mock()
        req.path = "/lt_cache/thumbnail/48/i/p.jpg"
        self.renderer.fs.save = Mock()
        with patch('lazythumbs.views.Image', self.mock_Image):
            with patch('lazythumbs.views.cache', MockCache()) as mc:
                resp = self.renderer.get(req, 'thumbnail', '48', 'i/p')

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['Content-Type'], 'image/jpeg')
        self.assertTrue('Cache-Control' in resp)
        self.assertEqual(resp.content, '') # empty buffer means raw_data is ''
        self.assertEqual(len(mc.cache.keys()), 1)

        key = mc.cache.keys()[0]
        cached = mc.cache[key]
        self.assertEqual(cached, False)