def setUp(self): self._BadVerbError = BadVerbError('test') self._SerializationError = SerializationError('test') self._DeSerializationError = DeSerializationError('test') self._ConfigError = ConfigError('test') self._NotSupportedError = NotSupportedError('test') self._InvalidTypeError = InvalidTypeError('test')
def _request_file(self,endpoint, filename,form_data=None, download=True,body_param="file"): """ :param endpoint: The api endpoint we want to call. :param filename: Path to the file to upload/download :param form_data: form data to submit. E.g password=password_123 :param download: If we should download default. Set to true to upload. In forum both export and import are http POST :raises requests.exceptions.HTTPError: When response code is not successful. :raises IOError: When file not found or unreadable :returns: True/False """ resp = None auth = HTTPBasicAuth(self.config.username, self.config.password) request_url = "{0}/{1}".format(self.config.forumsentry_url, endpoint) if download: #We are getting a file from a remote url if form_data is not None: if type(form_data) == type(dict()): resp = requests.post(request_url, auth=auth, verify=self.config.verify_ssl, data=form_data) else: self._logger.error("Expected a dictionary of form params to post") raise InvalidTypeError(form_data) else: resp = requests.post(request_url, auth=auth, verify=self.config.verify_ssl) resp.raise_for_status() with open(filename, 'wb') as f: f.write(resp.content) return True else: if not os.path.isfile(filename): raise IOError("{0} not found".format(filename)) files = { body_param : (open(filename, 'rb'))} if form_data is not None: if type(form_data) == type(dict()): resp = requests.post(request_url, auth=auth, verify=self.config.verify_ssl, files=files, data=form_data) else: self._logger.error("Expected a dictionary of form params to post") raise InvalidTypeError(form_data) else: resp = requests.post(request_url, auth=auth, verify=self.config.verify_ssl, files=files) resp.raise_for_status() self._logger.debug(resp.text) return True
def set(self,name, obj): ''' Creates/Updates a SslInitiationPolicy the forum sentry. :param name: The name of the SslInitiationPolicy we want to create/update.. :param obj: The SslInitiationPolicy to created/updated. :raises requests.exceptions.HTTPError: When response code is not successful. :returns: The SslInitiationPolicy object that was created/updated. ''' if not isinstance(obj, self.str2Class(self.policy_type)): raise InvalidTypeError(obj) #This doesnt follow the normal pattern. It doesnt take /name target_endpoint = "{0}".format(self.path) self._logger.debug("target_endpoint: {0}".format(target_endpoint)) serialized_json = self._serializer.serialize(obj) self._logger.debug("serialized_json: {0}".format(serialized_json)) try: # this method will be patched for unit test j = self._request("POST", target_endpoint, serialized_json) self._logger.debug(j) obj = self._serializer.deserialize(j, self.policy_type) return obj except HTTPError as e: wrapped_error = ForumHTTPError(e) self._logger.error(wrapped_error) raise wrapped_error
def set(self, name, obj): ''' Creates/Updates a TaskList on the forum sentry. Unfortunately task lists updated using this method do not contain any tasks. It is recommended to use deploy for task lists :param name: The name of the TaskList we want to create/update.. :param obj: The TaskList object to created/updated. :raises requests.exceptions.HTTPError: When response code is not successful. :returns: The TaskList object that was created/updated. ''' if not isinstance(obj, self.str2Class(self.policy_type)): raise InvalidTypeError(obj) target_endpoint = "{0}/{1}".format(self.path, name) self._logger.debug("target_endpoint: {0}".format(target_endpoint)) serialized_json = self._serializer.serialize(obj) self._logger.debug("serialized_json: {0}".format(serialized_json)) try: # this method will be patched for unit test j = self._request("PUT", target_endpoint, serialized_json) self._logger.debug(j) obj = self._serializer.deserialize(j, self.policy_type) return obj except HTTPError as e: wrapped_error = ForumHTTPError(e) self._logger.error(wrapped_error) raise wrapped_error
class TestErrors(unittest.TestCase): def setUp(self): self._BadVerbError = BadVerbError('test') self._SerializationError = SerializationError('test') self._DeSerializationError = DeSerializationError('test') self._ConfigError = ConfigError('test') self._NotSupportedError = NotSupportedError('test') self._InvalidTypeError = InvalidTypeError('test') def test_NotSupportedError_implements_str(self): self.assertTrue(self._NotSupportedError.__str__ is not object.__str__) string = self._NotSupportedError.__str__() self.assertIn('invalid', string) def test_InvalidTypeError_implements_str(self): self.assertTrue(self._InvalidTypeError.__str__ is not object.__str__) string = self._InvalidTypeError.__str__() self.assertIn('invalid', string) def test_ConfigError_implements_str(self): self.assertTrue(self._ConfigError.__str__ is not object.__str__) string = self._ConfigError.__str__() self.assertIn('invalid', string) def test_BadVerbError_implements_str(self): self.assertTrue(self._BadVerbError.__str__ is not object.__str__) string = self._BadVerbError.__str__() self.assertIn('invalid', string) def test_SerializationError_implements_str(self): self.assertTrue(self._SerializationError.__str__ is not object.__str__) string = self._SerializationError.__str__() self.assertIn('invalid', string) def test_DeSerializationError_implements_str(self): self.assertTrue( self._DeSerializationError.__str__ is not object.__str__) string = self._DeSerializationError.__str__() self.assertIn('invalid', string)
def set(self, name, obj): ''' Creates/Updates a TaskListGroup on the forum sentry. :param name: The name of the TaskListGroup we want to create/update.. :param obj: The TaskListGroup object to created/updated. :raises requests.exceptions.HTTPError: When response code is not successful. :returns: The TaskListGroup object that was created/updated. ''' if not isinstance(obj, self.str2Class(self.policy_type)): raise InvalidTypeError(obj) if obj.task_lists is not None: if not obj.task_lists: self._logger.warn( "task_lists cannot be set to an empty string. It must be a valid comma separated list of task lists that exist in the forum" ) obj.task_lists = None target_endpoint = "{0}/{1}".format(self.path, name) self._logger.debug("target_endpoint: {0}".format(target_endpoint)) serialized_json = self._serializer.serialize(obj) self._logger.debug("serialized_json: {0}".format(serialized_json)) try: # this method will be patched for unit test j = self._request("PUT", target_endpoint, serialized_json) self._logger.debug(j) obj = self._serializer.deserialize(j, self.policy_type) #Forum throws a 400 error if you set the taskLists to '' but forum return taskLists: '' if taskList is None :-( if not obj.task_lists: obj.task_lists = None return obj except HTTPError as e: wrapped_error = ForumHTTPError(e) self._logger.error(wrapped_error) raise wrapped_error
def set_virtual_directory(self, name, virtual_directory, obj): ''' Creates/Updates a virtual directory attached to a JsonPolicies on the forum sentry. :param name: The name of the JsonPolicies we want to create/update.. :param virtual_directory: The name of the VirtualDirectory we want to create/update. :param obj: The VirtualDirectory object to created/updated. :raises requests.exceptions.HTTPError: When response code is not successful. :returns: The VirtualDirectory object that was created/updated. ''' if not isinstance(obj, self.str2Class(self.virtual_directory_type)): raise InvalidTypeError(obj) target_endpoint = "{0}/{1}/{2}/{3}".format(self.path, name, self.virtual_directory_path, virtual_directory) self._logger.debug("target_endpoint: {0}".format(target_endpoint)) serialized_json = self._serializer.serialize(obj) self._logger.debug("serialized_json: {0}".format(serialized_json)) try: # this method will be patched for unit test j = self._request("PUT", target_endpoint, serialized_json) self._logger.debug(j) obj = self._serializer.deserialize(j, self.virtual_directory_type) return obj except HTTPError as e: wrapped_error = ForumHTTPError(e) self._logger.error(wrapped_error) raise wrapped_error