test_validators.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_email, validate_email_available,
  7. validate_password,
  8. validate_username,
  9. validate_username_available,
  10. validate_username_content,
  11. validate_username_length)
  12. class ValidateEmailAvailableTests(TestCase):
  13. def setUp(self):
  14. User = get_user_model()
  15. self.test_user = User.objects.create_user('EricTheFish',
  16. 'eric@test.com',
  17. 'pass123')
  18. def test_valid_email(self):
  19. """validate_email_available allows available emails"""
  20. validate_email_available('bob@boberson.com')
  21. def test_invalid_email(self):
  22. """validate_email_available disallows unvailable emails"""
  23. with self.assertRaises(ValidationError):
  24. validate_email_available(self.test_user.email)
  25. class ValidateEmailTests(TestCase):
  26. def test_validate_email(self):
  27. """validate_email has no crashes"""
  28. validate_email('bob@boberson.com')
  29. with self.assertRaises(ValidationError):
  30. validate_email('*')
  31. class ValidatePasswordTests(TestCase):
  32. def test_valid_password(self):
  33. """validate_password allows valid password"""
  34. validate_password('A' * (settings.password_length_min + 1))
  35. def test_invalid_name(self):
  36. """validate_password disallows invalid password"""
  37. with self.assertRaises(ValidationError):
  38. validate_password('A' * (settings.password_length_min - 1))
  39. class ValidateUsernameTests(TestCase):
  40. def test_validate_username(self):
  41. """validate_username has no crashes"""
  42. validate_username('LeBob')
  43. with self.assertRaises(ValidationError):
  44. validate_username('*')
  45. class ValidateUsernameAvailableTests(TestCase):
  46. def setUp(self):
  47. User = get_user_model()
  48. self.test_user = User.objects.create_user('EricTheFish',
  49. 'eric@test.com',
  50. 'pass123')
  51. def test_valid_name(self):
  52. """validate_username_available allows available names"""
  53. validate_username_available('BobBoberson')
  54. def test_invalid_name(self):
  55. """validate_username_available disallows unvailable names"""
  56. with self.assertRaises(ValidationError):
  57. validate_username_available(self.test_user.username)
  58. class ValidateUsernameContentTests(TestCase):
  59. def test_valid_name(self):
  60. """validate_username_content allows valid names"""
  61. validate_username_content('123')
  62. validate_username_content('Bob')
  63. validate_username_content('Bob123')
  64. def test_invalid_name(self):
  65. """validate_username_content disallows invalid names"""
  66. with self.assertRaises(ValidationError):
  67. validate_username_content('!')
  68. with self.assertRaises(ValidationError):
  69. validate_username_content('Bob!')
  70. with self.assertRaises(ValidationError):
  71. validate_username_content('Bob Boberson')
  72. with self.assertRaises(ValidationError):
  73. validate_username_content(u'Rafał')
  74. with self.assertRaises(ValidationError):
  75. validate_username_content(u'初音 ミク')
  76. class ValidateUsernameLengthTests(TestCase):
  77. def test_valid_name(self):
  78. """validate_username_length allows valid names"""
  79. validate_username_length('a' * settings.username_length_min)
  80. validate_username_length('a' * settings.username_length_max)
  81. def test_invalid_name(self):
  82. """validate_username_length disallows invalid names"""
  83. with self.assertRaises(ValidationError):
  84. validate_username_length('a' * (settings.username_length_min - 1))
  85. with self.assertRaises(ValidationError):
  86. validate_username_length('a' * (settings.username_length_max + 1))