Browse Source

add tests for empty label for workspace create/update

Guénaël Muller 6 years ago
parent
commit
3cc7cfba23

+ 4 - 1
tracim/lib/core/workspace.py View File

5
 from sqlalchemy.orm import Session
5
 from sqlalchemy.orm import Session
6
 
6
 
7
 from tracim import CFG
7
 from tracim import CFG
8
+from tracim.exceptions import EmptyLabelNotAllowed
8
 from tracim.lib.utils.translation import fake_translator as _
9
 from tracim.lib.utils.translation import fake_translator as _
9
 
10
 
10
 from tracim.lib.core.userworkspace import RoleApi
11
 from tracim.lib.core.userworkspace import RoleApi
69
             save_now: bool=False,
70
             save_now: bool=False,
70
     ) -> Workspace:
71
     ) -> Workspace:
71
         if not label:
72
         if not label:
72
-            label = self.generate_label()
73
+            raise EmptyLabelNotAllowed('Workspace label cannot be empty')
73
 
74
 
74
         workspace = Workspace()
75
         workspace = Workspace()
75
         workspace.label = label
76
         workspace.label = label
120
         :param save_now: database flush
121
         :param save_now: database flush
121
         :return: updated workspace
122
         :return: updated workspace
122
         """
123
         """
124
+        if not label:
125
+            raise EmptyLabelNotAllowed('Workspace label cannot be empty')
123
         workspace.label = label
126
         workspace.label = label
124
         workspace.description = description
127
         workspace.description = description
125
 
128
 

+ 42 - 0
tracim/tests/functional/test_workspaces.py View File

138
         assert workspace['description'] == 'mysuperdescription'
138
         assert workspace['description'] == 'mysuperdescription'
139
         assert len(workspace['sidebar_entries']) == 7
139
         assert len(workspace['sidebar_entries']) == 7
140
 
140
 
141
+    def test_api__update_workspace__err_400__empty_label(self) -> None:
142
+        """
143
+        Test update workspace with empty label
144
+        """
145
+        self.testapp.authorization = (
146
+            'Basic',
147
+            (
148
+                'admin@admin.admin',
149
+                'admin@admin.admin'
150
+            )
151
+        )
152
+        params = {
153
+            'label': '',
154
+            'description': 'mysuperdescription'
155
+        }
156
+        res = self.testapp.put_json(
157
+            '/api/v2/workspaces/1',
158
+            status=400,
159
+            params=params,
160
+        )
161
+
141
     def test_api__create_workspace__ok_200__nominal_case(self) -> None:
162
     def test_api__create_workspace__ok_200__nominal_case(self) -> None:
142
         """
163
         """
143
         Test create workspace
164
         Test create workspace
168
         workspace_2 = res.json_body
189
         workspace_2 = res.json_body
169
         assert workspace == workspace_2
190
         assert workspace == workspace_2
170
 
191
 
192
+    def test_api__create_workspace__err_400__empty_label(self) -> None:
193
+        """
194
+        Test create workspace with empty label
195
+        """
196
+        self.testapp.authorization = (
197
+            'Basic',
198
+            (
199
+                'admin@admin.admin',
200
+                'admin@admin.admin'
201
+            )
202
+        )
203
+        params = {
204
+            'label': '',
205
+            'description': 'mysuperdescription'
206
+        }
207
+        res = self.testapp.post_json(
208
+            '/api/v2/workspaces',
209
+            status=400,
210
+            params=params,
211
+        )
212
+
171
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
213
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
172
         """
214
         """
173
         Check obtain workspace unreachable for user
215
         Check obtain workspace unreachable for user

+ 2 - 0
tracim/views/core_api/workspace_controller.py View File

66
         return wapi.get_workspace_with_context(request.current_workspace)
66
         return wapi.get_workspace_with_context(request.current_workspace)
67
 
67
 
68
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
68
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
69
+    @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
69
     @require_workspace_role(UserRoleInWorkspace.WORKSPACE_MANAGER)
70
     @require_workspace_role(UserRoleInWorkspace.WORKSPACE_MANAGER)
70
     @hapic.input_path(WorkspaceIdPathSchema())
71
     @hapic.input_path(WorkspaceIdPathSchema())
71
     @hapic.input_body(WorkspaceModifySchema())
72
     @hapic.input_body(WorkspaceModifySchema())
89
         return wapi.get_workspace_with_context(request.current_workspace)
90
         return wapi.get_workspace_with_context(request.current_workspace)
90
 
91
 
91
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
92
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
93
+    @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
92
     @require_profile(Group.TIM_MANAGER)
94
     @require_profile(Group.TIM_MANAGER)
93
     @hapic.input_body(WorkspaceCreationSchema())
95
     @hapic.input_body(WorkspaceCreationSchema())
94
     @hapic.output_body(WorkspaceSchema())
96
     @hapic.output_body(WorkspaceSchema())