Exemplo n.º 1
0
 def test_unit__get_known__user__distinct_workspaces_users_by_name__exclude_workspace(self):
     admin = self.session.query(User) \
         .filter(User.email == '*****@*****.**') \
         .one()
     api = UserApi(
         current_user=None,
         session=self.session,
         config=self.app_config,
     )
     u1 = api.create_user(
         email='email@email',
         name='name',
         do_notify=False,
         do_save=True,
     )
     u2 = api.create_user(
         email='email2@email2',
         name='name2',
         do_notify=False,
         do_save=True,
     )
     u3 = api.create_user(
         email='notfound@notfound',
         name='notfound',
         do_notify=False,
         do_save=True,
     )
     wapi = WorkspaceApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     workspace = wapi.create_workspace(
         'test workspace n°1',
         save_now=True)
     wapi = WorkspaceApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     workspace_2 = wapi.create_workspace(
         'test workspace n°2',
         save_now=True)
     role_api = RoleApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u2, workspace_2, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace_2, UserRoleInWorkspace.READER, False)
     api2 = UserApi(
         current_user=u3,
         session=self.session,
         config=self.app_config,
     )
     users = api2.get_known_user('name', exclude_workspace_ids=[workspace.workspace_id])
     assert len(users) == 1
     assert users[0] == u2
Exemplo n.º 2
0
 def test_unit__get_known__user__distinct_workspaces_users_by_name__exclude_workspace_and_name(
     self
 ):
     admin = self.session.query(User).filter(User.email == "*****@*****.**").one()
     api = UserApi(current_user=None, session=self.session, config=self.app_config)
     u1 = api.create_user(email="email@email", name="name", do_notify=False, do_save=True)
     u2 = api.create_user(email="email2@email2", name="name2", do_notify=False, do_save=True)
     u3 = api.create_user(
         email="notfound@notfound", name="notfound", do_notify=False, do_save=True
     )
     u4 = api.create_user(email="email3@email3", name="name3", do_notify=False, do_save=True)
     wapi = WorkspaceApi(current_user=admin, session=self.session, config=self.app_config)
     workspace = wapi.create_workspace("test workspace n°1", save_now=True)
     wapi = WorkspaceApi(current_user=admin, session=self.session, config=self.app_config)
     workspace_2 = wapi.create_workspace("test workspace n°2", save_now=True)
     role_api = RoleApi(current_user=admin, session=self.session, config=self.app_config)
     role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u2, workspace_2, UserRoleInWorkspace.READER, False)
     role_api.create_one(u4, workspace_2, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace_2, UserRoleInWorkspace.READER, False)
     api2 = UserApi(current_user=u3, session=self.session, config=self.app_config)
     users = api2.get_known_user(
         "name", exclude_workspace_ids=[workspace.workspace_id], exclude_user_ids=[u4.user_id]
     )
     assert len(users) == 1
     assert users[0] == u2
Exemplo n.º 3
0
    def test_unit__get_all_manageable(
        self, admin_user, session, app_config, user_api_factory, role_api_factory
    ):

        uapi = user_api_factory.get()
        # Checks a case without workspaces.
        wapi = WorkspaceApi(session=session, current_user=admin_user, config=app_config)
        assert [] == wapi.get_all_manageable()
        # Checks an admin_user gets all workspaces.
        w4 = wapi.create_workspace(label="w4")
        w3 = wapi.create_workspace(label="w3")
        w2 = wapi.create_workspace(label="w2")
        w1 = wapi.create_workspace(label="w1")
        assert [w1, w2, w3, w4] == wapi.get_all_manageable()
        # Checks a regular user gets none workspace.

        u = uapi.create_minimal_user("[email protected]", profile=Profile.USER, save_now=True)
        wapi = WorkspaceApi(session=session, current_user=u, config=app_config)
        rapi = role_api_factory.get()
        rapi.create_one(u, w4, UserRoleInWorkspace.READER, False)
        rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, False)
        rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, False)
        rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
        assert [] == wapi.get_all_manageable()
        # Checks a manager gets only its own workspaces.
        u.profile = Profile.TRUSTED_USER
        rapi.delete_one(u.user_id, w2.workspace_id)
        rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
        assert [w1, w2] == wapi.get_all_manageable()
Exemplo n.º 4
0
    def test_unit__create_workspace_same__ok__same_workspace_name_allowed(
        self, admin_user, session, app_config
    ):

        wapi = WorkspaceApi(session=session, current_user=admin_user, config=app_config)
        wapi.create_workspace(label="business", save_now=True)
        wapi.create_workspace(label="business", save_now=True)
Exemplo n.º 5
0
 def test_unit__get_known__user__distinct_workspaces_users_by_name__exclude_workspace(self):
     admin = self.session.query(models.User) \
         .filter(models.User.email == '*****@*****.**') \
         .one()
     api = UserApi(
         current_user=None,
         session=self.session,
         config=self.config,
     )
     u1 = api.create_user(
         email='email@email',
         name='name',
         do_notify=False,
         do_save=True,
     )
     u2 = api.create_user(
         email='email2@email2',
         name='name2',
         do_notify=False,
         do_save=True,
     )
     u3 = api.create_user(
         email='notfound@notfound',
         name='notfound',
         do_notify=False,
         do_save=True,
     )
     wapi = WorkspaceApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     workspace = wapi.create_workspace(
         'test workspace n°1',
         save_now=True)
     wapi = WorkspaceApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     workspace_2 = wapi.create_workspace(
         'test workspace n°2',
         save_now=True)
     role_api = RoleApi(
         current_user=admin,
         session=self.session,
         config=self.app_config,
     )
     role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u2, workspace_2, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace_2, UserRoleInWorkspace.READER, False)
     api2 = UserApi(
         current_user=u3,
         session=self.session,
         config=self.config,
     )
     users = api2.get_known_user('name', exclude_workspace_ids=[workspace.workspace_id])
     assert len(users) == 1
     assert users[0] == u2
Exemplo n.º 6
0
 def test_proxy_user_agenda__ok__workspace_filter(self) -> None:
     dbsession = get_tm_session(self.session_factory, transaction.manager)
     admin = dbsession.query(User).filter(
         User.email == "*****@*****.**").one()
     uapi = UserApi(current_user=admin,
                    session=dbsession,
                    config=self.app_config)
     gapi = GroupApi(current_user=admin,
                     session=dbsession,
                     config=self.app_config)
     groups = [gapi.get_one_with_name("users")]
     user = uapi.create_user(
         "*****@*****.**",
         password="******",
         do_save=True,
         do_notify=False,
         groups=groups,
     )
     workspace_api = WorkspaceApi(current_user=admin,
                                  session=dbsession,
                                  config=self.app_config,
                                  show_deleted=True)
     workspace = workspace_api.create_workspace("wp1", save_now=True)
     workspace.agenda_enabled = True
     workspace2 = workspace_api.create_workspace("wp2", save_now=True)
     workspace2.agenda_enabled = True
     workspace3 = workspace_api.create_workspace("wp3", save_now=True)
     workspace3.agenda_enabled = True
     rapi = RoleApi(current_user=admin,
                    session=dbsession,
                    config=self.app_config)
     rapi.create_one(user, workspace, UserRoleInWorkspace.CONTRIBUTOR,
                     False)
     rapi.create_one(user, workspace2, UserRoleInWorkspace.READER, False)
     rapi.create_one(user, workspace3, UserRoleInWorkspace.READER, False)
     transaction.commit()
     self.testapp.authorization = ("Basic", ("*****@*****.**",
                                             "*****@*****.**"))
     params = {
         "workspace_ids":
         "{},{}".format(workspace.workspace_id, workspace3.workspace_id),
         "agenda_types":
         "workspace",
     }
     result = self.testapp.get("/api/v2/users/{}/agenda".format(
         user.user_id),
                               params=params,
                               status=200)
     assert len(result.json_body) == 2
     agenda = result.json_body[0]
     assert agenda[
         "agenda_url"] == "http://localhost:6543/agenda/workspace/{}/".format(
             workspace.workspace_id)
     assert agenda["with_credentials"] is True
     agenda = result.json_body[1]
     assert agenda[
         "agenda_url"] == "http://localhost:6543/agenda/workspace/{}/".format(
             workspace3.workspace_id)
     assert agenda["with_credentials"] is True
