test_registration.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import pytest
  2. from flaskbb.core.auth import registration
  3. from flaskbb.core.user.repo import UserRepository
  4. class RaisingValidator(registration.UserValidator):
  5. def validate(self, user_info):
  6. raise registration.UserRegistrationError(
  7. 'test', 'just a little whoopsie-diddle')
  8. def test_doesnt_register_user_if_validator_fails_with_UserRegistrationError(
  9. mocker):
  10. repo = mocker.Mock(UserRepository)
  11. service = registration.RegistrationService([RaisingValidator()], repo)
  12. with pytest.raises(registration.StopRegistration):
  13. service.register(
  14. registration.UserRegistrationInfo(
  15. username='fred',
  16. password='lol',
  17. email='fred@fred.fred',
  18. language='fredspeak',
  19. group=4))
  20. repo.add.assert_not_called()
  21. def test_gathers_up_all_errors_during_registration(mocker):
  22. repo = mocker.Mock(UserRepository)
  23. service = registration.RegistrationService(
  24. [RaisingValidator(), RaisingValidator()], repo)
  25. with pytest.raises(registration.StopRegistration) as excinfo:
  26. service.register(
  27. registration.UserRegistrationInfo(
  28. username='fred',
  29. password='lol',
  30. email='fred@fred.fred',
  31. language='fredspeak',
  32. group=4))
  33. repo.add.assert_not_called()
  34. assert len(excinfo.value.reasons) == 2
  35. assert all(('test', 'just a little whoopsie-diddle') == r
  36. for r in excinfo.value.reasons)
  37. def test_registers_user_if_no_errors_occurs(mocker):
  38. repo = mocker.Mock(UserRepository)
  39. service = registration.RegistrationService([], repo)
  40. user_info = registration.UserRegistrationInfo(
  41. username='fred',
  42. password='lol',
  43. email='fred@fred.fred',
  44. language='fredspeak',
  45. group=4)
  46. service.register(user_info)
  47. repo.add.assert_called_with(user_info)