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_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})