Skip to content

Commit

Permalink
Update slack backend and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Dec 19, 2016
1 parent 93ca299 commit 5ab8ec5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added Dockerfile to simplify the running of tests (`make docker-tox`)

### Changed
- Updated slack backend implementation, update API endpoints used, add test case.
- Changed Dailymotion user data API endpoint
- Changed how "false" values are treated in the user attributes update pipeline
- Fix google OpenID Connect (port from [#747](https://github.com/omab/python-social-auth/pull/747)
Expand Down
57 changes: 20 additions & 37 deletions social_core/backends/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
http://psa.matiasaguirre.net/docs/backends/slack.html
https://api.slack.com/docs/oauth
"""
import re

from .oauth import BaseOAuth2


Expand All @@ -14,6 +12,7 @@ class SlackOAuth2(BaseOAuth2):
AUTHORIZATION_URL = 'https://slack.com/oauth/authorize'
ACCESS_TOKEN_URL = 'https://slack.com/api/oauth.access'
ACCESS_TOKEN_METHOD = 'POST'
DEFAULT_SCOPE = ['identity.basic', 'identity.email']
SCOPE_SEPARATOR = ','
REDIRECT_STATE = False
EXTRA_DATA = [
Expand All @@ -26,41 +25,25 @@ def get_user_details(self, response):
"""Return user details from Slack account"""
# Build the username with the team $username@$team_url
# Necessary to get unique names for all of slack
username = response.get('user')
if self.setting('USERNAME_WITH_TEAM', True):
match = re.search(r'//([^.]+)\.slack\.com', response['url'])
username = '{0}@{1}'.format(username, match.group(1))

out = {'username': username}
if 'profile' in response:
out.update({
'email': response['profile'].get('email'),
'fullname': response['profile'].get('real_name'),
'first_name': response['profile'].get('first_name'),
'last_name': response['profile'].get('last_name')
})
return out
user = response['user']
team = response.get('team')
name = user['name']
email = user.get('email')
username = email and email.split('@', 1)[0] or name
fullname, first_name, last_name = self.get_user_names(name)

if self.setting('USERNAME_WITH_TEAM', True) and team and 'name' in team:
name = '{0}@{1}'.format(name, response['team']['name'])

return {
'username': username,
'email': email,
'fullname': fullname,
'first_name': first_name,
'last_name': last_name
}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
# Has to be two calls, because the users.info requires a username,
# And we want the team information. Check auth.test details at:
# https://api.slack.com/methods/auth.test
auth_test = self.get_json('https://slack.com/api/auth.test', params={
'token': access_token
})

# https://api.slack.com/methods/users.info
user_info = self.get_json('https://slack.com/api/users.info', params={
'token': access_token,
'user': auth_test.get('user_id')
})
if user_info.get('user'):
# Capture the user data, if available based on the scope
auth_test.update(user_info['user'])

# Clean up user_id vs id
auth_test['id'] = auth_test['user_id']
auth_test.pop('ok', None)
auth_test.pop('user_id', None)
return auth_test
return self.get_json('https://slack.com/api/users.identity',
params={'token': access_token})
31 changes: 31 additions & 0 deletions social_core/tests/backends/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

from .oauth import OAuth2Test


class SlackOAuth2Test(OAuth2Test):
backend_path = 'social_core.backends.slack.SlackOAuth2'
user_data_url = 'https://slack.com/api/users.identity'
access_token_body = json.dumps({
'access_token': 'foobar',
'token_type': 'bearer'
})
user_data_body = json.dumps({
'ok': True,
'user': {
'email': 'foobar@example.com',
'name': 'Foo Bar',
'id': u'123456'
},
'team': {
'id': u'456789'
},
'scope': u'identity.basic,identity.email'
})
expected_username = 'foobar'

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()

0 comments on commit 5ab8ec5

Please sign in to comment.