Skip to content

Commit

Permalink
Start midline survey timeout flow
Browse files Browse the repository at this point in the history
  • Loading branch information
erikh360 committed Jan 30, 2023
1 parent be55fb6 commit a8b670e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions mqr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
RAPIDPRO_MIDLINE_SURVEY_COMPLETE_FLOW = environ.get(
"RAPIDPRO_MIDLINE_SURVEY_COMPLETE_FLOW"
)
RAPIDPRO_MIDLINE_SURVEY_TIMEOUT_FLOW = environ.get(
"RAPIDPRO_MIDLINE_SURVEY_TIMEOUT_FLOW"
)
30 changes: 30 additions & 0 deletions mqr/midline_ussd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from mqr import config
from vaccine.base_application import BaseApplication
from vaccine.models import Message
from vaccine.states import Choice, ChoiceState, EndState
from vaccine.utils import HTTP_EXCEPTIONS, normalise_phonenumber

Expand All @@ -25,6 +26,35 @@ def get_rapidpro():
class Application(BaseApplication):
START_STATE = "state_eat_fruits"

async def process_message(self, message):
if message.session_event == Message.SESSION_EVENT.CLOSE:
self.user.session_id = None
msisdn = normalise_phonenumber(self.user.addr)
urn = f"whatsapp:{msisdn.lstrip(' + ')}"

async with get_rapidpro() as session:
for i in range(3):
try:
data = {
"flow": config.RAPIDPRO_MIDLINE_SURVEY_TIMEOUT_FLOW,
"urns": [urn],
}
response = await session.post(
urljoin(config.RAPIDPRO_URL, "/api/v2/flow_starts.json"),
json=data,
)
response.raise_for_status()
break
except HTTP_EXCEPTIONS as e:
if i == 2:
logger.exception(e)
return []
else:
continue
return []

return await super().process_message(message)

async def state_eat_fruits(self):
question = self._("1/16\n" "\n" "Do you eat fruits at least once a day?")
error = self._("Please use numbers from list.\n")
Expand Down
30 changes: 29 additions & 1 deletion mqr/tests/test_midline_ussd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from mqr import config
from mqr.midline_ussd import Application
from vaccine.models import Message
from vaccine.models import Message, StateData, User
from vaccine.testing import AppTester, TState, run_sanic


Expand Down Expand Up @@ -34,6 +34,34 @@ def start_flow(request):
config.RAPIDPRO_URL = url


@pytest.mark.asyncio
async def test_state_survey_timeout(rapidpro_mock):
u = User(
addr="27820001003",
state=StateData(name="state_vaccine_importance"),
session_id="1",
)
app = Application(u)
msg = Message(
to_addr="27820001002",
from_addr="27820001003",
transport_name="whatsapp",
transport_type=Message.TRANSPORT_TYPE.HTTP_API,
session_event=Message.SESSION_EVENT.CLOSE,
)
[] = await app.process_message(msg)

assert u.session_id is None
assert [r.path for r in rapidpro_mock.tstate.requests] == [
"/api/v2/flow_starts.json"
]
request = rapidpro_mock.tstate.requests[0]
assert json.loads(request.body.decode("utf-8")) == {
"flow": config.RAPIDPRO_MIDLINE_SURVEY_TIMEOUT_FLOW,
"urns": ["whatsapp:27820001003"],
}


@pytest.mark.asyncio
async def test_state_eat_fruits(tester: AppTester):
tester.setup_state("state_eat_fruits")
Expand Down

0 comments on commit a8b670e

Please sign in to comment.