def test_download_release_from_pypi(self, get_connection_mock, request_mock): from papaye.tasks.download import download_release_from_pypi from papaye.factories import repository_root_factory 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()), ] download_release_from_pypi(request.registry.settings, 'pyramid', '1.5') self.assertEqual(request_mock.call_count, 3) root = repository_root_factory(self.conn) self.assertIn('pyramid', root) self.assertIn('1.5', root['pyramid'].releases) self.assertIn('pyramid-1.5.tar.gz', root['pyramid']['1.5'].release_files) release_file = root['pyramid']['1.5']['pyramid-1.5.tar.gz'] self.assertEqual(release_file.md5_digest, "8747658dcbab709a9c491e43d3b0d58b") self.assertEqual(release_file.filename, "pyramid-1.5.tar.gz") self.assertEqual(release_file.size, 2413504) self.assertEqual(list(root['pyramid'].releases.keys()), ['1.5', ]) assert root['pyramid']['1.5'].metadata is not None assert root['pyramid'].__parent__ is root assert root['pyramid']['1.5'].__parent__ is root['pyramid'] assert root['pyramid']['1.5']['pyramid-1.5.tar.gz'].__parent__ is root['pyramid']['1.5']
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 setUp(self): self.request = testing.DummyRequest() self.blob_dir = set_database_connection(self.request) settings = disable_cache() self.config = testing.setUp(request=self.request, settings=settings) with open(get_resource('pyramid.json'), 'rb') as pyramid_json: self.pypi_response = FakeGRequestResponse(200, pyramid_json.read())
def test_get_remote_informations_404(self, mock): from papaye.proxy import PyPiProxy mock.return_value = FakeGRequestResponse(404, b'') url = "http://pypi.python.org/pypi/pyramid/json" proxy = PyPiProxy(self.request, 'pyramid') result = proxy.get_remote_informations(url) self.assertIsNone(result)
def test_build_repository_404_error(self, mock): from papaye.proxy import PyPiProxy mock.return_value = FakeGRequestResponse(404, b'') proxy = PyPiProxy(self.request, 'pyramid') result = proxy.build_repository() self.assertIsNone(result)
def test_exists_on_pypi_with_other_status_code(self, mock): mock.return_value = FakeGRequestResponse(500, '') view = self.FakeView() result = view.exists_on_pypi('http://pypi.python.org/pypi/example') self.assertFalse(result)
def test_exists_on_pypi_with_not_found(self, mock): mock.return_value = FakeGRequestResponse(404, '') view = self.FakeView() result = view.exists_on_pypi('http://pypi.python.org/pypi/example') self.assertFalse(result)
def test_exists_on_pypi(self, mock): mock.return_value = FakeGRequestResponse(200, '') view = self.FakeView() result = view.exists_on_pypi('http://pypi.python.org/pypi/example') self.assertTrue(result)
def test_download_release_from_pypi_with_bad_md5(self, get_connection_mock, request_mock): from papaye.tasks.download import download_release_from_pypi from papaye.factories import repository_root_factory request = get_current_request() get_connection_mock.return_value = self.conn json_response = open(get_resource('pyramid.json'), 'rb') release_file_content = io.BytesIO(b'corrupted_file') request_mock.side_effect = [ FakeGRequestResponse(200, b'', 'http://pypi.python.org/simple/pyramid/'), FakeGRequestResponse(200, json_response.read()), FakeGRequestResponse(200, release_file_content.read()), ] download_release_from_pypi(request.registry.settings, 'pyramid', '1.5') self.assertEqual(request_mock.call_count, 3) assert 'pyramid' not in self.root
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_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_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"')