From 63ac03dfec38e076d593e34692783177206c1b42 Mon Sep 17 00:00:00 2001 From: C'tri Date: Tue, 12 Apr 2022 12:49:21 +0100 Subject: [PATCH] freshdesk user handling improved --- serviceHelpers/freshdesk.py | 13 ++++++++++--- tests/test_freshdesk.py | 8 ++++++++ tests/test_zendesk.py | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/serviceHelpers/freshdesk.py b/serviceHelpers/freshdesk.py index 8c5ca0e..02bd29a 100644 --- a/serviceHelpers/freshdesk.py +++ b/serviceHelpers/freshdesk.py @@ -1,3 +1,4 @@ +import re import base64 import json import logging @@ -119,16 +120,22 @@ def _get_agent_by_id(self, agent_id): return agent_o def _get_agent_by_email(self, email): - """interanl method""" + """internal method""" + if email is None or not isinstance(email, str): - _LO.error("invalid parameters passed to _get_agent_by_email") + _LO.error("invalid parameters type passed to _get_agent_by_email") + return FreshdeskAgent() + if re.match(r".*@.*..*", email) is None: + _LO.error("invalid email format passed to _get_agent_by_email") return FreshdeskAgent() + email_f = quote_plus(email) url = f"https://{self.host}/api/v2/agents?email={email_f}" agent_j = self._request_and_validate(url) agent_o = FreshdeskAgent() - agent_o.from_dict(agent_j[0]) + if len(agent_j) > 0: + agent_o.from_dict(agent_j[0]) return agent_o def _request_and_validate(self, url, headers=None, body=None) -> dict: diff --git a/tests/test_freshdesk.py b/tests/test_freshdesk.py index ff0c43c..0e343a6 100644 --- a/tests/test_freshdesk.py +++ b/tests/test_freshdesk.py @@ -88,3 +88,11 @@ def test_user_email(caplog): for record in caplog.records: assert record.levelno < logging.WARNING + + +def test_user_invalid_email(caplog): + "fetch a user by email, with invalid items. Will" + fresh = FreshDesk(FRESHDESK_HOST, FRESHDESK_KEY) + emails = ["", 5, "not-real@test.com"] + for email in emails: + agent = fresh._get_agent_by_email(email) diff --git a/tests/test_zendesk.py b/tests/test_zendesk.py index 2f7d4b7..eb3c792 100644 --- a/tests/test_zendesk.py +++ b/tests/test_zendesk.py @@ -96,7 +96,7 @@ def test_search_user(caplog): assert isinstance(users[user_key], ZendeskUser) -def test_user_init(caplog): +def test_user_init_and_invalid_handling(caplog): """verifies the error handling of a ZendeskUser object's initialisation""" test_strs = [