Exemplo n.º 1
0
    def test_unit_test_generate_if_none__ok__nominal_case(self) -> None:
        tracim_context = TracimContext()

        a = None

        class ANotFound(Exception):
            pass

        def a_id_fetcher():
            return "12"

        def a_generator(id_fetcher: Callable):
            try:
                return int(id_fetcher())
            except ValueError:
                raise ANotFound()

        a = tracim_context._generate_if_none(a, a_generator, a_id_fetcher)
        assert isinstance(a, int)
        assert a == 12

        # redo
        a = tracim_context._generate_if_none(a, a_generator, a_id_fetcher)
        assert isinstance(a, int)
        assert a == 12
Exemplo n.º 2
0
    def test_unit_test_generate_if_none__ok__nominal_case(self) -> None:
        tracim_context = TracimContext()

        a = None

        class ANotFound(Exception):
            pass

        def a_id_fetcher():
            return '12'

        def a_generator(id_fetcher: Callable):
            try:
                return int(id_fetcher())
            except ValueError:
                raise ANotFound()

        a = tracim_context._generate_if_none(
            a,
            a_generator,
            a_id_fetcher
        )
        assert isinstance(a, int)
        assert a == 12

        # redo
        a = tracim_context._generate_if_none(
            a,
            a_generator,
            a_id_fetcher
        )
        assert isinstance(a, int)
        assert a == 12
Exemplo n.º 3
0
    def test_unit_test_generate_if_none__ok__already_exist(self) -> None:
        tracim_context = TracimContext()

        a = 12

        a = tracim_context._generate_if_none(a, None, None)
        assert isinstance(a, int)
        assert a == 12
Exemplo n.º 4
0
    def test_unit_test_generate_if_none__ok__already_exist(self) -> None:
        tracim_context = TracimContext()

        a = 12

        a = tracim_context._generate_if_none(
            a,
            None,
            None
        )
        assert isinstance(a, int)
        assert a == 12
Exemplo n.º 5
0
 def _create_subscription_event(self, operation: OperationType,
                                subscription: WorkspaceSubscription,
                                context: TracimContext) -> None:
     current_user = context.safe_current_user()
     workspace_api = WorkspaceApi(
         session=context.dbsession,
         config=self._config,
         current_user=None,
     )
     workspace_in_context = workspace_api.get_workspace_with_context(
         workspace_api.get_one(subscription.workspace_id))
     user_api = UserApi(current_user,
                        context.dbsession,
                        self._config,
                        show_deleted=True)
     subscription_author_in_context = user_api.get_user_with_context(
         subscription.author)
     fields = {
         Event.WORKSPACE_FIELD:
         EventApi.workspace_schema.dump(workspace_in_context).data,
         Event.SUBSCRIPTION_FIELD:
         EventApi.workspace_subscription_schema.dump(subscription).data,
         Event.USER_FIELD:
         EventApi.user_schema.dump(subscription_author_in_context).data,
     }
     event_api = EventApi(current_user, context.dbsession, self._config)
     event_api.create_event(
         entity_type=EntityType.WORKSPACE_SUBSCRIPTION,
         operation=operation,
         additional_fields=fields,
         context=context,
     )
Exemplo n.º 6
0
    def _create_content_event(self, operation: OperationType, content: Content,
                              context: TracimContext) -> None:
        current_user = context.safe_current_user()
        content_api = ContentApi(context.dbsession, current_user, self._config)
        content_in_context = content_api.get_content_in_context(content)
        content_schema = EventApi.get_content_schema_for_type(content.type)
        content_dict = content_schema.dump(content_in_context).data

        workspace_api = WorkspaceApi(context.dbsession,
                                     current_user,
                                     self._config,
                                     show_deleted=True)
        workspace_in_context = workspace_api.get_workspace_with_context(
            workspace_api.get_one(content_in_context.workspace.workspace_id))
        fields = {
            Event.CONTENT_FIELD:
            content_dict,
            Event.WORKSPACE_FIELD:
            EventApi.workspace_schema.dump(workspace_in_context).data,
        }
        event_api = EventApi(current_user, context.dbsession, self._config)
        event_api.create_event(
            entity_type=EntityType.CONTENT,
            operation=operation,
            additional_fields=fields,
            entity_subtype=content.type,
            context=context,
        )
Exemplo n.º 7
0
 def create_event(
     self,
     entity_type: EntityType,
     operation: OperationType,
     additional_fields: Dict[str, JsonDict],
     context: TracimContext,
     entity_subtype: Optional[str] = None,
 ) -> Event:
     current_user = context.safe_current_user()
     user_api = UserApi(
         current_user=current_user,
         session=context.dbsession,
         config=self._config,
         show_deleted=True,
     )
     if current_user:
         author = self.user_schema.dump(
             user_api.get_user_with_context(current_user)).data
     else:
         author = None
     fields = {
         Event.AUTHOR_FIELD: author,
         Event.CLIENT_TOKEN_FIELD: context.client_token,
     }
     fields.update(additional_fields)
     event = Event(
         entity_type=entity_type,
         operation=operation,
         entity_subtype=entity_subtype,
         fields=fields,
     )
     context.dbsession.add(event)
     context.pending_events.append(event)
     return event
Exemplo n.º 8
0
    def test_unit_test_generate_if_none__err__exception(self) -> None:
        tracim_context = TracimContext()

        a = None

        class ANotFound(Exception):
            pass

        def a_id_fetcher():
            raise ValueError()

        def a_generator(id_fetcher: Callable):
            try:
                return int(id_fetcher())
            except ValueError:
                raise ANotFound()

        with pytest.raises(ANotFound):
            tracim_context._generate_if_none(a, a_generator, a_id_fetcher)