Exemplo n.º 7
0
    def test_unit__rename_workspace_same_name_other_workspace__ok__same_workspace_name_allowed(
        self, session, admin_user, app_config
    ):

        wapi = WorkspaceApi(session=session, current_user=admin_user, config=app_config)
        wapi.create_workspace(label="business", save_now=True)
        workspace2 = wapi.create_workspace(label="meeting", save_now=True)
        wapi.update_workspace(workspace=workspace2, label="business", save_now=True, description="")
Exemplo n.º 8
0
 def test_unit__create_workspace_same__error__same_workspace_name_unallowed(
         self):
     admin = self.session.query(User).filter(
         User.email == "*****@*****.**").one()
     wapi = WorkspaceApi(session=self.session,
                         current_user=admin,
                         config=self.app_config)
     wapi.create_workspace(label="business", save_now=True)
     with pytest.raises(WorkspaceLabelAlreadyUsed):
         wapi.create_workspace(label="business", save_now=True)
Exemplo n.º 9
0
 def test_unit__get_all_manageable(self):
     admin = self.session.query(User) \
         .filter(User.email == '*****@*****.**').one()
     uapi = UserApi(
         session=self.session,
         current_user=admin,
         config=self.app_config,
     )
     # Checks a case without workspaces.
     wapi = WorkspaceApi(
         session=self.session,
         current_user=admin,
         config=self.app_config,
     )
     eq_([], wapi.get_all_manageable())
     # Checks an admin gets all workspaces.
     w4 = wapi.create_workspace(label='w4')
     w3 = wapi.create_workspace(label='w3')
     w2 = wapi.create_workspace(label='w2')
     w1 = wapi.create_workspace(label='w1')
     eq_([w1, w2, w3, w4], wapi.get_all_manageable())
     # Checks a regular user gets none workspace.
     gapi = GroupApi(
         session=self.session,
         current_user=None,
         config=self.app_config,
     )
     u = uapi.create_minimal_user('[email protected]', [gapi.get_one(Group.TIM_USER)],
                                  True)
     wapi = WorkspaceApi(
         session=self.session,
         current_user=u,
         config=self.app_config,
     )
     rapi = RoleApi(
         session=self.session,
         current_user=None,
         config=self.app_config,
     )
     rapi.create_one(u, w4, UserRoleInWorkspace.READER, False)
     rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, False)
     rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, False)
     rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
     eq_([], wapi.get_all_manageable())
     # Checks a manager gets only its own workspaces.
     u.groups.append(gapi.get_one(Group.TIM_MANAGER))
     rapi.delete_one(u.user_id, w2.workspace_id)
     rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
     eq_([w1, w2], wapi.get_all_manageable())
Exemplo n.º 10
0
 def test_unit__get_all_manageable(self):
     admin = self.session.query(User) \
         .filter(User.email == '*****@*****.**').one()
     uapi = UserApi(
         session=self.session,
         current_user=admin,
         config=self.app_config,
     )
     # Checks a case without workspaces.
     wapi = WorkspaceApi(
         session=self.session,
         current_user=admin,
         config=self.app_config,
     )
     eq_([], wapi.get_all_manageable())
     # Checks an admin gets all workspaces.
     w4 = wapi.create_workspace(label='w4')
     w3 = wapi.create_workspace(label='w3')
     w2 = wapi.create_workspace(label='w2')
     w1 = wapi.create_workspace(label='w1')
     eq_([w1, w2, w3, w4], wapi.get_all_manageable())
     # Checks a regular user gets none workspace.
     gapi = GroupApi(
         session=self.session,
         current_user=None,
         config=self.app_config,
     )
     u = uapi.create_minimal_user('[email protected]', [gapi.get_one(Group.TIM_USER)], True)
     wapi = WorkspaceApi(
         session=self.session,
         current_user=u,
         config=self.app_config,
     )
     rapi = RoleApi(
         session=self.session,
         current_user=None,
         config=self.app_config,
     )
     rapi.create_one(u, w4, UserRoleInWorkspace.READER, False)
     rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, False)
     rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, False)
     rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
     eq_([], wapi.get_all_manageable())
     # Checks a manager gets only its own workspaces.
     u.groups.append(gapi.get_one(Group.TIM_MANAGER))
     rapi.delete_one(u.user_id, w2.workspace_id)
     rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
     eq_([w1, w2], wapi.get_all_manageable())
Exemplo n.º 11
0
 def test_get_notifiable_roles(self):
     admin = self.session.query(User) \
         .filter(User.email == '*****@*****.**').one()
     wapi = WorkspaceApi(
         session=self.session,
         config=self.app_config,
         current_user=admin,
     )
     w = wapi.create_workspace(label='workspace w', save_now=True)
     uapi = UserApi(session=self.session,
                    current_user=admin,
                    config=self.app_config)
     u = uapi.create_minimal_user(email='[email protected]', save_now=True)
     eq_([], wapi.get_notifiable_roles(workspace=w))
     rapi = RoleApi(
         session=self.session,
         current_user=admin,
         config=self.app_config,
     )
     r = rapi.create_one(u, w, UserRoleInWorkspace.READER, with_notif=True)
     eq_([
         r,
     ], wapi.get_notifiable_roles(workspace=w))
     u.is_active = False
     eq_([], wapi.get_notifiable_roles(workspace=w))
