test_update_validator.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from uuid import uuid4
  2. import pytest
  3. from requests.exceptions import RequestException
  4. from flaskbb.core.exceptions import StopValidation, ValidationError
  5. from flaskbb.core.user.update import EmailUpdate, PasswordUpdate, UserDetailsChange
  6. from flaskbb.user.models import User
  7. from flaskbb.user.services import validators
  8. pytestmark = pytest.mark.usefixtures("default_settings")
  9. class TestEmailsMustBeDifferent(object):
  10. def test_raises_if_emails_match(self, Fred):
  11. matching_emails = EmailUpdate("same@email.example", Fred.email)
  12. with pytest.raises(ValidationError) as excinfo:
  13. validators.EmailsMustBeDifferent().validate(Fred, matching_emails)
  14. assert "New email address must be different" in str(excinfo.value)
  15. def test_doesnt_raise_if_emails_are_different(self, Fred):
  16. different_emails = EmailUpdate("old@email.example", "new@email.example")
  17. validators.EmailsMustBeDifferent().validate(Fred, different_emails)
  18. class TestPasswordsMustBeDifferent(object):
  19. def test_raises_if_passwords_are_the_same(self, Fred):
  20. change = PasswordUpdate("fred", "fred")
  21. with pytest.raises(ValidationError) as excinfo:
  22. validators.PasswordsMustBeDifferent().validate(Fred, change)
  23. assert "New password must be different" in str(excinfo.value)
  24. def test_doesnt_raise_if_passwords_dont_match(self, Fred):
  25. change = PasswordUpdate("fred", "actuallycompletelydifferent")
  26. validators.PasswordsMustBeDifferent().validate(Fred, change)
  27. class TestCantShareEmailValidator(object):
  28. def test_raises_if_email_is_already_registered(self, Fred, user):
  29. change = EmailUpdate("old@email.example", user.email)
  30. with pytest.raises(ValidationError) as excinfo:
  31. validators.CantShareEmailValidator(User).validate(Fred, change)
  32. assert "is already registered" in str(excinfo.value)
  33. def test_doesnt_raise_if_email_isnt_registered(self, Fred):
  34. change = EmailUpdate("old@email.example", "new@email.example")
  35. validators.CantShareEmailValidator(User).validate(Fred, change)
  36. class TestOldEmailMustMatchValidator(object):
  37. def test_raises_if_old_email_doesnt_match(self, Fred):
  38. change = EmailUpdate("not@the.same.one.bit", "probably@real.email.provider")
  39. with pytest.raises(StopValidation) as excinfo:
  40. validators.OldEmailMustMatch().validate(Fred, change)
  41. assert [("old_email", "Old email does not match")] == excinfo.value.reasons
  42. def test_doesnt_raise_if_old_email_matches(self, Fred):
  43. change = EmailUpdate(Fred.email, "probably@real.email.provider")
  44. validators.OldEmailMustMatch().validate(Fred, change)
  45. class TestOldPasswordMustMatchValidator(object):
  46. def test_raises_if_old_password_doesnt_match(self, Fred):
  47. change = PasswordUpdate(str(uuid4()), str(uuid4()))
  48. with pytest.raises(StopValidation) as excinfo:
  49. validators.OldPasswordMustMatch().validate(Fred, change)
  50. assert [("old_password", "Old password is wrong")] == excinfo.value.reasons
  51. def test_doesnt_raise_if_old_passwords_match(self, Fred):
  52. change = PasswordUpdate("fred", str(uuid4()))
  53. validators.OldPasswordMustMatch().validate(Fred, change)
  54. class TestValidateAvatarURL(object):
  55. def test_passes_if_avatar_url_is_none(self, Fred):
  56. change = UserDetailsChange()
  57. validators.ValidateAvatarURL().validate(Fred, change)
  58. def test_raises_if_check_raises_requests_error(self, Fred, responses):
  59. url = "http://notfake.example/image.png"
  60. change = UserDetailsChange(avatar=url)
  61. responses.add(responses.GET, url=url, body=RequestException())
  62. with pytest.raises(ValidationError) as excinfo:
  63. validators.ValidateAvatarURL().validate(Fred, change)
  64. assert excinfo.value.attribute == "avatar"
  65. assert excinfo.value.reason == "Could not retrieve avatar"
  66. def test_raises_if_image_doesnt_pass_checks(self, image_too_tall, Fred, responses):
  67. change = UserDetailsChange(avatar=image_too_tall.url)
  68. responses.add(image_too_tall)
  69. with pytest.raises(ValidationError) as excinfo:
  70. validators.ValidateAvatarURL().validate(Fred, change)
  71. assert "too high" in excinfo.value.reason
  72. def tests_passes_if_image_is_just_right(self, image_just_right, Fred, responses):
  73. change = UserDetailsChange(avatar=image_just_right.url)
  74. responses.add(image_just_right)
  75. validators.ValidateAvatarURL().validate(Fred, change)