Refactor GitHub authentication backend tests.

This commit is contained in:
umkay 2016-11-01 15:50:35 -07:00 committed by Tim Abbott
parent a2b6793174
commit 0e0a11bb5e
1 changed files with 10 additions and 25 deletions

View File

@ -308,6 +308,11 @@ class GitHubAuthBackendTest(ZulipTestCase):
request.user = self.user_profile request.user = self.user_profile
self.backend.strategy.request = request self.backend.strategy.request = request
def do_auth(self, *args, **kwargs):
# type: (*Any, **Any) -> UserProfile
with self.settings(AUTHENTICATION_BACKENDS=('zproject.backends.GitHubAuthBackend',)):
return self.backend.authenticate(*args, **kwargs)
def test_github_auth_enabled(self): def test_github_auth_enabled(self):
# type: () -> None # type: () -> None
with self.settings(AUTHENTICATION_BACKENDS=('zproject.backends.GitHubAuthBackend',)): with self.settings(AUTHENTICATION_BACKENDS=('zproject.backends.GitHubAuthBackend',)):
@ -319,12 +324,8 @@ class GitHubAuthBackendTest(ZulipTestCase):
def test_github_backend_do_auth_without_subdomains(self): def test_github_backend_do_auth_without_subdomains(self):
# type: () -> None # type: () -> None
def do_auth(*args, **kwargs):
# type: (*Any, **Any) -> UserProfile
return self.backend.authenticate(*args, **kwargs)
with mock.patch('social.backends.github.GithubOAuth2.do_auth', with mock.patch('social.backends.github.GithubOAuth2.do_auth',
side_effect=do_auth), \ side_effect=self.do_auth), \
mock.patch('zerver.views.auth.login'): mock.patch('zerver.views.auth.login'):
response=dict(email=self.email, name=self.name) response=dict(email=self.email, name=self.name)
result = self.backend.do_auth(response=response) result = self.backend.do_auth(response=response)
@ -332,12 +333,8 @@ class GitHubAuthBackendTest(ZulipTestCase):
def test_github_backend_do_auth_with_subdomains(self): def test_github_backend_do_auth_with_subdomains(self):
# type: () -> None # type: () -> None
def do_auth(*args, **kwargs):
# type: (*Any, **Any) -> UserProfile
return self.backend.authenticate(*args, **kwargs)
with mock.patch('social.backends.github.GithubOAuth2.do_auth', with mock.patch('social.backends.github.GithubOAuth2.do_auth',
side_effect=do_auth): side_effect=self.do_auth):
with self.settings(REALMS_HAVE_SUBDOMAINS=True): with self.settings(REALMS_HAVE_SUBDOMAINS=True):
response=dict(email=self.email, name=self.name) response=dict(email=self.email, name=self.name)
result = self.backend.do_auth(response=response) result = self.backend.do_auth(response=response)
@ -345,12 +342,8 @@ class GitHubAuthBackendTest(ZulipTestCase):
def test_github_backend_do_auth_for_default(self): def test_github_backend_do_auth_for_default(self):
# type: () -> None # type: () -> None
def do_auth(*args, **kwargs):
# type: (*Any, **Any) -> UserProfile
return self.backend.authenticate(*args, **kwargs)
with mock.patch('social.backends.github.GithubOAuth2.do_auth', with mock.patch('social.backends.github.GithubOAuth2.do_auth',
side_effect=do_auth), \ side_effect=self.do_auth), \
mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result: mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result:
response=dict(email=self.email, name=self.name) response=dict(email=self.email, name=self.name)
self.backend.do_auth('fake-access-token', response=response) self.backend.do_auth('fake-access-token', response=response)
@ -362,12 +355,8 @@ class GitHubAuthBackendTest(ZulipTestCase):
def test_github_backend_do_auth_for_team(self): def test_github_backend_do_auth_for_team(self):
# type: () -> None # type: () -> None
def do_auth(*args, **kwargs):
# type: (*Any, **Any) -> UserProfile
return self.backend.authenticate(*args, **kwargs)
with mock.patch('social.backends.github.GithubTeamOAuth2.do_auth', with mock.patch('social.backends.github.GithubTeamOAuth2.do_auth',
side_effect=do_auth), \ side_effect=self.do_auth), \
mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result: mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result:
response=dict(email=self.email, name=self.name) response=dict(email=self.email, name=self.name)
with self.settings(SOCIAL_AUTH_GITHUB_TEAM_ID='zulip-webapp'): with self.settings(SOCIAL_AUTH_GITHUB_TEAM_ID='zulip-webapp'):
@ -394,12 +383,8 @@ class GitHubAuthBackendTest(ZulipTestCase):
def test_github_backend_do_auth_for_org(self): def test_github_backend_do_auth_for_org(self):
# type: () -> None # type: () -> None
def do_auth(*args, **kwargs):
# type: (*Any, **Any) -> UserProfile
return self.backend.authenticate(*args, **kwargs)
with mock.patch('social.backends.github.GithubOrganizationOAuth2.do_auth', with mock.patch('social.backends.github.GithubOrganizationOAuth2.do_auth',
side_effect=do_auth), \ side_effect=self.do_auth), \
mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result: mock.patch('zproject.backends.SocialAuthMixin.process_do_auth') as result:
response=dict(email=self.email, name=self.name) response=dict(email=self.email, name=self.name)
with self.settings(SOCIAL_AUTH_GITHUB_ORG_NAME='Zulip'): with self.settings(SOCIAL_AUTH_GITHUB_ORG_NAME='Zulip'):