Skip to content

Commit

Permalink
Merge pull request #734 from praekeltfoundation/facebook-survey-invit…
Browse files Browse the repository at this point in the history
…e-flow

Remove endline survey keywords and tests
  • Loading branch information
erikh360 authored Jan 16, 2024
2 parents 0b0f997 + f30b55c commit f52295f
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 402 deletions.
64 changes: 0 additions & 64 deletions yal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@
QA_RESET_FEEDBACK_TIMESTAMP_KEYWORDS = {"resetfeedbacktimestampobzvmp"}
EMERGENCY_KEYWORDS = utils.get_keywords("emergency")
AAQ_KEYWORDS = {"ask a question"}
EJAF_ENDLINE_SURVEY_KEYWORDS = {
"answer",
"yes i want to answer",
"remind me tomorrow",
"i m not interested",
}
EJAF_LOCATION_SURVEY_KEYWORDS = {
"start survey",
"i m not interested",
Expand Down Expand Up @@ -143,14 +137,6 @@ async def process_message(self, message):

keyword = utils.clean_inbound(message.content)

baseline_survey_completed = self.user.metadata.get(
"baseline_survey_completed"
)
endline_survey_completed = self.user.metadata.get(
"endline_survey_completed"
)

endline_survey_started = self.user.metadata.get("endline_survey_started")
feedback_state = await self.get_feedback_state()
payload = utils.get_by_path(
message.transport_metadata, "message", "button", "payload"
Expand Down Expand Up @@ -225,47 +211,6 @@ async def process_message(self, message):
self.user.session_id = None
self.state_name = AssessmentApplication.REMINDER_STATE

elif (
keyword in EJAF_ENDLINE_SURVEY_KEYWORDS
and baseline_survey_completed
and not endline_survey_completed
):
self.save_metadata("assessment_reminder_sent", False)
self.save_metadata("assessment_reminder_name", "")
self.user.session_id = None

if keyword == "remind me tomorrow":
self.state_name = AssessmentApplication.REMINDER_STATE
elif keyword == "i m not interested":
self.state_name = AssessmentApplication.NOT_INTERESTED_STATE
else:
endline_study_max_participant_count = (
await rapidpro.get_global_value(
"endline_study_max_participant_count"
)
)

error, group_count = await rapidpro.get_group_membership_count(
group_name="Endline Survey Completed"
)
if error:
self.state_name = self.ERROR_STATE
elif group_count >= int(endline_study_max_participant_count):
self.state_name = (
EndlineTermsApplication.ENDLINE_LIMIT_REACHED_STATE
)
else:
self.state_name = EndlineTermsApplication.START_STATE

data = {
"endline_survey_started": "True",
}
error = await rapidpro.update_profile(
whatsapp_id, data, self.user.metadata
)
if error:
self.state_name = self.ERROR_STATE

# Fields that RapidPro sets after a feedback push message
elif feedback_state:
if not self.user.session_id:
Expand All @@ -287,12 +232,6 @@ async def process_message(self, message):
self.save_metadata("push_related_page_id", payload.split("_")[-1])
self.state_name = "state_prep_push_msg_related_page"

elif (
endline_survey_started == "Pending"
and keyword not in EJAF_ENDLINE_SURVEY_KEYWORDS
):
self.user.session_id = None
self.state_name = EndlineSurveyApplication.SURVEY_VALIDATION_STATE
elif (
keyword in EJAF_LOCATION_SURVEY_KEYWORDS
and self.user.metadata.get("ejaf_location_survey_status") == "pending"
Expand Down Expand Up @@ -363,7 +302,6 @@ async def get_feedback_state(self):
async def state_start(self):
terms_accepted = self.user.metadata.get("terms_accepted")
onboarding_completed = self.user.metadata.get("onboarding_completed")
endline_terms_accepted = self.user.metadata.get("endline_terms_accepted")

inbound = utils.clean_inbound(self.inbound.content)

Expand All @@ -389,8 +327,6 @@ async def state_start(self):
return await self.go_to_state(MainMenuApplication.START_STATE)
elif terms_accepted:
return await self.go_to_state(OnboardingApplication.START_STATE)
elif endline_terms_accepted:
return await self.go_to_state(EndlineSurveyApplication.START_STATE)
else:
return await self.go_to_state(TermsApplication.START_STATE)

Expand Down
144 changes: 1 addition & 143 deletions yal/tests/surveys/test_endline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from vaccine.testing import AppTester, TState, run_sanic
from yal import config
from yal.main import Application
from yal.utils import GENERIC_ERROR_OPTIONS, GENERIC_ERRORS, replace_persona_fields
from yal.utils import GENERIC_ERRORS, replace_persona_fields


@pytest.fixture
Expand Down Expand Up @@ -108,129 +108,6 @@ def get_group(request):
config.RAPIDPRO_URL = url


@pytest.mark.asyncio
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_endline_invitation_i_want_to_answer(
get_group_membership_count, tester: AppTester, rapidpro_mock
):

get_group_membership_count.return_value = False, 100

tester.user.metadata["baseline_survey_completed"] = True
tester.user.metadata["endline_survey_started"] = "Pending"
tester.user.metadata["terms_accepted"] = True
tester.user.metadata["onboarding_completed"] = True

await tester.user_input("Yes, I want to answer")

tester.assert_state("state_start_terms")


@pytest.mark.asyncio
async def test_endline_invitation_remind_me_tomorrow(
tester: AppTester,
rapidpro_mock,
):
user = User(
addr="278201234567",
state=StateData(),
session_id=1,
metadata={
"baseline_survey_completed": True,
"endline_survey_started": "Pending",
},
)
app = Application(user)
msg = Message(
content="Remind me tomorrow",
to_addr="27820001002",
from_addr="27820001003",
transport_name="whatsapp",
transport_type=Message.TRANSPORT_TYPE.HTTP_API,
)

[reply] = await app.process_message(msg)

assert user.state.name == "state_remind_tomorrow"


@pytest.mark.asyncio
async def test_endline_invitation_not_interested(
tester: AppTester,
rapidpro_mock,
):
user = User(
addr="278201234567",
state=StateData(),
session_id=1,
metadata={
"baseline_survey_completed": True,
"endline_survey_started": "Pending",
},
)
app = Application(user)
msg = Message(
content="I'm not interested",
to_addr="27820001002",
from_addr="27820001003",
transport_name="whatsapp",
transport_type=Message.TRANSPORT_TYPE.HTTP_API,
)

[reply] = await app.process_message(msg)

assert user.state.name == "state_not_interested"


@pytest.mark.asyncio
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_endline_invitation_answer(
get_group_membership_count, tester: AppTester, rapidpro_mock
):

get_group_membership_count.return_value = False, 100

tester.user.metadata["baseline_survey_completed"] = True
tester.user.metadata["endline_survey_started"] = "Pending"
tester.user.metadata["terms_accepted"] = True
tester.user.metadata["onboarding_completed"] = True

await tester.user_input("Yes, I want to answer")

tester.assert_state("state_start_terms")


@pytest.mark.asyncio
async def test_endline_survey_validation(tester: AppTester, rapidpro_mock):

user = User(
addr="278201234567",
state=StateData(),
session_id=1,
metadata={
"baseline_survey_completed": True,
"endline_survey_started": "Pending",
},
)
app = Application(user)
msg = Message(
content="this is wrong input",
to_addr="27820001002",
from_addr="27820001003",
transport_name="whatsapp",
transport_type=Message.TRANSPORT_TYPE.HTTP_API,
)

[reply] = await app.process_message(msg)

assert user.state.name == "state_survey_validation"
error_content = reply.content
if "Umm.." in error_content:
error_content = error_content.replace("🤖", "[persona_emoji]")

assert error_content in GENERIC_ERROR_OPTIONS


@pytest.mark.asyncio
async def test_state_platform_review_endline(tester: AppTester, rapidpro_mock):
await tester.user_input(
Expand Down Expand Up @@ -1290,25 +1167,6 @@ async def test_endline_flow(tester: AppTester, rapidpro_mock):
tester.assert_message(message)


@pytest.mark.asyncio
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_endline_agree_terms_and_condition(
get_group_membership_count, tester: AppTester, rapidpro_mock
):
get_group_membership_count.return_value = False, 100
tester.user.metadata["baseline_survey_completed"] = True
tester.user.metadata["endline_survey_started"] = "Pending"
tester.user.metadata["terms_accepted"] = True
tester.user.metadata["onboarding_completed"] = True

await tester.user_input("Yes, I want to answer")
tester.assert_state("state_start_terms")
tester.assert_metadata("endline_survey_started", "True")

await tester.user_input("Yes, I agree")
tester.assert_state("state_accept_consent")


@pytest.mark.asyncio
async def test_state_accept_consent_reminder_lets_do_this(
tester: AppTester, rapidpro_mock
Expand Down
Loading

0 comments on commit f52295f

Please sign in to comment.