def _delete_annotations(self, user): annotations = self.request.db.query(Annotation).filter_by( userid=user.userid) for annotation in annotations: storage.delete_annotation(self.request.db, annotation.id) event = AnnotationEvent(self.request, annotation.id, "delete") self.request.notify_after_commit(event)
def test_generates_and_sends_mail_for_any_notification(self, pyramid_request): send = FakeMailer() get_notification = mock.Mock( spec_set=[], return_value=mock.sentinel.notification ) generate_mail = mock.Mock(spec_set=[]) generate_mail.return_value = ( ["*****@*****.**"], "Your email", "Text body", "HTML body", ) event = AnnotationEvent(pyramid_request, None, None) subscribers.send_reply_notifications( event, get_notification=get_notification, generate_mail=generate_mail, send=send, ) generate_mail.assert_called_once_with( pyramid_request, mock.sentinel.notification ) assert send.lastcall == ( ["*****@*****.**"], "Your email", "Text body", "HTML body", )
def mark(context, request): svc = request.find_service(name="mark") svc.mark(request.user, context.annotation) event = AnnotationEvent(request, context.annotation.id, "mark") request.notify_after_commit(event) return HTTPNoContent()
def downvote(context, request): svc = request.find_service(name="downvote") svc.downvote(request.user, context.annotation) event = AnnotationEvent(request, context.annotation.id, "downvote") request.notify_after_commit(event) return HTTPNoContent()
def test_we_do_nothing_for_unexpected_actions(self, search_index, pyramid_request): event = AnnotationEvent(pyramid_request, {"id": "any"}, "strange_action") result = search_index.handle_annotation_event(event) assert result is False
def test_we_dispatch_correctly(self, search_index, pyramid_request, action, handler_for): event = AnnotationEvent(pyramid_request, {"id": "any"}, action) result = search_index.handle_annotation_event(event) handler = handler_for(action, synchronous=True) handler.assert_called_once_with(event.annotation_id) assert result == handler.return_value
def test_it_calls_sync_service(self, pyramid_request, search_index, transaction_manager): event = AnnotationEvent(pyramid_request, {"id": "any"}, "action") subscribers.annotation_sync(event) transaction_manager.__enter__.assert_called_once() search_index.handle_annotation_event.assert_called_once_with(event) transaction_manager.__exit__.assert_called_once()
def _delete_annotations(self, group): if group.pubid == "__world__": raise DeletePublicGroupError("Public group can not be deleted") annotations = self.request.db.query(Annotation).filter_by( groupid=group.pubid) for annotation in annotations: storage.delete_annotation(self.request.db, annotation.id) event = AnnotationEvent(self.request, annotation.id, "delete") self.request.notify_after_commit(event)
def test_it_calls_sync_service(self, pyramid_request, search_index, transaction_manager, synchronous): pyramid_request.feature.flags = {"synchronous_indexing": synchronous} event = AnnotationEvent(pyramid_request, {"id": "any"}, "action") subscribers.sync_annotation(event) transaction_manager.__enter__.assert_called_once() search_index.handle_annotation_event.assert_called_once_with( event, synchronous=synchronous) transaction_manager.__exit__.assert_called_once()
def test_delete_publishes_event(self, api_storage, db_session, factories, matchers, pyramid_request, svc): user = factories.User() ann = factories.Annotation(userid=user.userid) svc.delete(user) expected_event = AnnotationEvent(pyramid_request, ann.id, 'delete') actual_event = pyramid_request.notify_after_commit.call_args[0][0] assert (expected_event.request, expected_event.annotation_id, expected_event.action) == \ (actual_event.request, actual_event.annotation_id, actual_event.action)
def test_delete_publishes_annotation_events(self, storage, factories, pyramid_request, svc): group = factories.Group() annotation = factories.Annotation(groupid=group.pubid) svc.delete(group) expected_event = AnnotationEvent(pyramid_request, annotation.id, 'delete') actual_event = pyramid_request.notify_after_commit.call_args[0][0] assert (expected_event.request, expected_event.annotation_id, expected_event.action) == \ (actual_event.request, actual_event.annotation_id, actual_event.action)
def delete(self, annotation): """ Delete the given annotation. :param annotation: the annotation to be deleted :type annotation: h.models.Annotation """ annotation.updated = datetime.utcnow() annotation.deleted = True event = AnnotationEvent(self.request, annotation.id, "delete") self.request.notify_after_commit(event)
def test_we_fallback_to_async_if_sync_fails(self, search_index, pyramid_request, action, handler_for): event = AnnotationEvent(pyramid_request, {"id": "any"}, action) sync_handler = handler_for(action, synchronous=True) sync_handler.side_effect = ValueError result = search_index.handle_annotation_event(event, synchronous=True) sync_handler.assert_called_once_with(event.annotation_id) async_handler = handler_for(action, synchronous=False) async_handler.assert_called_once_with(event.annotation_id) assert result == async_handler.return_value
def test_it_publishes_a_delete_event(self, svc, pyramid_request, factories, annotation): ann = annotation() svc.delete(ann) expected_event = AnnotationEvent(pyramid_request, ann.id, "delete") actual_event = pyramid_request.notify_after_commit.call_args[0][0] assert ( expected_event.request, expected_event.annotation_id, expected_event.action, ) == (actual_event.request, actual_event.annotation_id, actual_event.action)
def test_calls_get_notification_with_request_annotation_and_action( self, fetch_annotation, pyramid_request): send = FakeMailer() get_notification = mock.Mock(spec_set=[], return_value=None) generate_mail = mock.Mock(spec_set=[], return_value=[]) event = AnnotationEvent(pyramid_request, mock.sentinel.annotation_id, mock.sentinel.action) subscribers.send_reply_notifications(event, get_notification=get_notification, generate_mail=generate_mail, send=send) fetch_annotation.assert_called_once_with(pyramid_request.db, mock.sentinel.annotation_id) get_notification.assert_called_once_with(pyramid_request, fetch_annotation.return_value, mock.sentinel.action)
def delete_user(request, user): """ Deletes a user with all their group memberships and annotations. Raises UserDeletionError when deletion fails with the appropriate error message. """ if models.Group.created_by(request.db, user).count() > 0: raise UserDeletionError('Cannot delete user who is a group creator.') user.groups = [] annotations = request.db.query(models.Annotation) \ .filter_by(userid=user.userid) for annotation in annotations: storage.delete_annotation(request.db, annotation.id) event = AnnotationEvent(request, annotation.id, 'delete') request.notify_after_commit(event) request.db.delete(user)
def _publish_annotation_event(request, annotation, action): """Publish an event to the annotations queue for this annotation action.""" event = AnnotationEvent(request, annotation.id, action) request.notify_after_commit(event)
def event(self, pyramid_request): pyramid_request.realtime = mock.Mock() event = AnnotationEvent(pyramid_request, "test_annotation_id", "create") return event
def event(self, pyramid_request): return AnnotationEvent(pyramid_request, {"id": "any"}, "action")
def test_annotation_event(): evt = AnnotationEvent(s.request, s.annotation_id, s.action) assert evt.request == s.request assert evt.annotation_id == s.annotation_id assert evt.action == s.action