Exemplo n.º 9
0
 def _create_workspace_historic_messages_for_current_user(
         self, role: UserRoleInWorkspace, context: TracimContext):
     current_user = context.safe_current_user()
     event_api = EventApi(current_user, context.dbsession, self._config)
     event_api.create_messages_history_for_user(
         user_id=role.user_id,
         workspace_ids=[role.workspace_id],
         max_messages_count=context.app_config.
         WORKSPACE__JOIN__MAX_MESSAGES_HISTORY_COUNT,
     )
Exemplo n.º 10
0
    def test_unit_test_generate_if_none__err__exception(self) -> None:
        tracim_context = TracimContext()

        a = None

        class ANotFound(Exception):
            pass

        def a_id_fetcher():
            raise ValueError()

        def a_generator(id_fetcher: Callable):
            try:
                return int(id_fetcher())
            except ValueError:
                raise ANotFound()

        with pytest.raises(ANotFound):
            tracim_context._generate_if_none(
                a,
                a_generator,
                a_id_fetcher
            )
Exemplo n.º 11
0
    def _create_role_event(self, operation: OperationType,
                           role: UserRoleInWorkspace,
                           context: TracimContext) -> None:
        current_user = context.safe_current_user()
        workspace_api = WorkspaceApi(
            session=context.dbsession,
            config=self._config,
            show_deleted=True,
            # INFO - G.M - 2020-17-09 - we do explicitly don't set user here to not
            # have filter on workspace, in some case we do want user create event on workspace
            # he doesn't have access: when he remove itself from workspace for example
            current_user=None,
        )
        workspace_in_context = workspace_api.get_workspace_with_context(
            workspace_api.get_one(role.workspace_id))
        user_api = UserApi(current_user,
                           context.dbsession,
                           self._config,
                           show_deleted=True)
        role_api = RoleApi(current_user=current_user,
                           session=context.dbsession,
                           config=self._config)
        try:
            user_field = EventApi.user_schema.dump(
                user_api.get_user_with_context(user_api.get_one(
                    role.user_id))).data
        except UserDoesNotExist:
            # It is possible to have an already deleted user when deleting his roles.
            user_field = None

        role_in_context = role_api.get_user_role_workspace_with_context(role)
        fields = {
            Event.USER_FIELD:
            user_field,
            Event.WORKSPACE_FIELD:
            EventApi.workspace_schema.dump(workspace_in_context).data,
            Event.MEMBER_FIELD:
            EventApi.workspace_user_role_schema.dump(role_in_context).data,
        }
        event_api = EventApi(current_user, context.dbsession, self._config)
        event_api.create_event(
            entity_type=EntityType.WORKSPACE_MEMBER,
            operation=operation,
            additional_fields=fields,
            context=context,
        )
Exemplo n.º 12
0
 def _create_workspace_event(self, operation: OperationType,
                             workspace: Workspace,
                             context: TracimContext) -> None:
     current_user = context.safe_current_user()
     api = WorkspaceApi(current_user=current_user,
                        session=context.dbsession,
                        config=self._config)
     workspace_in_context = api.get_workspace_with_context(workspace)
     fields = {
         Event.WORKSPACE_FIELD:
         EventApi.workspace_schema.dump(workspace_in_context).data
     }
     event_api = EventApi(current_user, context.dbsession, self._config)
     event_api.create_event(
         entity_type=EntityType.WORKSPACE,
         operation=operation,
         additional_fields=fields,
         context=context,
     )
Exemplo n.º 13
0
    def _create_mention_events(
        cls, mentions: typing.Iterable[Mention], content: Content, context: TracimContext
    ) -> None:
        role_api = RoleApi(session=context.dbsession, config=context.app_config, current_user=None)

        workspace_members_usernames = [
            user.username for user in role_api.get_workspace_members(content.workspace_id)
        ]

        for mention in mentions:
            recipient = mention.recipient
            if (
                recipient not in workspace_members_usernames
                and recipient not in ALL__GROUP_MENTIONS
            ):
                raise UserNotMemberOfWorkspace(
                    "This user is not a member of this workspace: {}".format(mention.recipient)
                )

        current_user = context.safe_current_user()
        content_api = ContentApi(context.dbsession, current_user, context.app_config)
        content_in_context = content_api.get_content_in_context(content)
        workspace_api = WorkspaceApi(context.dbsession, current_user, context.app_config)
        workspace_in_context = workspace_api.get_workspace_with_context(
            workspace_api.get_one(content_in_context.workspace.workspace_id)
        )
        content_schema = EventApi.get_content_schema_for_type(content.type)
        content_dict = content_schema.dump(content_in_context).data
        common_fields = {
            Event.CONTENT_FIELD: content_dict,
            Event.WORKSPACE_FIELD: EventApi.workspace_schema.dump(workspace_in_context).data,
        }

        event_api = EventApi(current_user, context.dbsession, context.app_config)
        for mention in mentions:
            fields = {cls.MENTION_FIELD: {"recipient": mention.recipient, "id": mention.id}}
            fields.update(common_fields)
            event_api.create_event(
                entity_type=EntityType.MENTION,
                operation=OperationType.CREATED,
                additional_fields=fields,
                context=context,
            )
Exemplo n.º 14
0
 def _create_user_event(self, operation: OperationType, user: User,
                        context: TracimContext) -> None:
     current_user = context.safe_current_user()
     user_api = UserApi(
         current_user=current_user,
         session=context.dbsession,
         config=self._config,
         show_deleted=True,
     )
     fields = {
         Event.USER_FIELD:
         EventApi.user_schema.dump(
             user_api.get_user_with_context(user)).data
     }
     event_api = EventApi(current_user, context.dbsession, self._config)
     event_api.create_event(
         entity_type=EntityType.USER,
         operation=operation,
         additional_fields=fields,
         context=context,
     )