def test_unit__get_receiver_ids_workspace_members_event__nominal_case( self, session, workspace_and_users, admin_user, user_api_factory, workspace_api_factory, role_api_factory, ): (my_workspace, same_workspace_user, role, other_user, event_initiator) = workspace_and_users workspace_api = workspace_api_factory.get() workspace_in_context = workspace_api.get_workspace_with_context(my_workspace) rapi = role_api_factory.get() user_api = user_api_factory.get() role_in_context = rapi.get_user_role_workspace_with_context(role) fields = { Event.AUTHOR_FIELD: UserSchema() .dump(user_api.get_user_with_context(event_initiator)) .data, Event.USER_FIELD: UserSchema() .dump(user_api.get_user_with_context(event_initiator)) .data, Event.CLIENT_TOKEN_FIELD: "test", Event.WORKSPACE_FIELD: WorkspaceSchema().dump(workspace_in_context).data, Event.MEMBER_FIELD: WorkspaceMemberDigestSchema().dump(role_in_context).data, } event = Event( entity_type=EntityType.WORKSPACE_MEMBER, operation=OperationType.MODIFIED, fields=fields ) receivers_ids = FakeLiveMessageBuilder()._get_receiver_ids(event, session) assert event_initiator.user_id in receivers_ids assert same_workspace_user.user_id in receivers_ids assert other_user.user_id not in receivers_ids assert admin_user.user_id in receivers_ids
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
def test_unit__get_receiver_ids_workspace_event__accessible_workspace( self, session, accessible_workspace_and_users, admin_user, user_api_factory, workspace_api_factory, ): ( my_workspace, same_workspace_user, _, other_user, event_initiator, ) = accessible_workspace_and_users workspace_api = workspace_api_factory.get() workspace_in_context = workspace_api.get_workspace_with_context(my_workspace) user_api = user_api_factory.get() fields = { Event.AUTHOR_FIELD: UserSchema() .dump(user_api.get_user_with_context(event_initiator)) .data, Event.CLIENT_TOKEN_FIELD: "test", Event.WORKSPACE_FIELD: WorkspaceSchema().dump(workspace_in_context).data, } event = Event( entity_type=EntityType.WORKSPACE, operation=OperationType.MODIFIED, fields=fields ) receivers_ids = FakeLiveMessageBuilder()._get_receiver_ids(event, session) assert event_initiator.user_id in receivers_ids assert same_workspace_user.user_id in receivers_ids assert other_user.user_id in receivers_ids assert admin_user.user_id in receivers_ids
def test_unit__get_receiver_ids_user_event__nominal_case( self, session, workspace_and_users, admin_user, user_api_factory): (my_workspace, same_workspace_user, _, other_user, event_initiator) = workspace_and_users user_api = user_api_factory.get() fields = { Event.AUTHOR_FIELD: UserSchema().dump( user_api.get_user_with_context(event_initiator)).data, Event.CLIENT_TOKEN_FIELD: "test", Event.USER_FIELD: UserSchema().dump( user_api.get_user_with_context(event_initiator)).data, } event = Event(entity_type=EntityType.USER, operation=OperationType.MODIFIED, fields=fields) receivers_ids = FakeLiveMessageBuilder()._get_receiver_ids( event, session) assert event_initiator.user_id in receivers_ids assert same_workspace_user.user_id in receivers_ids assert other_user.user_id not in receivers_ids assert admin_user.user_id in receivers_ids
def test_unit__get_receiver_ids_content_event__nominal_case( self, session, user_api_factory, content_type_list, content_api_factory, admin_user, workspace_api_factory, role_api_factory, workspace_and_users, ): (my_workspace, same_workspace_user, role, other_user, event_initiator) = workspace_and_users workspace_api = workspace_api_factory.get() user_api = user_api_factory.get() workspace_in_context = workspace_api.get_workspace_with_context( my_workspace) content_api = content_api_factory.get(current_user=event_initiator) folder = content_api.create( label="test_folder", content_type_slug=content_type_list.Folder.slug, workspace=my_workspace, do_save=True, do_notify=False, ) transaction.commit() content_in_context = content_api.get_content_in_context(folder) fields = { Event.AUTHOR_FIELD: UserSchema().dump( user_api.get_user_with_context(event_initiator)).data, Event.CONTENT_FIELD: ContentSchema().dump(content_in_context).data, Event.CLIENT_TOKEN_FIELD: "test", Event.WORKSPACE_FIELD: WorkspaceSchema().dump(workspace_in_context).data, } event = Event(entity_type=EntityType.CONTENT, operation=OperationType.MODIFIED, fields=fields) receivers_ids = FakeLiveMessageBuilder()._get_receiver_ids( event, session) assert event_initiator.user_id in receivers_ids assert same_workspace_user.user_id in receivers_ids assert other_user.user_id not in receivers_ids assert admin_user.user_id not in receivers_ids
def test_unit_get_receiver_ids( self, recipient: str, receiver_ids: typing.Tuple[int], one_content_with_a_mention: Content, session, app_config, ) -> None: event = Event( entity_type=EntityType.MENTION, operation=OperationType.CREATED, fields={ "mention": { "recipient": recipient, "id": "foobar123" }, "workspace": { "workspace_id": one_content_with_a_mention.workspace.workspace_id }, }, ) assert receiver_ids == MentionBuilder.get_receiver_ids( event, session, app_config)
def test_unit__get_receiver_ids_workspace_subscription_event__reject_subscription( self, session: TracimSession, user_api_factory: UserApiFactory, subscription_lib_factory: SubscriptionLibFactory, admin_user: User, workspace_api_factory: WorkspaceApiFactory, role_api_factory: RoleApiFactory, ): user_api = user_api_factory.get() profile = Profile.USER subscriber = user_api.create_user( "*****@*****.**", password="******", do_save=True, do_notify=False, profile=profile, ) workspace_content_manager = user_api.create_user( "*****@*****.**", password="******", do_save=True, do_notify=False, profile=profile, ) workspace_manager = user_api.create_user( "*****@*****.**", password="******", do_save=True, do_notify=False, profile=profile, ) other_user = user_api.create_user( "*****@*****.**", password="******", do_save=True, do_notify=False, profile=profile, ) workspace_api = workspace_api_factory.get(current_user=admin_user) my_workspace = workspace_api.create_workspace( "test workspace", save_now=True, access_type=WorkspaceAccessType.ON_REQUEST ) workspace_in_context = workspace_api.get_workspace_with_context(my_workspace) subscription_lib = subscription_lib_factory.get(current_user=subscriber) rapi = role_api_factory.get(current_user=subscriber) rapi.create_one( workspace_content_manager, my_workspace, UserRoleInWorkspace.CONTENT_MANAGER, False ) rapi.create_one( workspace_manager, my_workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, False ) subscription = subscription_lib.submit_subscription(my_workspace) subscription_lib.reject_subscription(subscription) transaction.commit() fields = { Event.AUTHOR_FIELD: UserSchema().dump(user_api.get_user_with_context(admin_user)).data, Event.WORKSPACE_FIELD: WorkspaceSchema().dump(workspace_in_context).data, Event.SUBSCRIPTION_FIELD: WorkspaceSubscriptionSchema().dump(subscription).data, } event = Event( entity_type=EntityType.WORKSPACE_SUBSCRIPTION, operation=OperationType.MODIFIED, fields=fields, ) receivers_ids = FakeLiveMessageBuilder()._get_receiver_ids(event, session) assert subscriber.user_id in receivers_ids assert workspace_manager.user_id in receivers_ids assert admin_user.user_id in receivers_ids assert workspace_content_manager.user_id not in receivers_ids assert other_user.user_id not in receivers_ids