def test_patch_role_description( self, http_client, base_url, mongo_role, operation, value, expected_value, succeed, ): mongo_role.save() body = PatchOperation(operation=operation, path="/description", value=value) request = HTTPRequest( base_url + "/api/v1/roles/" + str(mongo_role.id), method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) if succeed: assert response.code == 200 updated = SchemaParser.parse_role(response.body.decode("utf-8"), from_string=True) assert updated.description == expected_value else: assert response.code >= 400
def update_instance(self, instance_id, **kwargs): """Update an Instance status Args: instance_id (str): The Instance ID Keyword Args: new_status (str): The new status metadata (dict): Will be added to existing instance metadata Returns: Instance: The updated Instance """ operations = [] new_status = kwargs.pop("new_status", None) metadata = kwargs.pop("metadata", {}) if new_status: operations.append(PatchOperation("replace", "/status", new_status)) if metadata: operations.append(PatchOperation("update", "/metadata", metadata)) return self.client.patch_instance( instance_id, SchemaParser.serialize_patch(operations, many=True))
def test_patch_add_role(self, http_client, base_url, mongo_principal, mongo_role): mongo_role.save() mongo_principal.save() new_role = Role(name="new_role", description="Some desc", roles=[], permissions=["bg-all"]) new_role.save() body = PatchOperation(operation="add", path="/roles", value="new_role") url = base_url + "/api/v1/users/" + str(mongo_principal.id) request = HTTPRequest( url, method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) assert response.code == 200 updated = SchemaParser.parse_principal(response.body.decode("utf-8"), from_string=True) assert len(updated.roles) == 2
def update_request(self, request_id, status=None, output=None, error_class=None): """Update a Request Args: request_id (str): The Request ID status (Optional[str]): New Request status output (Optional[str]): New Request output error_class (Optional[str]): New Request error class Returns: Response: The updated response """ operations = [] if status: operations.append(PatchOperation("replace", "/status", status)) if output: operations.append(PatchOperation("replace", "/output", output)) if error_class: operations.append( PatchOperation("replace", "/error_class", error_class)) return self.client.patch_request( request_id, SchemaParser.serialize_patch(operations, many=True))
def rescan(self): """Rescan local plugin directory Returns: bool: True if rescan was successful """ return self.client.patch_admin(payload=SchemaParser.serialize_patch( PatchOperation(operation="rescan")))
def instance_heartbeat(self, instance_id): """Send an Instance heartbeat Args: instance_id (str): The Instance ID Returns: bool: True if the heartbeat was successful """ return self.client.patch_instance( instance_id, SchemaParser.serialize_patch(PatchOperation("heartbeat")))
def test_patch_commands( self, http_client, base_url, mongo_system, system_id, bg_command, field, value, dev, succeed, ): if dev: mongo_system.version += ".dev" mongo_system.deep_save() # Make changes to the new command if field: if field == "parameters": value = [value] setattr(bg_command, field, value) # Also delete the id, otherwise mongo gets really confused delattr(bg_command, "id") body = PatchOperation( operation="replace", path="/commands", value=SchemaParser.serialize_command( [bg_command], to_string=False, many=True ), ) request = HTTPRequest( base_url + "/api/v1/systems/" + system_id, method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) if succeed: assert response.code == 200 updated = SchemaParser.parse_system( response.body.decode("utf-8"), from_string=True ) assert_command_equal(bg_command, updated.commands[0]) else: assert response.code == 400
def initialize_instance(self, instance_id, runner_id=None): """Start an Instance Args: instance_id (str): The Instance ID runner_id (str): The PluginRunner ID, if any Returns: Instance: The updated Instance """ return self.client.patch_instance( instance_id, SchemaParser.serialize_patch( PatchOperation(operation="initialize", value={"runner_id": runner_id})), )
def update_system(self, system_id, new_commands=None, **kwargs): """Update a System Args: system_id (str): The System ID new_commands (Optional[List[Command]]): New System commands Keyword Args: add_instance (Instance): An Instance to append metadata (dict): New System metadata description (str): New System description display_name (str): New System display name icon_name (str): New System icon name template (str): New System template Returns: System: The updated system """ operations = [] if new_commands is not None: commands = SchemaParser.serialize_command(new_commands, to_string=False, many=True) operations.append(PatchOperation("replace", "/commands", commands)) add_instance = kwargs.pop("add_instance", None) if add_instance: instance = SchemaParser.serialize_instance(add_instance, to_string=False) operations.append(PatchOperation("add", "/instance", instance)) metadata = kwargs.pop("metadata", {}) if metadata: operations.append(PatchOperation("update", "/metadata", metadata)) # The remaining kwargs are all strings # Sending an empty string (instead of None) ensures they're actually cleared for key, value in kwargs.items(): operations.append( PatchOperation("replace", "/%s" % key, value or "")) return self.client.patch_system( system_id, SchemaParser.serialize_patch(operations, many=True))
def test_patch_password_update(self, http_client, base_url, mongo_principal, mongo_role): mongo_role.save() mongo_principal.save() body = PatchOperation(operation="update", path="/password", value="new_password") url = base_url + "/api/v1/users/" + str(mongo_principal.id) request = HTTPRequest( url, method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) assert response.code == 200
def test_patch_password_update_meta(self, http_client, base_url, mongo_principal, mongo_role): mongo_role.save() mongo_principal.metadata = {"auto_change": True, "changed": False} mongo_principal.save() body = PatchOperation(operation="update", path="/password", value="new_password") url = base_url + "/api/v1/users/" + str(mongo_principal.id) request = HTTPRequest( url, method="PATCH", headers={"content-type": "application/json"}, body=SchemaParser.serialize_patch(body), ) response = yield http_client.fetch(request, raise_error=False) assert response.code == 200 updated = SchemaParser.parse_principal(response.body.decode("utf-8"), from_string=True) assert updated.metadata.get("changed") is True
def test_patch_many(self, patch_dict_no_envelop, patch_dict_no_envelop2, bg_patch, bg_patch2): actual = SchemaParser.serialize_patch([bg_patch, bg_patch2], to_string=False, many=True) assert actual == [patch_dict_no_envelop, patch_dict_no_envelop2]
def test_patch(self, patch_dict_no_envelop, bg_patch, kwargs): actual = SchemaParser.serialize_patch(bg_patch, to_string=False, **kwargs) assert actual == patch_dict_no_envelop
def _patch_job(self, job_id, operations): return self.client.patch_job( job_id, SchemaParser.serialize_patch(operations, many=True))