def test_get_username_ban(self): """get_username_ban returns valid ban""" nonexistent_ban = get_username_ban('nonexistent') self.assertIsNone(nonexistent_ban) Ban.objects.create( banned_value='expired', expires_on=timezone.now() - timedelta(days=7) ) expired_ban = get_username_ban('expired') self.assertIsNone(expired_ban) Ban.objects.create( banned_value='wrongtype', check_type=Ban.EMAIL ) wrong_type_ban = get_username_ban('wrongtype') self.assertIsNone(wrong_type_ban) valid_ban = Ban.objects.create( banned_value='admi*', expires_on=timezone.now() + timedelta(days=7) ) self.assertEqual(get_username_ban('admiral').pk, valid_ban.pk)
def validate_username_banned(value): ban = get_username_ban(value) if ban: if ban.user_message: raise ValidationError(ban.user_message) else: raise ValidationError(_("This username is not allowed."))
def validate_username(self, data): ban = get_username_ban(data, registration_only=True) if ban: if ban.user_message: raise ValidationError(ban.user_message) else: raise ValidationError(_("This usernane is not allowed.")) return data
def test_get_username_ban(self): """get_username_ban returns valid ban""" nonexistent_ban = get_username_ban("nonexistent") self.assertIsNone(nonexistent_ban) Ban.objects.create(banned_value="expired", expires_on=timezone.now() - timedelta(days=7)) expired_ban = get_username_ban("expired") self.assertIsNone(expired_ban) Ban.objects.create(banned_value="wrongtype", check_type=BAN_EMAIL) wrong_type_ban = get_username_ban("wrongtype") self.assertIsNone(wrong_type_ban) valid_ban = Ban.objects.create(banned_value="admi*", expires_on=timezone.now() + timedelta(days=7)) self.assertEqual(get_username_ban("admiral").pk, valid_ban.pk)
def clean_username(self): data = self.cleaned_data['username'] ban = get_username_ban(data, registration_only=True) if ban: if ban.user_message: raise ValidationError(ban.user_message) else: raise ValidationError(_("This usernane is not allowed.")) return data
def test_get_username_ban(self): """get_username_ban returns valid ban""" nonexistent_ban = get_username_ban('nonexistent') self.assertIsNone(nonexistent_ban) Ban.objects.create(banned_value='expired', expires_on=timezone.now() - timedelta(days=7)) expired_ban = get_username_ban('expired') self.assertIsNone(expired_ban) Ban.objects.create(banned_value='wrongtype', check_type=BAN_EMAIL) wrong_type_ban = get_username_ban('wrongtype') self.assertIsNone(wrong_type_ban) valid_ban = Ban.objects.create( banned_value='admi*', expires_on=timezone.now() + timedelta(days=7)) self.assertEqual(get_username_ban('admiral').pk, valid_ban.pk)
def test_get_username_ban(self): """get_username_ban returns valid ban""" nonexistent_ban = get_username_ban('nonexistent') self.assertIsNone(nonexistent_ban) Ban.objects.create( banned_value='expired', expires_on=timezone.now() - timedelta(days=7), ) expired_ban = get_username_ban('expired') self.assertIsNone(expired_ban) Ban.objects.create( banned_value='wrongtype', check_type=Ban.EMAIL, ) wrong_type_ban = get_username_ban('wrongtype') self.assertIsNone(wrong_type_ban) valid_ban = Ban.objects.create( banned_value='admi*', expires_on=timezone.now() + timedelta(days=7), ) self.assertEqual(get_username_ban('admiral').pk, valid_ban.pk) regitration_ban = Ban.objects.create( banned_value='bob*', expires_on=timezone.now() + timedelta(days=7), registration_only=True, ) self.assertIsNone(get_username_ban('boberson')) self.assertEqual(get_username_ban('boberson', True).pk, regitration_ban.pk)