You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0 KiB

  1. from django.contrib.auth import get_user_model
  2. from django.test import TestCase, override_settings
  3. from social_core.backends.azuread_tenant import AzureADTenantOAuth2
  4. from social_core.backends.github import GithubOAuth2
  5. from vcr_unittest import VCRMixin
  6. from .. import social_auth
  7. User = get_user_model()
  8. class VCRTestCase(VCRMixin, TestCase):
  9. @property
  10. def access_token(self):
  11. raise NotImplementedError()
  12. def _get_vcr(self, **kwargs):
  13. kwargs['decode_compressed_response'] = True
  14. kwargs['record_mode'] = 'none' if self.access_token == 'censored' else 'all'
  15. return super()._get_vcr(**kwargs)
  16. def _get_vcr_kwargs(self, **kwargs):
  17. kwargs['filter_headers'] = ['Authorization']
  18. return super()._get_vcr_kwargs(**kwargs)
  19. @override_settings(GITHUB_ADMIN_ORG_NAME='CatalystCode')
  20. @override_settings(GITHUB_ADMIN_TEAM_NAME='doccano-dev')
  21. class TestGithubSocialAuth(VCRTestCase):
  22. strategy = None
  23. backend = GithubOAuth2(strategy=strategy)
  24. access_token = 'censored'
  25. def test_fetch_permissions_is_admin(self):
  26. user = User()
  27. social_auth.fetch_github_permissions(
  28. strategy=self.strategy,
  29. details={'username': 'c-w'},
  30. user=user,
  31. backend=self.backend,
  32. response={'access_token': self.access_token},
  33. )
  34. self.assertTrue(user.is_superuser)
  35. def test_fetch_permissions_not_admin(self):
  36. user = User()
  37. social_auth.fetch_github_permissions(
  38. strategy=self.strategy,
  39. details={'username': 'hirosan'},
  40. user=user,
  41. backend=self.backend,
  42. response={'access_token': self.access_token},
  43. )
  44. self.assertFalse(user.is_superuser)
  45. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')
  46. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=')
  47. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT='cccccccc-cccc-cccc-cccc-cccccccccccc')
  48. class TestAzureADTenantSocialAuth(VCRTestCase):
  49. strategy = None
  50. backend = AzureADTenantOAuth2(strategy=strategy)
  51. access_token = 'censored'
  52. @override_settings(AZUREAD_ADMIN_GROUP_ID='dddddddd-dddd-dddd-dddd-dddddddddddd')
  53. def test_fetch_permissions_is_admin(self):
  54. user = User()
  55. social_auth.fetch_azuread_permissions(
  56. strategy=self.strategy,
  57. details={},
  58. user=user,
  59. backend=self.backend,
  60. response={'access_token': self.access_token},
  61. )
  62. self.assertTrue(user.is_superuser)
  63. @override_settings(AZUREAD_ADMIN_GROUP_ID='eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee')
  64. def test_fetch_permissions_not_admin(self):
  65. user = User()
  66. social_auth.fetch_azuread_permissions(
  67. strategy=self.strategy,
  68. details={},
  69. user=user,
  70. backend=self.backend,
  71. response={'access_token': self.access_token},
  72. )
  73. self.assertFalse(user.is_superuser)