Skip to content

Commit

Permalink
Task/add freshdesk other (#2220)
Browse files Browse the repository at this point in the history
* Add an api for to send other category to freshdesk

* fix

* fix

---------

Co-authored-by: William B <7444334+whabanks@users.noreply.github.com>
  • Loading branch information
jzbahrai and whabanks authored Jul 15, 2024
1 parent bab5f15 commit c237e66
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/clients/freshdesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@ def _generate_description(self):
f"- Texte alternatif français : {self.contact.alt_text_fr}",
]
)

elif self.contact.is_new_template_category_request():
message = "<br>".join(
[
f"New template category request from {self.contact.name} ({self.contact.email_address}):",
f"- Service id: {self.contact.service_id}",
f"- New Template Category Request name: {self.contact.template_category_name_en}",
f"- Template id request: {self.contact.template_id_link}",
"<hr>",
f"Demande de nouvelle catégorie de modèle de {self.contact.name} ({self.contact.email_address}):",
f"- Identifiant du service: {self.contact.service_id}",
f"- Nom de la nouvelle catégorie de modèle demandée: {self.contact.template_category_name_fr}",
f"- Demande d'identifiant de modèle: {self.contact.template_id_link}",
]
)
if len(self.contact.user_profile):
message += f"<br><br>---<br><br> {self.contact.user_profile}"

Expand Down
6 changes: 6 additions & 0 deletions app/user/contact_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ContactRequest:
branding_logo_name: str = field(default="")
alt_text_en: str = field(default="")
alt_text_fr: str = field(default="")
template_category_name_en: str = field(default="")
template_category_name_fr: str = field(default="")
template_id_link: str = field(default="")

def __post_init__(self):
# email address is mandatory for us
Expand All @@ -56,3 +59,6 @@ def is_go_live_request(self):

def is_branding_request(self):
return "branding_request" in self.support_type.lower()

def is_new_template_category_request(self):
return "new_template_category_request" in self.support_type.lower()
30 changes: 30 additions & 0 deletions app/user/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,36 @@ def send_branding_request(user_id):
return jsonify({"status_code": status_code}), 204


@user_blueprint.route("/<uuid:user_id>/new-template-category-request", methods=["POST"])
def send_new_template_category_request(user_id):
contact = None
data = request.json
try:
user = get_user_by_id(user_id=user_id)
contact = ContactRequest(
support_type="new_template_category_request",
friendly_support_type="New template category request",
name=user.name,
email_address=user.email_address,
service_id=data["service_id"],
template_category_name_en=data["template_category_name_en"],
template_category_name_fr=data["template_category_name_fr"],
template_id_link=f"https://{current_app.config['ADMIN_BASE_URL']}/services/{data['service_id']}/templates/{data['template_id']}",
)
contact.tags = ["z_skip_opsgenie", "z_skip_urgent_escalation"]

except TypeError as e:
current_app.logger.error(e)
return jsonify({}), 400
except NoResultFound as e:
# This means that get_user_by_id couldn't find a user
current_app.logger.error(e)
return jsonify({}), 400

status_code = Freshdesk(contact).send_ticket()
return jsonify({"status_code": status_code}), 204


@user_blueprint.route("/<uuid:user_id>", methods=["GET"])
@user_blueprint.route("", methods=["GET"])
def get_user(user_id=None):
Expand Down
48 changes: 48 additions & 0 deletions tests/app/clients/test_freshdesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,54 @@ def match_json(request):
assert response == 201
assert email_freshdesk_ticket_mock.not_called()

def test_send_ticket_other_category(self, email_freshdesk_ticket_mock, notify_api: Flask):
def match_json(request):
expected = {
"product_id": 42,
"subject": "New template category request",
"description": "New template category request from name (test@email.com):<br>"
"- Service id: 8624bd36-b70b-4d4b-a459-13e1f4770b92<br>"
"- New Template Category Request name: test category name<br>"
"- Template id request: http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103<br>"
"<hr><br>"
"Demande de nouvelle catégorie de modèle de name (test@email.com):<br>"
"- Identifiant du service: 8624bd36-b70b-4d4b-a459-13e1f4770b92<br>"
"- Nom de la nouvelle catégorie de modèle demandée: test category name<br>"
"- Demande d'identifiant de modèle: http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103",
"email": "test@email.com",
"priority": 1,
"status": 2,
"tags": [],
}

encoded_auth = base64.b64encode(b"freshdesk-api-key:x").decode("ascii")
json_matches = request.json() == expected
basic_auth_header = request.headers.get("Authorization") == f"Basic {encoded_auth}"

return json_matches and basic_auth_header

with requests_mock.mock() as rmock:
rmock.request(
"POST",
"https://freshdesk-test.com/api/v2/tickets",
additional_matcher=match_json,
status_code=201,
)
data: Dict[str, Any] = {
"email_address": "test@email.com",
"name": "name",
"friendly_support_type": "New template category request",
"support_type": "new_template_category_request",
"service_id": "8624bd36-b70b-4d4b-a459-13e1f4770b92",
"template_category_name_en": "test category name",
"template_category_name_fr": "test category name",
"template_id_link": "http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103",
}
with notify_api.app_context():
response = freshdesk.Freshdesk(ContactRequest(**data)).send_ticket()
assert response == 201
assert email_freshdesk_ticket_mock.not_called()

def test_send_ticket_other(self, email_freshdesk_ticket_mock, notify_api: Flask):
def match_json(request):
expected = {
Expand Down
25 changes: 25 additions & 0 deletions tests/app/user/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,31 @@ def test_send_branding_request(client, sample_service, sample_organisation, mock
mocked_salesforce_client.engagement_update.assert_not_called()


class TestFreshDeskRequestTickets:
def test_send_request_for_new_category(self, client, sample_service, sample_organisation, mocker):
sample_user = sample_service.users[0]
sample_service.organisation = sample_organisation
post_data = {
"service_name": sample_service.name,
"email_address": sample_user.email_address,
"service_id": str(sample_service.id),
"template_category_name_en": "test",
"template_category_name_fr": "test",
"template_id": "1234",
}
mocked_freshdesk = mocker.patch("app.user.rest.Freshdesk.send_ticket", return_value=201)
mocked_salesforce_client = mocker.patch("app.user.rest.salesforce_client")

resp = client.post(
url_for("user.send_new_template_category_request", user_id=str(sample_user.id)),
data=json.dumps(post_data),
headers=[("Content-Type", "application/json"), create_authorization_header()],
)
assert resp.status_code == 204
mocked_freshdesk.assert_called_once_with()
mocked_salesforce_client.engagement_update.assert_not_called()


def test_send_user_confirm_new_email_returns_204(client, sample_user, change_email_confirmation_template, mocker):
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
new_email = "new_address@dig.gov.uk"
Expand Down

0 comments on commit c237e66

Please sign in to comment.