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 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 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_list_releases_files(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['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'] 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'])])
def test_list_releases_files(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["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"] 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.__name__) for url, release in response["objects"]], [ ( "http://example.com/simple/package1/release1/releasefile1.tar.gz#md5=12345", root["package1"]["release1"]["releasefile1.tar.gz"].__name__, ) ], )
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_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 __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 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_instantiante(self): from papaye.models import Release result = Release('1.0', '1.0', metadata={}) assert result.version == '1.0' assert result.metadata is not None
def test_instantiante_without_deserialize_metadata(self): from papaye.models import Release result = Release('1.0', '1.0', metadata={}, deserialize_metadata=False) assert result.version == '1.0' assert not hasattr(result, 'metadata')
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_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 remove_release(request): package_name = request.matchdict.get("package_name") version = request.matchdict.get("version") release = Release.by_releasename(package_name, version, request) if release is None: return HTTPNotFound() del request.root[package_name][version] return {"success": True}
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_get_index(self): from papaye.models import Package, Release, ReleaseFile release = Release('1.0', '1.0', metadata={}) release_file = ReleaseFile('filename.tar.gz', b'') release['filename.tar.gz'] = release_file result = release[0] assert result == release_file
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_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_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_iter(self): from papaye.models import Package, Release, ReleaseFile release = Release('1.0', '1.0', metadata={}) release_file = ReleaseFile('filename.tar.gz', b'') release['filename.tar.gz'] = release_file result = iter(release) assert isinstance(result, types.GeneratorType) assert list(result) == [ release_file, ]
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_clone(self): from papaye.models import Release release = Release('A release', '1.0', metadata={}) result = Release.clone(release) assert result is not release assert result.__name__ == release.__name__ assert result.version == release.version assert result.original_metadata == release.original_metadata assert result.metadata == release.metadata assert hasattr(result, 'release_files') assert len(list(result)) == len(list(release))
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_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_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 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_list_releases_with_proxy(self, mock): from papaye.views.simple import ListReleaseFileView from papaye.models import Package, Release, Root, ReleaseFile mock_proxy_response(mock) # Test packages root = Root() root["pyramid"] = Package(name="pyramid") root["pyramid"]["release1"] = Release(name="release1", version="1.0", metadata={}) root["pyramid"]["release1"].__parent__ = root["pyramid"] root["pyramid"]["release1"]["releasefile1.tar.gz"] = ReleaseFile( filename="releasefile1.tar.gz", content=b"", md5_digest="12345" ) root["pyramid"]["release1"]["releasefile1.tar.gz"].__parent__ = root["pyramid"]["release1"] self.request.matchdict["traverse"] = (root["pyramid"].__name__, root["pyramid"]["release1"].__name__) self.request.registry.settings["papaye.proxy"] = "true" view = ListReleaseFileView(root["pyramid"], self.request) response = view() self.assertIsInstance(response, dict) assert len(list(response["objects"])) == 82
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_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 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 test_by_packagename_not_found(self): from papaye.models import Release result = Release.by_packagename('package1', self.request) self.assertEqual(result, None)