test_social_pipeline.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from django.contrib.auth import get_user_model
  2. from social_core.backends.github import GithubOAuth2
  3. from misago.core.exceptions import SocialAuthFailed, SocialAuthBanned
  4. from misago.users.models import Ban, BanCache
  5. from misago.users.social.pipeline import (
  6. associate_by_email, validate_ip_not_banned, validate_user_not_banned
  7. )
  8. from misago.users.testutils import UserTestCase
  9. UserModel = get_user_model()
  10. class MockRequest(object):
  11. def __init__(self, user_ip='0.0.0.0'):
  12. self.session = {}
  13. self.user_ip = user_ip
  14. class MockStrategy(object):
  15. def __init__(self, user_ip='0.0.0.0'):
  16. self.request = MockRequest(user_ip=user_ip)
  17. class PipelineTestCase(UserTestCase):
  18. def get_initial_user(self):
  19. self.user = self.get_authenticated_user()
  20. class AssociateByEmailTests(PipelineTestCase):
  21. def test_skip_if_user_is_already_set(self):
  22. """pipeline step is skipped if user was found by previous step"""
  23. result = associate_by_email(None, {}, GithubOAuth2, self.user)
  24. self.assertIsNone(result)
  25. def test_skip_if_no_email_passed(self):
  26. """pipeline step is skipped if no email was passed"""
  27. result = associate_by_email(None, {}, GithubOAuth2)
  28. self.assertIsNone(result)
  29. def test_skip_if_user_with_email_not_found(self):
  30. """pipeline step is skipped if no email was passed"""
  31. result = associate_by_email(None, {'email': 'not@found.com'}, GithubOAuth2)
  32. self.assertIsNone(result)
  33. def test_raise_if_user_is_inactive(self):
  34. """pipeline raises if user was inactive"""
  35. self.user.is_active = False
  36. self.user.save()
  37. try:
  38. associate_by_email(None, {'email': self.user.email}, GithubOAuth2)
  39. self.fail("associate_by_email should raise SocialAuthFailed")
  40. except SocialAuthFailed as e:
  41. self.assertEqual(
  42. e.message,
  43. (
  44. "The e-mail address associated with your GitHub account is not available for "
  45. "use on this site."
  46. ),
  47. )
  48. def test_raise_if_user_needs_admin_activation(self):
  49. """pipeline raises if user needs admin activation"""
  50. self.user.requires_activation = UserModel.ACTIVATION_ADMIN
  51. self.user.save()
  52. try:
  53. associate_by_email(None, {'email': self.user.email}, GithubOAuth2)
  54. self.fail("associate_by_email should raise SocialAuthFailed")
  55. except SocialAuthFailed as e:
  56. self.assertEqual(
  57. e.message,
  58. (
  59. "Your account has to be activated by site administrator before you will be "
  60. "able to sign in with GitHub."
  61. ),
  62. )
  63. def test_return_user(self):
  64. """pipeline returns user if email was found"""
  65. result = associate_by_email(None, {'email': self.user.email}, GithubOAuth2)
  66. self.assertEqual(result, {'user': self.user, 'is_new': False})
  67. def test_return_user_email_inactive(self):
  68. """pipeline returns user even if they didn't activate their account manually"""
  69. self.user.requires_activation = UserModel.ACTIVATION_USER
  70. self.user.save()
  71. result = associate_by_email(None, {'email': self.user.email}, GithubOAuth2)
  72. self.assertEqual(result, {'user': self.user, 'is_new': False})
  73. class ValidateIpNotBannedTests(PipelineTestCase):
  74. def test_skip_if_user_not_set(self):
  75. """pipeline step is skipped if no user was passed"""
  76. result = associate_by_email(None, {}, GithubOAuth2)
  77. self.assertIsNone(result)
  78. def test_raise_if_banned(self):
  79. """pipeline raises if user's IP is banned"""
  80. Ban.objects.create(banned_value='188.*', check_type=Ban.IP)
  81. try:
  82. validate_ip_not_banned(MockStrategy(user_ip='188.1.2.3'), {}, GithubOAuth2, self.user)
  83. self.fail("validate_ip_not_banned should raise SocialAuthBanned")
  84. except SocialAuthBanned as e:
  85. self.assertTrue(isinstance(e.ban, Ban))
  86. def test_exclude_staff(self):
  87. """pipeline excludes staff from bans"""
  88. self.user.is_staff = True
  89. self.user.save()
  90. Ban.objects.create(banned_value='188.*', check_type=Ban.IP)
  91. result = validate_ip_not_banned(
  92. MockStrategy(user_ip='188.1.2.3'), {}, GithubOAuth2, self.user)
  93. self.assertIsNone(result)
  94. class ValidateUserNotBannedTests(PipelineTestCase):
  95. def test_skip_if_user_not_set(self):
  96. """pipeline step is skipped if no user was passed"""
  97. result = associate_by_email(None, {}, GithubOAuth2)
  98. self.assertIsNone(result)
  99. def test_raise_if_banned(self):
  100. """pipeline raises if user's IP is banned"""
  101. Ban.objects.create(banned_value=self.user.username, check_type=Ban.USERNAME)
  102. try:
  103. validate_user_not_banned(MockStrategy(), {}, GithubOAuth2, self.user)
  104. self.fail("validate_ip_not_banned should raise SocialAuthBanned")
  105. except SocialAuthBanned as e:
  106. self.assertEqual(e.ban.user, self.user)
  107. self.assertTrue(isinstance(e.ban, BanCache))
  108. def test_exclude_staff(self):
  109. """pipeline excludes staff from bans"""
  110. self.user.is_staff = True
  111. self.user.save()
  112. Ban.objects.create(banned_value=self.user.username, check_type=Ban.USERNAME)
  113. result = validate_user_not_banned(MockStrategy(), {}, GithubOAuth2, self.user)
  114. self.assertIsNone(result)