test_commands.py 6.6KB

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