Skip to content

Commit

Permalink
freshdesk user handling improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Apr 12, 2022
1 parent ed85992 commit 63ac03d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions serviceHelpers/freshdesk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import base64
import json
import logging
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_freshdesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "[email protected]"]
for email in emails:
agent = fresh._get_agent_by_email(email)
2 changes: 1 addition & 1 deletion tests/test_zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 63ac03d

Please sign in to comment.