From d95364b94fdd9897c7affd2acb4b0bbd5b9104d0 Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Tue, 10 Jul 2018 15:33:42 +0530 Subject: [PATCH] auth: GitHubAuthBackend.get_verified_emails returns user's all emails. The email_list returned has the primary email as the first element. Testing: The order of the emails in the test was changed to put a verified email before the primary one. The tests would fail without this commit's change after the changes in the order of test emails. --- zerver/tests/test_auth_backends.py | 8 ++++++-- zproject/backends.py | 12 +++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index d0aabd8df5..5e26c1e7e1 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -477,14 +477,18 @@ class GitHubAuthBackendTest(ZulipTestCase): primary=True), ] else: + # Keeping a verified email before the primary email makes sure + # get_verified_emails puts the primary email at the start of the + # email list returned as social_associate_user_helper assumes the + # first email as the primary email. email_data = [ + dict(email="notprimary@example.com", + verified=True), dict(email=account_data_dict["email"], verified=True, primary=True), dict(email="ignored@example.com", verified=False), - dict(email="notprimary@example.com", - verified=True), ] # We register callbacks for the key URLs on github.com that # /complete/github will call diff --git a/zproject/backends.py b/zproject/backends.py index 90c3b74181..102cc464aa 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -598,14 +598,16 @@ class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2): # case without any verified emails emails = [] - verified_emails = [] + verified_emails = [] # type: List[str] for email_obj in emails: if not email_obj.get("verified"): continue - # TODO: When we add a screen to let the user select an email, remove this line. - if not email_obj.get("primary"): - continue - verified_emails.append(email_obj["email"]) + # social_associate_user_helper assumes that the first email in + # verified_emails is primary. + if email_obj.get("primary"): + verified_emails.insert(0, email_obj["email"]) + else: + verified_emails.append(email_obj["email"]) return verified_emails