Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add location survey tests #667

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 138 additions & 1 deletion yal/tests/surveys/test_location.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json

import pytest
from sanic import Sanic, response

from vaccine.models import Message
from vaccine.testing import AppTester
from vaccine.testing import AppTester, TState, run_sanic
from yal import config

# TODO: fix this import once this flow is hooked up in main application
from yal.surveys.location import Application
Expand All @@ -12,6 +16,26 @@ def tester():
return AppTester(Application)


@pytest.fixture(autouse=True)
async def rapidpro_mock():
Sanic.test_mode = True
app = Sanic("mock_rapidpro")
tstate = TState()

@app.route("/api/v2/contacts.json", methods=["POST"])
def update_contact(request):
tstate.requests.append(request)
return response.json({}, status=200)

async with run_sanic(app) as server:
url = config.RAPIDPRO_URL
config.RAPIDPRO_URL = f"http://{server.host}:{server.port}"
config.RAPIDPRO_TOKEN = "testtoken"
server.tstate = tstate
yield server
config.RAPIDPRO_URL = url


@pytest.mark.asyncio
async def test_state_location_introduction_already_completed(tester: AppTester):
tester.setup_state("state_location_introduction")
Expand Down Expand Up @@ -83,3 +107,116 @@ async def test_state_location_introduction_pending(tester: AppTester):
]
)
)


@pytest.mark.asyncio
async def test_state_location_province(tester: AppTester):
tester.setup_state("state_location_introduction")
tester.user.metadata["ejaf_location_survey_status"] = "pending"
await tester.user_input("1")

tester.assert_state("state_location_province")
tester.assert_message("*What province do you live in?*")


@pytest.mark.asyncio
async def test_state_location_province_excluded(tester: AppTester):
tester.setup_state("state_location_province")
await tester.user_input("4")

tester.assert_state("state_location_introduction")
tester.assert_message(
"\n".join(
[
"Sorry, we`re only recruiting people for group discussions in "
"Gauteng, KZN and the Western Cape."
"",
"Reply with “menu” to return to the main menu",
]
)
)


@pytest.mark.asyncio
async def test_state_location_name_city(tester: AppTester):
tester.setup_state("state_location_province")
await tester.user_input("1")

tester.assert_state("state_location_name_city")
tester.assert_message(
"\n".join(
[
"*What is the name of the city or town you live in or live closest "
"to?*",
"",
"Please *TYPE* in the name of the city or town.",
]
)
)


@pytest.mark.asyncio
async def test_state_location_area_type(tester: AppTester):
tester.setup_state("state_location_name_city")
await tester.user_input("jhb")

tester.assert_state("state_location_area_type")
tester.assert_message("What type of area are you living in?")


@pytest.mark.asyncio
async def test_state_location_group_invite(tester: AppTester):
tester.setup_state("state_location_area_type")
await tester.user_input("1")

tester.assert_state("state_location_group_invite")
tester.assert_message(
"\n".join(
[
"All good, thank you! 🙌🏾",
"",
"We are organising group discussions for BWise users in September. "
"The focus groups will be with other users aged 15-24 years.",
"",
"We'd ask about your experiences on the platform and how feasible, "
"usable and effective the BWise chatbot is as a mobile health "
"platform for young South Africans.",
"",
"*Remember that you do not have to be interested in joining the "
"focus groups to complete this survey. If you indicate you are "
"interested you can still reject any invitation if we do contact "
"you.*",
"",
"Are you interested in being invited to one of these discussions "
"in the future?",
]
)
)


@pytest.mark.asyncio
async def test_state_location_group_invite_submit(tester: AppTester, rapidpro_mock):
tester.setup_state("state_location_group_invite")
await tester.user_input("1")

tester.assert_state("state_location_introduction")
tester.assert_message(
"\n".join(
[
"*And that's a wrap!*",
"",
"Thank you for taking part in our survey 🙏🏽",
"",
"*You will get your R10 airtime within 24 hours.*",
"",
"You can engage with the B-Wise chatbot at any time for some "
"helpful messages or to ask any questions.",
]
)
)

assert len(rapidpro_mock.tstate.requests) == 1
request = rapidpro_mock.tstate.requests[0]
assert json.loads(request.body.decode("utf-8")) == {
"fields": {"ejaf_location_survey_status": "completed"},
}
Loading