소스 검색

add endpoint to get all workspaces for admin

Guénaël Muller 6 년 전
부모
커밋
f5b25f04ed
2개의 변경된 파일124개의 추가작업 그리고 0개의 파일을 삭제
  1. 100 0
      backend/tracim_backend/tests/functional/test_workspaces.py
  2. 24 0
      backend/tracim_backend/views/core_api/workspace_controller.py

+ 100 - 0
backend/tracim_backend/tests/functional/test_workspaces.py 파일 보기

@@ -875,6 +875,106 @@ class TestWorkspaceEndpoint(FunctionalTest):
875 875
         assert 'details' in res.json.keys()
876 876
 
877 877
 
878
+class TestWorkspacesEndpoints(FunctionalTest):
879
+    """
880
+    Tests for /api/v2/workspaces
881
+    """
882
+    fixtures = [BaseFixture]
883
+
884
+    def test_api__get_workspaces__ok_200__nominal_case(self):
885
+        """
886
+        Check obtain all workspaces reachables for user with user auth.
887
+        """
888
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
889
+        admin = dbsession.query(models.User) \
890
+            .filter(models.User.email == 'admin@admin.admin') \
891
+            .one()
892
+
893
+        workspace_api = WorkspaceApi(
894
+            session=dbsession,
895
+            current_user=admin,
896
+            config=self.app_config,
897
+        )
898
+        workspace_api.create_workspace('test', save_now=True)  # nopep8
899
+        workspace_api.create_workspace('test2', save_now=True)  # nopep8
900
+        workspace_api.create_workspace('test3', save_now=True)  # nopep8
901
+        transaction.commit()
902
+        self.testapp.authorization = (
903
+            'Basic',
904
+            (
905
+                'admin@admin.admin',
906
+                'admin@admin.admin'
907
+            )
908
+        )
909
+        res = self.testapp.get('/api/v2/workspaces', status=200)
910
+        res = res.json_body
911
+        assert len(res) == 3
912
+        workspace = res[0]
913
+        assert workspace['label'] == 'test'
914
+        assert workspace['slug'] == 'test'
915
+        workspace = res[1]
916
+        assert workspace['label'] == 'test2'
917
+        assert workspace['slug'] == 'test2'
918
+        workspace = res[2]
919
+        assert workspace['label'] == 'test3'
920
+        assert workspace['slug'] == 'test3'
921
+
922
+    def test_api__get_workspaces__err_403__unallowed_user(self):
923
+        """
924
+        Check obtain all workspaces reachables for one user
925
+        with another non-admin user auth.
926
+        """
927
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
928
+        admin = dbsession.query(models.User) \
929
+            .filter(models.User.email == 'admin@admin.admin') \
930
+            .one()
931
+        uapi = UserApi(
932
+            current_user=admin,
933
+            session=dbsession,
934
+            config=self.app_config,
935
+        )
936
+        gapi = GroupApi(
937
+            current_user=admin,
938
+            session=dbsession,
939
+            config=self.app_config,
940
+        )
941
+        groups = [gapi.get_one_with_name('users')]
942
+        user = uapi.create_user('test@test.test', password='test@test.test',
943
+                                do_save=True, do_notify=False,
944
+                                groups=groups)  # nopep8
945
+        transaction.commit()
946
+        self.testapp.authorization = (
947
+            'Basic',
948
+            (
949
+                'test@test.test',
950
+                'test@test.test'
951
+            )
952
+        )
953
+        res = self.testapp.get('/api/v2/workspaces', status=403)
954
+        assert isinstance(res.json, dict)
955
+        assert 'code' in res.json.keys()
956
+        assert 'message' in res.json.keys()
957
+        assert 'details' in res.json.keys()
958
+
959
+    def test_api__get_workspaces__err_401__unregistered_user(self):
960
+        """
961
+        Check obtain all workspaces reachables for one user
962
+        without correct user auth (user unregistered).
963
+        """
964
+        self.testapp.authorization = (
965
+            'Basic',
966
+            (
967
+                'john@doe.doe',
968
+                'lapin'
969
+            )
970
+        )
971
+        res = self.testapp.get('/api/v2/workspaces', status=401)
972
+        assert isinstance(res.json, dict)
973
+        assert 'code' in res.json.keys()
974
+        assert 'message' in res.json.keys()
975
+        assert 'details' in res.json.keys()
976
+
977
+
878 978
 class TestWorkspaceMembersEndpoint(FunctionalTest):
879 979
     """
880 980
     Tests for /api/v2/workspaces/{workspace_id}/members endpoint

+ 24 - 0
backend/tracim_backend/views/core_api/workspace_controller.py 파일 보기

@@ -18,6 +18,7 @@ from tracim_backend.lib.core.workspace import WorkspaceApi
18 18
 from tracim_backend.lib.core.content import ContentApi
19 19
 from tracim_backend.lib.core.userworkspace import RoleApi
20 20
 from tracim_backend.lib.utils.authorization import require_workspace_role
21
+from tracim_backend.lib.utils.authorization import require_same_user_or_profile
21 22
 from tracim_backend.lib.utils.authorization import require_profile_or_other_profile_with_workspace_role
22 23
 from tracim_backend.lib.utils.authorization import require_profile
23 24
 from tracim_backend.models import Group
@@ -77,6 +78,26 @@ class WorkspaceController(Controller):
77 78
         return wapi.get_workspace_with_context(request.current_workspace)
78 79
 
79 80
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
81
+    @require_profile(Group.TIM_ADMIN)
82
+    @hapic.output_body(WorkspaceSchema(many=True), )
83
+    def workspaces(self, context, request: TracimRequest, hapic_data=None):
84
+        """
85
+        Get list of all workspaces
86
+        """
87
+        app_config = request.registry.settings['CFG']
88
+        wapi = WorkspaceApi(
89
+            current_user=request.current_user,  # User
90
+            session=request.dbsession,
91
+            config=app_config,
92
+        )
93
+
94
+        workspaces = wapi.get_all()
95
+        return [
96
+            wapi.get_workspace_with_context(workspace)
97
+            for workspace in workspaces
98
+        ]
99
+
100
+    @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
80 101
     @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
81 102
     @require_workspace_role(UserRoleInWorkspace.WORKSPACE_MANAGER)
82 103
     @hapic.input_path(WorkspaceIdPathSchema())
@@ -664,6 +685,9 @@ class WorkspaceController(Controller):
664 685
         pyramid configurator for this controller
665 686
         """
666 687
 
688
+        # Workspaces
689
+        configurator.add_route('workspaces', '/workspaces', request_method='GET')  # nopep8
690
+        configurator.add_view(self.workspaces, route_name='workspaces')
667 691
         # Workspace
668 692
         configurator.add_route('workspace', '/workspaces/{workspace_id}', request_method='GET')  # nopep8
669 693
         configurator.add_view(self.workspace, route_name='workspace')