Exemple #1
0
    def testPreviewStats(self):
        contents_hash = hash_contents(self.CONTENTS_WITH_Q_EXT)

        readme_contents = 'Hello, World!'
        self._mock_object('test_user', self.HASH2, readme_contents.encode())

        # Push.
        resp = self.app.put('/api/package/test_user/foo/%s' % contents_hash,
                            data=json.dumps(dict(
                                is_public=True,
                                description="",
                                contents=self.CONTENTS_WITH_Q_EXT,
                                sizes={
                                    self.HASH1: 5,
                                    self.HASH2: 37
                                }),
                                            default=encode_node),
                            content_type='application/json',
                            headers={'Authorization': 'test_user'})
        assert resp.status_code == requests.codes.ok

        # Get preview.
        resp = self.app.get('/api/package_preview/test_user/foo/%s' %
                            contents_hash,
                            headers={'Authorization': 'test_user'})
        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'), object_hook=decode_node)
        assert data['total_size_uncompressed'] == 42
        assert data['file_types'] == {'': 4, '.jpg': 2, '.exe': 1}
        ts = data['install_timeseries']
        assert ts['startDate'] == ts['endDate']
        assert ts['frequency'] == 'week'
        assert ts['timeSeries'] == [0]
Exemple #2
0
    def testPackageTimeseries(self):
        huge_contents_hash = hash_contents(self.HUGE_CONTENTS)

        readme_contents = 'Hello, World!'
        self._mock_object('test_user', self.HASH1, readme_contents.encode())
        # Push
        resp = self.app.put(
            '/api/package/test_user/foo/%s' % huge_contents_hash,
            data=json.dumps(dict(
                is_public=True,
                description="",
                contents=self.HUGE_CONTENTS
            ), default=encode_node),
            content_type='application/json',
            headers={
                'Authorization': 'test_user'
            }
        )
        assert resp.status_code == requests.codes.ok

        # Get timeseries
        resp = self.app.get(
            '/api/package_timeseries/test_user/foo/install',
            content_type='application/json',
            headers={
                'Authorization': 'test_user'
            }
        )

        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'), object_hook=decode_node)
        assert data['total'] == 0
        assert data['startDate'] < data['endDate']
        assert data['timeSeries'] == [0] * 52
        assert data['frequency'] == 'week'

        # install as anonymous user
        resp = self.app.get(
            '/api/package/test_user/foo/%s' % huge_contents_hash,
        )
        assert resp.status_code == requests.codes.ok

        # Get timeseries again
        # Get timeseries
        resp = self.app.get(
            '/api/package_timeseries/test_user/foo/install',
            content_type='application/json',
            headers={
                'Authorization': 'test_user'
            }
        )

        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'), object_hook=decode_node)
        assert data['total'] == 1
        assert data['startDate'] < data['endDate']
        assert data['timeSeries'] == [0] * 51 + [1]
        assert data['frequency'] == 'week'
Exemple #3
0
    def testSharerCantPushNewVersion(self):
        """
        Push a package, share it and test that the
        recipient can't add a new version.
        """
        sharewith = "anotheruser"
        resp = self._share_package(self.user, self.pkg, sharewith)
        assert resp.status_code == requests.codes.ok

        newcontents = RootNode(dict(bar=GroupNode(dict())))
        newpkgurl = '/api/package/{usr}/{pkg}/{hash}'.format(
            usr=self.user, pkg=self.pkg, hash=hash_contents(newcontents))

        # Test that the receiver can't create a new version
        # of the package
        resp = self.app.put(newpkgurl,
                            data=json.dumps(dict(description="",
                                                 contents=newcontents),
                                            default=encode_node),
                            content_type='application/json',
                            headers={'Authorization': sharewith})

        assert resp.status_code == requests.codes.forbidden

        data = json.loads(resp.data.decode('utf8'))
        assert 'message' in data
Exemple #4
0
    def testNonSharerCantPushToPublicPkg(self):
        """
        Push a package, share it publicly, and test that other users
        can't push new versions.
        """
        otheruser = "******"
        resp = self._share_package(self.user, self.pkg, PUBLIC)
        assert resp.status_code == requests.codes.ok

        newcontents = RootNode(dict(bar=GroupNode(dict())))
        newpkgurl = '/api/package/{usr}/{pkg}/{hash}'.format(
            usr=self.user, pkg=self.pkg, hash=hash_contents(newcontents))

        # Test that the receiver can't create a new version
        # of the package
        resp = self.app.put(newpkgurl,
                            data=json.dumps(dict(
                                description="",
                                contents=newcontents,
                                sizes=fake_obj_sizes(newcontents),
                            ),
                                            default=encode_node),
                            content_type='application/json',
                            headers={'Authorization': otheruser})

        assert resp.status_code == requests.codes.forbidden

        data = json.loads(resp.data.decode('utf8'))
        assert 'message' in data
