Просмотр исходного кода

Fix bans to be consistent with rest of the app

Rafał Pitoń 7 лет назад
Родитель
Сommit
c4e904641e

+ 3 - 1
misago/users/decorators.py

@@ -36,7 +36,9 @@ def deny_banned_ips(f):
         ban = get_request_ip_ban(request)
         if ban:
             hydrated_ban = Ban(
-                check_type=Ban.IP, user_message=ban['message'], expires_on=ban['expires_on']
+                check_type=Ban.IP,
+                user_message=ban['message'],
+                expires_on=ban['expires_on'],
             )
             raise Banned(hydrated_ban)
         else:

+ 9 - 3
misago/users/social/pipeline.py

@@ -3,6 +3,7 @@ from django.utils.translation import ugettext as _
 
 from misago.core.exceptions import SocialAuthFailed, SocialAuthBanned
 
+from misago.users.models import Ban
 from misago.users.bans import get_request_ip_ban, get_user_ban
 
 from .utils import get_social_auth_backend_name
@@ -16,9 +17,14 @@ def validate_ip_not_banned(strategy, details, backend, user=None, *args, **kwarg
     if user and user.is_staff:
         return None
     
-    ip_ban = get_request_ip_ban(strategy.request)
-    if ip_ban:
-        raise SocialAuthBanned(backend, ip_ban)
+    ban = get_request_ip_ban(strategy.request)
+    if ban:
+        hydrated_ban = Ban(
+            check_type=Ban.IP,
+            user_message=ban['message'],
+            expires_on=ban['expires_on'],
+        )
+        raise SocialAuthBanned(backend, hydrated_ban)
 
 
 def validate_user_not_banned(strategy, details, backend, user=None, *args, **kwargs):

+ 3 - 8
misago/users/tests/test_social_pipeline.py

@@ -3,7 +3,7 @@ from social_core.backends.github import GithubOAuth2
 
 from misago.core.exceptions import SocialAuthFailed, SocialAuthBanned
 
-from misago.users.models import Ban
+from misago.users.models import Ban, BanCache
 from misago.users.social.pipeline import (
     associate_by_email, validate_ip_not_banned, validate_user_not_banned
 )
@@ -107,13 +107,7 @@ class ValidateIpNotBannedTests(PipelineTestCase):
             validate_ip_not_banned(MockStrategy(user_ip='188.1.2.3'), {}, GithubOAuth2, self.user)
             self.fail("validate_ip_not_banned should raise SocialAuthBanned")
         except SocialAuthBanned as e:
-            self.assertEqual(e.ban, {
-                'version': 0,
-                'ip': '188.1.2.3',
-                'expires_on': None,
-                'is_banned': True,
-                'message': None,
-            })
+            self.assertTrue(isinstance(e.ban, Ban))
 
     def test_exclude_staff(self):
         """pipeline excludes staff from bans"""
@@ -141,6 +135,7 @@ class ValidateUserNotBannedTests(PipelineTestCase):
             self.fail("validate_ip_not_banned should raise SocialAuthBanned")
         except SocialAuthBanned as e:
             self.assertEqual(e.ban.user, self.user)
+            self.assertTrue(isinstance(e.ban, BanCache))
 
     def test_exclude_staff(self):
         """pipeline excludes staff from bans"""