test_validators.py 4.4 KB

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