Skip to content
Snippets Groups Projects
Commit 71d2f9a9 authored by Robert Habermann's avatar Robert Habermann
Browse files

change logger and fix exception unittest

parent e9bcae7d
No related branches found
Tags 0.1.22
No related merge requests found
......@@ -49,7 +49,6 @@ class PyOTRS(object):
session_id_file="/tmp/.session_id.tmp",
https_verify=True):
self.logger = logger
self.baseurl = baseurl
self.webservicename = webservicename
self.data = None
......@@ -82,21 +81,21 @@ class PyOTRS(object):
if self._read_session_id_from_file():
# got one.. check whether it's still valid
if self._check_is_valid_session_id():
self.logger.debug("Using valid Session ID from file (" + self.session_id_file + ")")
logger.debug("Using valid Session ID from file (" + self.session_id_file + ")")
return True
# got no (valid) Session ID from file.. try to create new one
if self.create_session():
# safe new Session ID to file
if self._write_session_id_to_file():
self.logger.debug("Saved new Session ID to file: " + self.session_id_file)
logger.debug("Saved new Session ID to file: " + self.session_id_file)
return True
else:
self.logger.error("Failed to save Session ID to file: " + self.session_id_file)
logger.error("Failed to save Session ID to file: " + self.session_id_file)
#pymisc.send_message_via_mail(subject="OTRS PyOTRS - Error - Failed to save Session ID!")
sys.exit(1)
else:
self.logger.error("Failed to create a Session ID!")
logger.error("Failed to create a Session ID!")
#pymisc.send_message_via_mail(subject="OTRS PyOTRS - Error - Failed to create a Session ID!")
sys.exit(1)
......@@ -174,8 +173,8 @@ class PyOTRS(object):
self.data = requests.get(url, proxies=proxies, params=payload, verify=self.https_verify)
# Just assume that we get valid JSON
self.data_json = json.loads(self.data.content)
self.logger.debug(self.data_json)
self.data_json = json.loads(self.data.content.decode())
logger.debug(self.data_json)
if self.data_json.get('Ticket') is not None:
self.data_type = 'Ticket'
......@@ -184,17 +183,17 @@ class PyOTRS(object):
elif self.data_json.get('Session') is not None:
self.data_type = 'Session'
else:
self.logger.error('Unkown key in response JSON DICT!')
logger.error('Unkown key in response JSON DICT!')
# critical error: Unknown response from OTRS API - FAIL NOW!
sys.exit(1)
self.logger.debug('Type was set to: ' + self.data_type)
logger.debug('Type was set to: ' + self.data_type)
get_request_result_bool = True
# critical error: HTTP request resulted in an error!
except Exception as e:
self.logger.error('Error with http communication: %s', e)
self.logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
logger.error('Error with http communication: %s', e)
logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
get_request_result_bool = False
raise Exception('get http')
......@@ -202,8 +201,8 @@ class PyOTRS(object):
# critical error: OTRS API sent an error!
if self.data_type == "Error":
self.logger.error(self.data_json['Error'])
self.logger.error("Failed to access OTRS API. Check Username and Password! Session ID expired?! Does requested Ticket exist?")
logger.error(self.data_json['Error'])
logger.error("Failed to access OTRS API. Check Username and Password! Session ID expired?! Does requested Ticket exist?")
get_request_result_bool = False
raise Exception('get api')
......@@ -257,36 +256,36 @@ class PyOTRS(object):
try:
self.data = requests.patch(url, proxies=proxies, data=json.dumps(payload), verify=self.https_verify)
self.logger.debug("Received HTTP Status Code: " + str(self.data.status_code))
self.logger.debug(self.data.content)
logger.debug("Received HTTP Status Code: " + str(self.data.status_code))
logger.debug(self.data.content)
# Just assume that we get valid JSON
self.data_json = json.loads(self.data.content)
#self.logger.debug(self.data_json)
self.data_json = json.loads(self.data.content.decode())
#logger.debug(self.data_json)
if self.data_json.get('Error') is not None:
self.data_type = 'Error'
elif self.data_json.get('TicketID') is not None:
self.data_type = 'TicketID'
else:
self.logger.error('Unkown key in response JSON DICT!')
logger.error('Unkown key in response JSON DICT!')
patch_request_result_bool = False
self.logger.debug('Type was set to: ' + self.data_type)
logger.debug('Type was set to: ' + self.data_type)
patch_request_result_bool = True
# critical error: HTTP request resulted in an error!
except Exception as e:
self.logger.error('Error with http communication: %s', e)
self.logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
logger.error('Error with http communication: %s', e)
logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
patch_request_result_bool = False
raise Exception('update http')
# critical error: OTRS API sent an error!
if self.data_type == "Error":
self.logger.error(self.data_json['Error'])
self.logger.error("Failed to access OTRS API. Check Username and Password! Does requested Ticket exist?")
logger.error(self.data_json['Error'])
logger.error("Failed to access OTRS API. Check Username and Password! Does requested Ticket exist?")
patch_request_result_bool = False
raise Exception('update api')
......@@ -306,8 +305,8 @@ class PyOTRS(object):
'Ticket': {'Title': title}
}
self.logger.debug("Updating " + str(ticket_id) + " with: ")
self.logger.debug(json.dumps(payload))
logger.debug("Updating " + str(ticket_id) + " with: ")
logger.debug(json.dumps(payload))
try:
return self._update_by_ticket_id(url, payload)
......@@ -328,8 +327,8 @@ class PyOTRS(object):
'DynamicField': {'Name': 'ScoutIM', 'Value': scout_id}
}
self.logger.debug("Updating " + str(ticket_id) + " with: ")
self.logger.debug(json.dumps(payload))
logger.debug("Updating " + str(ticket_id) + " with: ")
logger.debug(json.dumps(payload))
try:
return self._update_by_ticket_id(url, payload)
......@@ -349,8 +348,8 @@ class PyOTRS(object):
'Ticket': {'State': new_state}
}
self.logger.debug("Updating " + str(ticket_id) + " with: ")
self.logger.debug(json.dumps(payload))
logger.debug("Updating " + str(ticket_id) + " with: ")
logger.debug(json.dumps(payload))
try:
return self._update_by_ticket_id(url, payload)
......@@ -381,8 +380,8 @@ class PyOTRS(object):
'Ticket': {'State': new_state, 'PendingTime': pending_time_str}
}
self.logger.debug("Updating " + str(ticket_id) + " with: ")
self.logger.debug(json.dumps(payload))
logger.debug("Updating " + str(ticket_id) + " with: ")
logger.debug(json.dumps(payload))
try:
return self._update_by_ticket_id(url, payload)
......@@ -408,8 +407,8 @@ class PyOTRS(object):
}
}
self.logger.debug("Updating " + str(ticket_id) + " with: ")
self.logger.debug(json.dumps(payload))
logger.debug("Updating " + str(ticket_id) + " with: ")
logger.debug(json.dumps(payload))
try:
return self._update_by_ticket_id(url, payload)
......@@ -452,28 +451,28 @@ class PyOTRS(object):
self.data = requests.get(url, proxies=proxies, data=json_payload, headers=headers, verify=self.https_verify)
# Just assume that we get valid JSON
self.data_json = json.loads(self.data.content)
self.logger.debug(self.data_json)
self.data_json = json.loads(self.data.content.decode())
logger.debug(self.data_json)
if self.data_json.get('TicketID') is not None:
self.data_type = 'TicketID'
get_request_result_bool = True
else:
self.logger.debug('search found no results')
logger.debug('search found no results')
get_request_result_bool = False
# critical error: HTTP request resulted in an error!
except Exception as e:
self.logger.error('Error with http communication: %s', e)
self.logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
logger.error('Error with http communication: %s', e)
logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate and so on!")
get_request_result_bool = False
raise Exception('get http')
# critical error: OTRS API sent an error!
if self.data_type == "Error":
self.logger.error(self.data_json['Error'])
self.logger.error("Failed to access OTRS API. Check Username and Password! Session ID expired?! Does requested Ticket exist?")
logger.error(self.data_json['Error'])
logger.error("Failed to access OTRS API. Check Username and Password! Session ID expired?! Does requested Ticket exist?")
get_request_result_bool = False
raise Exception('get api')
......
......@@ -36,8 +36,8 @@ class PyOTRSTests(unittest.TestCase):
### setUp for Test Class
### define logger (either Debug or Silent)
#self.logger = PyOTRSTests._setUpLoggerDebug(self)
self.logger = PyOTRSTests._setUpLoggerSilent(self)
self.logger = PyOTRSTests._setUpLoggerDebug(self)
#self.logger = PyOTRSTests._setUpLoggerSilent(self)
### base_url and webservice name (this should not change)
self.base_url = "https://fqdn"
......@@ -96,10 +96,8 @@ class PyOTRSTests(unittest.TestCase):
obj = PyOTRS(self.base_url, self.webservice_name, username, password)
with self.assertRaises(Exception) as context:
obj._get_json_ticket_data(url, payload)
self.assertRaisesRegexp(Exception, 'get api', obj._get_json_ticket_data, url=url, payload=payload)
self.assertTrue('get api' in context.exception)
### Test Responses: when calling: get_json_ticket_data_by_ticket_id - Test 200 OK; Body Error
@responses.activate
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment