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.

169 lines
5.5 KiB

4 years ago
4 years ago
  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 social_core.backends.okta import OktaOAuth2
  6. from social_core.backends.okta_openidconnect import OktaOpenIdConnect
  7. from vcr_unittest import VCRMixin
  8. from .. import social_auth
  9. User = get_user_model()
  10. class VCRTestCase(VCRMixin, TestCase):
  11. @property
  12. def access_token(self):
  13. raise NotImplementedError()
  14. def _get_vcr(self, **kwargs):
  15. kwargs['decode_compressed_response'] = True
  16. kwargs['record_mode'] = 'none' if self.access_token == 'censored' else 'all'
  17. return super()._get_vcr(**kwargs)
  18. def _get_vcr_kwargs(self, **kwargs):
  19. kwargs['filter_headers'] = ['Authorization']
  20. return super()._get_vcr_kwargs(**kwargs)
  21. @override_settings(GITHUB_ADMIN_ORG_NAME='CatalystCode')
  22. @override_settings(GITHUB_ADMIN_TEAM_NAME='doccano-dev')
  23. class TestGithubSocialAuth(VCRTestCase):
  24. strategy = None
  25. backend = GithubOAuth2(strategy=strategy)
  26. access_token = 'censored'
  27. def test_fetch_permissions_is_admin(self):
  28. user = User()
  29. social_auth.fetch_github_permissions(
  30. strategy=self.strategy,
  31. details={'username': 'c-w'},
  32. user=user,
  33. backend=self.backend,
  34. response={'access_token': self.access_token},
  35. )
  36. self.assertTrue(user.is_superuser)
  37. def test_fetch_permissions_not_admin(self):
  38. user = User()
  39. social_auth.fetch_github_permissions(
  40. strategy=self.strategy,
  41. details={'username': 'hirosan'},
  42. user=user,
  43. backend=self.backend,
  44. response={'access_token': self.access_token},
  45. )
  46. self.assertFalse(user.is_superuser)
  47. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')
  48. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=')
  49. @override_settings(SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT='cccccccc-cccc-cccc-cccc-cccccccccccc')
  50. class TestAzureADTenantSocialAuth(VCRTestCase):
  51. strategy = None
  52. backend = AzureADTenantOAuth2(strategy=strategy)
  53. access_token = 'censored'
  54. @override_settings(AZUREAD_ADMIN_GROUP_ID='dddddddd-dddd-dddd-dddd-dddddddddddd')
  55. def test_fetch_permissions_is_admin(self):
  56. user = User()
  57. social_auth.fetch_azuread_permissions(
  58. strategy=self.strategy,
  59. details={},
  60. user=user,
  61. backend=self.backend,
  62. response={'access_token': self.access_token},
  63. )
  64. self.assertTrue(user.is_superuser)
  65. @override_settings(AZUREAD_ADMIN_GROUP_ID='eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee')
  66. def test_fetch_permissions_not_admin(self):
  67. user = User()
  68. social_auth.fetch_azuread_permissions(
  69. strategy=self.strategy,
  70. details={},
  71. user=user,
  72. backend=self.backend,
  73. response={'access_token': self.access_token},
  74. )
  75. self.assertFalse(user.is_superuser)
  76. @override_settings(SOCIAL_AUTH_OKTA_OAUTH2_KEY='0000000000aaaaaaaaaa') # nosec
  77. @override_settings(SOCIAL_AUTH_OKTA_OAUTH2_SECRET='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=') # nosec
  78. @override_settings(SOCIAL_AUTH_OKTA_OAUTH2_API_URL='https://dev-000000.okta.com/oauth2') # nosec
  79. @override_settings(OKTA_OAUTH2_ADMIN_GROUP_NAME='admin-group')
  80. class TestOktaOAuth2SocialAuth(VCRTestCase):
  81. strategy = None
  82. backend = OktaOAuth2(strategy=strategy)
  83. access_token = 'censored'
  84. def test_fetch_permissions_is_admin(self):
  85. user = User()
  86. social_auth.fetch_okta_oauth2_permissions(
  87. strategy=self.strategy,
  88. details={},
  89. user=user,
  90. backend=self.backend,
  91. response={'access_token': self.access_token},
  92. )
  93. self.assertTrue(user.is_superuser)
  94. def test_fetch_permissions_not_admin(self):
  95. user = User()
  96. social_auth.fetch_okta_oauth2_permissions(
  97. strategy=self.strategy,
  98. details={},
  99. user=user,
  100. backend=self.backend,
  101. response={'access_token': self.access_token},
  102. )
  103. self.assertFalse(user.is_superuser)
  104. @override_settings(SOCIAL_AUTH_OKTA_OPENIDCONNECT_KEY='0000000000aaaaaaaaaa') # nosec
  105. @override_settings(SOCIAL_AUTH_OKTA_OPENIDCONNECT_SECRET='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=') # nosec
  106. @override_settings(SOCIAL_AUTH_OKTA_OPENIDCONNECT_API_URL='https://dev-000000.okta.com/oauth2') # nosec
  107. @override_settings(OKTA_OPENIDCONNECT_ADMIN_GROUP_NAME='admin-group')
  108. class TestOktaOpenIdConnectSocialAuth(VCRTestCase):
  109. strategy = None
  110. backend = OktaOpenIdConnect(strategy=strategy)
  111. access_token = 'censored'
  112. def test_fetch_permissions_is_admin(self):
  113. user = User()
  114. social_auth.fetch_okta_openidconnect_permissions(
  115. strategy=self.strategy,
  116. details={},
  117. user=user,
  118. backend=self.backend,
  119. response={'access_token': self.access_token},
  120. )
  121. self.assertTrue(user.is_superuser)
  122. def test_fetch_permissions_not_admin(self):
  123. user = User()
  124. social_auth.fetch_okta_openidconnect_permissions(
  125. strategy=self.strategy,
  126. details={},
  127. user=user,
  128. backend=self.backend,
  129. response={'access_token': self.access_token},
  130. )
  131. self.assertFalse(user.is_superuser)