Exemplo n.º 12
0
    def test_api___simple_search_ok__no_search_string(self) -> None:
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(User).filter(User.email == "*****@*****.**").one()
        uapi = UserApi(current_user=admin, session=dbsession, config=self.app_config)
        gapi = GroupApi(current_user=admin, session=dbsession, config=self.app_config)
        groups = [gapi.get_one_with_name("trusted-users")]
        user = uapi.create_user(
            "*****@*****.**",
            password="******",
            do_save=True,
            do_notify=False,
            groups=groups,
        )
        workspace_api = WorkspaceApi(
            current_user=admin, session=dbsession, config=self.app_config, show_deleted=True
        )
        workspace = workspace_api.create_workspace("test", save_now=True)
        rapi = RoleApi(current_user=admin, session=dbsession, config=self.app_config)
        rapi.create_one(user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
        api = ContentApi(session=dbsession, current_user=user, config=self.app_config)
        api.create(
            content_type_slug="html-document", workspace=workspace, label="test", do_save=True
        )
        transaction.commit()

        self.testapp.authorization = ("Basic", ("*****@*****.**", "*****@*****.**"))
        res = self.testapp.get("/api/v2/search/content".format(), status=200)
        search_result = res.json_body
        assert search_result
        assert search_result["total_hits"] == 0
        assert search_result["is_total_hits_accurate"] is True
        assert len(search_result["contents"]) == 0
Exemplo n.º 13
0
 def test_proxy_workspace_agenda__err__other_workspace_agenda(self) -> None:
     dbsession = get_tm_session(self.session_factory, transaction.manager)
     admin = dbsession.query(User).filter(
         User.email == "*****@*****.**").one()
     uapi = UserApi(current_user=admin,
                    session=dbsession,
                    config=self.app_config)
     gapi = GroupApi(current_user=admin,
                     session=dbsession,
                     config=self.app_config)
     groups = [gapi.get_one_with_name("users")]
     uapi.create_user(
         "*****@*****.**",
         password="******",
         do_save=True,
         do_notify=False,
         groups=groups,
     )
     workspace_api = WorkspaceApi(current_user=admin,
                                  session=dbsession,
                                  config=self.app_config,
                                  show_deleted=True)
     workspace = workspace_api.create_workspace("test", save_now=True)
     transaction.commit()
     self.testapp.authorization = ("Basic", ("*****@*****.**",
                                             "*****@*****.**"))
     result = self.testapp.get("/agenda/workspace/{}/".format(
         workspace.workspace_id),
                               status=403)
     assert result.json_body["code"] == 5001
Exemplo n.º 14
0
 def test__unit__get_notifiable_roles__ok__nominal_case(self):
     admin = self.session.query(User).filter(
         User.email == "*****@*****.**").one()
     wapi = WorkspaceApi(session=self.session,
                         config=self.app_config,
                         current_user=admin)
     workspace = wapi.create_workspace(label="workspace w", save_now=True)
     uapi = UserApi(session=self.session,
                    current_user=admin,
                    config=self.app_config)
     user_1 = uapi.create_user(email="[email protected]",
                               auth_type=AuthType.INTERNAL,
                               do_save=True,
                               do_notify=False)
     user_2 = uapi.create_user(email="[email protected]",
                               auth_type=AuthType.INTERNAL,
                               do_save=True,
                               do_notify=False)
     assert wapi.get_notifiable_roles(workspace=workspace) == []
     rapi = RoleApi(session=self.session,
                    current_user=admin,
                    config=self.app_config)
     role_1 = rapi.create_one(user_1,
                              workspace,
                              UserRoleInWorkspace.READER,
                              with_notif=True)
     role_2 = rapi.create_one(user_2,
                              workspace,
                              UserRoleInWorkspace.READER,
                              with_notif=False)
     assert role_1 in wapi.get_notifiable_roles(workspace=workspace)
     assert role_2 not in wapi.get_notifiable_roles(workspace=workspace)
Exemplo n.º 15
0
 def create_workspace(self,
                      context,
                      request: TracimRequest,
                      hapic_data=None):
     """
     Create a workspace. This route is for trusted users and administrators.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     wapi = WorkspaceApi(
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config  # User
     )
     parent = None
     if hapic_data.body.parent_id:
         parent = wapi.get_one(workspace_id=hapic_data.body.parent_id)
     workspace = wapi.create_workspace(
         label=hapic_data.body.label,
         description=hapic_data.body.description,
         access_type=hapic_data.body.access_type,
         save_now=True,
         agenda_enabled=hapic_data.body.agenda_enabled,
         public_download_enabled=hapic_data.body.public_download_enabled,
         public_upload_enabled=hapic_data.body.public_upload_enabled,
         default_user_role=hapic_data.body.default_user_role,
         parent=parent,
     )
     wapi.execute_created_workspace_actions(workspace)
     return wapi.get_workspace_with_context(workspace)
Exemplo n.º 16
0
    def test__unit__get_notifiable_roles__ok__do_not_show_deleted(
        self, admin_user, session, app_config, user_api_factory, role_api_factory
    ):

        wapi = WorkspaceApi(session=session, config=app_config, current_user=admin_user)
        workspace = wapi.create_workspace(label="workspace w", save_now=True)
        uapi = user_api_factory.get()
        user_1 = uapi.create_user(
            email="[email protected]", auth_type=AuthType.INTERNAL, do_save=True, do_notify=False
        )
        user_2 = uapi.create_user(
            email="[email protected]", auth_type=AuthType.INTERNAL, do_save=True, do_notify=False
        )
        assert wapi.get_notifiable_roles(workspace=workspace) == []

        rapi = role_api_factory.get()
        role_1 = rapi.create_one(user_1, workspace, UserRoleInWorkspace.READER, with_notif=True)
        role_2 = rapi.create_one(user_2, workspace, UserRoleInWorkspace.READER, with_notif=True)

        assert role_1 in wapi.get_notifiable_roles(workspace=workspace)
        assert role_2 in wapi.get_notifiable_roles(workspace=workspace)

        user_1.is_deleted = True
        assert role_1 not in wapi.get_notifiable_roles(workspace=workspace)
        assert role_2 in wapi.get_notifiable_roles(workspace=workspace)
Exemplo n.º 17
0
    def test_api___simple_search_ok__by_comment_content(
        self,
        created_content_name,
        search_string,
        nb_content_result,
        first_search_result_content_name,
        first_created_comment_content,
        second_created_comment_content,
    ) -> None:
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(User).filter(User.email == "*****@*****.**").one()
        uapi = UserApi(current_user=admin, session=dbsession, config=self.app_config)
        gapi = GroupApi(current_user=admin, session=dbsession, config=self.app_config)
        groups = [gapi.get_one_with_name("trusted-users")]
        user = uapi.create_user(
            "*****@*****.**",
            password="******",
            do_save=True,
            do_notify=False,
            groups=groups,
        )
        workspace_api = WorkspaceApi(
            current_user=admin, session=dbsession, config=self.app_config, show_deleted=True
        )
        workspace = workspace_api.create_workspace("test", save_now=True)
        rapi = RoleApi(current_user=admin, session=dbsession, config=self.app_config)
        rapi.create_one(user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, False)
        api = ContentApi(session=dbsession, current_user=user, config=self.app_config)
        content = api.create(
            content_type_slug="html-document",
            workspace=workspace,
            label=created_content_name,
            do_save=True,
        )
        api.create_comment(
            workspace=workspace, parent=content, content=first_created_comment_content, do_save=True
        )
        api.create_comment(
            workspace=workspace,
            parent=content,
            content=second_created_comment_content,
            do_save=True,
        )
        api.create(
            content_type_slug="html-document", workspace=workspace, label="report", do_save=True
        )
        api.create(
            content_type_slug="thread", workspace=workspace, label="discussion", do_save=True
        )
        transaction.commit()

        self.testapp.authorization = ("Basic", ("*****@*****.**", "*****@*****.**"))
        params = {"search_string": search_string}
        res = self.testapp.get("/api/v2/search/content".format(), status=200, params=params)
        search_result = res.json_body
        assert search_result
        assert search_result["total_hits"] == nb_content_result
        assert search_result["is_total_hits_accurate"] is False
        assert search_result["contents"][0]["label"] == first_search_result_content_name
Exemplo n.º 18
0
    def test_api__elasticsearch_search__ok__in_file_ingest_search(self):
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(User).filter(
            User.email == "*****@*****.**").one()
        uapi = UserApi(current_user=admin,
                       session=dbsession,
                       config=self.app_config)
        gapi = GroupApi(current_user=admin,
                        session=dbsession,
                        config=self.app_config)
        groups = [gapi.get_one_with_name("trusted-users")]
        user = uapi.create_user(
            "*****@*****.**",
            password="******",
            do_save=True,
            do_notify=False,
            groups=groups,
        )
        workspace_api = WorkspaceApi(current_user=admin,
                                     session=dbsession,
                                     config=self.app_config,
                                     show_deleted=True)
        workspace = workspace_api.create_workspace("test", save_now=True)
        rapi = RoleApi(current_user=admin,
                       session=dbsession,
                       config=self.app_config)
        rapi.create_one(user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER,
                        False)
        api = ContentApi(session=dbsession,
                         current_user=user,
                         config=self.app_config)
        with self.session.no_autoflush:
            text_file = api.create(
                content_type_slug=content_type_list.File.slug,
                workspace=workspace,
                label="important",
                do_save=False,
            )
            api.update_file_data(text_file, "test_file", "text/plain",
                                 b"we need to find stringtosearch here !")
            api.save(text_file)
            api.execute_created_content_actions(text_file)
        content_id = text_file.content_id
        transaction.commit()
        self.refresh_elasticsearch()

        params = {"search_string": "stringtosearch"}
        self.testapp.authorization = ("Basic", ("*****@*****.**",
                                                "*****@*****.**"))
        res = self.testapp.get("/api/v2/search/content".format(),
                               status=200,
                               params=params)
        search_result = res.json_body
        assert search_result
        assert search_result["total_hits"] == 1
        assert search_result["is_total_hits_accurate"] is True
        assert len(search_result["contents"]) == 1
        assert search_result["contents"][0]["content_id"] == content_id
Exemplo n.º 19
0
    def test_unit__rename_workspace_same_workspace_same_name__ok__nominal_case(
        self, admin_user, session, app_config
    ):

        wapi = WorkspaceApi(session=session, current_user=admin_user, config=app_config)
        workspace1 = wapi.create_workspace(label="business", save_now=True)
        modified_datetime = workspace1.updated
        wapi.update_workspace(workspace=workspace1, label="business", description="")
        assert workspace1.updated != modified_datetime
Exemplo n.º 20
0
    def test__unit__workspace_deletion__ok__nominal_case(
        self, session, admin_user, app_config
    ) -> None:

        wapi = WorkspaceApi(session=session, current_user=admin_user, config=app_config)
        business_workspace = wapi.create_workspace(label="business")
        assert business_workspace.label == "business"
        wapi.delete(business_workspace)
        assert business_workspace.is_deleted is True
        assert business_workspace.label != "business"
        assert business_workspace.label.startswith("business-deleted-")
Exemplo n.º 21
0
 def test__unit__workspace_deletion__ok__nominal_case(self) -> None:
     admin = self.session.query(User).filter(
         User.email == "*****@*****.**").one()
     wapi = WorkspaceApi(session=self.session,
                         current_user=admin,
                         config=self.app_config)
     business_workspace = wapi.create_workspace(label="business")
     assert business_workspace.label == "business"
     wapi.delete(business_workspace)
     assert business_workspace.is_deleted is True
     assert business_workspace.label != "business"
     assert business_workspace.label.startswith("business-deleted-")
Exemplo n.º 22
0
    def test__unit__get_notifiable_roles__ok__do_not_show_inactive(self):
        admin = self.session.query(User) \
            .filter(User.email == '*****@*****.**').one()
        wapi = WorkspaceApi(
            session=self.session,
            config=self.app_config,
            current_user=admin,
        )
        workspace = wapi.create_workspace(label='workspace w',
                                          save_now=True)
        uapi = UserApi(
            session=self.session,
            current_user=admin,
            config=self.app_config
        )
        user_1 = uapi.create_user(
            email='[email protected]',
            auth_type=AuthType.INTERNAL,
            do_save=True,
            do_notify=False
        )
        user_2 = uapi.create_user(
            email='[email protected]',
            auth_type=AuthType.INTERNAL,
            do_save=True,
            do_notify = False
        )
        assert wapi.get_notifiable_roles(workspace=workspace) == []

        rapi = RoleApi(
            session=self.session,
            current_user=admin,
            config=self.app_config,
        )
        role_1 = rapi.create_one(
            user_1,
            workspace,
            UserRoleInWorkspace.READER,
            with_notif=True
        )
        role_2 = rapi.create_one(
            user_2,
            workspace,
            UserRoleInWorkspace.READER,
            with_notif=True
        )

        assert role_1 in wapi.get_notifiable_roles(workspace=workspace)
        assert role_2 in wapi.get_notifiable_roles(workspace=workspace)

        user_1.is_active = False
        assert not role_1 in wapi.get_notifiable_roles(workspace=workspace)
        assert role_2 in wapi.get_notifiable_roles(workspace=workspace)
Exemplo n.º 23
0
    def test__unit__get_notifiable_roles__ok__do_not_show_unknown_auth(self):
        admin = self.session.query(User) \
            .filter(User.email == '*****@*****.**').one()
        wapi = WorkspaceApi(
            session=self.session,
            config=self.app_config,
            current_user=admin,
        )
        workspace = wapi.create_workspace(label='workspace w', save_now=True)
        uapi = UserApi(session=self.session,
                       current_user=admin,
                       config=self.app_config)

        user_1 = uapi.create_user(email='[email protected]',
                                  auth_type=AuthType.INTERNAL,
                                  do_save=True,
                                  do_notify=False)
        user_2 = uapi.create_user(email='[email protected]',
                                  auth_type=AuthType.UNKNOWN,
                                  do_save=True,
                                  do_notify=False)
        user_3 = uapi.create_user(email='[email protected]',
                                  auth_type=AuthType.REMOTE,
                                  do_save=True,
                                  do_notify=False)
        assert wapi.get_notifiable_roles(workspace=workspace) == []

        rapi = RoleApi(
            session=self.session,
            current_user=admin,
            config=self.app_config,
        )
        role_1 = rapi.create_one(user_1,
                                 workspace,
                                 UserRoleInWorkspace.READER,
                                 with_notif=True)
        role_2 = rapi.create_one(user_2,
                                 workspace,
                                 UserRoleInWorkspace.READER,
                                 with_notif=True)
        role_3 = rapi.create_one(user_3,
                                 workspace,
                                 UserRoleInWorkspace.READER,
                                 with_notif=True)

        assert role_1 in wapi.get_notifiable_roles(workspace=workspace)
        assert not role_2 in wapi.get_notifiable_roles(workspace=workspace)
        assert role_3 in wapi.get_notifiable_roles(workspace=workspace)
Exemplo n.º 24
0
 def test_proxy_workspace_agenda__ok__nominal_case(self) -> None:
     dbsession = get_tm_session(self.session_factory, transaction.manager)
     admin = dbsession.query(User).filter(
         User.email == "*****@*****.**").one()
     uapi = UserApi(current_user=admin,
                    session=dbsession,
                    config=self.app_config)
     gapi = GroupApi(current_user=admin,
                     session=dbsession,
                     config=self.app_config)
     groups = [gapi.get_one_with_name("users")]
     user = uapi.create_user(
         "*****@*****.**",
         password="******",
         do_save=True,
         do_notify=False,
         groups=groups,
     )
     workspace_api = WorkspaceApi(current_user=admin,
                                  session=dbsession,
                                  config=self.app_config,
                                  show_deleted=True)
     workspace = workspace_api.create_workspace("test", save_now=True)
     workspace.agenda_enabled = True
     rapi = RoleApi(current_user=admin,
                    session=dbsession,
                    config=self.app_config)
     rapi.create_one(user, workspace, UserRoleInWorkspace.CONTENT_MANAGER,
                     False)
     transaction.commit()
     self.testapp.authorization = ("Basic", ("*****@*****.**",
                                             "*****@*****.**"))
     self.testapp.get("/agenda/workspace/{}/".format(
         workspace.workspace_id),
                      status=404)
     event = VALID_CALDAV_BODY_PUT_EVENT
     self.testapp.put(
         "/agenda/workspace/{}/".format(workspace.workspace_id),
         event,
         content_type="text/agenda",
         status=201,
     )
     self.testapp.get("/agenda/workspace/{}/".format(
         workspace.workspace_id),
                      status=200)
     self.testapp.delete("/agenda/workspace/{}/".format(
         workspace.workspace_id),
                         status=200)
Exemplo n.º 25
0
 def create_workspace(self, context, request: TracimRequest, hapic_data=None):  # nopep8
     """
     Create a workspace. This route is for trusted users and administrators.
     """
     app_config = request.registry.settings['CFG']
     wapi = WorkspaceApi(
         current_user=request.current_user,  # User
         session=request.dbsession,
         config=app_config,
     )
     workspace = wapi.create_workspace(
         label=hapic_data.body.label,
         description=hapic_data.body.description,
         save_now=True,
     )
     return wapi.get_workspace_with_context(workspace)
Exemplo n.º 26
0
 def test_unit__rename_workspace_same_wworkspace_same_name__ok__nominal_case(
         self):
     admin = self.session.query(User).filter(
         User.email == "*****@*****.**").one()
     wapi = WorkspaceApi(session=self.session,
                         current_user=admin,
                         config=self.app_config)
     workspace1 = wapi.create_workspace(label="business", save_now=True)
     modified_datetime = workspace1.updated
     try:
         wapi.update_workspace(workspace=workspace1,
                               label="business",
                               description="")
     except WorkspaceLabelAlreadyUsed:
         pytest.fail("Unexpected WorkspaceLabelAlreadyUsed..")
     assert workspace1.updated != modified_datetime
Exemplo n.º 27
0
    def test_functional__webdav_access_to_workspace__nominal_case(self) -> None:
        dbsession = get_tm_session(self.session_factory, transaction.manager)
        admin = dbsession.query(User) \
            .filter(User.email == '*****@*****.**') \
            .one()
        uapi = UserApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        gapi = GroupApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        groups = [gapi.get_one_with_name('users')]
        user = uapi.create_user('*****@*****.**', password='******',
                                do_save=True, do_notify=False,
                                groups=groups)  # nopep8
        workspace_api = WorkspaceApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
            show_deleted=True,
        )
        workspace = workspace_api.create_workspace('test', save_now=True)  # nopep8
        rapi = RoleApi(
            current_user=admin,
            session=dbsession,
            config=self.app_config,
        )
        rapi.create_one(user, workspace, UserRoleInWorkspace.READER,
                        False)  # nopep8
        transaction.commit()

        self.testapp.authorization = (
            'Basic',
            (
                '*****@*****.**',
                '*****@*****.**'
            )
        )
        res = self.testapp.get('/test', status=200)
Exemplo n.º 28
0
 def test_unit__get_known__user__same_workspaces_users_by_email(self):
     admin = self.session.query(User).filter(User.email == "*****@*****.**").one()
     api = UserApi(current_user=None, session=self.session, config=self.app_config)
     u1 = api.create_user(email="email@email", name="name", do_notify=False, do_save=True)
     u2 = api.create_user(email="email2@email2", name="name2", do_notify=False, do_save=True)
     u3 = api.create_user(
         email="notfound@notfound", name="notfound", do_notify=False, do_save=True
     )
     wapi = WorkspaceApi(current_user=admin, session=self.session, config=self.app_config)
     workspace = wapi.create_workspace("test workspace n°1", save_now=True)
     role_api = RoleApi(current_user=admin, session=self.session, config=self.app_config)
     role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u2, workspace, UserRoleInWorkspace.READER, False)
     role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
     api2 = UserApi(current_user=u1, session=self.session, config=self.app_config)
     users = api2.get_known_user("email")
     assert len(users) == 2
     assert users[0] == u1
     assert users[1] == u2
Exemplo n.º 29
0
 def create_workspace(self,
                      context,
                      request: TracimRequest,
                      hapic_data=None):
     """
     Create a workspace. This route is for trusted users and administrators.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     wapi = WorkspaceApi(
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config  # User
     )
     workspace = wapi.create_workspace(
         label=hapic_data.body.label,
         description=hapic_data.body.description,
         save_now=True,
         agenda_enabled=hapic_data.body.agenda_enabled,
     )
     wapi.execute_created_workspace_actions(workspace)
     return wapi.get_workspace_with_context(workspace)
Exemplo n.º 30
0
    def insert(self):
        admin = self._session.query(User) \
            .filter(User.email == '*****@*****.**') \
            .one()
        bob = self._session.query(User) \
            .filter(User.email == '*****@*****.**') \
            .one()
        john_the_reader = self._session.query(User) \
            .filter(User.email == '*****@*****.**') \
            .one()

        admin_workspace_api = WorkspaceApi(
            current_user=admin,
            session=self._session,
            config=self._config,
        )
        bob_workspace_api = WorkspaceApi(
            current_user=bob,
            session=self._session,
            config=self._config
        )
        content_api = ContentApi(
            current_user=admin,
            session=self._session,
            config=self._config
        )
        bob_content_api = ContentApi(
            current_user=bob,
            session=self._session,
            config=self._config
        )
        reader_content_api = ContentApi(
            current_user=john_the_reader,
            session=self._session,
            config=self._config
        )
        role_api = RoleApi(
            current_user=admin,
            session=self._session,
            config=self._config,
        )

        # Workspaces
        business_workspace = admin_workspace_api.create_workspace(
            'Business',
            description='All importants documents',
            save_now=True,
        )
        recipe_workspace = admin_workspace_api.create_workspace(
            'Recipes',
            description='Our best recipes',
            save_now=True,
        )
        other_workspace = bob_workspace_api.create_workspace(
            'Others',
            description='Other Workspace',
            save_now=True,
        )

        # Workspaces roles
        role_api.create_one(
            user=bob,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.CONTENT_MANAGER,
            with_notif=False,
        )
        role_api.create_one(
            user=john_the_reader,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.READER,
            with_notif=False,
        )
        # Folders

        tool_workspace = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label='Tools',
            do_save=True,
            do_notify=False,
        )
        menu_workspace = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label='Menus',
            do_save=True,
            do_notify=False,
        )

        dessert_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Desserts',
            do_save=True,
            do_notify=False,
        )
        salads_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Salads',
            do_save=True,
            do_notify=False,
        )
        other_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=other_workspace,
            label='Infos',
            do_save=True,
            do_notify=False,
        )

        # Pages, threads, ..
        tiramisu_page = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Tiramisu Recipes!!!',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=tiramisu_page,
        ):
            content_api.update_content(
                item=tiramisu_page,
                new_content='<p>To cook a greet Tiramisu, you need many ingredients.</p>',  # nopep8
                new_label='Tiramisu Recipes!!!',
            )
            content_api.save(tiramisu_page)

        best_cake_thread = content_api.create(
            content_type_slug=content_type_list.Thread.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Best Cake',
            do_save=False,
            do_notify=False,
        )
        best_cake_thread.description = 'Which is the best cake?'
        self._session.add(best_cake_thread)
        apple_pie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Apple_Pie',
            do_save=False,
            do_notify=False,
        )
        apple_pie_recipe.file_extension = '.txt'
        apple_pie_recipe.depot_file = FileIntent(
            b'Apple pie Recipe',
            'apple_Pie.txt',
            'text/plain',
        )
        self._session.add(apple_pie_recipe)
        Brownie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Brownie Recipe',
            do_save=False,
            do_notify=False,
        )
        Brownie_recipe.file_extension = '.html'
        Brownie_recipe.depot_file = FileIntent(
            b'<p>Brownie Recipe</p>',
            'brownie_recipe.html',
            'text/html',
        )
        self._session.add(Brownie_recipe)
        fruits_desserts_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Fruits Desserts',
            parent=dessert_folder,
            do_save=True,
        )

        menu_page = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=business_workspace,
            parent=menu_workspace,
            label='Current Menu',
            do_save=True,
        )

        new_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='New Fruit Salad',
            do_save=True,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='Fruit Salad',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=old_fruit_salad,
        ):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='Bad Fruit Salad',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=bad_fruit_salad,
        ):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad)

        # File at the root for test
        new_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='New Fruit Salad',
            do_save=True,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='Fruit Salad',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=old_fruit_salad,
        ):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='Bad Fruit Salad',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=bad_fruit_salad,
        ):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad)

        content_api.create_comment(
            parent=best_cake_thread,
            content='<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>',  # nopep8
            do_save=True,
        )
        bob_content_api.create_comment(
            parent=best_cake_thread,
            content='<p>What about Apple Pie? There are Awesome!</p>',
            do_save=True,
        )
        reader_content_api.create_comment(
            parent=best_cake_thread,
            content='<p>You are right, but Kouign-amann are clearly better.</p>',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=best_cake_thread,
        ):
            bob_content_api.update_content(
                item=best_cake_thread,
                new_content='What is the best cake?',
                new_label='Best Cakes?',
            )
            bob_content_api.save(best_cake_thread)

        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=tiramisu_page,
        ):
            bob_content_api.update_content(
                item=tiramisu_page,
                new_content='<p>To cook a great Tiramisu, you need many ingredients.</p>',  # nopep8
                new_label='Tiramisu Recipe',
            )
            bob_content_api.save(tiramisu_page)
        self._session.flush()
