Skip to content

Commit

Permalink
(OperationCode#47) Military status communicated to pybot during signu…
Browse files Browse the repository at this point in the history
…p info flow

This adds a new background task to communicate to pybot.

When an update is performed, it checks that the slack id and military
status is present and not equal to old values and kicks off the
background task.

The test is not yet complete, it needs to exercise the background task.
  • Loading branch information
brownnrl committed Oct 2, 2019
1 parent de0d2a0 commit e45dbd3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ coverage.xml

## Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-delete-this-directory.txt

## Vim
*.swp
16 changes: 16 additions & 0 deletions src/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@ def send_welcome_email(email: str) -> None:
logger.exception("Exception trying to send welcome email to user", e)


@background(schedule=0)
def send_slack_update(slack_id: str, military_status:str) -> None:
try:
logger.info(f"Sending slack message after profile update: {slack_id} {military_status}")
url = f"{settings.PYBOT_URL}/pybot/api/v1/slack/update"
headers = {"Authorization": f"Bearer {settings.PYBOT_AUTH_TOKEN}"}
res = requests.post(url, json={"slack_id": slack_id, "military_status": military_status}, headers=headers)

logger.info("Slack update response:", res)
except Exception as e: # pragma: no cover
logger.exception(
f"Exception while trying to send slack update for slack id {slack_id}", e
)


@background(schedule=0)
def send_slack_invite_job(email: str) -> None:
"""
Background task that sends pybot a request triggering an invite for
a newly registered user
:param email: Email the user signed up with
:param military_status: Status that the user indicated in their profile

This comment has been minimized.

Copy link
@brownnrl

brownnrl Oct 2, 2019

Author Owner

This is from an old update and needs to be removed.

"""
try:
logger.info(f"Sending slack invite for email: {email}")
Expand Down
24 changes: 24 additions & 0 deletions src/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
ProfileSerializer,
UserSerializer,
)
from core.tasks import (
send_slack_update,
)

import logging

logger = logging.getLogger(__name__)

sensitive_param = method_decorator(
sensitive_post_parameters("password"), name="dispatch"
Expand Down Expand Up @@ -54,6 +61,22 @@ def get_object(self):
self.check_object_permissions(self.request, obj)
return obj

def perform_update(self, serializer):
"""
Overrides the `perform_update` to check if both slack_id
and military status have been updated and trigger a task
to communicate that update to the slack bot
"""
mil_status_before_update = serializer.instance.military_status
slack_id_before_update = serializer.instance.slack_id
instance = serializer.save()
mil_status = instance.military_status
slack_id = instance.slack_id
if (((slack_id != slack_id_before_update) or
(mil_status_before_update != mil_status))
and slack_id and mil_status):
send_slack_update(slack_id, mil_status)


class AdminUpdateProfile(RetrieveUpdateAPIView):
"""
Expand Down Expand Up @@ -109,6 +132,7 @@ def get_object(self):
self.check_object_permissions(self.request, obj)
return obj



@sensitive_param
class RegisterView(BaseRegisterView):
Expand Down
17 changes: 17 additions & 0 deletions src/tests/integration/test_profile_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ def test_update_profile_frontend_params(
assert getattr(profile, key) == val


def test_update_profile_random_params_update_task_to_pybot(
authed_client: test.Client, user: User, random_profile_dict
):
random_profile_dict['military_status'] = 'current'
random_profile_dict['slack_id'] = 'slack1234'

This comment has been minimized.

Copy link
@brownnrl

brownnrl Oct 2, 2019

Author Owner

After the update is made, need to confirm the task is kicked off.

res = authed_client.patch(
reverse("update_profile"), humps.camelize(random_profile_dict)
)

assert res.status_code == 200

user.refresh_from_db()
profile = user.profile

for key, val in random_profile_dict.items():
assert getattr(profile, key) == val

@pytest.mark.parametrize(
argnames="method, status", argvalues=[("post", 405), ("get", 200), ("patch", 200)]
)
Expand Down

0 comments on commit e45dbd3

Please sign in to comment.