test_validators.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #-*- coding: utf-8 -*-
  2. from django.contrib.auth import get_user_model
  3. from django.core.exceptions import ValidationError
  4. from django.test import TestCase
  5. from misago.conf import settings
  6. from misago.users.validators import (validate_username_available,
  7. validate_username_content,
  8. validate_username_length,
  9. validate_username)
  10. class ValidateUsernameAvailableTests(TestCase):
  11. def setUp(self):
  12. User = get_user_model()
  13. self.test_user = User.objects.create_user('EricTheFish',
  14. 'eric@test.com',
  15. 'pass123')
  16. def test_valid_names(self):
  17. """validate_username_available allows available names"""
  18. validate_username_available('BobBoberson')
  19. def test_invalid_names(self):
  20. """validate_username_available disallows unvailable names"""
  21. with self.assertRaises(ValidationError):
  22. validate_username_available(self.test_user.username)
  23. class ValidateUsernameContentTests(TestCase):
  24. def test_valid_names(self):
  25. """validate_username_content allows valid names"""
  26. validate_username_content('123')
  27. validate_username_content('Bob')
  28. validate_username_content('Bob123')
  29. def test_invalid_names(self):
  30. """validate_username_content disallows invalid names"""
  31. with self.assertRaises(ValidationError):
  32. validate_username_content('!')
  33. with self.assertRaises(ValidationError):
  34. validate_username_content('Bob!')
  35. with self.assertRaises(ValidationError):
  36. validate_username_content('Bob Boberson')
  37. with self.assertRaises(ValidationError):
  38. validate_username_content(u'Rafał')
  39. with self.assertRaises(ValidationError):
  40. validate_username_content(u'初音 ミク')
  41. class ValidateUsernameLengthTests(TestCase):
  42. def test_valid_names(self):
  43. """validate_username_length allows valid names"""
  44. validate_username_length('a' * settings.username_length_min)
  45. validate_username_length('a' * settings.username_length_max)
  46. def test_invalid_names(self):
  47. """validate_username_length disallows invalid names"""
  48. with self.assertRaises(ValidationError):
  49. validate_username_length('a' * (settings.username_length_min - 1))
  50. with self.assertRaises(ValidationError):
  51. validate_username_length('a' * (settings.username_length_max + 1))
  52. class ValidateUsernameTests(TestCase):
  53. def test_validate_username(self):
  54. """validate_username has no crashes"""
  55. validate_username('LeBob')