Exemplo n.º 31
0
class TestFolderMove(FunctionalTest):
    def setUp(self):
        super().setUp()
        self.testapp.authorization = ("Basic", ("*****@*****.**",
                                                "*****@*****.**"))
        self.dbsession = get_tm_session(self.session_factory,
                                        transaction.manager)
        self.admin = self.dbsession.query(User).filter(
            User.email == "*****@*****.**").one()
        self.workspace_api = WorkspaceApi(current_user=self.admin,
                                          session=self.dbsession,
                                          config=self.app_config)
        self.content_api = ContentApi(current_user=self.admin,
                                      session=self.dbsession,
                                      config=self.app_config)
        self.workspace = self.workspace_api.create_workspace(label="test",
                                                             save_now=True)
        transaction.commit()

    def test_api__move_folder_into_itself__err_400(self) -> None:
        moved_folder = self.content_api.create(
            label="test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            do_save=True,
            do_notify=False,
        )
        transaction.commit()
        move_url = "/api/v2/workspaces/{}/contents/{}/move".format(
            self.workspace.workspace_id, moved_folder.content_id)
        body = {
            "new_parent_id": moved_folder.content_id,
            "new_workspace_id": self.workspace.workspace_id,
        }
        response = self.testapp.put_json(move_url, params=body, status=400)
        assert response.json_body[
            "code"] == ErrorCode.CONFLICTING_MOVE_IN_ITSELF

    def test_api__move_folder_in_a_direct_child__err_400(self) -> None:
        moved_folder = self.content_api.create(
            label="test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            do_save=True,
            do_notify=False,
        )
        child_folder = self.content_api.create(
            label="test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            parent=moved_folder,
            do_save=True,
            do_notify=False,
        )
        transaction.commit()
        move_url = "/api/v2/workspaces/{}/contents/{}/move".format(
            self.workspace.workspace_id, moved_folder.content_id)
        body = {
            "new_parent_id": child_folder.content_id,
            "new_workspace_id": self.workspace.workspace_id,
        }
        response = self.testapp.put_json(move_url, params=body, status=400)
        assert response.json_body[
            "code"] == ErrorCode.CONFLICTING_MOVE_IN_CHILD

    def test_api__move_folder_in_a_sub_child__err_400(self) -> None:
        moved_folder = self.content_api.create(
            label="test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            do_save=True,
            do_notify=False,
        )
        child_folder = self.content_api.create(
            label="child_test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            parent=moved_folder,
            do_save=True,
            do_notify=False,
        )
        sub_child_folder = self.content_api.create(
            label="sub_child_test_folder",
            content_type_slug=content_type_list.Folder.slug,
            workspace=self.workspace,
            parent=child_folder,
            do_save=True,
            do_notify=False,
        )
        transaction.commit()
        move_url = "/api/v2/workspaces/{}/contents/{}/move".format(
            self.workspace.workspace_id, moved_folder.content_id)
        body = {
            "new_parent_id": sub_child_folder.content_id,
            "new_workspace_id": self.workspace.workspace_id,
        }
        response = self.testapp.put_json(move_url, params=body, status=400)
        assert response.json_body[
            "code"] == ErrorCode.CONFLICTING_MOVE_IN_CHILD
