def ban_user(self): new_ban = Ban(banned_value=self.user.username, user_message=self.cleaned_data['user_message'], staff_message=self.cleaned_data['staff_message'], valid_until=self.cleaned_data['valid_until']) new_ban.save() Ban.objects.invalidate_cache()
def is_request_banned(self, request): ban = get_request_ip_ban(request) if ban: hydrated_ban = Ban(check_type=BAN_IP, user_message=ban['message'], expires_on=ban['expires_on']) raise Banned(hydrated_ban)
def test_banned(self): """banned exception is correctly handled""" ban = Ban(user_message="This is test ban!") response = exceptionhandler.handle_api_exception(Banned(ban), None) self.assertEqual(response.status_code, 403) self.assertEqual(response.data['detail']['html'], "<p>This is test ban!</p>") self.assertIn('expires_on', response.data)
def decorator(request, *args, **kwargs): ban = get_request_ip_ban(request) if ban: hydrated_ban = Ban( check_type=BAN_IP, user_message=ban['message'], expires_on=ban['expires_on']) raise Banned(hydrated_ban) else: return f(request, *args, **kwargs)
def validate_ip_not_banned(strategy, details, backend, user=None, *args, **kwargs): """Pipeline step that interrupts pipeline if found user is non-staff and IP banned""" if not user or user.is_staff: return None 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 handle(self, *args, **options): try: fake_bans_to_create = int(args[0]) except IndexError: fake_bans_to_create = 5 except ValueError: self.stderr.write("\nOptional argument should be integer.") sys.exit(1) fake = Factory.create() message = 'Creating %s fake bans...\n' self.stdout.write(message % fake_bans_to_create) message = '\n\nSuccessfully created %s fake bans' created_count = 0 show_progress(self, created_count, fake_bans_to_create) for i in range(fake_bans_to_create): ban = Ban(check_type=random.randint(Ban.USERNAME, Ban.IP)) ban.banned_value = create_fake_test(fake, ban.check_type) if random.randint(0, 10) == 0: ban.user_message = fake.sentence() if random.randint(0, 10) == 0: ban.staff_message = fake.sentence() if random.randint(0, 1): # Lets make ban temporary ban_length = timedelta(days=random.randint(0, 300)) if random.randint(0, 1): ban.valid_until = timezone.now().date() - ban_length else: ban.valid_until = timezone.now().date() + ban_length ban.save() created_count += 1 show_progress(self, created_count, fake_bans_to_create) self.stdout.write(message % created_count)
def handle(self, *args, **options): try: fake_bans_to_create = int(args[0]) except IndexError: fake_bans_to_create = 5 except ValueError: self.stderr.write("\nOptional argument should be integer.") sys.exit(1) fake = Factory.create() message = 'Creating %s fake bans...\n' self.stdout.write(message % fake_bans_to_create) message = '\n\nSuccessfully created %s fake bans' created_count = 0 show_progress(self, created_count, fake_bans_to_create) for i in xrange(fake_bans_to_create): ban = Ban(check_type=random.randint(BAN_USERNAME, BAN_IP)) ban.banned_value = create_fake_test(fake, ban.check_type) if random.randint(0, 10) == 0: ban.user_message = fake.sentence() if random.randint(0, 10) == 0: ban.staff_message = fake.sentence() if random.randint(0, 1): # Lets make ban temporary ban_length = timedelta(days=random.randint(0, 300)) if random.randint(0, 1): ban.valid_until = timezone.now().date() - ban_length else: ban.valid_until = timezone.now().date() + ban_length ban.save() created_count += 1 show_progress(self, created_count, fake_bans_to_create) self.stdout.write(message % created_count)
def raise_misago_banned(request): ban = Ban(user_message="Banned for test!") raise Banned(ban)
def raise_social_auth_banned(request): ban = Ban(user_message="Banned in auth!") raise SocialAuthBanned(GithubOAuth2, ban)
def setUp(self): Ban.objects.bulk_create([ Ban(check_type=Ban.USERNAME, banned_value='bob'), Ban(check_type=Ban.EMAIL, banned_value='*****@*****.**'), Ban(check_type=Ban.IP, banned_value='127.0.0.1'), ])
def test_check_value_ends_witch(self): """ban correctly tests given values""" test_ban = Ban(banned_value='*bob') self.assertTrue(test_ban.check_value('lebob')) self.assertFalse(test_ban.check_value('bobby'))
def test_check_value_middle_match(self): """ban correctly tests given values""" test_ban = Ban(banned_value='b*b') self.assertTrue(test_ban.check_value('bob')) self.assertFalse(test_ban.check_value('bobby'))
def test_check_value_starts_with(self): """ban correctly tests given values""" test_ban = Ban(banned_value='bob*') self.assertTrue(test_ban.check_value('bob')) self.assertTrue(test_ban.check_value('bobby'))
def setUp(self): Ban.objects.bulk_create([ Ban(test=BAN_USERNAME, banned_value='bob'), Ban(test=BAN_EMAIL, banned_value='*****@*****.**'), Ban(test=BAN_IP, banned_value='127.0.0.1'), ])
def test_test_value_literal(self): """ban correctly tests given values""" test_ban = Ban(banned_value='bob') self.assertTrue(test_ban.test_value('bob')) self.assertFalse(test_ban.test_value('bobby'))