Exemple #5
0
    def testPreview(self):
        huge_contents_hash = hash_contents(self.HUGE_CONTENTS)

        readme_contents = 'Hello, World!'
        self._mock_object('test_user', self.HASH1, readme_contents.encode())

        # Push.
        resp = self.app.put('/api/package/test_user/foo/%s' %
                            huge_contents_hash,
                            data=json.dumps(dict(is_public=True,
                                                 description="",
                                                 contents=self.HUGE_CONTENTS),
                                            default=encode_node),
                            content_type='application/json',
                            headers={'Authorization': 'test_user'})
        assert resp.status_code == requests.codes.ok

        # Get preview.
        resp = self.app.get('/api/package_preview/test_user/foo/%s' %
                            huge_contents_hash,
                            headers={'Authorization': 'test_user'})
        assert resp.status_code == requests.codes.ok

        # Get preview as an anonymous user.
        resp = self.app.get(
            '/api/package_preview/test_user/foo/%s' % huge_contents_hash, )
        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'), object_hook=decode_node)
        assert data['is_public'] == True
        assert data['is_team'] == False
        assert data['readme_url']
        assert data['readme_preview'] == readme_contents
        preview = data['preview']

        assert preview == [
            ['README', None],
            [
                'big_group',
                [
                    ['child01', []],
                    ['child02', []],
                    ['child03', []],
                    ['child04', []],
                    ['child05', []],
                    ['child06', []],
                    ['child07', []],
                    ['child08', []],
                    ['child09', []],
                    ['child10', []],
                    ['...', None],
                ]
            ],
            [
                'group1',
                [['group2', [['group3', [['group4', [['...', None]]]]]]]]
            ],
        ]
Exemple #6
0
 def make_version(i, version):
     resp = self.app.put(
         '/api/version/{usr}/{pkg}/{version}'.format(usr=self.user,
                                                     pkg=self.pkg,
                                                     version=version),
         data=json.dumps(
             dict(hash=hash_contents(self.contents_list[i]))),
         content_type='application/json',
         headers={'Authorization': self.user})
     assert resp.status_code == requests.codes.ok
Exemple #7
0
    def testLog(self):
        resp = self.app.get('/api/log/{usr}/{pkg}/'.format(usr=self.user,
                                                           pkg=self.pkg),
                            headers={'Authorization': self.user})
        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'))
        logs = data['logs']

        assert len(logs) == 3

        for log, contents in zip(logs, self.contents_list):
            assert log['author'] == self.user
            assert log['hash'] == hash_contents(contents)
Exemple #8
0
    def put_package(self, owner, package, contents, public=False):
        pkgurl = '/api/package/{usr}/{pkg}/{hash}'.format(
            usr=owner, pkg=package, hash=hash_contents(contents))

        resp = self.app.put(pkgurl,
                            data=json.dumps(dict(
                                description="",
                                contents=contents,
                                public=public,
                            ),
                                            default=encode_node),
                            content_type='application/json',
                            headers={'Authorization': owner})
        assert resp.status_code == requests.codes.ok
        return pkgurl
Exemple #9
0
    def setUp(self):
        super(TagTestCase, self).setUp()

        self.user = "******"
        self.pkg = "pkg"
        self.contents_list = [
            RootNode(dict(foo=GroupNode(dict()))),
            RootNode(dict(bar=GroupNode(dict()))),
            RootNode(dict(baz=GroupNode(dict())))
        ]
        self.hashes = [
            hash_contents(contents) for contents in self.contents_list
        ]

        # Upload three package instances.
        for contents in self.contents_list:
            self.put_package(self.user, self.pkg, contents, public=True)
Exemple #10
0
    def testLog(self):
        resp = self.app.get('/api/log/{usr}/{pkg}/'.format(usr=self.user,
                                                           pkg=self.pkg),
                            headers={'Authorization': self.user})
        assert resp.status_code == requests.codes.ok

        data = json.loads(resp.data.decode('utf8'))
        logs = data['logs']

        assert len(logs) == 3

        tag_list = [None, None, ['latest']]
        version_list = [['1.0.0'], ['2.0.0'], ['3.0.0']]
        for log, contents, tags, versions in zip(logs, self.contents_list,
                                                 tag_list, version_list):
            assert log['author'] == self.user
            assert log['hash'] == hash_contents(contents)
            assert log['tags'] == tags
            assert log['versions'] == versions