Exemplo n.º 32
0
    def insert(self):
        admin = self._session.query(models.User) \
            .filter(models.User.email == '*****@*****.**') \
            .one()
        bob = self._session.query(models.User) \
            .filter(models.User.email == '*****@*****.**') \
            .one()
        john_the_reader = self._session.query(models.User) \
            .filter(models.User.email == '*****@*****.**') \
            .one()

        admin_workspace_api = WorkspaceApi(
            current_user=admin,
            session=self._session,
            config=self._config,
        )
        bob_workspace_api = WorkspaceApi(current_user=bob,
                                         session=self._session,
                                         config=self._config)
        content_api = ContentApi(current_user=admin,
                                 session=self._session,
                                 config=self._config)
        bob_content_api = ContentApi(current_user=bob,
                                     session=self._session,
                                     config=self._config)
        reader_content_api = ContentApi(current_user=john_the_reader,
                                        session=self._session,
                                        config=self._config)
        role_api = RoleApi(
            current_user=admin,
            session=self._session,
            config=self._config,
        )

        # Workspaces
        business_workspace = admin_workspace_api.create_workspace(
            'Business',
            description='All importants documents',
            save_now=True,
        )
        recipe_workspace = admin_workspace_api.create_workspace(
            'Recipes',
            description='Our best recipes',
            save_now=True,
        )
        other_workspace = bob_workspace_api.create_workspace(
            'Others',
            description='Other Workspace',
            save_now=True,
        )

        # Workspaces roles
        role_api.create_one(
            user=bob,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.CONTENT_MANAGER,
            with_notif=False,
        )
        role_api.create_one(
            user=john_the_reader,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.READER,
            with_notif=False,
        )
        # Folders

        tool_workspace = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label='Tools',
            do_save=True,
            do_notify=False,
        )
        menu_workspace = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label='Menus',
            do_save=True,
            do_notify=False,
        )

        dessert_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Desserts',
            do_save=True,
            do_notify=False,
        )
        salads_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Salads',
            do_save=True,
            do_notify=False,
        )
        other_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=other_workspace,
            label='Infos',
            do_save=True,
            do_notify=False,
        )

        # Pages, threads, ..
        tiramisu_page = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Tiramisu Recipes!!!',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=tiramisu_page,
        ):
            content_api.update_content(
                item=tiramisu_page,
                new_content=
                '<p>To cook a greet Tiramisu, you need many ingredients.</p>',  # nopep8
                new_label='Tiramisu Recipes!!!',
            )
            content_api.save(tiramisu_page)

        best_cake_thread = content_api.create(
            content_type_slug=content_type_list.Thread.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Best Cake',
            do_save=False,
            do_notify=False,
        )
        best_cake_thread.description = 'Which is the best cake?'
        self._session.add(best_cake_thread)
        apple_pie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Apple_Pie',
            do_save=False,
            do_notify=False,
        )
        apple_pie_recipe.file_extension = '.txt'
        apple_pie_recipe.depot_file = FileIntent(
            b'Apple pie Recipe',
            'apple_Pie.txt',
            'text/plain',
        )
        self._session.add(apple_pie_recipe)
        Brownie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label='Brownie Recipe',
            do_save=False,
            do_notify=False,
        )
        Brownie_recipe.file_extension = '.html'
        Brownie_recipe.depot_file = FileIntent(
            b'<p>Brownie Recipe</p>',
            'brownie_recipe.html',
            'text/html',
        )
        self._session.add(Brownie_recipe)
        fruits_desserts_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label='Fruits Desserts',
            parent=dessert_folder,
            do_save=True,
        )

        menu_page = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=business_workspace,
            parent=menu_workspace,
            label='Current Menu',
            do_save=True,
        )

        new_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='New Fruit Salad',
            do_save=True,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='Fruit Salad',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=old_fruit_salad,
        ):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label='Bad Fruit Salad',
            do_save=True,
            do_notify=False,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=bad_fruit_salad,
        ):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad)

        # File at the root for test
        new_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='New Fruit Salad',
            do_save=True,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='Fruit Salad',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=old_fruit_salad,
        ):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label='Bad Fruit Salad',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=bad_fruit_salad,
        ):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad)

        content_api.create_comment(
            parent=best_cake_thread,
            content=
            '<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>',  # nopep8
            do_save=True,
        )
        bob_content_api.create_comment(
            parent=best_cake_thread,
            content='<p>What about Apple Pie? There are Awesome!</p>',
            do_save=True,
        )
        reader_content_api.create_comment(
            parent=best_cake_thread,
            content=
            '<p>You are right, but Kouign-amann are clearly better.</p>',
            do_save=True,
        )
        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=best_cake_thread,
        ):
            bob_content_api.update_content(
                item=best_cake_thread,
                new_content='What is the best cake?',
                new_label='Best Cakes?',
            )
            bob_content_api.save(best_cake_thread)

        with new_revision(
                session=self._session,
                tm=transaction.manager,
                content=tiramisu_page,
        ):
            bob_content_api.update_content(
                item=tiramisu_page,
                new_content=
                '<p>To cook a great Tiramisu, you need many ingredients.</p>',  # nopep8
                new_label='Tiramisu Recipe',
            )
            bob_content_api.save(tiramisu_page)
        self._session.flush()
