Ver código fonte

add endpoint to get only one user_role for workspace

Guénaël Muller 6 anos atrás
pai
commit
e2fab8c02e

+ 137 - 0
backend/tracim_backend/tests/functional/test_workspaces.py Ver arquivo

@@ -945,6 +945,143 @@ class TestWorkspaceMembersEndpoint(FunctionalTest):
945 945
         assert 'message' in res.json.keys()
946 946
         assert 'details' in res.json.keys()
947 947
 
948
+    def test_api__get_workspace_member__ok_200__self(self):
949
+        """
950
+        Check obtain workspace members list with a reachable workspace for user
951
+        """
952
+        self.testapp.authorization = (
953
+            'Basic',
954
+            (
955
+                'admin@admin.admin',
956
+                'admin@admin.admin'
957
+            )
958
+        )
959
+        res = self.testapp.get('/api/v2/workspaces/1/members/1', status=200).json_body   # nopep8
960
+        user_role = res
961
+        assert user_role['role'] == 'workspace-manager'
962
+        assert user_role['user_id'] == 1
963
+        assert user_role['workspace_id'] == 1
964
+        assert user_role['workspace']['workspace_id'] == 1
965
+        assert user_role['workspace']['label'] == 'Business'
966
+        assert user_role['workspace']['slug'] == 'business'
967
+        assert user_role['user']['public_name'] == 'Global manager'
968
+        assert user_role['user']['user_id'] == 1
969
+        assert user_role['is_active'] is True
970
+        assert user_role['do_notify'] is True
971
+        # TODO - G.M - 24-05-2018 - [Avatar] Replace
972
+        # by correct value when avatar feature will be enabled
973
+        assert user_role['user']['avatar_url'] is None
974
+
975
+    def test_api__get_workspace_member__ok_200__other_user(self):
976
+        """
977
+        Check obtain workspace members list with a reachable workspace for user
978
+        """
979
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
980
+        admin = dbsession.query(models.User) \
981
+            .filter(models.User.email == 'admin@admin.admin') \
982
+            .one()
983
+        uapi = UserApi(
984
+            current_user=admin,
985
+            session=dbsession,
986
+            config=self.app_config,
987
+        )
988
+        gapi = GroupApi(
989
+            current_user=admin,
990
+            session=dbsession,
991
+            config=self.app_config,
992
+        )
993
+        groups = [gapi.get_one_with_name('managers')]
994
+        user = uapi.create_user('test@test.test', password='test@test.test', do_save=True, do_notify=False, groups=groups)  # nopep8
995
+        workspace_api = WorkspaceApi(
996
+            current_user=admin,
997
+            session=dbsession,
998
+            config=self.app_config,
999
+        )
1000
+        workspace = workspace_api.create_workspace('test_2', save_now=True)  # nopep8
1001
+        rapi = RoleApi(
1002
+            current_user=admin,
1003
+            session=dbsession,
1004
+            config=self.app_config,
1005
+        )
1006
+        rapi.create_one(user, workspace, UserRoleInWorkspace.READER, False)  # nopep8
1007
+        transaction.commit()
1008
+        user_id = user.user_id
1009
+        workspace_id = workspace.workspace_id
1010
+        admin_id = admin.user_id
1011
+        self.testapp.authorization = (
1012
+            'Basic',
1013
+            (
1014
+                'admin@admin.admin',
1015
+                'admin@admin.admin'
1016
+            )
1017
+        )
1018
+        print(str(user_id) + '##' + str(workspace_id))
1019
+        res = self.testapp.get('/api/v2/workspaces/{}/members/{}'.format(
1020
+            workspace_id,
1021
+            user_id
1022
+        ), status=200).json_body
1023
+        user_role = res
1024
+        assert user_role['role'] == 'reader'
1025
+        assert user_role['user_id'] == user_id
1026
+        assert user_role['workspace_id'] == workspace_id
1027
+        assert user_role['is_active'] is True
1028
+        assert user_role['do_notify'] is False
1029
+
1030
+        self.testapp.authorization = (
1031
+            'Basic',
1032
+            (
1033
+                'test@test.test',
1034
+                'test@test.test'
1035
+            )
1036
+        )
1037
+        res = self.testapp.get('/api/v2/workspaces/{}/members/{}'.format(
1038
+            workspace_id,
1039
+            admin_id
1040
+        ), status=200).json_body
1041
+        user_role = res
1042
+        assert user_role['role'] == 'workspace-manager'
1043
+        assert user_role['user_id'] == admin_id
1044
+        assert user_role['workspace_id'] == workspace_id
1045
+        assert user_role['is_active'] is True
1046
+        assert user_role['do_notify'] is True
1047
+
1048
+
1049
+    def test_api__get_workspace_member__err_400__unallowed_user(self):
1050
+        """
1051
+        Check obtain workspace members info with an unreachable workspace for
1052
+        user
1053
+        """
1054
+        self.testapp.authorization = (
1055
+            'Basic',
1056
+            (
1057
+                'lawrence-not-real-email@fsf.local',
1058
+                'foobarbaz'
1059
+            )
1060
+        )
1061
+        res = self.testapp.get('/api/v2/workspaces/3/members/1', status=400)
1062
+        assert isinstance(res.json, dict)
1063
+        assert 'code' in res.json.keys()
1064
+        assert 'message' in res.json.keys()
1065
+        assert 'details' in res.json.keys()
1066
+
1067
+    def test_api__get_workspace_member__err_401__unregistered_user(self):
1068
+        """
1069
+        Check obtain workspace member info with an unregistered user
1070
+        """
1071
+        self.testapp.authorization = (
1072
+            'Basic',
1073
+            (
1074
+                'john@doe.doe',
1075
+                'lapin'
1076
+            )
1077
+        )
1078
+        res = self.testapp.get('/api/v2/workspaces/1/members/1', status=401)
1079
+        assert isinstance(res.json, dict)
1080
+        assert 'code' in res.json.keys()
1081
+        assert 'message' in res.json.keys()
1082
+        assert 'details' in res.json.keys()
1083
+
1084
+
948 1085
     def test_api__get_workspace_members__err_400__workspace_does_not_exist(self):  # nopep8
