test_workspace.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from tracim_backend.lib.core.content import ContentApi
  3. from tracim_backend.lib.core.group import GroupApi
  4. from tracim_backend.lib.core.user import UserApi
  5. from tracim_backend.lib.core.userworkspace import RoleApi
  6. from tracim_backend.lib.core.workspace import WorkspaceApi
  7. from tracim_backend.models import Content
  8. from tracim_backend.models import User
  9. from tracim_backend.models.auth import Group
  10. from tracim_backend.models.data import UserRoleInWorkspace
  11. from tracim_backend.models.data import Workspace
  12. #from tracim.tests import BaseTestThread
  13. from tracim_backend.tests import DefaultTest
  14. from tracim_backend.tests import eq_
  15. class TestThread(DefaultTest):
  16. def test_children(self):
  17. admin = self.session.query(User).filter(
  18. User.email == 'admin@admin.admin'
  19. ).one()
  20. self._create_thread_and_test(
  21. workspace_name='workspace_1',
  22. folder_name='folder_1',
  23. thread_name='thread_1',
  24. user=admin
  25. )
  26. workspace = self.session.query(Workspace).filter(
  27. Workspace.label == 'workspace_1'
  28. ).one()
  29. content_api = ContentApi(
  30. session=self.session,
  31. current_user=admin,
  32. config=self.app_config,
  33. )
  34. folder = content_api.get_canonical_query().filter(
  35. Content.label == 'folder_1'
  36. ).one()
  37. eq_([folder, ], list(workspace.get_valid_children()))
  38. def test_get_notifiable_roles(self):
  39. admin = self.session.query(User) \
  40. .filter(User.email == 'admin@admin.admin').one()
  41. wapi = WorkspaceApi(
  42. session=self.session,
  43. config=self.app_config,
  44. current_user=admin,
  45. )
  46. w = wapi.create_workspace(label='workspace w', save_now=True)
  47. uapi = UserApi(
  48. session=self.session,
  49. current_user=admin,
  50. config=self.config
  51. )
  52. u = uapi.create_minimal_user(email='u.u@u.u', save_now=True)
  53. eq_([], wapi.get_notifiable_roles(workspace=w))
  54. rapi = RoleApi(
  55. session=self.session,
  56. current_user=admin,
  57. config=self.app_config,
  58. )
  59. r = rapi.create_one(u, w, UserRoleInWorkspace.READER, with_notif=True)
  60. eq_([r, ], wapi.get_notifiable_roles(workspace=w))
  61. u.is_active = False
  62. eq_([], wapi.get_notifiable_roles(workspace=w))
  63. def test_unit__get_all_manageable(self):
  64. admin = self.session.query(User) \
  65. .filter(User.email == 'admin@admin.admin').one()
  66. uapi = UserApi(
  67. session=self.session,
  68. current_user=admin,
  69. config=self.config,
  70. )
  71. # Checks a case without workspaces.
  72. wapi = WorkspaceApi(
  73. session=self.session,
  74. current_user=admin,
  75. config=self.app_config,
  76. )
  77. eq_([], wapi.get_all_manageable())
  78. # Checks an admin gets all workspaces.
  79. w4 = wapi.create_workspace(label='w4')
  80. w3 = wapi.create_workspace(label='w3')
  81. w2 = wapi.create_workspace(label='w2')
  82. w1 = wapi.create_workspace(label='w1')
  83. eq_([w1, w2, w3, w4], wapi.get_all_manageable())
  84. # Checks a regular user gets none workspace.
  85. gapi = GroupApi(
  86. session=self.session,
  87. current_user=None,
  88. config=self.app_config,
  89. )
  90. u = uapi.create_minimal_user('u.s@e.r', [gapi.get_one(Group.TIM_USER)], True)
  91. wapi = WorkspaceApi(
  92. session=self.session,
  93. current_user=u,
  94. config=self.app_config,
  95. )
  96. rapi = RoleApi(
  97. session=self.session,
  98. current_user=u,
  99. config=self.app_config,
  100. )
  101. rapi.create_one(u, w4, UserRoleInWorkspace.READER, False)
  102. rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, False)
  103. rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, False)
  104. rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
  105. eq_([], wapi.get_all_manageable())
  106. # Checks a manager gets only its own workspaces.
  107. u.groups.append(gapi.get_one(Group.TIM_MANAGER))
  108. rapi.delete_one(u.user_id, w2.workspace_id)
  109. rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
  110. eq_([w1, w2], wapi.get_all_manageable())