Exemplo n.º 33
0
class RootResource(DAVCollection):
    """
    RootResource ressource that represents tracim's home, which contains all workspaces
    """

    def __init__(self, path: str, environ: dict, tracim_context: 'WebdavTracimContext'):
        super(RootResource, self).__init__(path, environ)
        self.tracim_context = tracim_context
        self.user = tracim_context.current_user
        self.session = tracim_context.dbsession
        # TODO BS 20170221: Web interface should list all workspace to. We
        # disable it here for moment. When web interface will be updated to
        # list all workspace, change this here to.
        self.workspace_api = WorkspaceApi(
            current_user=self.user,
            session=self.session,
            force_role=True,
            config=tracim_context.app_config
        )

    def __repr__(self) -> str:
        return '<DAVCollection: RootResource>'

    @webdav_check_right(is_user)
    def getMemberNames(self) -> [str]:
        """
        This method returns the names (here workspace's labels) of all its children

        Though for perfomance issue, we're not using this function anymore
        """
        return [
            webdav_convert_file_name_to_display(workspace.label)
            for workspace in self.workspace_api.get_all()
        ]

    @webdav_check_right(is_user)
    def getMember(self, label: str) -> DAVCollection:
        """
        This method returns the child Workspace that corresponds to a given name

        Though for perfomance issue, we're not using this function anymore
        """
        try:
            workspace = self.workspace_api.get_one_by_label(label)
            # fix path
            workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(workspace.label))
            # return item
            return WorkspaceResource(
                workspace_path,
                self.environ,
                workspace,
                tracim_context=self.tracim_context
            )
        except AttributeError:
            return None

    @webdav_check_right(is_trusted_user)
    def createEmptyResource(self, name: str):
        """
        This method is called whenever the user wants to create a DAVNonCollection resource (files in our case).

        There we don't allow to create files at the root;
        only workspaces (thus collection) can be created.
        """
        raise DAVError(HTTP_FORBIDDEN)

    @webdav_check_right(is_trusted_user)
    def createCollection(self, name: str):
        """
        This method is called whenever the user wants to create a DAVCollection resource as a child (in our case,
        we create workspaces as this is the root).

        [For now] we don't allow to create new workspaces through
        webdav client. Though if we come to allow it, deleting the error's raise will
        make it possible.
        """
        # TODO : remove comment here
        # raise DAVError(HTTP_FORBIDDEN)
        workspace_name = webdav_convert_file_name_to_bdd(name)
        new_workspace = self.workspace_api.create_workspace(workspace_name)
        self.workspace_api.save(new_workspace)
        transaction.commit()
        # fix path
        workspace_path = '%s%s%s' % (
            self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(new_workspace.label)
        )

        # create item
        return WorkspaceResource(
            workspace_path,
            self.environ,
            new_workspace,
            tracim_context=self.tracim_context
        )

    @webdav_check_right(is_user)
    def getMemberList(self):
        """
        This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource
        of all its direct children
        """

        members = []
        for workspace in self.workspace_api.get_all():
            # fix path
            workspace_label = webdav_convert_file_name_to_display(workspace.label)
            path = add_trailing_slash(self.path)
            # return item
            workspace_path = '{}{}'.format(path, workspace_label)
            members.append(
                WorkspaceResource(
                    path=workspace_path,
                    environ=self.environ,
                    workspace=workspace,
                    tracim_context=self.tracim_context
                )
            )

        return members
