def test_get_last_release_without_release(self): from papaye.models import Package package = Package(name='package1') result = package.get_last_release() self.assertIsNone(result)
def not_found(request, stop=None): if not proxy_activated(request.registry.settings) or stop is not None: return HTTPNotFound() proxy = PyPiProxy() package_name = request.matchdict['traverse'][0] if package_name in request.root: local_package = request.root[package_name] else: local_package = Package(package_name) local_package.fake = True merged_repository = proxy.merged_repository(local_package) if merged_repository: package = merged_repository[package_name] traversed = len(request.matchdict['traverse']) if traversed == 1: view = ListReleaseFileView(package, request) view.stop = True elif traversed == 2: context = package[request.matchdict['traverse'][1]] view = ListReleaseFileByReleaseView(context, request) elif traversed == 3: release_file = package[request.matchdict['traverse'][1]][request.matchdict['traverse'][2]] filename = request.matchdict['traverse'][2] package_name, release_name, _ = request.matchdict['traverse'] download_release_from_pypi.delay(request.registry._zodb_databases[''], package_name, release_name, filename) return HTTPTemporaryRedirect(location=release_file.pypi_url) return view() return HTTPNotFound()
def test_smart_merge_with_existing_release(self, mock): from papaye.proxy import PyPiProxy from papaye.factories import repository_root_factory from papaye.models import Package, Release, ReleaseFile mock.return_value = self.pypi_response root = repository_root_factory(self.request) # Existing releases package = Package(name='pyramid') package['1.5'] = Release(name='1.5', version='1.5', metadata={}) package['1.5']['pyramid-1.5.tar.gz'] = ReleaseFile( filename='pyramid-1.5.tar.gz', content=b'', md5_digest='12345') package = Package(name='pyramid') package['1.5'] = Release(name='1.5', version='1.5', metadata={}) package['1.5']['pyramid-1.5.tar.gz'] = ReleaseFile( filename='pyramid-1.5.tar.gz', content=b'', md5_digest='12345') proxy = PyPiProxy(self.request, 'pyramid') result = proxy.smart_merge(root, package) self.assertEqual([key for key in result.releases.keys()], [ '1.5', ]) self.assertEqual(result['1.5']['pyramid-1.5.tar.gz'].md5_digest, '12345')
def build_remote_repository(self, package_name, release_name=None, metadata=False, content=False): package_name = self.get_remote_package_name(package_name) if package_name: info = self.get_remote_informations(self.pypi_url.format(package_name)) package_root = Root() package = Package(info['info']['name']) package.__parent__ = package_root package_root[package.name] = package remote_releases = info['releases'].keys() if not release_name else [release_name, ] for remote_release in remote_releases: release = Release( remote_release, remote_release, metadata=info['info'], deserialize_metadata=metadata ) package[remote_release] = release for remote_release_file in info['releases'][remote_release]: filename = remote_release_file['filename'] md5_digest = remote_release_file['md5_digest'] if not content: release_file = ReleaseFile(filename, b'', md5_digest) else: content_file = download_file(filename, remote_release_file['url'], md5_digest) if content_file is not None: release_file = ReleaseFile(filename, content_file, md5_digest) else: continue setattr(release_file, 'pypi_url', remote_release_file['url']) release[filename] = release_file return package_root return None
def test_get_last_release_with_alpha(self): from papaye.models import Package, Release package = Package(name='package1') package.releases.update([('1.0{}'.format(version), Release('', '1.0{}'.format(version), metadata={})) for version in ['', 'a1', 'a2', 'b1', 'b2', 'rc1']]) result = package.get_last_release() self.assertEqual(result.version, '1.0')
def test_by_name(self): from papaye.models import Package from papaye.factories import repository_root_factory root = repository_root_factory(self.request) root['package1'] = Package(name='package1') result = Package.by_name('package1', self.request) self.assertEqual(result, root['package1'])
def test_get_last_release_with_minor(self): from papaye.models import Package, Release package = Package(name='package1') package.releases.update([( '1.{}'.format(index), Release('', '1.{}'.format(index), metadata={})) for index in range(1, 3)] ) result = package.get_last_release() self.assertEqual(result.version, '1.2')
def test_get_last_release_with_minor(self): from papaye.models import Package, Release package = Package(name='package1') package.releases.update([('1.{}'.format(index), Release('', '1.{}'.format(index), metadata={})) for index in range(1, 3)]) result = package.get_last_release() self.assertEqual(result.version, '1.2')
def test_get_last_release_with_alpha(self): from papaye.models import Package, Release package = Package(name='package1') package.releases.update([ ('1.0{}'.format(version), Release('', '1.0{}'.format(version), metadata={})) for version in ['', 'a1', 'a2', 'b1', 'b2', 'rc1'] ]) result = package.get_last_release() self.assertEqual(result.version, '1.0')
def test_list_releases_files_with_another_package(self): from papaye.views.simple import ListReleaseFileView from papaye.models import Package, Release, Root, ReleaseFile # Test packages root = Root() root['package1'] = Package(name='package1') root['package2'] = Package(name='package2') root['package1']['release1'] = Release(name='release1', version='1.0', metadata={}) root['package1']['release1'].__parent__ = root['package1'] root['package2']['release2'] = Release(name='release2', version='2.0', metadata={}) root['package2']['release2'].__parent__ = root['package2'] root['package1']['release1']['releasefile1.tar.gz'] = ReleaseFile( filename='releasefile1.tar.gz', content=b'', md5_digest='12345') root['package1']['release1']['releasefile1.tar.gz'].__parent__ = root[ 'package1']['release1'] root['package2']['release2']['releasefile2.tar.gz'] = ReleaseFile( filename='releasefile2.tar.gz', content=b'', md5_digest='12345') root['package2']['release2']['releasefile2.tar.gz'].__parent__ = root[ 'package2']['release2'] view = ListReleaseFileView(root['package1'], self.request) response = view() self.assertIsInstance(response, dict) self.assertIn('objects', response) self.assertIsInstance(response['objects'], types.GeneratorType) self.assertEqual( [(url, release) for url, release in response['objects']], [ ('http://example.com/simple/package1/release1/releasefile1.tar.gz#md5=12345', root['package1']['release1']['releasefile1.tar.gz']), ], ) view = ListReleaseFileView(root['package2'], self.request) response = view() self.assertIsInstance(response, dict) self.assertIn('objects', response) self.assertIsInstance(response['objects'], types.GeneratorType) self.assertEqual( [(url, release) for url, release in response['objects']], [ ('http://example.com/simple/package2/release2/releasefile2.tar.gz#md5=12345', root['package2']['release2']['releasefile2.tar.gz']), ], )
def test_get_last_remote_version_without_proxy(self, mock): from papaye.models import Package fake_response = Response(status=200, body='{"info": {"version": "1.0"}}') mock.return_value = fake_response # Test data package = Package('test') result = Package.get_last_remote_version(False, package.name) self.assertEqual(mock.call_count, 0) self.assertIsNone(result)
def test_get_last_remote_version_with_proxy_404(self, mock): from papaye.models import Package fake_response = FakeGRequestResponse( status_code=404, content=b'{"info": {"version": "1.0"}}') mock.return_value = fake_response # Test data package = Package('test') result = Package.get_last_remote_version(True, package.name) self.assertEqual(mock.call_count, 1) self.assertIsNone(result)
def test_upload_release_already_exists(self): from papaye.models import Root, Package, Release, ReleaseFile from papaye.views.simple import UploadView # Create a fake test file uploaded_file = io.BytesIO(b"content") storage = FieldStorage() storage.filename = 'foo.tar.gz' storage.file = uploaded_file self.request.POST = { "content": storage, "some_metadata": "Fake Metadata", "version": "1.0", "name": "my_package", ":action": "file_upload", } root = Root() # Create initial release package = Package('my_package') package['1.0'] = Release('1.0', '1.0', metadata={}) package['1.0']['foo.tar.gz'] = ReleaseFile('foo.tar.gz', b'') root['my_package'] = package view = UploadView(root, self.request) result = view() self.assertIsInstance(result, Response) self.assertEqual(result.status_int, 409)
def test_list_release_with_two_release(self): from papaye.views.simple import ListReleaseFileByReleaseView from papaye.models import Package, Release, ReleaseFile from papaye.factories import repository_root_factory # Initial data root = repository_root_factory(self.request) package = Package('my_package') package['1.0'] = Release('1.0', '1.0', metadata={}) package['1.0']['foo.tar.gz'] = ReleaseFile('foo.tar.gz', b'') package['2.0'] = Release('2.0', '2.0', metadata={}) package['2.0']['foo2.tar.gz'] = ReleaseFile('foo2.tar.gz', b'') root['my_package'] = package view1 = ListReleaseFileByReleaseView(package['1.0'], self.request) view2 = ListReleaseFileByReleaseView(package['2.0'], self.request) result1 = view1() result2 = view2() self.assertIsInstance(result1, dict) self.assertIn('objects', result1) self.assertEqual( list(result1['objects']), [('http://example.com/simple/my_package/1.0/foo.tar.gz/', package['1.0']['foo.tar.gz'])]) self.assertIsInstance(result2, dict) self.assertIn('objects', result2) self.assertEqual( list(result2['objects']), [('http://example.com/simple/my_package/2.0/foo2.tar.gz/', package['2.0']['foo2.tar.gz'])])
def __call__(self): post = dict(self.request.POST) metadata = dict([(key, value) for key, value in post.items() if key != 'content']) if post.get(':action') == "file_upload": name = post.get('name') if ' ' in name: name = name.replace(' ', '-') version = post.get('version') content = post.get('content') md5_digest = post.get('md5_digest') package = self.context[name] if self.context.get( name) else Package(name) package.__parent__ = self.context self.context[name] = package release = package[version] if package.releases.get( version) else Release( name=version, version=version, metadata=metadata) release.__parent__ = package self.context[name][version] = release if release.release_files.get(content.filename): return HTTPConflict() release_file = ReleaseFile(filename=content.filename, content=content.file.read(), md5_digest=md5_digest) release = self.context[name][version] self.context[name][version][content.filename] = release_file return Response() else: return HTTPBadRequest()
def remove_package(request): package_name = request.matchdict.get("package_name") package = Package.by_name(package_name, request) if package is None: return HTTPNotFound() del request.root[package_name] return {"success": True}
def test_download_release_from_pypi_with_existing_package(self, get_connection_mock, request_mock): from papaye.factories import repository_root_factory from papaye.models import Package, Release, ReleaseFile from papaye.tasks.download import download_release_from_pypi request = get_current_request() get_connection_mock.return_value = self.conn json_response = open(get_resource('pyramid.json'), 'rb') release_file_content = open(get_resource('pyramid-1.5.tar.gz'), 'rb') request_mock.side_effect = [ FakeGRequestResponse(200, b'', 'http://pypi.python.org/simple/pyramid/'), FakeGRequestResponse(200, json_response.read()), FakeGRequestResponse(200, release_file_content.read()), ] root = repository_root_factory(self.conn) package = Package('pyramid') release = Release('1.0', '1.0', metadata={}) release_file = ReleaseFile('pyramid-1.0.tar.gz', b'') root['pyramid'] = package root['pyramid']['1.0'] = release root['pyramid']['1.0']['pyramid-1.0.tar.gz']= release_file download_release_from_pypi(request.registry.settings, 'pyramid', '1.5') self.assertEqual(request_mock.call_count, 3) assert request_mock.call_count == 3 assert len(list(root['pyramid'])) == 2
def test_get_package_by_version_with_unknown_release(setup): from papaye.views.api import get_package_by_version from papaye.models import Package, Root, Release request = testing.DummyRequest() root = Root() root['package1'] = Package(name='package1') root['package2'] = Package(name='package2') root['package1']['1.0'] = Release('1.0', '1.0', {'summary': 'The package 1'}) root['package2']['1.0'] = Release('1.0', '1.0', {'summary': 'The package 2'}) request.context = root request.matchdict = {'package_name': 'package2', 'version': '2.0'} result = get_package_by_version(request) assert isinstance(result, HTTPNotFound) is True
def test_list_packages(self): from papaye.views.simple import ListPackagesView from papaye.models import Package, Root # Test packages root = Root() root['package1'] = Package(name='package1') root['package2'] = Package(name='package2') view = ListPackagesView(root, self.request) response = view() self.assertIsInstance(response, dict) self.assertIn('objects', response) self.assertIsInstance(response['objects'], types.GeneratorType) self.assertEqual( [package.name for url, package in response['objects']], ['package1', 'package2'])
def test_get_index(self): from papaye.models import Root, Package root = Root() package = Package(name='package1') root['package'] = package result = root[0] assert result == package
def test_get_index(self): from papaye.models import Package, Release package = Package(name='package1') release = Release('1.0', '1.0', metadata={}) package['release'] = release result = package[0] assert result == release
def test_iter(self): from papaye.models import Root, Package root = Root() package = Package(name='package1') root['package'] = package result = iter(root) assert isinstance(result, types.GeneratorType) assert list(result) == [ package, ]
def test_by_packagename(self): from papaye.models import Release, Package from papaye.factories import repository_root_factory root = repository_root_factory(self.request) root['package1'] = Package(name='package1') root['package1']['1.0'] = Release('1.0', '1.0', metadata={}) result = Release.by_packagename('package1', self.request) self.assertEqual(result, [ root['package1']['1.0'], ])
def test_iter(self): from papaye.models import Package, Release package = Package(name='package1') release = Release('1.0', '1.0', metadata={}) package['release'] = release result = iter(package) assert isinstance(result, types.GeneratorType) assert list(result) == [ release, ]
def test_get_last_remote_version_with_proxy_404(self, mock): from papaye.models import Package fake_response = FakeGRequestResponse(status_code=404, content=b'{"info": {"version": "1.0"}}') mock.return_value = fake_response # Test data package = Package('test') result = Package.get_last_remote_version(True, package.name) self.assertEqual(mock.call_count, 1) self.assertIsNone(result)
def __call__(self): check_update = True if self.request.GET.get('check_update', 'true') == 'true' else False package = self.context.__parent__.__parent__ last_remote_version = Package.get_last_remote_version(self.proxy, package.name) if check_update: if not package.repository_is_up_to_date(last_remote_version): return not_found(self.request) response = Response() response.content_disposition = 'attachment; filename="{}"'.format(self.context.filename) response.charset = 'utf-8' response.content_type = self.context.content_type response.body_file = self.context.content.open() return response
def test_repository_is_up_to_date(self): from papaye.models import Package, Release # Test package package = Package(name='package1') release = Release(name='1.0', version='1.0', metadata={}) package['release'] = release self.assertTrue(package.repository_is_up_to_date('1.0')) self.assertTrue(package.repository_is_up_to_date('0.9')) self.assertTrue(package.repository_is_up_to_date('1.0a')) self.assertFalse(package.repository_is_up_to_date('1.1')) self.assertFalse(package.repository_is_up_to_date('1.1a')) self.assertTrue(package.repository_is_up_to_date('')) self.assertTrue(package.repository_is_up_to_date(None))
def test_get_package(setup): from papaye.views.api import get_package from papaye.models import Package, Root, Release request = testing.DummyRequest() root = Root() root['package1'] = Package(name='package1') root['package2'] = Package(name='package2') root['package1']['1.0'] = Release('1.0', '1.0', { 'summary': 'The package 1', 'description': 'A description', }) root['package2']['1.0'] = Release('1.0', '1.0', { 'summary': 'The package 2', 'description': 'A description', }) request.context = root request.matchdict = {'package_name': 'package1'} result = get_package(request) assert isinstance(result, dict) is True assert result['name'] == 'package1' assert 'metadata' in result
def test_serialize(self): from papaye.serializers import ReleaseAPISerializer from papaye.models import Package, Release, ReleaseFile serializer = ReleaseAPISerializer(request=self.request) package = Package(name='package') package['1.0'] = Release('1.0', '1.0', { 'summary': 'The package', 'description': 'A description', }) package['2.0'] = Release('2.0', '2.0', { 'summary': 'The package', 'description': 'A description', }) package['1.0']['package-1.0.tar.gz'] = ReleaseFile('package-1.0.tar.gz', b'') expected = { 'name': 'package', 'version': '1.0', 'gravatar_hash': None, 'metadata': { 'summary': 'The package', 'description': {'html': True, 'content': '<p>A description</p>\n'}, 'version': None, 'author': None, 'author_email': None, 'home_page': None, 'keywords': [], 'license': None, 'maintainer': None, 'maintainer_email': None, 'platform': None, 'classifiers': [], 'name': None, }, 'download_url': 'http://example.com/simple/package/1.0/package-1.0.tar.gz/', 'release_files': [{ 'size': '0', 'filename': 'package-1.0.tar.gz', 'url': 'http://example.com/simple/package/1.0/package-1.0.tar.gz/', 'version': '1.0', 'upload_date': str(package['1.0']['package-1.0.tar.gz'].upload_date), }], 'other_releases': [{ 'url': 'http://example.com/#/package/package/2.0', 'version': '2.0', }] } result = serializer.serialize(package['1.0']) self.assertEqual(result, expected)
def test_get_packages(setup): from papaye.views.api import list_packages from papaye.models import Package, Root, Release request = testing.DummyRequest() root = Root() root['package1'] = Package(name='package1') root['package2'] = Package(name='package2') root['package1']['1.0'] = Release('1.0', '1.0', { 'summary': 'The package 1', 'description': 'A description', }) root['package2']['1.0'] = Release('1.0', '1.0', { 'summary': 'The package 2', 'description': 'A description', }) expected = [ { 'name': 'package1', 'summary': 'The package 1', }, { 'name': 'package2', 'summary': 'The package 2', }, ] request.context = root result = list_packages(request) assert isinstance(result, dict) is True assert 'result' in result assert isinstance(result['result'], list) is True assert 'count' in result assert result['count'] == 2 assert len(result['result']) == 2 assert isinstance(result['result'][0], dict) is True assert [dict(element) for element in result['result']] == expected
def build_repository(self, release_name=None, with_metadata=False, root=None): package_name = self.get_remote_package_name(self.package_name) if not package_name: return None info = self.get_remote_informations(self.pypi_url.format(package_name)) if info: package_root = Root() if root is None else root package = Package(info['info']['name']) package.__parent__ = package_root package_root[package.name] = package remote_releases = [ release_name, ] if release_name else info['releases'].keys() for remote_release in remote_releases: release = Release(remote_release, remote_release, metadata=info['info'], deserialize_metadata=with_metadata) package[remote_release] = release for remote_release_file in info['releases'][remote_release]: filename = remote_release_file['filename'] md5_digest = remote_release_file['md5_digest'] release_file = ReleaseFile(filename, b'', md5_digest) setattr(release_file, 'pypi_url', remote_release_file['url']) release[filename] = release_file if root: return self.smart_merge(root, package) return package return None
def __call__(self): package = self.context release_files = [] for release in package.releases.values(): for release_file in release.release_files.values(): release_files.append(release_file) context = { 'objects': ((self.request.resource_url( release_file, route_name='simple' )[:-1] + "#md5={}".format(release_file.md5_digest), release_file) for release_file in release_files), } if package.repository_is_up_to_date(Package.get_last_remote_version(self.proxy, package.name)): return context else: return not_found(self.request)
def __call__(self): check_update = True if self.request.GET.get( 'check_update', 'true') == 'true' else False package = self.context.__parent__.__parent__ last_remote_version = Package.get_last_remote_version( self.proxy, package.name) if check_update: if not package.repository_is_up_to_date(last_remote_version): return not_found(self.request) response = Response() response.content_disposition = 'attachment; filename="{}"'.format( self.context.filename) response.charset = 'utf-8' response.content_type = self.context.content_type response.body_file = self.context.content.open() response.content_length = self.context.size return response
def __call__(self): package = self.context release_files = [] for release in package.releases.values(): for release_file in release.release_files.values(): release_file.__name__ = release_file.__name__.replace(' ', '-') release_files.append(release_file) context = { 'objects': ((self.request.resource_url(release_file, route_name='simple')[:-1] + "#md5={}".format(release_file.md5_digest), release_file) for release_file in release_files), } if package.repository_is_up_to_date( Package.get_last_remote_version(self.proxy, package.name)): return context else: return not_found(self.request)
def test_list_releases_files_with_new_remotes_release( self, mock, requests_mock): from papaye.views.simple import ListReleaseFileView from papaye.models import Package, Release, Root, ReleaseFile mock.return_value = "3.0" # Test packages root = Root() root['package1'] = Package(name='package1') root['package1']['release1'] = Release(name='release1', version='1.0', metadata={}) root['package1']['release1'].__parent__ = root['package1'] root['package1']['release1']['releasefile1.tar.gz'] = ReleaseFile( filename='releasefile1.tar.gz', content=b'', md5_digest='12345') root['package1']['release1']['releasefile1.tar.gz'].__parent__ = root[ 'package1']['release1'] self.request.matchdict['traverse'] = ( root['package1'].__name__, root['package1']['release1'].__name__) self.request.registry.settings['papaye.proxy'] = 'true' view = ListReleaseFileView(root['package1'], self.request) pypi_result = { 'info': { 'name': 'package1', }, 'releases': { 'release1': [{ 'filename': 'releasefile1.tar.gz', 'url': 'http://example.com/', 'md5_digest': 'fake md5', }] } } requests_mock.return_value = FakeGRequestResponse( 200, bytes(json.dumps(pypi_result), 'utf-8')) response = view() self.assertIsInstance(response, dict)
def test_download_release_with_old_release(self, mock_requests): from papaye.views.simple import DownloadReleaseView from papaye.models import ReleaseFile, Release, Package pypi_response = { 'info': { 'name': 'package', 'version': '2.0', }, 'releases': { '2.0': [{ 'filename': 'releasefile1.tar.gz', 'url': 'http://example.com/', 'md5_digest': 'fake md5', }] } } # mock_last_version.return_value = '2.0' mock_requests.return_value = FakeGRequestResponse( 200, bytes(json.dumps(pypi_response), 'utf-8')) package = Package(name='package') release = Release(name='1.0', version='1.0', metadata={}) release_file = ReleaseFile(filename='releasefile-1.0.tar.gz', content=b'Hello') package['1.0'] = release package['1.0']['releasefile-1.0.tar.gz'] = release_file self.request.matchdict['traverse'] = (package.__name__, release.__name__, release_file.__name__) self.request.traversed = (package.__name__, release.__name__) view = DownloadReleaseView(release_file, self.request) view.proxy = True result = view() self.assertIsInstance(result, HTTPNotFound)
def test_serialize_with_metadata_is_none(self): from papaye.serializers import ReleaseAPISerializer from papaye.models import Package, Release, ReleaseFile serializer = ReleaseAPISerializer(request=self.request) package = Package(name='package') package['1.0'] = Release('1.0', '1.0', { 'summary': 'The package', 'description': 'A description', }) package['1.0'].metadata = None package['2.0'] = Release('2.0', '2.0', { 'summary': 'The package', 'description': 'A description', }) package['1.0']['package-1.0.tar.gz'] = ReleaseFile('package-1.0.tar.gz', b'') expected = { 'name': 'package', 'version': '1.0', 'gravatar_hash': None, 'metadata': None, 'download_url': 'http://example.com/simple/package/1.0/package-1.0.tar.gz/', 'release_files': [{ 'size': '0', 'filename': 'package-1.0.tar.gz', 'url': 'http://example.com/simple/package/1.0/package-1.0.tar.gz/', 'version': '1.0', 'upload_date': str(package['1.0']['package-1.0.tar.gz'].upload_date), }], 'other_releases': [{ 'url': 'http://example.com/#/package/package/2.0', 'version': '2.0', }] } result = serializer.serialize(package['1.0']) self.assertEqual(result, expected)
def test_download_release(self, mock): from papaye.views.simple import DownloadReleaseView from papaye.models import ReleaseFile, Release, Package package = Package(name='package') release = Release(name='1.0', version='1.0', metadata={}) release_file = ReleaseFile(filename='releasefile-1.0.tar.gz', content=b'Hello') release_file.content_type = 'text/plain' package['1.0'] = release package['1.0']['releasefile-1.0.tar.gz'] = release_file pypi_response = { 'info': { 'name': 'package', 'version': '1.0' }, 'releases': { '1.0': [{ 'filename': 'releasefile1.tar.gz', 'url': 'http://example.com/', 'md5_digest': 'fake md5', }] } } mock.return_value = FakeGRequestResponse( 200, bytes(json.dumps(pypi_response), 'utf-8')) self.request.registry.settings['papaye.proxy'] = 'true' view = DownloadReleaseView(release_file, self.request) result = view() self.assertEqual(result.status_code, 200) self.assertEqual(result.body, b'Hello') self.assertEqual(result.content_type, 'text/plain') self.assertEqual(result.content_disposition, 'attachment; filename="releasefile-1.0.tar.gz"')
def test_by_name_not_found(self): from papaye.models import Package result = Package.by_name('package1', self.request) self.assertEqual(result, None)