def test_download_fallback_cache(self, fetch_dist): """Downloading missing package caches result from fallback""" db = self.request.db = MagicMock() locator = self.request.locator = MagicMock() self.request.registry.fallback = "cache" self.request.fallback_simple = "https://pypi.org/simple" self.request.access.can_update_cache.return_value = True db.fetch.return_value = None fetch_dist.return_value = (MagicMock(), MagicMock()) context = MagicMock() context.filename = "package.tar.gz" url = "https://pypi.org/simple/%s" % context.filename dist = make_dist(url=url) locator.get_releases.return_value = [dist] ret = api.download_package(context, self.request) fetch_dist.assert_called_with( self.request, dist["url"], dist["name"], dist["version"], dist["summary"], dist["requires_python"], ) self.assertEqual(ret.body, fetch_dist()[1]) ret.headers.update.assert_any_call([("Cache-Control", "public, max-age=0")])
def test_download_fallback_cache_max_age(self, fetch_dist): """ Downloading missing package caches result from fallback """ db = self.request.db = MagicMock() locator = self.request.locator = MagicMock() self.request.registry.fallback = "cache" self.request.fallback_simple = "https://pypi.org/simple" self.request.access.can_update_cache.return_value = True self.request.registry.package_max_age = 30 db.fetch.return_value = None fetch_dist.return_value = (MagicMock(), MagicMock()) context = MagicMock() context.filename = "package.tar.gz" dist = MagicMock() url = "https://pypi.org/simple/%s" % context.filename locator.get_project.return_value = { "0.1": dist, "urls": { "0.1": set([url]) } } ret = api.download_package(context, self.request) fetch_dist.assert_called_with(self.request, dist.name, url) self.assertEqual(ret.body, fetch_dist()[1]) ret.headers.update.assert_any_call([("Cache-Control", "public, max-age=30")])
def test_download(self): """ Downloading package returns download response from db """ db = self.request.db = MagicMock() context = MagicMock() ret = api.download_package(context, self.request) db.fetch.assert_called_with(context.filename) db.download_response.assert_called_with(db.fetch()) self.assertEqual(ret, db.download_response())
def test_download_fallback_no_cache(self): """ Downloading missing package on non-'cache' fallback returns 404 """ db = self.request.db = MagicMock() self.request.registry.fallback = 'none' db.fetch.return_value = None context = MagicMock() ret = api.download_package(context, self.request) self.assertEqual(ret.status_code, 404)
def test_download_fallback_cache_no_perm(self): """ Downloading missing package without cache perm returns 403 """ db = self.request.db = MagicMock() self.request.registry.fallback = 'cache' self.request.access.can_update_cache.return_value = False db.fetch.return_value = None context = MagicMock() ret = api.download_package(context, self.request) self.assertEqual(ret, self.request.forbid())
def test_download_fallback_cache_missing(self, locator): """ If fallback url is missing dist, return 404 """ db = self.request.db = MagicMock() self.request.registry.fallback = 'cache' self.request.registry.fallback_url = 'http://pypi.com' self.request.access.can_update_cache.return_value = True db.fetch.return_value = None context = MagicMock() locator().get_project.return_value = {context.filename: None} ret = api.download_package(context, self.request) self.assertEqual(ret.status_code, 404)
def test_download_with_stream_files(self): """Downloading package returns download response from db with max age""" db = self.request.db = MagicMock() self.request.registry.stream_files = True self.request.registry.package_max_age = 30 context = MagicMock() ret = api.download_package(context, self.request) db.fetch.assert_called_with(context.filename) db.storage.open.assert_called_once_with(db.fetch()) db.download_response.assert_not_called() ret.headers.update.assert_any_call([("Cache-Control", "public, max-age=30")])
def test_download_fallback_cache_missing(self, locator): """ If fallback url is missing dist, return 404 """ db = self.request.db = MagicMock() self.request.registry.fallback = 'cache' self.request.registry.fallback_url = 'http://pypi.com' self.request.access.can_update_cache.return_value = True db.fetch.return_value = None context = MagicMock() locator().get_project.return_value = { context.filename: None } ret = api.download_package(context, self.request) self.assertEqual(ret.status_code, 404)
def test_download_with_stream_files(self): """Downloading package returns download response from db with max age""" db = self.request.db = MagicMock() data = MagicMock() db.storage.open.return_value.__enter__.return_value = data data.read.return_value = b"" self.request.registry.stream_files = True self.request.registry.package_max_age = 30 context = MagicMock() ret = api.download_package(context, self.request) db.fetch.assert_called_with(context.filename) db.storage.open.assert_called_once_with(db.fetch()) db.download_response.assert_not_called() self.assertDictContainsSubset({"Cache-Control": "public, max-age=30"}, ret.headers)
def test_download_fallback_cache(self, locator, fetch_dist): """ Downloading missing package caches result from fallback """ db = self.request.db = MagicMock() self.request.registry.fallback = 'cache' self.request.registry.fallback_url = 'http://pypi.com' self.request.access.can_update_cache.return_value = True db.fetch.return_value = None fetch_dist.return_value = (MagicMock(), MagicMock()) context = MagicMock() dist = MagicMock(spec=Distribution) locator().get_project.return_value = { context.filename: dist, } ret = api.download_package(context, self.request) fetch_dist.assert_called_with(self.request, dist) self.assertEqual(ret.body, fetch_dist()[1])
def test_download_fallback_cache(self, locator, fetch_dist): """ Downloading missing package caches result from fallback """ db = self.request.db = MagicMock() self.request.registry.fallback = 'cache' self.request.registry.fallback_url = 'http://pypi.com' self.request.access.can_update_cache.return_value = True db.fetch.return_value = None context = MagicMock() dist = MagicMock(spec=Distribution) locator().get_project.return_value = { context.filename: dist, } ret = api.download_package(context, self.request) fetch_dist.assert_called_with(self.request, dist) db.download_response.assert_called_with(fetch_dist()) self.assertEqual(ret, db.download_response())
def test_download_fallback_cache_max_age(self, fetch_dist): """Downloading missing package caches result from fallback""" db = self.request.db = MagicMock() locator = self.request.locator = MagicMock() self.request.registry.fallback = "cache" self.request.fallback_simple = "https://pypi.org/simple" self.request.access.can_update_cache.return_value = True self.request.registry.package_max_age = 30 db.fetch.return_value = None fetch_dist.return_value = (MagicMock(), BytesIO(b"fds")) context = MagicMock() context.filename = "package.tar.gz" url = "https://pypi.org/simple/%s" % context.filename dist = make_dist(url=url) locator.get_releases.return_value = [dist] ret = api.download_package(context, self.request) fetch_dist.assert_called_once() self.assertEqual(ret.body, b"fds") self.assertDictContainsSubset({"Cache-Control": "public, max-age=30"}, ret.headers)
def test_download_fallback_cache(self, fetch_dist): """ Downloading missing package caches result from fallback """ db = self.request.db = MagicMock() locator = self.request.locator = MagicMock() self.request.registry.fallback = 'cache' self.request.registry.fallback_url = 'http://pypi.com' self.request.access.can_update_cache.return_value = True db.fetch.return_value = None fetch_dist.return_value = (MagicMock(), MagicMock()) context = MagicMock() context.filename = 'package.tar.gz' dist = MagicMock() url = 'http://pypi.com/simple/%s' % context.filename locator.get_project.return_value = { '0.1': dist, 'urls': { '0.1': set([url]) } } ret = api.download_package(context, self.request) fetch_dist.assert_called_with(self.request, dist.name, url) self.assertEqual(ret.body, fetch_dist()[1])