Skip to content

Commit

Permalink
Fix creation of superusers
Browse files Browse the repository at this point in the history
The old slack `access_token` stopped working and prevented the update
of the users' `is_superuser` flag.

This fixes the problem.

Context/Technicalities
======================

The old Slack application' `access_token` stopped working causing the
Control Panel to raise the following error:

```
{'ok': False, 'error': 'account_inactive'}
```

This may have been caused by the person setting up the corresponding
app being deactivated in Slack (because they left).

We tried to tweak the old app - required some approval - but we couldn't
make it work.

I've now created a new application in Slack but when testing its new
`access_token` I was getting another error:

```
{'ok': False, 'error': 'invalid_arguments', 'deprecated_argument': 'as_user'}
```

This is caused by the change in new Slack Applications behaviour.
According to [Slack's `chat.postMessage` documentation](https://api.slack.com/methods/chat.postMessage#authorship):

> The `as_user` parameter may not be used by new Slack apps (i.e., apps
> installed using our V2 of Oauth 2.0). New Slack apps act on their own
> behalf, rather than a user's.

Remove the `as_user` argument worked but the `user` argument was completely
ignored (again, because of the change in behaviour above).
This is the reason why I've moved the environment name at the end of the
message - as we still want to distinguish between `dev` and `alpha`
messages to avoid confusion.

Ticket
======
Part of ticket: https://trello.com/c/GrJPGKxm
  • Loading branch information
xoen committed Feb 11, 2020
1 parent d9ef133 commit ec3d19f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
7 changes: 3 additions & 4 deletions controlpanel/api/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def notify_superuser_created(username, by_username=None):


def send_notification(message):
slack.WebClient(token=settings.SLACK['api_token']).chat_postMessage(
as_user=False,
username=f"Control Panel [{settings.ENV}]",
client = slack.WebClient(token=settings.SLACK['api_token'])
client.chat_postMessage(
channel=settings.SLACK["channel"],
text=message,
text=f"{message} [{settings.ENV}]",
)
4 changes: 1 addition & 3 deletions tests/api/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ def message_sent(settings, slack_WebClient):
def check_sent(message):
slack_WebClient.assert_called_with(token=settings.SLACK["api_token"])
slack_WebClient.return_value.chat_postMessage.assert_called_with(
as_user=False,
username=f"Control Panel [{settings.ENV}]",
channel=settings.SLACK["channel"],
text=message,
text=f"{message} [{settings.ENV}]",
)
return True
return check_sent
Expand Down

0 comments on commit ec3d19f

Please sign in to comment.