test_social_pipeline.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  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.assertEqual(e.ban, {
  86. 'version': 0,
  87. 'ip': '188.1.2.3',
  88. 'expires_on': None,
  89. 'is_banned': True,
  90. 'message': None,
  91. })
  92. def test_exclude_staff(self):
  93. """pipeline excludes staff from bans"""
  94. self.user.is_staff = True
  95. self.user.save()
  96. Ban.objects.create(banned_value='188.*', check_type=Ban.IP)
  97. result = validate_ip_not_banned(MockStrategy(user_ip='188.1.2.3'), {}, GithubOAuth2, self.user)
  98. self.assertIsNone(result)
  99. class ValidateUserNotBannedTests(PipelineTestCase):
  100. def test_skip_if_user_not_set(self):
  101. """pipeline step is skipped if no user was passed"""
  102. result = associate_by_email(None, {}, GithubOAuth2)
  103. self.assertIsNone(result)
  104. def test_raise_if_banned(self):
  105. """pipeline raises if user's IP is banned"""
  106. Ban.objects.create(banned_value=self.user.username, check_type=Ban.USERNAME)
  107. try:
  108. validate_user_not_banned(MockStrategy(), {}, GithubOAuth2, self.user)
  109. self.fail("validate_ip_not_banned should raise SocialAuthBanned")
  110. except SocialAuthBanned as e:
  111. self.assertEqual(e.ban.user, self.user)
  112. def test_exclude_staff(self):
  113. """pipeline excludes staff from bans"""
  114. self.user.is_staff = True
  115. self.user.save()
  116. Ban.objects.create(banned_value=self.user.username, check_type=Ban.USERNAME)
  117. result = validate_user_not_banned(MockStrategy(), {}, GithubOAuth2, self.user)
  118. self.assertIsNone(result)