test_role_api.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # coding=utf-8
  2. import pytest
  3. from sqlalchemy.orm.exc import NoResultFound
  4. from tracim_backend.lib.core.userworkspace import RoleApi
  5. from tracim_backend.models import User
  6. from tracim_backend.models.roles import WorkspaceRoles
  7. from tracim_backend.tests import DefaultTest
  8. from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
  9. from tracim_backend.fixtures.content import Content as ContentFixture
  10. class TestRoleApi(DefaultTest):
  11. fixtures = [BaseFixture, ContentFixture]
  12. def test_unit__get_one__ok__nominal_case(self):
  13. admin = self.session.query(User)\
  14. .filter(User.email == 'admin@admin.admin').one()
  15. rapi = RoleApi(
  16. current_user=admin,
  17. session=self.session,
  18. config=self.config,
  19. )
  20. rapi.get_one(admin.user_id, 1)
  21. def test_unit__get_one__err__role_does_not_exist(self):
  22. admin = self.session.query(User)\
  23. .filter(User.email == 'admin@admin.admin').one()
  24. rapi = RoleApi(
  25. current_user=admin,
  26. session=self.session,
  27. config=self.config,
  28. )
  29. with pytest.raises(NoResultFound):
  30. rapi.get_one(admin.user_id, 100) # workspace 100 does not exist
  31. def test_unit__create_one__nominal_case(self):
  32. admin = self.session.query(User)\
  33. .filter(User.email == 'admin@admin.admin').one()
  34. workspace = self._create_workspace_and_test(
  35. 'workspace_1',
  36. admin
  37. )
  38. bob = self.session.query(User)\
  39. .filter(User.email == 'bob@fsf.local').one()
  40. rapi = RoleApi(
  41. current_user=admin,
  42. session=self.session,
  43. config=self.config,
  44. )
  45. created_role = rapi.create_one(
  46. user=bob,
  47. workspace=workspace,
  48. role_level=WorkspaceRoles.CONTENT_MANAGER.level,
  49. with_notif=False,
  50. )
  51. obtain_role = rapi.get_one(bob.user_id, workspace.workspace_id)
  52. assert created_role == obtain_role
  53. def test_unit__get_all_for_usages(self):
  54. admin = self.session.query(User)\
  55. .filter(User.email == 'admin@admin.admin').one()
  56. rapi = RoleApi(
  57. current_user=admin,
  58. session=self.session,
  59. config=self.config,
  60. )
  61. workspace = self._create_workspace_and_test(
  62. 'workspace_1',
  63. admin
  64. )
  65. roles = rapi.get_all_for_workspace(workspace)
  66. len(roles) == 1
  67. roles[0].user_id == admin.user_id
  68. roles[0].role == WorkspaceRoles.WORKSPACE_MANAGER.level