def test_import_images_404(self):
        """ An image responds a 404 error, skip and take the first valid """
        env = mock.MagicMock()
        env.get_connector_unit.return_value = ProductProductAdapter(env)
        model = mock.MagicMock(name='model')
        model.browse.return_value = model
        env.model.with_context.return_value = model

        importer = CatalogImageImporter(env)
        url_tee1 = ('http://localhost:9100/media/catalog/product'
                    '/i/n/ink-eater-krylon-bombear-destroyed-tee-1.jpg')
        url_tee2 = ('http://localhost:9100/media/catalog/product/'
                    'i/n/ink-eater-krylon-bombear-destroyed-tee-2.jpg')
        with mock.patch('urllib2.urlopen') as urlopen:
            def image_url_response(url):
                if url._Request__original in (url_tee1, url_tee2):
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                else:
                    return MockResponseImage(PNG_IMG_4PX_GREEN)

            urlopen.side_effect = image_url_response
            with mock_api(simple_product_and_images):
                importer.run(122, 999)

        model.browse.assert_called_with(999)
        model.write.assert_called_with({'image': B64_PNG_IMG_4PX_GREEN})
Example #2
0
    def test_import_images_404(self):
        """ An image responds a 404 error, skip and take the first valid """
        env = mock.MagicMock()
        env.get_connector_unit.return_value = ProductProductAdapter(env)
        model = mock.MagicMock(name='model')
        model.browse.return_value = model
        env.model.with_context.return_value = model

        importer = CatalogImageImporter(env)
        url_tee1 = ('http://localhost:9100/media/catalog/product'
                    '/i/n/ink-eater-krylon-bombear-destroyed-tee-1.jpg')
        url_tee2 = ('http://localhost:9100/media/catalog/product/'
                    'i/n/ink-eater-krylon-bombear-destroyed-tee-2.jpg')
        with mock.patch('urllib2.urlopen') as urlopen:

            def image_url_response(url):
                if url._Request__original in (url_tee1, url_tee2):
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                else:
                    return MockResponseImage(PNG_IMG_4PX_GREEN)

            urlopen.side_effect = image_url_response
            with mock_api(simple_product_and_images):
                importer.run(122, 999)

        model.browse.assert_called_with(999)
        model.write.assert_called_with({'image': B64_PNG_IMG_4PX_GREEN})
 def test_image_priority(self):
     """ Check if the images are sorted in the correct priority """
     env = mock.Mock()
     importer = CatalogImageImporter(env)
     file1 = {'file': 'file1', 'types': ['image'], 'position': '10'}
     file2 = {'file': 'file2', 'types': ['thumbnail'], 'position': '3'}
     file3 = {'file': 'file3', 'types': ['thumbnail'], 'position': '4'}
     file4 = {'file': 'file4', 'types': [], 'position': '10'}
     images = [file2, file1, file4, file3]
     self.assertEquals(importer._sort_images(images),
                       [file4, file3, file2, file1])
 def test_image_priority(self):
     """ Check if the images are sorted in the correct priority """
     env = mock.Mock()
     importer = CatalogImageImporter(env)
     file1 = {'file': 'file1', 'types': ['image'], 'position': '10'}
     file2 = {'file': 'file2', 'types': ['thumbnail'], 'position': '3'}
     file3 = {'file': 'file3', 'types': ['thumbnail'], 'position': '4'}
     file4 = {'file': 'file4', 'types': [], 'position': '10'}
     images = [file2, file1, file4, file3]
     self.assertEquals(importer._sort_images(images),
                       [file4, file3, file2, file1])
Example #5
0
    def test_import_images_403(self):
        """ Import a product when an image respond a 403 error, should fail """
        env = mock.MagicMock()
        env.get_connector_unit.return_value = ProductProductAdapter(env)
        importer = CatalogImageImporter(env)
        with mock.patch('urllib2.urlopen') as urlopen:
            def image_url_response(url):
                if url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-2.jpg':
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                elif url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-1.jpg':
                    raise urllib2.HTTPError(url, 403, '403', None, None)
                else:
                    return MockResponseImage(PNG_IMG_4PX_GREEN)

            urlopen.side_effect = image_url_response
            with mock_api(simple_product_and_images):
                with self.assertRaises(urllib2.HTTPError):
                    importer.run(122, 999)
    def test_import_images_403(self):
        """ Import a product when an image respond a 403 error, should fail """
        env = mock.MagicMock()
        env.get_connector_unit.return_value = ProductProductAdapter(env)
        importer = CatalogImageImporter(env)
        with mock.patch('urllib2.urlopen') as urlopen:

            def image_url_response(url):
                url = url.get_full_url()
                if url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-2.jpg':
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                elif url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-1.jpg':
                    raise urllib2.HTTPError(url, 403, '403', None, None)
                else:
                    return MockResponseImage(PNG_IMG_4PX_GREEN)

            urlopen.side_effect = image_url_response
            with mock_api(simple_product_and_images):
                with self.assertRaises(urllib2.HTTPError):
                    importer.run(122, 999)
    def test_import_images_404(self):
        """ Import a product when an image respond a 404 error, should skip and take the first valid """
        env = mock.MagicMock()
        env.get_connector_unit.return_value = ProductProductAdapter(env)
        importer = CatalogImageImporter(env)
        with mock.patch('urllib2.urlopen') as urlopen:

            def image_url_response(url):
                if url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-2.jpg':
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                elif url == 'http://localhost:9100/media/catalog/product/i/n/ink-eater-krylon-bombear-destroyed-tee-1.jpg':
                    raise urllib2.HTTPError(url, 404, '404', None, None)
                else:
                    return MockResponseImage(PNG_IMG_4PX_GREEN)

            urlopen.side_effect = image_url_response
            with mock_api(simple_product_and_images):
                importer.run(122, 999)

        env.session.write.assert_called_with(mock.ANY, 999,
                                             {'image': B64_PNG_IMG_4PX_GREEN})