test_workspace.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. from tracim.lib.core.content import ContentApi
  3. from tracim.lib.core.group import GroupApi
  4. from tracim.lib.core.user import UserApi
  5. from tracim.lib.core.userworkspace import RoleApi
  6. from tracim.lib.core.workspace import WorkspaceApi
  7. from tracim.models import Content
  8. from tracim.models import User
  9. from tracim.models.auth import Group
  10. from tracim.models.data import UserRoleInWorkspace
  11. from tracim.models.data import Workspace
  12. #from tracim.tests import BaseTestThread
  13. from tracim.tests import DefaultTest
  14. from tracim.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. current_user=admin,
  44. )
  45. w = wapi.create_workspace(label='workspace w', save_now=True)
  46. uapi = UserApi(
  47. session=self.session,
  48. current_user=admin,
  49. config=self.config
  50. )
  51. u = uapi.create_minimal_user(email='u.u@u.u', save_now=True)
  52. eq_([], wapi.get_notifiable_roles(workspace=w))
  53. rapi = RoleApi(
  54. session=self.session,
  55. current_user=admin,
  56. )
  57. r = rapi.create_one(u, w, UserRoleInWorkspace.READER, with_notif=True)
  58. eq_([r, ], wapi.get_notifiable_roles(workspace=w))
  59. u.is_active = False
  60. eq_([], wapi.get_notifiable_roles(workspace=w))
  61. def test_unit__get_all_manageable(self):
  62. admin = self.session.query(User) \
  63. .filter(User.email == 'admin@admin.admin').one()
  64. uapi = UserApi(
  65. session=self.session,
  66. current_user=admin,
  67. config=self.config,
  68. )
  69. # Checks a case without workspaces.
  70. wapi = WorkspaceApi(
  71. session=self.session,
  72. current_user=admin,
  73. )
  74. eq_([], wapi.get_all_manageable())
  75. # Checks an admin gets all workspaces.
  76. w4 = wapi.create_workspace(label='w4')
  77. w3 = wapi.create_workspace(label='w3')
  78. w2 = wapi.create_workspace(label='w2')
  79. w1 = wapi.create_workspace(label='w1')
  80. eq_([w1, w2, w3, w4], wapi.get_all_manageable())
  81. # Checks a regular user gets none workspace.
  82. gapi = GroupApi(
  83. session=self.session,
  84. current_user=None,
  85. )
  86. u = uapi.create_minimal_user('u.s@e.r', [gapi.get_one(Group.TIM_USER)], True)
  87. wapi = WorkspaceApi(
  88. session=self.session,
  89. current_user=u
  90. )
  91. rapi = RoleApi(
  92. session=self.session,
  93. current_user=u
  94. )
  95. rapi.create_one(u, w4, UserRoleInWorkspace.READER, False)
  96. rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, False)
  97. rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, False)
  98. rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
  99. eq_([], wapi.get_all_manageable())
  100. # Checks a manager gets only its own workspaces.
  101. u.groups.append(gapi.get_one(Group.TIM_MANAGER))
  102. rapi.delete_one(u.user_id, w2.workspace_id)
  103. rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
  104. eq_([w1, w2], wapi.get_all_manageable())