def test_class_name_is_unique(self): value = self._stub_package(class_definitions=('foo', 'bar')) api.package_upload(value, self.tenant_id) value = self._stub_package(class_definitions=('bar', 'baz'), fully_qualified_name='com.example.package2') self.assertRaises(exc.HTTPConflict, api.package_upload, value, self.tenant_id)
def test_pagination_loops_through_names(self): """Tests that packages with same display name are not skipped Creates 10 packages with the same display name and iterates through them, checking that package are not skipped. """ for dummy in range(10): api.package_upload( self._stub_package(name='test', fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({}, self.context, limit=4) self.assertEqual(4, len(res)) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(4, len(res)) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(2, len(res)) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(0, len(res))
def test_package_update_public_public_fqn_violation(self): id1 = api.package_upload(self._stub_package(), self.tenant_id).id id2 = api.package_upload(self._stub_package(), self.tenant_id_2).id patch = self.get_change('replace', ['is_public'], True) api.package_update(id1, [patch], self.context) self.assertRaises(exc.HTTPConflict, api.package_update, id2, [patch], self.context_2)
def test_package_fqn_is_unique(self): self._create_categories() values = self._stub_package() api.package_upload(values, self.tenant_id) self.assertRaises(db_exception.DBDuplicateEntry, api.package_upload, values, self.tenant_id)
def test_package_search_search(self): pkg1 = api.package_upload( self._stub_package( fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkg2 = api.package_upload( self._stub_package( tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search( {'search': 'tag1'}, self.context) self.assertEqual(1, len(res)) res = api.package_search( {'search': pkg1.fully_qualified_name}, self.context) self.assertEqual(1, len(res)) res = api.package_search( {'search': pkg2.fully_qualified_name}, self.context) self.assertEqual(1, len(res)) res = api.package_search( {'search': 'not_a_valid_uuid'}, self.context) self.assertEqual(0, len(res)) res = api.package_search( {'search': 'some text'}, self.context) self.assertEqual(2, len(res))
def test_order_by_compound(self): pkgs_a, pkgs_z = [], [] for _ in range(5): package = api.package_upload( self._stub_package(name='z', fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkgs_z.append(package) for _ in range(5): package = api.package_upload( self._stub_package(name='a', fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkgs_a.append(package) # sort pkg ids by pkg created pkg_a_id = [ pkg.id for pkg in sorted(pkgs_a, key=lambda _pkg: _pkg.created) ] pkg_z_id = [ pkg.id for pkg in sorted(pkgs_z, key=lambda _pkg: _pkg.created) ] res = api.package_search({'order_by': ['name', 'created']}, self.context, limit=10) self.assertEqual(10, len(res)) self.assertEqual(pkg_a_id + pkg_z_id, [r.id for r in res])
def test_class_name_public_public_violation(self): value = self._stub_package(class_definitions=("foo", "bar"), is_public=True) api.package_upload(value, self.tenant_id) value = self._stub_package( class_definitions=("foo", "bar"), is_public=True, fully_qualified_name="com.example.package2" ) self.assertRaises(exc.HTTPConflict, api.package_upload, value, self.tenant_id_2)
def test_class_name_private_public_no_violation(self): value = self._stub_package(class_definitions=("foo", "bar"), is_public=False) api.package_upload(value, self.tenant_id) value = self._stub_package( class_definitions=("foo", "bar"), is_public=True, fully_qualified_name="com.example.package2" ) api.package_upload(value, self.tenant_id_2)
def test_packages_filter_by_id(self): """GET /catalog/packages with parameter "id" returns packages filtered by id. """ self._set_policy_rules({ 'get_package': '', 'manage_public_package': '' }) _, package1_data = self._test_package() _, package2_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1', ) package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2', ) expected_package = db_catalog_api.package_upload(package1_data, '') db_catalog_api.package_upload(package2_data, '') req = self._get('/catalog/packages', params={'id': expected_package.id}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(1, len(res.json['packages'])) found_package = res.json['packages'][0] self.assertEqual(expected_package.id, found_package['id'])
def test_packages_filter_by_id(self): """GET /catalog/packages with parameter "id" returns packages filtered by id. """ self._set_policy_rules( {'get_package': '', 'manage_public_package': ''} ) _, package1_data = self._test_package() _, package2_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1',) package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2',) expected_package = db_catalog_api.package_upload(package1_data, '') db_catalog_api.package_upload(package2_data, '') req = self._get('/catalog/packages', params={'id': expected_package.id}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(1, len(res.json['packages'])) found_package = res.json['packages'][0] self.assertEqual(expected_package.id, found_package['id'])
def test_package_search_owned(self): api.package_upload(self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload(self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) res = api.package_search({"owned": "true"}, self.context_admin) self.assertEqual(1, len(res)) res = api.package_search({"owned": "false"}, self.context_admin) self.assertEqual(2, len(res))
def test_class_name_private_public_no_violation(self): value = self._stub_package(class_definitions=('foo', 'bar'), is_public=False) api.package_upload(value, self.tenant_id) value = self._stub_package(class_definitions=('foo', 'bar'), is_public=True, fully_qualified_name='com.example.package2') api.package_upload(value, self.tenant_id_2)
def test_package_update_public_public_class_name_violation(self): id1 = api.package_upload(self._stub_package(class_definitions=("foo", "bar")), self.tenant_id).id id2 = api.package_upload( self._stub_package(class_definitions=("foo", "bar"), fully_qualified_name="com.example.package2"), self.tenant_id_2, ).id patch = self.get_change("replace", ["is_public"], True) api.package_update(id1, [patch], self.context) self.assertRaises(exc.HTTPConflict, api.package_update, id2, [patch], self.context_2)
def test_class_name_public_public_violation(self): value = self._stub_package(class_definitions=('foo', 'bar'), is_public=True) api.package_upload(value, self.tenant_id) value = self._stub_package(class_definitions=('foo', 'bar'), is_public=True, fully_qualified_name='com.example.package2') self.assertRaises(exc.HTTPConflict, api.package_upload, value, self.tenant_id_2)
def test_package_search_type(self): api.package_upload( self._stub_package(type="Application", fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) api.package_upload(self._stub_package(type="Library", fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({"type": "Library"}, self.context) self.assertEqual(1, len(res)) res = api.package_search({"type": "Application"}, self.context) self.assertEqual(1, len(res))
def test_package_update_public_public_class_name_violation(self): id1 = api.package_upload(self._stub_package( class_definitions=('foo', 'bar')), self.tenant_id).id id2 = api.package_upload(self._stub_package( class_definitions=('foo', 'bar'), fully_qualified_name='com.example.package2'), self.tenant_id_2).id patch = self.get_change('replace', ['is_public'], True) api.package_update(id1, [patch], self.context) self.assertRaises(exc.HTTPConflict, api.package_update, id2, [patch], self.context_2)
def test_package_search_tags(self): api.package_upload(self._stub_package(fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload(self._stub_package(tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({"tag": ["tag1"]}, self.context) self.assertEqual(1, len(res)) res = api.package_search({"tag": ["tag2"]}, self.context) self.assertEqual(1, len(res)) res = api.package_search({"tag": ["tag3"]}, self.context) self.assertEqual(0, len(res))
def test_package_search_disabled(self): api.package_upload( self._stub_package(is_public=True, enabled=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) api.package_upload( self._stub_package(is_public=True, enabled=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) res = api.package_search({"include_disabled": "false"}, self.context) self.assertEqual(1, len(res)) res = api.package_search({"include_disabled": "true"}, self.context) self.assertEqual(2, len(res))
def test_packages_filter_by_in_category(self): """Test that packages are filtered by in:cat1,cat2,cat3 GET /catalog/packages with parameter "category=in:cat1,cat2,cat3" returns packages filtered by category. """ names = ['cat1', 'cat2', 'cat3', 'cat4'] for name in names: db_catalog_api.category_add(name) self._set_policy_rules({ 'get_package': '', 'manage_public_package': '' }) _, package1_data = self._test_package() _, package2_data = self._test_package() _, package3_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1', ) package1_data['categories'] = ('cat1', 'cat2') package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2', ) package2_data['categories'] = ('cat2', 'cat3') package3_data['fully_qualified_name'] += '_3' package3_data['name'] += '_3' package3_data['class_definitions'] = (u'test.mpl.v1.app.Thing3', ) package3_data['categories'] = ('cat2', 'cat4') expected_packages = [ db_catalog_api.package_upload(package1_data, ''), db_catalog_api.package_upload(package2_data, '') ] db_catalog_api.package_upload(package3_data, '') categories_in = "in:cat1,cat3" req = self._get('/catalog/packages', params={'category': categories_in}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(2, len(res.json['packages'])) found_packages = res.json['packages'] self.assertEqual([pack.id for pack in expected_packages], [pack['id'] for pack in found_packages])
def test_packages_filter_by_in_category(self): """Test that packages are filtered by in:cat1,cat2,cat3 GET /catalog/packages with parameter "category=in:cat1,cat2,cat3" returns packages filtered by category. """ names = ['cat1', 'cat2', 'cat3', 'cat4'] for name in names: db_catalog_api.category_add(name) self._set_policy_rules( {'get_package': '', 'manage_public_package': ''} ) _, package1_data = self._test_package() _, package2_data = self._test_package() _, package3_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1',) package1_data['categories'] = ('cat1', 'cat2') package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2',) package2_data['categories'] = ('cat2', 'cat3') package3_data['fully_qualified_name'] += '_3' package3_data['name'] += '_3' package3_data['class_definitions'] = (u'test.mpl.v1.app.Thing3',) package3_data['categories'] = ('cat2', 'cat4') expected_packages = [db_catalog_api.package_upload(package1_data, ''), db_catalog_api.package_upload(package2_data, '')] db_catalog_api.package_upload(package3_data, '') categories_in = "in:cat1,cat3" req = self._get('/catalog/packages', params={'category': categories_in}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(2, len(res.json['packages'])) found_packages = res.json['packages'] self.assertEqual([pack.id for pack in expected_packages], [pack['id'] for pack in found_packages])
def test_package_search_no_filters_catalog(self): res = api.package_search({}, self.context, catalog=True) self.assertEqual(0, len(res)) api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) api.package_upload( self._stub_package( is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) # catalog=True should show public + mine res = api.package_search({}, self.context, catalog=True) self.assertEqual(3, len(res)) res = api.package_search({}, self.context_admin, catalog=True) self.assertEqual(3, len(res))
def test_package_search_no_filters(self): res = api.package_search({}, self.context) self.assertEqual(0, len(res)) api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) api.package_upload( self._stub_package( is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) # I can only edit mine pkgs res = api.package_search({}, self.context) self.assertEqual(2, len(res)) for pkg in res: self.assertEqual(self.tenant_id, pkg.owner_id) # Admin can see everything res = api.package_search({}, self.context_admin) self.assertEqual(4, len(res))
def test_package_search_no_filters_catalog(self): res = api.package_search({}, self.context, catalog=True) self.assertEqual(0, len(res)) api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) api.package_upload( self._stub_package(is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) # catalog=True should show public + mine res = api.package_search({}, self.context, catalog=True) self.assertEqual(3, len(res)) res = api.package_search({}, self.context_admin, catalog=True) self.assertEqual(3, len(res))
def test_package_search_no_filters(self): res = api.package_search({}, self.context) self.assertEqual(0, len(res)) api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) api.package_upload( self._stub_package(is_public=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) # I can only edit mine pkgs res = api.package_search({}, self.context) self.assertEqual(2, len(res)) for pkg in res: self.assertEqual(self.tenant_id, pkg.owner_id) # Admin can see everything res = api.package_search({}, self.context_admin) self.assertEqual(4, len(res))
def test_package_search_owned(self): api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) res = api.package_search({'owned': 'true'}, self.context_admin) self.assertEqual(len(res), 1) res = api.package_search({'owned': 'false'}, self.context_admin) self.assertEqual(len(res), 2)
def test_package_search_type(self): api.package_upload( self._stub_package(type="Application", fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(type="Library", fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({'type': 'Library'}, self.context) self.assertEqual(1, len(res)) res = api.package_search({'type': 'Application'}, self.context) self.assertEqual(1, len(res))
def test_package_search_owned(self): api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id_2) res = api.package_search({'owned': 'true'}, self.context_admin) self.assertEqual(1, len(res)) res = api.package_search({'owned': 'false'}, self.context_admin) self.assertEqual(2, len(res))
def test_package_search_search(self): pkg1 = api.package_upload(self._stub_package(fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkg2 = api.package_upload(self._stub_package(tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({"search": "tag1"}, self.context) self.assertEqual(len(res), 1) res = api.package_search({"search": pkg1.fully_qualified_name}, self.context) self.assertEqual(len(res), 1) res = api.package_search({"search": pkg2.fully_qualified_name}, self.context) self.assertEqual(len(res), 1) res = api.package_search({"search": "not_a_valid_uuid"}, self.context) self.assertEqual(len(res), 0) res = api.package_search({"search": "some text"}, self.context) self.assertEqual(len(res), 2)
def test_package_search_tags(self): api.package_upload( self._stub_package(fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({'tag': ['tag1']}, self.context) self.assertEqual(1, len(res)) res = api.package_search({'tag': ['tag2']}, self.context) self.assertEqual(1, len(res)) res = api.package_search({'tag': ['tag3']}, self.context) self.assertEqual(0, len(res))
def test_pagination_backwards(self): """Creates 10 packages with unique names and iterates backwards, checking that package order is correct. """ pkgs = [] for dummy in range(10): package = api.package_upload( self._stub_package(name=str(uuid.uuid4()), fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) pkgs.append(package) # sort pkg ids by pkg name pkg_ids = [pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.name)] res = api.package_search({}, self.context, limit=10) self.assertEqual(10, len(res)) self.assertEqual(pkg_ids, [r.id for r in res]) marker = res[-1].id res = api.package_search({"marker": marker, "sort_dir": "desc"}, self.context, limit=5) self.assertEqual(5, len(res)) self.assertEqual(list(reversed(pkg_ids[4:9])), [r.id for r in res]) marker = res[-1].id res = api.package_search({"marker": marker, "sort_dir": "desc"}, self.context, limit=5) self.assertEqual(4, len(res)) self.assertEqual(list(reversed(pkg_ids[0:4])), [r.id for r in res]) marker = res[-1].id res = api.package_search({"marker": marker, "sort_dir": "desc"}, self.context, limit=5) self.assertEqual(0, len(res))
def test_package_delete(self): values = self._stub_package() package = api.package_upload(values, self.tenant_id) api.package_delete(package.id, self.context) self.assertRaises(exc.HTTPNotFound, api.package_get, package.id, self.context)
def upload(self, req, body=None): """Upload new file archive for the new package together with package metadata. """ policy.check("upload_package", req.context) _check_content_type(req, 'multipart/form-data') file_obj, package_meta = _validate_body(body) if package_meta: try: jsonschema.validate(package_meta, schemas.PKG_UPLOAD_SCHEMA) except jsonschema.ValidationError as e: msg = _("Package schema is not valid: {reason}").format( reason=e) LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) else: package_meta = {} if package_meta.get('is_public'): policy.check('publicize_package', req.context) with tempfile.NamedTemporaryFile(delete=False) as tempf: LOG.debug("Storing package archive in a temporary file") content = file_obj.file.read() if not content: msg = _("Uploading file can't be empty") LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) tempf.write(content) package_meta['archive'] = content try: with load_utils.load_from_file( tempf.name, target_dir=None, drop_dir=True) as pkg_to_upload: # extend dictionary for update db for k, v in six.iteritems(PKG_PARAMS_MAP): if hasattr(pkg_to_upload, k): package_meta[v] = getattr(pkg_to_upload, k) if len(package_meta['name']) > 80: msg = _('Package name should be 80 characters maximum') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) try: package = db_api.package_upload( package_meta, req.context.tenant) except db_exc.DBDuplicateEntry: msg = _('Package with specified full ' 'name is already registered') LOG.exception(msg) raise exc.HTTPConflict(msg) return package.to_dict() except pkg_exc.PackageLoadError as e: msg = _("Couldn't load package from file: {reason}").format( reason=e) LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) finally: LOG.debug("Deleting package archive temporary file") os.remove(tempf.name)
def upload(self, req, body=None): """Upload new file archive for the new package together with package metadata. """ policy.check("upload_package", req.context) _check_content_type(req, 'multipart/form-data') file_obj, package_meta = _validate_body(body) if package_meta: try: jsonschema.validate(package_meta, schemas.PKG_UPLOAD_SCHEMA) except jsonschema.ValidationError as e: msg = _("Package schema is not valid: {reason}").format( reason=e) LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) else: package_meta = {} if package_meta.get('is_public'): policy.check('publicize_package', req.context) with tempfile.NamedTemporaryFile(delete=False) as tempf: LOG.debug("Storing package archive in a temporary file") content = file_obj.file.read() if not content: msg = _("Uploading file can't be empty") LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) tempf.write(content) package_meta['archive'] = content try: with load_utils.load_from_file(tempf.name, target_dir=None, drop_dir=True) as pkg_to_upload: # extend dictionary for update db for k, v in six.iteritems(PKG_PARAMS_MAP): if hasattr(pkg_to_upload, k): package_meta[v] = getattr(pkg_to_upload, k) if len(package_meta['name']) > 80: msg = _('Package name should be 80 characters maximum') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) try: package = db_api.package_upload(package_meta, req.context.tenant) except db_exc.DBDuplicateEntry: msg = _('Package with specified full ' 'name is already registered') LOG.exception(msg) raise exc.HTTPConflict(msg) return package.to_dict() except pkg_exc.PackageLoadError as e: msg = _("Couldn't load package from file: {reason}").format( reason=e) LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) finally: LOG.debug("Deleting package archive temporary file") os.remove(tempf.name)
def test_package_search_disabled(self): api.package_upload( self._stub_package(is_public=True, enabled=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package(is_public=True, enabled=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({'include_disabled': 'false'}, self.context) self.assertEqual(1, len(res)) res = api.package_search({'include_disabled': 'true'}, self.context) self.assertEqual(2, len(res))
def test_pagination(self): """Tests that package order is correct Creates 10 packages with unique names and iterates through them, checking that package order is correct. """ pkgs = [] for dummy in range(10): package = api.package_upload( self._stub_package(name=str(uuid.uuid4()), fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkgs.append(package) # sort pkg ids by pkg name pkg_ids = [pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.name)] res = api.package_search({}, self.context, limit=4) self.assertEqual(4, len(res)) self.assertEqual(pkg_ids[0:4], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(4, len(res)) self.assertEqual(pkg_ids[4:8], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(2, len(res)) self.assertEqual(pkg_ids[8:10], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(0, len(res))
def test_order_by(self): pkgs = [] for dummy in range(10): package = api.package_upload( self._stub_package(name=str(uuid.uuid4()), fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkgs.append(package) pkg_created = [ pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.created) ] pkg_name = [pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.name)] pkg_fqn = [ pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.fully_qualified_name) ] for order, pkg_ids in zip(['created', 'name', 'fqn'], [pkg_created, pkg_name, pkg_fqn]): res = api.package_search({'order_by': [order]}, self.context, limit=10) self.assertEqual(10, len(res)) self.assertEqual(pkg_ids, [r.id for r in res])
def test_pagination(self): """Creates 10 packages with unique names and iterates through them, checking that package order is correct. """ pkgs = [] for dummy in range(10): package = api.package_upload(self._stub_package( name=str(uuid.uuid4()), fully_qualified_name=str(uuid.uuid4())), self.tenant_id) pkgs.append(package) # sort pkg ids by pkg name pkg_ids = [pkg.id for pkg in sorted(pkgs, key=lambda _pkg: _pkg.name)] res = api.package_search({}, self.context, limit=4) self.assertEqual(4, len(res)) self.assertEqual(pkg_ids[0:4], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(4, len(res)) self.assertEqual(pkg_ids[4:8], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(2, len(res)) self.assertEqual(pkg_ids[8:10], [r.id for r in res]) marker = res[-1].id res = api.package_search({'marker': marker}, self.context, limit=4) self.assertEqual(0, len(res))
def test_packages_filter_by_in_id(self): """Test that packages are filtered by in:id1,id2,id3 GET /catalog/packages with parameter "id=in:id1,id2" returns packages filtered by id. """ self._set_policy_rules({ 'get_package': '', 'manage_public_package': '' }) _, package1_data = self._test_package() _, package2_data = self._test_package() _, package3_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1', ) package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2', ) package3_data['fully_qualified_name'] += '_3' package3_data['name'] += '_3' package3_data['class_definitions'] = (u'test.mpl.v1.app.Thing3', ) expected_packages = [ db_catalog_api.package_upload(package1_data, ''), db_catalog_api.package_upload(package2_data, '') ] db_catalog_api.package_upload(package3_data, '') id_in = "in:" + ",".join(pack.id for pack in expected_packages) req = self._get('/catalog/packages', params={'id': id_in}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(2, len(res.json['packages'])) found_packages = res.json['packages'] self.assertEqual([pack.id for pack in expected_packages], [pack['id'] for pack in found_packages])
def test_package_search_tags(self): api.package_upload( self._stub_package( fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search( {'tag': ['tag1']}, self.context) self.assertEqual(len(res), 1) res = api.package_search( {'tag': ['tag2']}, self.context) self.assertEqual(len(res), 1) res = api.package_search( {'tag': ['tag3']}, self.context) self.assertEqual(len(res), 0)
def test_package_search_disabled(self): api.package_upload( self._stub_package( is_public=True, enabled=True, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) api.package_upload( self._stub_package( is_public=True, enabled=False, fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search( {'include_disabled': 'false'}, self.context) self.assertEqual(len(res), 1) res = api.package_search( {'include_disabled': 'true'}, self.context) self.assertEqual(len(res), 2)
def test_package_upload(self): self._create_categories() values = self._stub_package() package = api.package_upload(values, self.tenant_id) self.assertIsNotNone(package.id) for k in values.keys(): self.assertEqual(values[k], package[k])
def test_package_search_search_order(self): pkg1 = api.package_upload( self._stub_package(fully_qualified_name=str(uuid.uuid4()), name='mysql', description='awcloud'), self.tenant_id) pkg2 = api.package_upload( self._stub_package(fully_qualified_name=str(uuid.uuid4()), name='awcloud', description='mysql'), self.tenant_id) api.package_upload( self._stub_package(tags=[], fully_qualified_name=str(uuid.uuid4())), self.tenant_id) res = api.package_search({'search': 'mysql'}, self.context) self.assertEqual(2, len(res)) self.assertEqual(pkg1.name, res[0].name) self.assertEqual(pkg2.description, res[1].description)
def test_packages_filter_by_in_id(self): """Test that packages are filtered by in:id1,id2,id3 GET /catalog/packages with parameter "id=in:id1,id2" returns packages filtered by id. """ self._set_policy_rules( {'get_package': '', 'manage_public_package': ''} ) _, package1_data = self._test_package() _, package2_data = self._test_package() _, package3_data = self._test_package() package1_data['fully_qualified_name'] += '_1' package1_data['name'] += '_1' package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1',) package2_data['fully_qualified_name'] += '_2' package2_data['name'] += '_2' package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2',) package3_data['fully_qualified_name'] += '_3' package3_data['name'] += '_3' package3_data['class_definitions'] = (u'test.mpl.v1.app.Thing3',) expected_packages = [db_catalog_api.package_upload(package1_data, ''), db_catalog_api.package_upload(package2_data, '')] db_catalog_api.package_upload(package3_data, '') id_in = "in:" + ",".join(pack.id for pack in expected_packages) req = self._get('/catalog/packages', params={'id': id_in}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(2, len(res.json['packages'])) found_packages = res.json['packages'] self.assertEqual([pack.id for pack in expected_packages], [pack['id'] for pack in found_packages])
def _add_pkg(self, tenant_name, public=False, classes=None, **kwargs): package_to_upload = self.test_package.copy() package_to_upload["is_public"] = public package_to_upload["fully_qualified_name"] = str(uuid.uuid4()) if classes: package_to_upload["class_definitions"] = classes else: package_to_upload["class_definitions"] = [] package_to_upload.update(kwargs) return db_catalog_api.package_upload(package_to_upload, tenant_name)
def _add_pkg(self, tenant_name, public=False, classes=None, **kwargs): package_to_upload = self.test_package.copy() package_to_upload['is_public'] = public package_to_upload['fully_qualified_name'] = str(uuid.uuid4()) if classes: package_to_upload['class_definitions'] = classes else: package_to_upload['class_definitions'] = [] package_to_upload.update(kwargs) return db_catalog_api.package_upload(package_to_upload, tenant_name)
def test_get_logo_negative(self): _, package = self._test_package() saved_package = db_catalog_api.package_upload(package, "") req = self._get_with_accept("/catalog/packages/%s/logo" % saved_package.id, accept="application/foo") result = req.get_response(self.api) self.assertEqual(415, result.status_code) self.assertTrue("Unsupported Content-Type" in result.body)
def test_order_by_compound(self): pkgs_a, pkgs_z = [], [] for _ in range(5): package = api.package_upload( self._stub_package(name="z", fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) pkgs_z.append(package) for _ in range(5): package = api.package_upload( self._stub_package(name="a", fully_qualified_name=str(uuid.uuid4())), self.tenant_id ) pkgs_a.append(package) # sort pkg ids by pkg created pkg_a_id = [pkg.id for pkg in sorted(pkgs_a, key=lambda _pkg: _pkg.created)] pkg_z_id = [pkg.id for pkg in sorted(pkgs_z, key=lambda _pkg: _pkg.created)] res = api.package_search({"order_by": ["name", "created"]}, self.context, limit=10) self.assertEqual(10, len(res)) self.assertEqual(pkg_a_id + pkg_z_id, [r.id for r in res])
def upload(self, req, body=None): """Upload new file archive for the new package together with package metadata. """ policy.check("upload_package", req.context) _check_content_type(req, 'multipart/form-data') file_obj, package_meta = _validate_body(body) if package_meta: try: jsonschema.validate(package_meta, schemas.PKG_UPLOAD_SCHEMA) except jsonschema.ValidationError as e: LOG.exception(e) raise exc.HTTPBadRequest(explanation=e.message) else: package_meta = {} with tempfile.NamedTemporaryFile(delete=False) as tempf: LOG.debug("Storing package archive in a temporary file") content = file_obj.file.read() if not content: msg = _("Uploading file can't be empty") LOG.error(msg) raise exc.HTTPBadRequest(msg) tempf.write(content) package_meta['archive'] = content try: pkg_to_upload = load_utils.load_from_file(tempf.name, target_dir=None, drop_dir=True) except pkg_exc.PackageLoadError as e: LOG.exception(e) raise exc.HTTPBadRequest(e) finally: LOG.debug("Deleting package archive temporary file") os.remove(tempf.name) # extend dictionary for update db for k, v in PKG_PARAMS_MAP.iteritems(): if hasattr(pkg_to_upload, k): package_meta[v] = getattr(pkg_to_upload, k) if req.params.get('is_public', '').lower() == 'true': policy.check('publicize_image', req.context) package_meta['is_public'] = True try: package = db_api.package_upload(package_meta, req.context.tenant) except db_exc.DBDuplicateEntry: msg = _('Package with specified full name is already registered') LOG.exception(msg) raise exc.HTTPServerError(msg) return package.to_dict()
def test_authorize_package_delete_error(self): values = self._stub_package() package = api.package_upload(values, self.tenant_id) self.assertRaises(exc.HTTPForbidden, api._authorize_package, package, self.context_2) self.assertRaises(exc.HTTPForbidden, api.package_delete, package.id, self.context_2) id = package.id patch = self.get_change('replace', ['is_public'], False) api.package_update(id, [patch], self.context) self.assertRaises(exc.HTTPForbidden, api._authorize_package, package, self.context_2, allow_public=True)
def test_get_logo_negative(self): _, package = self._test_package() saved_package = db_catalog_api.package_upload(package, '') req = self._get_with_accept('/catalog/packages/%s/logo' % saved_package.id, accept='application/foo') result = req.get_response(self.api) self.assertEqual(415, result.status_code) self.assertTrue('Unsupported Content-Type' in result.body)
def test_packages_filter_by_in_id_empty(self): """Test that packages are filtered by "id=in:" GET /catalog/packages with parameter "id=in:" returns packages filtered by id, in this case no packages should be returned. """ self._set_policy_rules({ 'get_package': '', 'manage_public_package': '' }) _, package1_data = self._test_package() db_catalog_api.package_upload(package1_data, '') req = self._get('/catalog/packages', params={'id': "in:"}) self.expect_policy_check('get_package') self.expect_policy_check('manage_public_package') res = req.get_response(self.api) self.assertEqual(200, res.status_code) self.assertEqual(0, len(res.json['packages']))
def test_get_logo_negative(self): _, package = self._test_package() saved_package = db_catalog_api.package_upload(package, '') req = self._get_with_accept('/catalog/packages/%s/logo' % saved_package.id, accept='application/foo') result = req.get_response(self.api) self.assertEqual(406, result.status_code) self.assertTrue( 'Acceptable response can not be provided' in result.body)