Skip to content

Commit

Permalink
Merge pull request #570 from praekeltfoundation/MOMZA-2292-add-privac…
Browse files Browse the repository at this point in the history
…y-policy-pdf

Manage fallback channel
  • Loading branch information
erikh360 authored Jun 29, 2023
2 parents 71c9845 + a55bcf1 commit 374acba
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
52 changes: 52 additions & 0 deletions ndoh_hub/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest import TestCase

import responses
Expand All @@ -6,6 +7,7 @@
msisdn_to_whatsapp_id,
normalise_msisdn,
send_whatsapp_template_message,
update_turn_contact_details,
)


Expand All @@ -29,6 +31,28 @@ def test_msisdn_to_whatsapp_id(self):
self.assertEqual(msisdn_to_whatsapp_id("+27820001001"), "27820001001")


class TestUpdateTurnContactDetails(TestCase):
@responses.activate
def test_update_turn_contact_details(self):
"""
Should call the turn api with the correct body
"""
responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

update_turn_contact_details("27820001001", {"field": "new value"})

request = json.loads(responses.calls[0].request.body)
self.assertEqual(
request,
{"field": "new value"},
)


class TestSendWhatsappTemplateMessage(TestCase):
@responses.activate
def test_send_whatsapp_template_message_number_on_whatsapp(self):
Expand All @@ -40,6 +64,13 @@ def test_send_whatsapp_template_message_number_on_whatsapp(self):
msisdn = "+27820001001"
template_name = "test template"

responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

responses.add(
method=responses.POST,
url="http://turn/v1/messages",
Expand All @@ -53,6 +84,8 @@ def test_send_whatsapp_template_message_number_on_whatsapp(self):

self.assertEqual(response, "WhatsApp")

self.assertEqual(len(responses.calls), 2)

@responses.activate
def test_send_whatsapp_template_message_number_not_on_whatsapp(self):
"""
Expand All @@ -63,6 +96,13 @@ def test_send_whatsapp_template_message_number_not_on_whatsapp(self):
msisdn = "+27820001001"
template_name = "test template"

responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

responses.add(
method=responses.POST,
url="http://turn/v1/messages",
Expand All @@ -87,3 +127,15 @@ def test_send_whatsapp_template_message_number_not_on_whatsapp(self):
)

self.assertEqual(response, "SMS")

self.assertEqual(len(responses.calls), 3)
request = json.loads(responses.calls[0].request.body)
self.assertEqual(
request,
{"is_fallback_active": False},
)
request = json.loads(responses.calls[2].request.body)
self.assertEqual(
request,
{"is_fallback_active": True},
)
27 changes: 24 additions & 3 deletions ndoh_hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def test_send_whatsapp_template_message_number_on_whatsapp(self):
msisdn = "+27820001001"
template_name = "test template"

responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

responses.add(
method=responses.POST,
url="http://turn/v1/messages",
Expand All @@ -53,7 +60,7 @@ def test_send_whatsapp_template_message_number_on_whatsapp(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["preferred_channel"], "WhatsApp")

request = json.loads(responses.calls[0].request.body)
request = json.loads(responses.calls[1].request.body)
self.assertEqual(
request,
{
Expand Down Expand Up @@ -83,6 +90,13 @@ def test_send_whatsapp_template_message_number_not_on_whatsapp(self):
msisdn = "+27820001001"
template_name = "test template"

responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

responses.add(
method=responses.POST,
url="http://turn/v1/messages",
Expand Down Expand Up @@ -113,7 +127,7 @@ def test_send_whatsapp_template_message_number_not_on_whatsapp(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["preferred_channel"], "SMS")

request = json.loads(responses.calls[0].request.body)
request = json.loads(responses.calls[1].request.body)
self.assertEqual(
request,
{
Expand All @@ -137,6 +151,13 @@ def test_send_whatsapp_template_message_with_media(self):
msisdn = "+27820001001"
template_name = "test template"

responses.add(
method=responses.PATCH,
url="http://turn/v1/contacts/27820001001",
json={},
status=200,
)

responses.add(
method=responses.POST,
url="http://turn/v1/messages",
Expand All @@ -156,7 +177,7 @@ def test_send_whatsapp_template_message_with_media(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["preferred_channel"], "WhatsApp")

request = json.loads(responses.calls[0].request.body)
request = json.loads(responses.calls[1].request.body)
self.assertEqual(
request,
{
Expand Down
18 changes: 17 additions & 1 deletion ndoh_hub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,29 @@ def send_slack_message(channel, text):
return False


def update_turn_contact_details(wa_id, fields):
headers = {
"Authorization": f"Bearer {settings.TURN_TOKEN}",
"content-type": "application/json",
"Accept": "application/vnd.v1+json",
}
response = requests.patch(
urljoin(settings.TURN_URL, "/v1/contacts/{}".format(wa_id)),
json=fields,
headers=headers,
)
response.raise_for_status()


def send_whatsapp_template_message(msisdn, template_name, parameters, media=None):
# send whatsapp template
headers = {
"Authorization": "Bearer {}".format(settings.TURN_TOKEN),
"Authorization": f"Bearer {settings.TURN_TOKEN}",
"Content-Type": "application/json",
}

wa_id = msisdn_to_whatsapp_id(msisdn)
update_turn_contact_details(wa_id, {"is_fallback_active": False})

components = []
if parameters:
Expand Down Expand Up @@ -247,5 +262,6 @@ def send_whatsapp_template_message(msisdn, template_name, parameters, media=None
if "messages" not in response_data:
if response_data["errors"][0]["code"] == 1013:
prefered_chanel = "SMS"
update_turn_contact_details(wa_id, {"is_fallback_active": True})

return prefered_chanel

0 comments on commit 374acba

Please sign in to comment.