Skip to content

Commit

Permalink
[providers][s]: make sure user with multiple emails on github is iden…
Browse files Browse the repository at this point in the history
…tified as one - fixes #31 (#32)
  • Loading branch information
zelima authored and akariv committed Nov 7, 2018
1 parent 79f7c53 commit 4929e4c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
12 changes: 8 additions & 4 deletions auth/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ def _get_user_profile(provider, access_token):
return None

response = response.json()
# Make sure we have private Emails from github
if provider == 'github' and response['email'] is None:
# Make sure we have private Emails from github.
# Also make sure we don't have user registered with other email than primary
if provider == 'github':
emails_resp = requests.get(remote_app['get_profile'] + '/emails', headers=headers)
for email in emails_resp.json():
if email.get('primary'):
id_ = hash_email(email['email'])
user = get_user(id_)
if user is not None:
response['email'] = email['email']
break

if email.get('primary'):
response['email'] = email['email']
return response


Expand Down
37 changes: 37 additions & 0 deletions tests/test_user_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,15 @@ def test___check___google_works_fine(self):

def test___check___git_works_fine_with_public_email(self):
with requests_mock.Mocker() as mock:
emails_resp = '''
[{
"email": "email@moshe.com",
"primary": true,
"verified": true
}]
'''
mock.get('https://api.github.com/user', text=self.mocked_resp)
mock.get('https://api.github.com/user/emails', text=emails_resp)
res = self.ctrl._get_user_profile('github', 'access_token')
self.assertEquals(res['email'], 'email@moshe.com')
self.assertEquals(res['name'], 'Moshe')
Expand All @@ -334,6 +342,35 @@ def test___check___git_works_fine_with_private_email(self):
self.assertEquals(res['name'], 'Moshe')


def test___check___git_works_fine_if_multiple_emails_and_one_exists(self):
self.mocked_resp = '''
{
"name": "Moshe",
"email": null
}
'''
emails_resp = '''
[{
"email": "email@newuser.com",
"primary": false,
"verified": true
},
{
"email": "email@another.com",
"primary": true,
"verified": true
}
]
'''
with requests_mock.Mocker() as mock:
models.create_or_get_user('gtihib', '', '', 'email@newuser.com', '')
mock.get('https://api.github.com/user', text=self.mocked_resp)
mock.get('https://api.github.com/user/emails', text=emails_resp)
res = self.ctrl._get_user_profile('github', 'access_token')
self.assertEquals(res['email'], 'email@newuser.com')
self.assertEquals(res['name'], 'Moshe')


class NormalizeProfileTestCase(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 4929e4c

Please sign in to comment.