test_user_api.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from sqlalchemy.orm.exc import NoResultFound
  4. import transaction
  5. from tracim.lib.core.user import UserApi
  6. from tracim.tests import DefaultTest
  7. from tracim.tests import eq_
  8. class TestUserApi(DefaultTest):
  9. def test_unit__create_minimal_user__ok__nominal_case(self):
  10. api = UserApi(
  11. current_user=None,
  12. session=self.session,
  13. config=self.config,
  14. )
  15. u = api.create_minimal_user('bob@bob')
  16. assert u.email == 'bob@bob'
  17. assert u.display_name is None
  18. def test_unit__create_minimal_user_and_update__ok__nominal_case(self):
  19. api = UserApi(
  20. current_user=None,
  21. session=self.session,
  22. config=self.config,
  23. )
  24. u = api.create_minimal_user('bob@bob')
  25. api.update(u, 'bob', 'bob@bob', 'pass', do_save=True)
  26. nu = api.get_one_by_email('bob@bob')
  27. assert nu is not None
  28. assert nu.email == 'bob@bob'
  29. assert nu.display_name == 'bob'
  30. assert nu.validate_password('pass')
  31. def test__unit__create__user__ok_nominal_case(self):
  32. api = UserApi(
  33. current_user=None,
  34. session=self.session,
  35. config=self.config,
  36. )
  37. u = api.create_user(
  38. email='bob@bob',
  39. password='pass',
  40. name='bob',
  41. timezone='+2',
  42. do_save=True,
  43. do_notify=False,
  44. )
  45. assert u is not None
  46. assert u.email == "bob@bob"
  47. assert u.validate_password('pass')
  48. assert u.display_name == 'bob'
  49. assert u.timezone == '+2'
  50. def test_unit__user_with_email_exists__ok__nominal_case(self):
  51. api = UserApi(
  52. current_user=None,
  53. session=self.session,
  54. config=self.config,
  55. )
  56. u = api.create_minimal_user('bibi@bibi')
  57. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  58. transaction.commit()
  59. eq_(True, api.user_with_email_exists('bibi@bibi'))
  60. eq_(False, api.user_with_email_exists('unknown'))
  61. def test_unit__get_one_by_email__ok__nominal_case(self):
  62. api = UserApi(
  63. current_user=None,
  64. session=self.session,
  65. config=self.config,
  66. )
  67. u = api.create_minimal_user('bibi@bibi')
  68. self.session.flush()
  69. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  70. uid = u.user_id
  71. transaction.commit()
  72. eq_(uid, api.get_one_by_email('bibi@bibi').user_id)
  73. def test_unit__get_one_by_email__ok__user_not_found(self):
  74. api = UserApi(
  75. current_user=None,
  76. session=self.session,
  77. config=self.config,
  78. )
  79. with pytest.raises(NoResultFound):
  80. api.get_one_by_email('unknown')
  81. def test_unit__get_all__ok__nominal_case(self):
  82. api = UserApi(
  83. current_user=None,
  84. session=self.session,
  85. config=self.config,
  86. )
  87. u1 = api.create_minimal_user('bibi@bibi')
  88. users = api.get_all()
  89. # u1 + Admin user from BaseFixture
  90. assert 2 == len(users)
  91. def test_unit__get_one__ok__nominal_case(self):
  92. api = UserApi(
  93. current_user=None,
  94. session=self.session,
  95. config=self.config,
  96. )
  97. u = api.create_minimal_user('titi@titi')
  98. api.update(u, 'titi', 'titi@titi', 'pass', do_save=True)
  99. one = api.get_one(u.user_id)
  100. eq_(u.user_id, one.user_id)