Browse Source

add test + handle exception for unallowed subcontent content creation

Guénaël Muller 6 years ago
parent
commit
1a230413e7

+ 63 - 0
backend/tracim_backend/tests/functional/test_workspaces.py View File

@@ -1620,6 +1620,69 @@ class TestWorkspaceContents(FunctionalTest):
1620 1620
             status=400,
1621 1621
         )
1622 1622
 
1623
+    def test_api__post_content_create_generic_content__err_400__unallowed_content_type(self) -> None:  # nopep8
1624
+        """
1625
+        Create generic content
1626
+        """
1627
+        dbsession = get_tm_session(self.session_factory, transaction.manager)
1628
+        admin = dbsession.query(models.User) \
1629
+            .filter(models.User.email == 'admin@admin.admin') \
1630
+            .one()
1631
+        workspace_api = WorkspaceApi(
1632
+            current_user=admin,
1633
+            session=dbsession,
1634
+            config=self.app_config
1635
+        )
1636
+        content_api = ContentApi(
1637
+            current_user=admin,
1638
+            session=dbsession,
1639
+            config=self.app_config
1640
+        )
1641
+        test_workspace = workspace_api.create_workspace(
1642
+            label='test',
1643
+            save_now=True,
1644
+        )
1645
+        folder = content_api.create(
1646
+            label='test-folder',
1647
+            content_type_slug=CONTENT_TYPES.Folder.slug,
1648
+            workspace=test_workspace,
1649
+            do_save=False,
1650
+            do_notify=False
1651
+        )
1652
+        content_api.set_allowed_content(folder, [CONTENT_TYPES.Folder.slug])
1653
+        content_api.save(folder)
1654
+        transaction.commit()
1655
+        self.testapp.authorization = (
1656
+            'Basic',
1657
+            (
1658
+                'admin@admin.admin',
1659
+                'admin@admin.admin'
1660
+            )
1661
+        )
1662
+        # unallowed_content_type
1663
+        params = {
1664
+            'label': 'GenericCreatedContent',
1665
+            'content_type': 'markdownpage',
1666
+            'parent_id': folder.content_id
1667
+        }
1668
+        res = self.testapp.post_json(
1669
+            '/api/v2/workspaces/{workspace_id}/contents'.format(workspace_id=test_workspace.workspace_id),
1670
+            params=params,
1671
+            status=400,
1672
+        )
1673
+
1674
+        # allowed_content_type
1675
+        params = {
1676
+            'label': 'GenericCreatedContent',
1677
+            'content_type': 'folder',
1678
+            'parent_id': folder.content_id
1679
+        }
1680
+        res = self.testapp.post_json(
1681
+            '/api/v2/workspaces/{workspace_id}/contents'.format(workspace_id=test_workspace.workspace_id),
1682
+            params=params,
1683
+            status=200,
1684
+        )
1685
+
1623 1686
     def test_api_put_move_content__ok_200__nominal_case(self):
1624 1687
         """
1625 1688
         Move content

+ 2 - 1
backend/tracim_backend/views/core_api/workspace_controller.py View File

@@ -23,7 +23,7 @@ from tracim_backend.models.data import UserRoleInWorkspace
23 23
 from tracim_backend.models.data import ActionDescription
24 24
 from tracim_backend.models.context_models import UserRoleWorkspaceInContext
25 25
 from tracim_backend.models.context_models import ContentInContext
26
-from tracim_backend.exceptions import EmptyLabelNotAllowed
26
+from tracim_backend.exceptions import EmptyLabelNotAllowed, UnallowedSubContent
27 27
 from tracim_backend.exceptions import EmailValidationFailed
28 28
 from tracim_backend.exceptions import UserCreationFailed
29 29
 from tracim_backend.exceptions import UserDoesNotExist
@@ -269,6 +269,7 @@ class WorkspaceController(Controller):
269 269
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
270 270
     @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
271 271
     @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
272
+    @hapic.handle_exception(UnallowedSubContent, HTTPStatus.BAD_REQUEST)
272 273
     @hapic.input_path(WorkspaceIdPathSchema())
273 274
     @hapic.input_body(ContentCreationSchema())
274 275
     @hapic.output_body(ContentDigestSchema())