From cda43944b86bc97ba0fc0edf441f14374e3ce19b Mon Sep 17 00:00:00 2001 From: Austin Ewens Date: Fri, 2 Oct 2020 17:44:11 +0000 Subject: [PATCH 1/4] Move slack invite job out of registration callback and into email confirmation callback --- src/core/handlers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/handlers.py b/src/core/handlers.py index 3811e722..c317dfaf 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -51,10 +51,9 @@ def get_username_from_jwt(payload: dict) -> str: def registration_callback(user: User, **kwargs: dict) -> None: """ Listens for the `user_signed_up` signal and adds a background tasks to - send the welcome email and slack invite + send the welcome email """ logger.info(f"Received user_signed_up signal for {user}") - send_slack_invite_job(user.email) send_welcome_email(user.email) @@ -62,7 +61,8 @@ def registration_callback(user: User, **kwargs: dict) -> None: def email_confirmed_callback(email_address: EmailConfirmation, **kwargs: dict) -> None: """ Listens for the `email_confirmed` signal and adds a background task to - add the user to the mailing list + add the user to the mailing list and send the slack invite """ logger.info(f"Received email_confirmed signal for {email_address.email}") + send_slack_invite_job(user.email) add_user_to_mailing_list(email_address.email) From 3b41dbca2c3ce858b481c81bf73f64bbbd2c90b1 Mon Sep 17 00:00:00 2001 From: Austin Ewens Date: Fri, 2 Oct 2020 18:22:07 +0000 Subject: [PATCH 2/4] Fixing bug, using email from argument instead of getting it from user object --- src/core/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/handlers.py b/src/core/handlers.py index c317dfaf..46df1ab8 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -64,5 +64,5 @@ def email_confirmed_callback(email_address: EmailConfirmation, **kwargs: dict) - add the user to the mailing list and send the slack invite """ logger.info(f"Received email_confirmed signal for {email_address.email}") - send_slack_invite_job(user.email) + send_slack_invite_job(email_address.email) add_user_to_mailing_list(email_address.email) From d086736562ccb3a2b20b3366d076b71cd8f46492 Mon Sep 17 00:00:00 2001 From: Austin Ewens Date: Fri, 2 Oct 2020 18:29:40 +0000 Subject: [PATCH 3/4] Changed tests to account for new behavior of when slack invites are sent Instead of running upon the account being registered, the slack invite is sent when the email is verified. The existing test has been changed to anticipate the slack invite no longer being sent on on registration and a new test has been added to check for the test when the email has been verified. --- .../integration/test_rest_registration.py | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/tests/integration/test_rest_registration.py b/src/tests/integration/test_rest_registration.py index 4cb6656e..0bfb42dd 100644 --- a/src/tests/integration/test_rest_registration.py +++ b/src/tests/integration/test_rest_registration.py @@ -57,7 +57,7 @@ def test_user_profile_created(client: APIClient, register_form: Dict[str, str]): @pytest.mark.django_db -def test_slack_invite_task_created( +def test_slack_invite_task_not_created( client: APIClient, register_form: Dict[str, str], mailoutbox: List[EmailMultiAlternatives], @@ -68,8 +68,7 @@ def test_slack_invite_task_created( tasks = BackgroundTask.objects.filter(task_name="core.tasks.send_slack_invite_job") - assert len(tasks) == 1 - assert tasks[0].task_name.split(".")[-1] == "send_slack_invite_job" + assert len(tasks) == 0 @pytest.mark.django_db @@ -140,3 +139,27 @@ def test_mailing_list_task_created( assert any( task.task_name.split(".")[-1] == "add_user_to_mailing_list" for task in tasks ) + + +@pytest.mark.django_db +def test_slack_invite_task_created( + client: APIClient, + register_form: Dict[str, str], + mailoutbox: List[EmailMultiAlternatives], +): + client.post(reverse("rest_register"), register_form) + + body = mailoutbox[0].body + groups = key_pattern.search(body).groupdict() + + res = client.post(reverse("rest_verify_email"), {"key": groups["key"]}) + + assert res.status_code == 200 + tasks = BackgroundTask.objects.filter( + task_name="core.tasks.send_slack_invite_job" + ) + + assert len(tasks) == 1 + assert any( + task.task_name.split(".")[-1] == "send_slack_invite_job" for task in tasks + ) From fffe6f8a5e1e41e44c455f4735ee7007fe59ab2b Mon Sep 17 00:00:00 2001 From: Austin Ewens Date: Fri, 2 Oct 2020 18:35:21 +0000 Subject: [PATCH 4/4] Reformatting to appease linter --- src/tests/integration/test_rest_registration.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tests/integration/test_rest_registration.py b/src/tests/integration/test_rest_registration.py index 0bfb42dd..5940ef97 100644 --- a/src/tests/integration/test_rest_registration.py +++ b/src/tests/integration/test_rest_registration.py @@ -155,9 +155,7 @@ def test_slack_invite_task_created( res = client.post(reverse("rest_verify_email"), {"key": groups["key"]}) assert res.status_code == 200 - tasks = BackgroundTask.objects.filter( - task_name="core.tasks.send_slack_invite_job" - ) + tasks = BackgroundTask.objects.filter(task_name="core.tasks.send_slack_invite_job") assert len(tasks) == 1 assert any(