test_commands.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import subprocess
  4. import pytest
  5. import transaction
  6. from pkg_resources import load_entry_point
  7. from sqlalchemy.orm.exc import NoResultFound
  8. import tracim
  9. from tracim.command import TracimCLI
  10. from tracim.command.user import UserCommand
  11. from tracim.exceptions import UserAlreadyExistError, BadCommandError, \
  12. GroupNotExist
  13. from tracim.lib.core.user import UserApi
  14. from tracim.tests import CommandFunctionalTest
  15. class TestCommands(CommandFunctionalTest):
  16. """
  17. Test tracimcli command line ui.
  18. """
  19. config_section = 'app:command_test'
  20. def test_func__check_commands_list__ok__nominal_case(self) -> None:
  21. """
  22. Test listing of tracimcli command: Tracim commands must be listed
  23. :return:
  24. """
  25. os.chdir(os.path.dirname(tracim.__file__) + '/../')
  26. output = subprocess.check_output(["tracimcli", "-h"])
  27. output = output.decode('utf-8')
  28. assert output.find('user create') > 0
  29. assert output.find('user update') > 0
  30. assert output.find('db init') > 0
  31. assert output.find('db delete') > 0
  32. assert output.find('webdav start') > 0
  33. def test_func__user_create_command__ok__nominal_case(self) -> None:
  34. """
  35. Test User creation
  36. """
  37. api = UserApi(
  38. current_user=None,
  39. session=self.session,
  40. config=self.app_config,
  41. )
  42. with pytest.raises(NoResultFound):
  43. api.get_one_by_email('command_test@user')
  44. app = TracimCLI()
  45. result = app.run([
  46. 'user', 'create',
  47. '-c', 'tests_configs.ini#command_test',
  48. '-l', 'command_test@user',
  49. '-p', 'new_password',
  50. '--debug',
  51. ])
  52. new_user = api.get_one_by_email('command_test@user')
  53. assert new_user.email == 'command_test@user'
  54. assert new_user.validate_password('new_password')
  55. assert new_user.profile.name == 'users'
  56. def test_func__user_create_command__ok__in_admin_group(self) -> None:
  57. """
  58. Test User creation with admin as group
  59. """
  60. api = UserApi(
  61. current_user=None,
  62. session=self.session,
  63. config=self.app_config,
  64. )
  65. with pytest.raises(NoResultFound):
  66. api.get_one_by_email('command_test@user')
  67. app = TracimCLI()
  68. result = app.run([
  69. 'user', 'create',
  70. '-c', 'tests_configs.ini#command_test',
  71. '-l', 'command_test@user',
  72. '-p', 'new_password',
  73. '-g', 'administrators',
  74. '--debug',
  75. ])
  76. new_user = api.get_one_by_email('command_test@user')
  77. assert new_user.email == 'command_test@user'
  78. assert new_user.validate_password('new_password')
  79. assert new_user.profile.name == 'administrators'
  80. def test_func__user_create_command__err__in_unknown_group(self) -> None:
  81. """
  82. Test User creation with an unknown group
  83. """
  84. api = UserApi(
  85. current_user=None,
  86. session=self.session,
  87. config=self.app_config,
  88. )
  89. app = TracimCLI()
  90. with pytest.raises(GroupNotExist):
  91. result = app.run([
  92. 'user', 'create',
  93. '-c', 'tests_configs.ini#command_test',
  94. '-l', 'command_test@user',
  95. '-p', 'new_password',
  96. '-g', 'unknown',
  97. '--debug',
  98. ])
  99. def test_func__user_create_command__err_user_already_exist(self) -> None:
  100. """
  101. Test User creation with existing user login
  102. """
  103. api = UserApi(
  104. current_user=None,
  105. session=self.session,
  106. config=self.app_config,
  107. )
  108. app = TracimCLI()
  109. with pytest.raises(UserAlreadyExistError):
  110. result = app.run([
  111. '--debug',
  112. 'user', 'create',
  113. '-c', 'tests_configs.ini#command_test',
  114. '-l', 'admin@admin.admin',
  115. '-p', 'new_password',
  116. '--debug',
  117. ])
  118. def test_func__user_create_command__err__password_required(self) -> None:
  119. """
  120. Test User creation without filling password
  121. """
  122. api = UserApi(
  123. current_user=None,
  124. session=self.session,
  125. config=self.app_config,
  126. )
  127. app = TracimCLI()
  128. with pytest.raises(BadCommandError):
  129. result = app.run([
  130. '--debug',
  131. 'user', 'create',
  132. '-c', 'tests_configs.ini#command_test',
  133. '-l', 'admin@admin.admin',
  134. '--debug',
  135. ])
  136. def test_func__user_update_command__ok__nominal_case(self) -> None:
  137. """
  138. Test user password update
  139. """
  140. api = UserApi(
  141. current_user=None,
  142. session=self.session,
  143. config=self.app_config,
  144. )
  145. user = api.get_one_by_email('admin@admin.admin')
  146. assert user.email == 'admin@admin.admin'
  147. assert user.validate_password('admin@admin.admin')
  148. assert not user.validate_password('new_password')
  149. app = TracimCLI()
  150. result = app.run([
  151. 'user', 'update',
  152. '-c', 'tests_configs.ini#command_test',
  153. '-l', 'admin@admin.admin',
  154. '-p', 'new_password',
  155. '--debug',
  156. ])
  157. new_user = api.get_one_by_email('admin@admin.admin')
  158. assert new_user.email == 'admin@admin.admin'
  159. assert new_user.validate_password('new_password')
  160. assert not new_user.validate_password('admin@admin.admin')
  161. def test_func__user_update_command__ok__remove_group(self) -> None:
  162. """
  163. Test user password update
  164. """
  165. api = UserApi(
  166. current_user=None,
  167. session=self.session,
  168. config=self.app_config,
  169. )
  170. user = api.get_one_by_email('admin@admin.admin')
  171. assert user.email == 'admin@admin.admin'
  172. assert user.validate_password('admin@admin.admin')
  173. assert not user.validate_password('new_password')
  174. assert user.profile.name == 'administrators'
  175. app = TracimCLI()
  176. result = app.run([
  177. 'user', 'update',
  178. '-c', 'tests_configs.ini#command_test',
  179. '-l', 'admin@admin.admin',
  180. '-p', 'new_password',
  181. '-rmg', 'administrators',
  182. '--debug',
  183. ])
  184. new_user = api.get_one_by_email('admin@admin.admin')
  185. assert new_user.email == 'admin@admin.admin'
  186. assert new_user.validate_password('new_password')
  187. assert not new_user.validate_password('admin@admin.admin')
  188. assert new_user.profile.name == 'managers'