Exemplo n.º 34
0
class RootResource(DAVCollection):
    """
    RootResource ressource that represents tracim's home, which contains all workspaces
    """
    def __init__(self, path: str, environ: dict,
                 tracim_context: "WebdavTracimContext"):
        super(RootResource, self).__init__(path, environ)
        self.tracim_context = tracim_context
        self.user = tracim_context.current_user
        self.session = tracim_context.dbsession
        # TODO BS 20170221: Web interface should list all workspace to. We
        # disable it here for moment. When web interface will be updated to
        # list all workspace, change this here to.
        self.workspace_api = WorkspaceApi(
            current_user=self.user,
            session=self.session,
            force_role=True,
            config=tracim_context.app_config,
        )

    def __repr__(self) -> str:
        return "<DAVCollection: RootResource>"

    @webdav_check_right(is_user)
    def getMemberNames(self) -> [str]:
        """
        This method returns the names (here workspace's labels) of all its children

        Though for perfomance issue, we're not using this function anymore
        """
        members_names = []
        for workspace in self.workspace_api.get_all():
            if webdav_convert_file_name_to_display(
                    workspace.label) in members_names:
                label = "{workspace_label}~~{workspace_id}".format(
                    workspace_label=workspace.label,
                    workspace_id=workspace.workspace_id)
            else:
                label = workspace.label
            members_names.append(webdav_convert_file_name_to_display(label))

    @webdav_check_right(is_user)
    def getMember(self, label: str) -> DAVCollection:
        """
        This method returns the child Workspace that corresponds to a given name

        Though for perfomance issue, we're not using this function anymore
        """
        try:
            workspace = self.workspace_api.get_one_by_label(label)
            # fix path
            workspace_path = "%s%s%s" % (
                self.path,
                "" if self.path == "/" else "/",
                webdav_convert_file_name_to_display(workspace.label),
            )
            # return item
            return WorkspaceResource(
                path=workspace_path,
                environ=self.environ,
                workspace=workspace,
                tracim_context=self.tracim_context,
                label=workspace.label,
            )
        except AttributeError:
            return None

    @webdav_check_right(is_trusted_user)
    def createEmptyResource(self, name: str):
        """
        This method is called whenever the user wants to create a DAVNonCollection resource (files in our case).

        There we don't allow to create files at the root;
        only workspaces (thus collection) can be created.
        """
        raise DAVError(HTTP_FORBIDDEN,
                       contextinfo="Not allowed to create new root")

    @webdav_check_right(is_trusted_user)
    def createCollection(self, name: str):
        """
        This method is called whenever the user wants to create a DAVCollection resource as a child (in our case,
        we create workspaces as this is the root).

        [For now] we don't allow to create new workspaces through
        webdav client. Though if we come to allow it, deleting the error's raise will
        make it possible.
        """
        # TODO : remove comment here
        # raise DAVError(HTTP_FORBIDDEN)
        workspace_name = webdav_convert_file_name_to_bdd(name)
        try:
            new_workspace = self.workspace_api.create_workspace(workspace_name)
        except (UserNotAllowedToCreateMoreWorkspace,
                EmptyLabelNotAllowed) as exc:
            raise DAVError(HTTP_FORBIDDEN, contextinfo=str(exc))
        self.workspace_api.save(new_workspace)
        self.workspace_api.execute_created_workspace_actions(new_workspace)
        transaction.commit()
        # fix path
        workspace_path = "%s%s%s" % (
            self.path,
            "" if self.path == "/" else "/",
            webdav_convert_file_name_to_display(new_workspace.label),
        )

        # create item
        return WorkspaceResource(
            path=workspace_path,
            environ=self.environ,
            workspace=new_workspace,
            tracim_context=self.tracim_context,
            label=new_workspace.label,
        )

    @webdav_check_right(is_user)
    def getMemberList(self):
        """
        This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource
        of all its direct children
        """

        members = []
        members_names = []
        for workspace in self.workspace_api.get_all():
            if webdav_convert_file_name_to_display(
                    workspace.label) in members_names:
                label = "{workspace_label}~~{workspace_id}".format(
                    workspace_label=workspace.label,
                    workspace_id=workspace.workspace_id)
            else:
                label = workspace.label
            # fix path
            workspace_label = webdav_convert_file_name_to_display(label)
            path = add_trailing_slash(self.path)
            # return item
            workspace_path = "{}{}".format(path, workspace_label)
            members.append(
                WorkspaceResource(
                    path=workspace_path,
                    environ=self.environ,
                    workspace=workspace,
                    tracim_context=self.tracim_context,
                    label=label,
                ))
            members_names.append(workspace_label)

        return members
