Преглед на файлове

add tests for empty label for workspace create/update

Guénaël Muller преди 6 години
родител
ревизия
3cc7cfba23
променени са 3 файла, в които са добавени 48 реда и са изтрити 1 реда
  1. 4 1
      tracim/lib/core/workspace.py
  2. 42 0
      tracim/tests/functional/test_workspaces.py
  3. 2 0
      tracim/views/core_api/workspace_controller.py

+ 4 - 1
tracim/lib/core/workspace.py Целия файл

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

+ 42 - 0
tracim/tests/functional/test_workspaces.py Целия файл

@@ -138,6 +138,27 @@ class TestWorkspaceEndpoint(FunctionalTest):
138 138
         assert workspace['description'] == 'mysuperdescription'
139 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 162
     def test_api__create_workspace__ok_200__nominal_case(self) -> None:
142 163
         """
143 164
         Test create workspace
@@ -168,6 +189,27 @@ class TestWorkspaceEndpoint(FunctionalTest):
168 189
         workspace_2 = res.json_body
169 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 213
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
172 214
         """
173 215
         Check obtain workspace unreachable for user

+ 2 - 0
tracim/views/core_api/workspace_controller.py Целия файл

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