Exemple #11
0
    def put_package(self, owner, package, contents, is_public=False, is_team=False, tag_latest=False):
        contents_hash = hash_contents(contents)
        pkgurl = '/api/package/{usr}/{pkg}/{hash}'.format(
            usr=owner,
            pkg=package,
            hash=contents_hash
        )

        resp = self.app.put(
            pkgurl,
            data=json.dumps(dict(
                description="",
                contents=contents,
                is_public=is_public,
                is_team=is_team,
                sizes=fake_obj_sizes(contents),
            ), default=encode_node),
            content_type='application/json',
            headers={
                'Authorization': owner
            }
        )
        assert resp.status_code == requests.codes.ok

        if tag_latest:
            resp = self.app.put(
                '/api/tag/{usr}/{pkg}/{tag}'.format(
                    usr=owner,
                    pkg=package,
                    tag='latest'
                ),
                data=json.dumps(dict(
                    hash=contents_hash
                )),
                content_type='application/json',
                headers={
                    'Authorization': owner
                }
            )
            assert resp.status_code == requests.codes.ok

        return pkgurl
Exemple #12
0
 def testContentsHash(self):
     assert hash_contents(self.CONTENTS) == self.CONTENTS_HASH
     assert hash_contents(self.CONTENTS_WITH_METADATA) == self.CONTENTS_HASH
Exemple #13
0
    def testDeleteAccessTagVersionLog(self):
        hashes = [hash_contents(contents) for contents in self.contents_list]

        sharewith = "share_with"
        tag = 'tag1'
        version = '1.0'

        def _has_access():
            resp = self.app.get(
                '/api/access/{owner}/{pkg}/'.format(owner=self.user, pkg=self.pkg),
                headers={
                    'Authorization': self.user
                }
            )
            assert resp.status_code == requests.codes.ok
            data = json.loads(resp.data.decode('utf8'))
            return sharewith in data['users']

        def _has_tag():
            resp = self.app.get(
                '/api/tag/{usr}/{pkg}/{tag}'.format(
                    usr=self.user,
                    pkg=self.pkg,
                    tag=tag
                ),
                headers={
                    'Authorization': self.user
                }
            )
            return resp.status_code == requests.codes.ok

        def _has_version():
            resp = self.app.get(
                '/api/version/{usr}/{pkg}/{version}'.format(
                    usr=self.user,
                    pkg=self.pkg,
                    version=version
                ),
                headers={
                    'Authorization': self.user
                }
            )
            return resp.status_code == requests.codes.ok

        def _has_log():
            resp = self.app.get(
                '/api/log/{usr}/{pkg}/'.format(
                    usr=self.user,
                    pkg=self.pkg
                ),
                headers={
                    'Authorization': self.user
                }
            )
            assert resp.status_code == requests.codes.ok
            data = json.loads(resp.data.decode('utf8'))
            return len(data['logs']) > 1

        # Add a user
        resp = self._share_package(self.user, self.pkg, sharewith)
        assert resp.status_code == requests.codes.ok

        # Add a tag
        resp = self.app.put(
            '/api/tag/{usr}/{pkg}/{tag}'.format(
                usr=self.user,
                pkg=self.pkg,
                tag=tag
            ),
            data=json.dumps(dict(
                hash=hashes[0]
            ), default=encode_node),
            content_type='application/json',
            headers={
                'Authorization': self.user
            }
        )
        assert resp.status_code == requests.codes.ok

        # Add a version
        resp = self.app.put(
            '/api/version/{usr}/{pkg}/{version}'.format(
                usr=self.user,
                pkg=self.pkg,
                version=version
            ),
            data=json.dumps(dict(
                hash=hashes[1]
            ), default=encode_node),
            content_type='application/json',
            headers={
                'Authorization': self.user
            }
        )
        assert resp.status_code == requests.codes.ok

        # Verify that everything looks as expected before deleting the package
        assert _has_access()
        assert _has_tag()
        assert _has_version()
        assert _has_log()

        # Delete the package
        resp = self.app.delete(
            '/api/package/{usr}/{pkg}/'.format(
                usr=self.user,
                pkg=self.pkg
            ),
            headers={
                'Authorization': self.user
            }
        )
        assert resp.status_code == requests.codes.ok

        # Create a new package with the same name
        self.put_package(self.user, self.pkg, self.contents_list[0])

        # Verify that users, tags, and versions didn't survive
        assert not _has_access()
        assert not _has_tag()
        assert not _has_version()
        assert not _has_log()