Exemplo n.º 35
0
    def insert(self):
        admin = self._session.query(User).filter(User.email == "*****@*****.**").one()
        bob = self._session.query(User).filter(User.email == "*****@*****.**").one()
        john_the_reader = (
            self._session.query(User).filter(User.email == "*****@*****.**").one()
        )

        admin_workspace_api = WorkspaceApi(
            current_user=admin, session=self._session, config=self._config
        )
        bob_workspace_api = WorkspaceApi(
            current_user=bob, session=self._session, config=self._config
        )
        content_api = ContentApi(current_user=admin, session=self._session, config=self._config)
        bob_content_api = ContentApi(current_user=bob, session=self._session, config=self._config)
        reader_content_api = ContentApi(
            current_user=john_the_reader, session=self._session, config=self._config
        )
        role_api = RoleApi(current_user=admin, session=self._session, config=self._config)

        # Workspaces
        business_workspace = admin_workspace_api.create_workspace(
            "Business", description="All importants documents", save_now=True
        )
        recipe_workspace = admin_workspace_api.create_workspace(
            "Recipes", description="Our best recipes", save_now=True
        )
        other_workspace = bob_workspace_api.create_workspace(
            "Others", description="Other Workspace", save_now=True
        )

        # Workspaces roles
        role_api.create_one(
            user=bob,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.CONTENT_MANAGER,
            with_notif=False,
        )
        role_api.create_one(
            user=john_the_reader,
            workspace=recipe_workspace,
            role_level=UserRoleInWorkspace.READER,
            with_notif=False,
        )
        # Folders

        content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label="Tools",
            do_save=True,
            do_notify=False,
        )
        menu_workspace = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=business_workspace,
            label="Menus",
            do_save=True,
            do_notify=False,
        )

        dessert_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label="Desserts",
            do_save=True,
            do_notify=False,
        )
        content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label="Salads",
            do_save=True,
            do_notify=False,
        )
        content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=other_workspace,
            label="Infos",
            do_save=True,
            do_notify=False,
        )

        # Pages, threads, ..
        tiramisu_page = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label="Tiramisu Recipes!!!",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=tiramisu_page):
            content_api.update_content(
                item=tiramisu_page,
                new_content="<p>To cook a greet Tiramisu, you need many ingredients.</p>",
                new_label="Tiramisu Recipes!!!",
            )
            content_api.save(tiramisu_page, do_notify=False)

        best_cake_thread = content_api.create(
            content_type_slug=content_type_list.Thread.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label="Best Cake",
            do_save=False,
            do_notify=False,
        )
        best_cake_thread.description = "Which is the best cake?"
        self._session.add(best_cake_thread)
        apple_pie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label="Apple_Pie",
            do_save=False,
            do_notify=False,
        )
        apple_pie_recipe.file_extension = ".txt"
        apple_pie_recipe.depot_file = FileIntent(b"Apple pie Recipe", "apple_Pie.txt", "text/plain")
        self._session.add(apple_pie_recipe)
        brownie_recipe = content_api.create(
            content_type_slug=content_type_list.File.slug,
            workspace=recipe_workspace,
            parent=dessert_folder,
            label="Brownie Recipe",
            do_save=False,
            do_notify=False,
        )
        brownie_recipe.file_extension = ".html"
        brownie_recipe.depot_file = FileIntent(
            b"<p>Brownie Recipe</p>", "brownie_recipe.html", "text/html"
        )
        self._session.add(brownie_recipe)
        fruits_desserts_folder = content_api.create(
            content_type_slug=content_type_list.Folder.slug,
            workspace=recipe_workspace,
            label="Fruits Desserts",
            parent=dessert_folder,
            do_save=True,
            do_notify=False,
        )

        content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=business_workspace,
            parent=menu_workspace,
            label="Current Menu",
            do_save=True,
            do_notify=False,
        )

        content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label="New Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label="Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=old_fruit_salad):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad, do_notify=False)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=recipe_workspace,
            parent=fruits_desserts_folder,
            label="Bad Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=bad_fruit_salad):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad, do_notify=False)

        # File at the root for test
        content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label="New Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        old_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label="Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=old_fruit_salad):
            content_api.archive(old_fruit_salad)
        content_api.save(old_fruit_salad, do_notify=False)

        bad_fruit_salad = content_api.create(
            content_type_slug=content_type_list.Page.slug,
            workspace=other_workspace,
            label="Bad Fruit Salad",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=bad_fruit_salad):
            content_api.delete(bad_fruit_salad)
        content_api.save(bad_fruit_salad, do_notify=False)

        content_api.create_comment(
            parent=best_cake_thread,
            content="<p>What is for you the best cake ever? <br/> I personnally vote for Chocolate cupcake!</p>",
            do_save=True,
            do_notify=False,
        )
        bob_content_api.create_comment(
            parent=best_cake_thread,
            content="<p>What about Apple Pie? There are Awesome!</p>",
            do_save=True,
            do_notify=False,
        )
        reader_content_api.create_comment(
            parent=best_cake_thread,
            content="<p>You are right, but Kouign-amann are clearly better.</p>",
            do_save=True,
            do_notify=False,
        )
        with new_revision(session=self._session, tm=transaction.manager, content=best_cake_thread):
            bob_content_api.update_content(
                item=best_cake_thread, new_content="What is the best cake?", new_label="Best Cakes?"
            )
            bob_content_api.save(best_cake_thread, do_notify=False)

        with new_revision(session=self._session, tm=transaction.manager, content=tiramisu_page):
            bob_content_api.update_content(
                item=tiramisu_page,
                new_content="<p>To cook a great Tiramisu, you need many ingredients.</p>",
                new_label="Tiramisu Recipe",
            )
            bob_content_api.save(tiramisu_page, do_notify=False)
        self._session.flush()