Exemple #1
0
def create_app_root(dbconn):
    zodb_root = get_database_root(dbconn)
    if not '{}_root'.format(APP_NAME) in zodb_root:
        zodb_root['{}_root'.format(APP_NAME)] = Root()
    if not 'user' in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['user'] = Root()
    if not 'repository' in zodb_root['{}_root'.format(APP_NAME)]:
        zodb_root['{}_root'.format(APP_NAME)]['repository'] = Root()
Exemple #2
0
    def get_root(self):
        from papaye.models import Root
        root = Root()

        def root_func():
            return {'papaye_root': {'repository': {}, 'user': {}}}

        class P_jar:
            pass
        root._p_jar = P_jar()
        root._p_jar.root = root_func
        return root
Exemple #3
0
    def get_root(self):
        from papaye.models import Root
        root = Root()

        def root_func():
            return {'papaye_root': {'repository': {}, 'user': {}}}

        class P_jar:
            pass

        root._p_jar = P_jar()
        root._p_jar.root = root_func
        return root
Exemple #4
0
    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'])])
Exemple #5
0
    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)
Exemple #6
0
    def test_build_repository_with_specified_root(self, mock):
        from papaye.proxy import PyPiProxy
        from papaye.models import Package, ReleaseFile, Root
        mock.return_value = self.pypi_response
        info_dict = json.loads(self.pypi_response.content.decode('utf-8'))
        root = Root()

        proxy = PyPiProxy(self.request, 'pyramid')
        result = proxy.build_repository(with_metadata=True, root=root)

        self.assertIsInstance(result, Package)
        for release in result.releases.values():
            self.assertIn(release.__name__, info_dict['releases'].keys())
        self.assertEqual(len(result.releases.keys()),
                         len(info_dict['releases'].keys()))
        self.assertEqual(len(result['1.5'].release_files.keys()), 1)
        self.assertIsInstance(result['1.5'].release_files.values()[0],
                              ReleaseFile)
        self.assertTrue(
            getattr(result['1.5'].release_files.values()[0], 'pypi_url', None))
        self.assertEqual(
            result['1.5'].release_files.values()[0].pypi_url,
            "https://pypi.python.org/packages/source/p/pyramid/pyramid-1.5.tar.gz"
        )
        self.assertIsNotNone(result['1.5'].metadata)
        assert result.__parent__ is root
Exemple #7
0
    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
Exemple #8
0
def test_get_package_unknown(setup):
    from papaye.views.api import get_package
    from papaye.models import Root
    request = testing.DummyRequest()
    root = Root()
    request.context = root
    request.matchdict = {'package_name': 'package1'}

    result = get_package(request)

    assert isinstance(result, HTTPNotFound) is True
Exemple #9
0
    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,
        ]
Exemple #10
0
    def test_list_packages_without_package(self):
        from papaye.views.simple import ListPackagesView
        from papaye.models import Root

        # Test packages
        root = Root()

        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']], [])
Exemple #11
0
def test_get_packages_with_no_package_in_database(setup):
    from papaye.views.api import list_packages
    from papaye.models import Root
    request = testing.DummyRequest()
    root = Root()
    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'] == 0
    assert len(result['result']) == 0
    assert result['result'] == []
Exemple #12
0
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
Exemple #13
0
    def test_upload_release_with_spaces(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",
            "md5_digest": "Fake MD5"
        }
        root = Root()
        self.request.root = root
        view = UploadView(root, self.request)
        result = view()

        self.assertIsInstance(result, Response)
        self.assertEqual(result.status_int, 200)
        self.assertTrue('my-package' in root)
        self.assertIsInstance(root['my-package'], Package)
        self.assertTrue(root['my-package'].releases.get('1.0', False))
        self.assertIsInstance(root['my-package']['1.0'], Release)
        self.assertTrue(root['my-package']['1.0'].release_files.get(
            'foo.tar.gz', b''))
        self.assertIsInstance(root['my-package']['1.0']['foo.tar.gz'],
                              ReleaseFile)
        self.assertEqual(root['my-package']['1.0']['foo.tar.gz'].md5_digest,
                         "Fake MD5")
        self.assertIsNotNone(root['my-package']['1.0'].metadata)
        self.assertIsInstance(root['my-package']['1.0'].metadata, dict)
        self.assertEqual(
            root['my-package']['1.0'].release_files.get('foo.tar.gz',
                                                        b'').size, 7)
Exemple #14
0
    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)
Exemple #15
0
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
Exemple #16
0
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
Exemple #17
0
    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