949 1086
         """
950 1087
         Check obtain workspace members list with an existing user but

+ 29 - 0
backend/tracim_backend/views/core_api/workspace_controller.py Ver arquivo

@@ -194,6 +194,32 @@ class WorkspaceController(Controller):
194 194
         ]
195 195
 
196 196
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
197
+    @require_workspace_role(UserRoleInWorkspace.READER)
198
+    @hapic.input_path(WorkspaceAndUserIdPathSchema())
199
+    @hapic.output_body(WorkspaceMemberSchema())
200
+    def workspaces_member_role(
201
+            self,
202
+            context,
203
+            request: TracimRequest,
204
+            hapic_data=None
205
+    ) -> UserRoleWorkspaceInContext:
206
+        """
207
+        Get role of user in workspace
208
+        """
209
+        app_config = request.registry.settings['CFG']
210
+        rapi = RoleApi(
211
+            current_user=request.current_user,
212
+            session=request.dbsession,
213
+            config=app_config,
214
+        )
215
+
216
+        role = rapi.get_one(
217
+            user_id=hapic_data.path.user_id,
218
+            workspace_id=hapic_data.path.workspace_id,
219
+        )
220
+        return rapi.get_user_role_workspace_with_context(role)
221
+
222
+    @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
197 223
     @require_workspace_role(UserRoleInWorkspace.WORKSPACE_MANAGER)
198 224
     @hapic.input_path(WorkspaceAndUserIdPathSchema())
199 225
     @hapic.input_body(RoleUpdateSchema())
@@ -655,6 +681,9 @@ class WorkspaceController(Controller):
655 681
         # Workspace Members (Roles)
656 682
         configurator.add_route('workspace_members', '/workspaces/{workspace_id}/members', request_method='GET')  # nopep8
657 683
         configurator.add_view(self.workspaces_members, route_name='workspace_members')  # nopep8
684
+        # Workspace Members (Role) Individual
685
+        configurator.add_route('workspace_member_role', '/workspaces/{workspace_id}/members/{user_id}', request_method='GET')  # nopep8
686
+        configurator.add_view(self.workspaces_member_role, route_name='workspace_member_role')  # nopep8
658 687
         # Update Workspace Members roles
659 688
         configurator.add_route('update_workspace_member', '/workspaces/{workspace_id}/members/{user_id}', request_method='PUT')  # nopep8
660 689
         configurator.add_view(self.update_workspaces_members_role, route_name='update_workspace_member')  # nopep8