From 9d31bbb8ffd70aaa522590fd78a03548f5e84ff4 Mon Sep 17 00:00:00 2001 From: Gerrit Vermeulen Date: Wed, 25 Sep 2024 11:34:25 +0200 Subject: [PATCH 1/3] Add a test using fake time --- .github/workflows/ci.yaml | 2 +- Onboarding/QA/tests/basic-questions_test.exs | 1 + .../QA/tests/intro-and-welcome_test.exs | 3 +-- .../tests/personal-profile-questions_test.exs | 1 + Onboarding/QA/tests/profile-generic_test.exs | 1 + Onboarding/QA/tests/profile-hcw_test.exs | 1 + .../tests/profile-pregnancy-health_test.exs | 25 ++++++++++++++++++- .../QA/tests/profile-pregnant-nurse_test.exs | 1 + 8 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 63d9901..3a50803 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -29,7 +29,7 @@ jobs: uses: actions/checkout@v4 with: repository: praekeltfoundation/flow_tester - ref: v0.2.2 + ref: v0.3.0 path: flow_tester ssh-key: ${{ secrets.SSH_PRIVATE_KEY }} diff --git a/Onboarding/QA/tests/basic-questions_test.exs b/Onboarding/QA/tests/basic-questions_test.exs index cff8e57..ccb3b42 100644 --- a/Onboarding/QA/tests/basic-questions_test.exs +++ b/Onboarding/QA/tests/basic-questions_test.exs @@ -8,6 +8,7 @@ defmodule BasicQuestionsTest do import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/Onboarding/QA/tests/intro-and-welcome_test.exs b/Onboarding/QA/tests/intro-and-welcome_test.exs index 5f7a79a..e593103 100644 --- a/Onboarding/QA/tests/intro-and-welcome_test.exs +++ b/Onboarding/QA/tests/intro-and-welcome_test.exs @@ -3,13 +3,12 @@ defmodule IntroAndWelcomeTest do alias FlowTester.WebhookHandler, as: WH - alias FlowTester.WebhookHandler.FakeCMS.Content.Document - alias Onboarding.QA.Helpers import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/Onboarding/QA/tests/personal-profile-questions_test.exs b/Onboarding/QA/tests/personal-profile-questions_test.exs index 82a65dc..ee1e8c6 100644 --- a/Onboarding/QA/tests/personal-profile-questions_test.exs +++ b/Onboarding/QA/tests/personal-profile-questions_test.exs @@ -6,6 +6,7 @@ defmodule PersonalProfileQuestionsTest do alias Onboarding.QA.Helpers def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/Onboarding/QA/tests/profile-generic_test.exs b/Onboarding/QA/tests/profile-generic_test.exs index 0f33426..a098815 100644 --- a/Onboarding/QA/tests/profile-generic_test.exs +++ b/Onboarding/QA/tests/profile-generic_test.exs @@ -8,6 +8,7 @@ defmodule ProfileGenericTest do import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/Onboarding/QA/tests/profile-hcw_test.exs b/Onboarding/QA/tests/profile-hcw_test.exs index 3917ae7..abbe45c 100644 --- a/Onboarding/QA/tests/profile-hcw_test.exs +++ b/Onboarding/QA/tests/profile-hcw_test.exs @@ -8,6 +8,7 @@ defmodule ProfileHCWTest do import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/Onboarding/QA/tests/profile-pregnancy-health_test.exs b/Onboarding/QA/tests/profile-pregnancy-health_test.exs index 0091a6c..201aacf 100644 --- a/Onboarding/QA/tests/profile-pregnancy-health_test.exs +++ b/Onboarding/QA/tests/profile-pregnancy-health_test.exs @@ -2,13 +2,13 @@ defmodule ProfilePregnancyHealthTest do use FlowTester.Case alias FlowTester.WebhookHandler, as: WH - alias FlowTester.WebhookHandler.FakeCMS.Content.{Image} alias Onboarding.QA.Helpers import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) @@ -1323,6 +1323,29 @@ defmodule ProfilePregnancyHealthTest do }) end + test "edd day then above max day error february 29" do + months = get_months() + month_words = get_month_words(months) + {list_of_months, _edd_confirmation_text, _full_edd} = get_edd(months, month_words) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(~U[2024-02-29 00:00:00Z]) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("30") + |> receive_message(%{ + text: "I don't understand your reply.\r\n\r\nšŸ‘‡šŸ½ Please try that again and respond with the number that comes before your answer." + }) + end + # TODO: Tests for Feb, long months, and short months. This requires us to be able to mock the return value of now() so that we can dictacte what options # are available in the list of months. diff --git a/Onboarding/QA/tests/profile-pregnant-nurse_test.exs b/Onboarding/QA/tests/profile-pregnant-nurse_test.exs index 4dd0d09..bf41533 100644 --- a/Onboarding/QA/tests/profile-pregnant-nurse_test.exs +++ b/Onboarding/QA/tests/profile-pregnant-nurse_test.exs @@ -8,6 +8,7 @@ defmodule ProfilePregnantNurseTest do import Onboarding.QA.Helpers.Macros def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) From 2483aafd5476fa3150d59ca056d0d17319d012cf Mon Sep 17 00:00:00 2001 From: Gerrit Vermeulen Date: Wed, 25 Sep 2024 11:41:39 +0200 Subject: [PATCH 2/3] Fix helpcentre tests --- HelpCentre/QA/tests/agent-greeting_test.exs | 1 + HelpCentre/QA/tests/agent-wrap-up_test.exs | 1 + HelpCentre/QA/tests/intro-human-agent_test.exs | 1 + HelpCentre/QA/tests/intro-to-helpcentre_test.exs | 1 + HelpCentre/QA/tests/scheduled-callback-confirmation_test.exs | 1 + HelpCentre/QA/tests/scheduled-callback-followup_test.exs | 1 + HelpCentre/QA/tests/scheduled-query-rating_test.exs | 1 + .../QA/tests/scheduled-topics-no-response-followup_test.exs | 1 + 8 files changed, 8 insertions(+) diff --git a/HelpCentre/QA/tests/agent-greeting_test.exs b/HelpCentre/QA/tests/agent-greeting_test.exs index 87d2f8d..129866a 100644 --- a/HelpCentre/QA/tests/agent-greeting_test.exs +++ b/HelpCentre/QA/tests/agent-greeting_test.exs @@ -6,6 +6,7 @@ defmodule AgentGreetingTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/agent-wrap-up_test.exs b/HelpCentre/QA/tests/agent-wrap-up_test.exs index 890e108..c0caea0 100644 --- a/HelpCentre/QA/tests/agent-wrap-up_test.exs +++ b/HelpCentre/QA/tests/agent-wrap-up_test.exs @@ -5,6 +5,7 @@ defmodule AgentWrapUpTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/intro-human-agent_test.exs b/HelpCentre/QA/tests/intro-human-agent_test.exs index 88a8f7b..ffce6a0 100644 --- a/HelpCentre/QA/tests/intro-human-agent_test.exs +++ b/HelpCentre/QA/tests/intro-human-agent_test.exs @@ -5,6 +5,7 @@ defmodule IntroHumanAgentTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/intro-to-helpcentre_test.exs b/HelpCentre/QA/tests/intro-to-helpcentre_test.exs index 910696a..f84e54a 100644 --- a/HelpCentre/QA/tests/intro-to-helpcentre_test.exs +++ b/HelpCentre/QA/tests/intro-to-helpcentre_test.exs @@ -8,6 +8,7 @@ defmodule IntroToHelpCentreTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/scheduled-callback-confirmation_test.exs b/HelpCentre/QA/tests/scheduled-callback-confirmation_test.exs index 7505b58..6e6cb22 100644 --- a/HelpCentre/QA/tests/scheduled-callback-confirmation_test.exs +++ b/HelpCentre/QA/tests/scheduled-callback-confirmation_test.exs @@ -5,6 +5,7 @@ defmodule ScheduledCallbackConfirmationTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/scheduled-callback-followup_test.exs b/HelpCentre/QA/tests/scheduled-callback-followup_test.exs index 74c8507..f2f2053 100644 --- a/HelpCentre/QA/tests/scheduled-callback-followup_test.exs +++ b/HelpCentre/QA/tests/scheduled-callback-followup_test.exs @@ -5,6 +5,7 @@ defmodule ScheduledCallbackfollowupTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/scheduled-query-rating_test.exs b/HelpCentre/QA/tests/scheduled-query-rating_test.exs index c0aa5c9..634ebf3 100644 --- a/HelpCentre/QA/tests/scheduled-query-rating_test.exs +++ b/HelpCentre/QA/tests/scheduled-query-rating_test.exs @@ -5,6 +5,7 @@ defmodule ScheduledQueryRatingTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) diff --git a/HelpCentre/QA/tests/scheduled-topics-no-response-followup_test.exs b/HelpCentre/QA/tests/scheduled-topics-no-response-followup_test.exs index d3968d3..bb310a5 100644 --- a/HelpCentre/QA/tests/scheduled-topics-no-response-followup_test.exs +++ b/HelpCentre/QA/tests/scheduled-topics-no-response-followup_test.exs @@ -6,6 +6,7 @@ defmodule ScheduledTopicsNoResponseFollowupTest do defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"]) def setup_fake_cms(auth_token) do + use FakeCMS # Start the handler. wh_pid = start_link_supervised!({FakeCMS, %FakeCMS.Config{auth_token: auth_token}}) From 9c96922d30b1f62114797427583b369f6d3497af Mon Sep 17 00:00:00 2001 From: Gerrit Vermeulen Date: Wed, 25 Sep 2024 13:50:28 +0200 Subject: [PATCH 3/3] Add more tests for feb, long months, and short months --- .../tests/profile-pregnancy-health_test.exs | 134 +++++++++++++++++- 1 file changed, 127 insertions(+), 7 deletions(-) diff --git a/Onboarding/QA/tests/profile-pregnancy-health_test.exs b/Onboarding/QA/tests/profile-pregnancy-health_test.exs index 201aacf..75d286b 100644 --- a/Onboarding/QA/tests/profile-pregnancy-health_test.exs +++ b/Onboarding/QA/tests/profile-pregnancy-health_test.exs @@ -801,8 +801,7 @@ defmodule ProfilePregnancyHealthTest do context |> FlowTester.set_contact_properties(%{"gender" => "", "name" => "Lily", "opted_in" => "true"}) end - defp get_months() do - this_month = DateTime.utc_now() + defp get_months(this_month \\ DateTime.utc_now()) do [ this_month, Date.shift(this_month, month: 1), @@ -1323,14 +1322,40 @@ defmodule ProfilePregnancyHealthTest do }) end - test "edd day then above max day error february 29" do - months = get_months() + test "edd day then feb 29 is valid" do + fake_time = ~U[2023-02-28 00:00:00Z] + months = get_months(fake_time) + month_words = get_month_words(months) + {list_of_months, edd_confirmation_text, _full_edd} = get_edd(months, month_words, 29, 0) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(fake_time) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("29") + |> receive_message(%{ + text: ^edd_confirmation_text, + buttons: button_labels(["Yes, that's right", "Pick another date"]) + }) + end + + test "edd day then feb 30 is not valid" do + fake_time = ~U[2023-02-28 00:00:00Z] + months = get_months(fake_time) month_words = get_month_words(months) {list_of_months, _edd_confirmation_text, _full_edd} = get_edd(months, month_words) month = elem(Enum.at(list_of_months, 0), 0) setup_flow() - |> FlowTester.set_fake_time(~U[2024-02-29 00:00:00Z]) + |> FlowTester.set_fake_time(fake_time) |> Helpers.init_contact_fields() |> init_contact_fields() |> init_pregnancy_info() @@ -1346,8 +1371,103 @@ defmodule ProfilePregnancyHealthTest do }) end - # TODO: Tests for Feb, long months, and short months. This requires us to be able to mock the return value of now() so that we can dictacte what options - # are available in the list of months. + test "edd day then long month 31 is valid" do + fake_time = ~U[2023-01-01 00:00:00Z] # January + months = get_months(fake_time) + month_words = get_month_words(months) + {list_of_months, edd_confirmation_text, _full_edd} = get_edd(months, month_words, 31, 0) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(fake_time) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("31") + |> receive_message(%{ + text: ^edd_confirmation_text, + buttons: button_labels(["Yes, that's right", "Pick another date"]) + }) + end + + test "edd day then long month 32 is invalid" do + fake_time = ~U[2024-01-01 00:00:00Z] # January + months = get_months(fake_time) + month_words = get_month_words(months) + {list_of_months, _edd_confirmation_text, _full_edd} = get_edd(months, month_words) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(fake_time) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("32") + |> receive_message(%{ + text: "I don't understand your reply.\r\n\r\nšŸ‘‡šŸ½ Please try that again and respond with the number that comes before your answer." + }) + end + + test "edd day then short month 30 is valid" do + fake_time = ~U[2024-04-01 00:00:00Z] # April + months = get_months(fake_time) + month_words = get_month_words(months) + {list_of_months, edd_confirmation_text, _full_edd} = get_edd(months, month_words, 30, 0) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(fake_time) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("30") + |> receive_message(%{ + text: ^edd_confirmation_text, + buttons: button_labels(["Yes, that's right", "Pick another date"]) + }) + end + + test "edd day then short month 31 is invalid" do + fake_time = ~U[2024-04-01 00:00:00Z] # April + months = get_months(fake_time) + month_words = get_month_words(months) + {list_of_months, _edd_confirmation_text, _full_edd} = get_edd(months, month_words) + month = elem(Enum.at(list_of_months, 0), 0) + + setup_flow() + |> FlowTester.set_fake_time(fake_time) + |> Helpers.init_contact_fields() + |> init_contact_fields() + |> init_pregnancy_info() + |> FlowTester.start() + |> receive_message(%{}) + |> FlowTester.send(button_label: "I'm pregnant") + |> receive_message(%{}) + |> FlowTester.send(month) + |> receive_message(%{}) + |> FlowTester.send("31") + |> receive_message(%{ + text: "I don't understand your reply.\r\n\r\nšŸ‘‡šŸ½ Please try that again and respond with the number that comes before your answer." + }) + end test "edd confirm then error" do months = get_months()