def test_delete_type(self, rest_api, example_type, full_access_user, none_access_user): # Test default route rest_api.post(f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type)) default_response = rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}') assert default_response.status_code == HTTPStatus.ACCEPTED default_response = rest_api.post(f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type)) assert default_response.status_code == HTTPStatus.CREATED # ACCESS OK access_update_types_response = rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}', user=full_access_user) assert access_update_types_response.status_code != (HTTPStatus.FORBIDDEN or HTTPStatus.UNAUTHORIZED) validate_response = rest_api.get(f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.NOT_FOUND # ACCESS FORBIDDEN none_update_types_response = rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}', user=none_access_user) assert none_update_types_response.status_code == HTTPStatus.FORBIDDEN # ACCESS UNAUTHORIZED un_get_types_response = rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}', unauthorized=True) assert un_get_types_response.status_code == HTTPStatus.UNAUTHORIZED
def test_update_type(self, rest_api, example_type, full_access_user, none_access_user): example_type.name = 'updated' # Test default route default_response = rest_api.put(f'{self.ROUTE_URL}/{example_type.public_id}', json=TypeModel.to_json(example_type)) assert default_response.status_code == HTTPStatus.ACCEPTED validate_response = rest_api.get(f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.OK assert validate_response.get_json()['result']['name'] == 'updated' # ACCESS OK access_update_types_response = rest_api.put(f'{self.ROUTE_URL}/{example_type.public_id}', json=TypeModel.to_json(example_type), user=full_access_user) assert access_update_types_response.status_code != (HTTPStatus.FORBIDDEN or HTTPStatus.UNAUTHORIZED) validate_response = rest_api.get(f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.OK rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}') # ACCESS FORBIDDEN none_update_types_response = rest_api.put(f'{self.ROUTE_URL}/{example_type.public_id}', json=TypeModel.to_json(example_type), user=none_access_user) assert none_update_types_response.status_code == HTTPStatus.FORBIDDEN # ACCESS UNAUTHORIZED un_get_types_response = rest_api.put(f'{self.ROUTE_URL}/{example_type.public_id}', json=TypeModel.to_json(example_type), unauthorized=True) assert un_get_types_response.status_code == HTTPStatus.UNAUTHORIZED example_type.public_id = 1 example_type.name = 'test'
def setup(request, collection, example_type): collection.insert_one(document=TypeModel.to_json(example_type)) dummy_type = example_type dummy_type.public_id = 2 dummy_type.fields = [] dummy_type.fields.append({ "type": "ref", "name": "test-field", "label": "simple reference field", "ref_types": [1], "summaries": [{ "type_id": 1, "line": "ReferenceTO: {}", "label": "ReferenceTO", "fields": ["test-dummy-field"], "icon": "fa fa-cube", "prefix": False }], "value": "" }) collection.insert_one(document=TypeModel.to_json(dummy_type)) def drop_collection(): collection.drop() request.addfinalizer(drop_collection)
def test_insert_type(self, rest_api, example_type, full_access_user, none_access_user): example_type.public_id = 2 example_type.name = 'test2' # Test default route default_response = rest_api.post(f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type)) assert default_response.status_code == HTTPStatus.CREATED validate_response = rest_api.get( f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.OK double_check_response = rest_api.post( f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type)) assert double_check_response.status_code == HTTPStatus.BAD_REQUEST rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}') # ACCESS OK access_insert_types_response = rest_api.post( f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type), user=full_access_user) assert access_insert_types_response.status_code != ( HTTPStatus.FORBIDDEN or HTTPStatus.UNAUTHORIZED) validate_response = rest_api.get( f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.OK rest_api.delete(f'{self.ROUTE_URL}/{example_type.public_id}') # ACCESS FORBIDDEN forbidden_insert_types_response = rest_api.post( f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type), user=none_access_user) assert forbidden_insert_types_response.status_code == HTTPStatus.FORBIDDEN validate_response = rest_api.get( f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.NOT_FOUND # ACCESS UNAUTHORIZED un_insert_types_response = rest_api.post( f'{self.ROUTE_URL}/', json=TypeModel.to_json(example_type), unauthorized=True) assert un_insert_types_response.status_code == HTTPStatus.UNAUTHORIZED validate_response = rest_api.get( f'{self.ROUTE_URL}/{example_type.public_id}') assert validate_response.status_code == HTTPStatus.NOT_FOUND example_type.public_id = 1 example_type.name = 'test'
def export_type_by_ids(public_ids): try: query_list = [] for key, value in {'public_id': public_ids}.items(): for v in value.split(","): try: query_list.append({key: int(v)}) except (ValueError, TypeError): return abort(400) type_list_data = json.dumps([ TypeModel.to_json(type_) for type_ in object_manager.get_types_by(sort="public_id", **{'$or': query_list}) ], default=json_encoding.default, indent=2) except TypeNotFoundError as e: return abort(400, e.message) except ModuleNotFoundError as e: return abort(400, e) except CMDBError as e: return abort(404, jsonify(message='Not Found', error=e.message)) timestamp = datetime.datetime.fromtimestamp( time.time()).strftime('%Y_%m_%d-%H_%M_%S') return Response(type_list_data, mimetype="text/json", headers={ "Content-Disposition": "attachment; filename=%s.%s" % (timestamp, 'json') })
def setup(request, collection, example_type): collection.insert_one(document=TypeModel.to_json(example_type)) def drop_collection(): collection.drop() request.addfinalizer(drop_collection)
def insert(self, type: dict) -> PublicID: """ Insert a single type into the system. Args: type (dict): Raw data of the type. Notes: If no public id was given, the database manager will auto insert the next available ID. Returns: int: The Public ID of the new inserted type """ if isinstance(type, TypeModel): type = TypeModel.to_json(type) return self._insert(self.collection, resource=type)
def update(self, public_id: Union[PublicID, int], type: Union[TypeModel, dict]): """ Update a existing type in the system. Args: public_id (int): PublicID of the type in the system. type: Notes: If a TypeModel instance was passed as type argument, \ it will be auto converted via the model `to_json` method. """ if isinstance(type, TypeModel): type = TypeModel.to_json(type) return super(TypeManager, self).update(public_id=public_id, resource=type)
def insert(self, type: Union[TypeModel, dict]) -> PublicID: """ Insert a single type into the system. Args: type (dict): Raw data of the type. Notes: If no public id was given, the database manager will auto insert the next available ID. Returns: int: The Public ID of the new inserted type """ if isinstance(type, TypeModel): type = TypeModel.to_json(type) elif isinstance(type, dict): type = json.loads(json.dumps(type, default=json_util.default), object_hook=object_hook) return self._insert(self.collection, resource=type)
def update(self, public_id: Union[PublicID, int], type: Union[TypeModel, dict]): """ Update a existing type in the system. Args: public_id (int): PublicID of the type in the system. type: New type data Notes: If a TypeModel instance was passed as type argument, \ it will be auto converted via the model `to_json` method. """ if isinstance(type, TypeModel): type = TypeModel.to_json(type) update_result = self._update(self.collection, filter={'public_id': public_id}, resource=type) if update_result.matched_count != 1: raise ManagerUpdateError(f'Something happened during the update!') return update_result
def export_type(): try: type_list = [ TypeModel.to_json(type) for type in object_manager.get_all_types() ] resp = json.dumps(type_list, default=json_encoding.default, indent=2) except TypeNotFoundError as e: return abort(400, e.message) except ModuleNotFoundError as e: return abort(400, e) except CMDBError as e: return abort(404, jsonify(message='Not Found', error=e.message)) timestamp = datetime.datetime.fromtimestamp( time.time()).strftime('%Y_%m_%d-%H_%M_%S') return Response(resp, mimetype="text/json", headers={ "Content-Disposition": "attachment; filename=%s.%s" % (timestamp, 'json') })