Exemplo n.º 1
0
def delete(context, request):
    context.annotation.moderation = None

    event = events.AnnotationEvent(request, context.annotation.id, "update")
    request.notify_after_commit(event)

    return HTTPNoContent()
Exemplo n.º 2
0
def test_query_mismatch():
    """Make sure that the lack of matching prevents calling
    the AnnotationNotifier"""
    annotation = {
        'permissions': {
            'read': ["group:__world__"]
        },
        'parent': {
            'user': '******'
        },
        'user': '******'
    }
    request = DummyRequest()
    event = events.AnnotationEvent(request, annotation, 'create')
    with patch('h.notifier.AnnotationNotifier') as mock_notif:
        with patch('h.auth.local.models.UserSubscriptions') as mock_subs:
            with patch('h.notifier.FilterHandler') as mock_filter:
                query = QueryMock(active=True)
                als = Mock()
                als.all = Mock(return_value=[query])
                mock_subs.get_all = Mock(return_value=als)

                mock_filter().match = Mock(return_value=False)
                notifier.send_notifications(event)
                actual = mock_notif().send_notification_to_owner.call_count
                assert actual == 0
Exemplo n.º 3
0
Arquivo: store.py Projeto: shepazu/h
def after_request(response):
    if flask.request.method == 'OPTIONS':
        in_value = response.headers.get('Access-Control-Allow-Headers', '')
        allowed = [h.strip() for h in in_value.split(',')]
        allowed.append('X-Client-ID')
        out_value = ', '.join(allowed)
        response.headers['Access-Control-Allow-Headers'] = out_value
        return response

    if 200 <= response.status_code < 300:
        match = re.match(r'^store\.(\w+)_annotation$', flask.request.endpoint)
        if match:
            request = get_current_request()

            action = match.group(1)
            if action == 'delete':
                data = json.loads(flask.request.data)
            else:
                data = json.loads(response.data)

            annotation = wrap_annotation(data)
            event = events.AnnotationEvent(request, annotation, action)

            request.registry.notify(event)
    return response
Exemplo n.º 4
0
def test_send_if_everything_is_okay():
    """Test whether we call the send_email() if every condition is okay"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()

        annotation = store_fake_data[1]
        event = events.AnnotationEvent(request, annotation, 'create')
        with patch('h.notification.reply_template.Subscriptions') as mock_subs:
            mock_subs.get_active_subscriptions_for_a_type.return_value = [
                MockSubscription(id=1, uri='acct:[email protected]')
            ]
            with patch('h.notification.reply_template.check_conditions'
                       ) as mock_conditions:
                mock_conditions.return_value = True
                with patch(
                        'h.notification.reply_template.render') as mock_render:
                    mock_render.return_value = ''
                    with patch('h.notification.reply_template.get_user_by_name'
                               ) as mock_user_db:
                        user = Mock()
                        user.email = '*****@*****.**'
                        mock_user_db.return_value = user
                        with patch('h.notification.reply_template.send_email'
                                   ) as mock_mail:
                            rt.send_notifications(event)
                            assert mock_mail.called is True
Exemplo n.º 5
0
def test_action_update():
    """It action is not create, it should immediately return"""
    annotation = {}
    request = DummyRequest()
    event = events.AnnotationEvent(request, annotation, 'update')
    with patch('h.notification.reply_template.parent_values') as mock_parent:
        rt.send_notifications(event)
        assert mock_parent.call_count == 0
Exemplo n.º 6
0
    def test_it_enqueues_delete_annotation_celery_task_for_delete(
            self, add_annotation, delete_annotation, pyramid_request):
        event = events.AnnotationEvent(pyramid_request,
                                       {"id": "test_annotation_id"}, "delete")

        subscribers.subscribe_annotation_event(event)

        delete_annotation.delay.assert_called_once_with(event.annotation_id)
        assert not add_annotation.delay.called
Exemplo n.º 7
0
def create(context, request):

    svc = request.find_service(name='annotation_moderation')
    svc.hide(context.annotation)

    event = events.AnnotationEvent(request, context.annotation.id, 'update')
    request.notify_after_commit(event)

    return HTTPNoContent()
Exemplo n.º 8
0
def delete(context, request):

    svc = request.find_service(name="annotation_moderation")
    svc.unhide(context.annotation)

    event = events.AnnotationEvent(request, context.annotation.id, "update")
    request.notify_after_commit(event)

    return HTTPNoContent()
Exemplo n.º 9
0
def create(context, request):
    annotation = context.annotation

    if not annotation.is_hidden:
        annotation.moderation = AnnotationModeration()

    event = events.AnnotationEvent(request, context.annotation.id, "update")
    request.notify_after_commit(event)

    return HTTPNoContent()
Exemplo n.º 10
0
def test_authorization():
    """Make sure private annotations don't send notifications
    """
    annotation = {'permissions': {'read': ['acct:[email protected]']}}
    request = DummyRequest()
    event = events.AnnotationEvent(request, annotation, 'create')

    with patch('h.notifier.AnnotationNotifier') as mock:
        notifier.send_notifications(event)
        assert mock.call_count == 0
Exemplo n.º 11
0
def delete(context, request):
    if not request.has_permission('admin', context.group):
        raise HTTPNotFound()

    svc = request.find_service(name='annotation_moderation')
    svc.unhide(context.annotation)

    event = events.AnnotationEvent(request, context.annotation.id, 'update')
    request.notify_after_commit(event)

    return HTTPNoContent()
Exemplo n.º 12
0
def test_action_create():
    """If the action is create, it'll try to get the subscriptions"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()

        annotation = store_fake_data[1]
        event = events.AnnotationEvent(request, annotation, 'create')
        with patch('h.notification.reply_template.Subscriptions') as mock_subs:
            mock_subs.get_active_subscriptions_for_a_type.return_value = []
            rt.send_notifications(event)
            assert mock_subs.get_active_subscriptions_for_a_type.called
Exemplo n.º 13
0
    def test_it_enqueues_add_annotation_celery_task(self,
                                                    action,
                                                    add_annotation,
                                                    delete_annotation,
                                                    pyramid_request):
        event = events.AnnotationEvent(pyramid_request,
                                       {'id': 'test_annotation_id'},
                                       action)

        subscribers.subscribe_annotation_event(event)

        add_annotation.delay.assert_called_once_with(event.annotation_id)
        assert not delete_annotation.delay.called
Exemplo n.º 14
0
def test_passive_queries():
    """Make sure if a query is passive the notifier system
    does not get called"""
    annotation = {'permissions': {'read': ["group:__world__"]}}
    request = DummyRequest()
    event = events.AnnotationEvent(request, annotation, 'create')

    with patch('h.notifier.AnnotationNotifier') as mock_notif:
        with patch('h.auth.local.models.UserSubscriptions.get_all') \
                as mock_subscription:
            query = QueryMock()
            mock_subscription.all = Mock(return_value=[query])
            mock_notif().send_notification_to_owner = Mock()
            notifier.send_notifications(event)
            assert mock_notif().send_notification_to_owner.call_count == 0
Exemplo n.º 15
0
def test_check_conditions_false_stops_sending():
    """If the check conditions() returns False, no notification is sent"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()

        annotation = store_fake_data[1]
        event = events.AnnotationEvent(request, annotation, 'create')
        with patch('h.notification.reply_template.Subscriptions') as mock_subs:
            mock_subs.get_active_subscriptions_for_a_type.return_value = [
                MockSubscription(id=1, uri='acct:[email protected]')
            ]
            with patch('h.notification.reply_template.check_conditions'
                       ) as mock_conditions:
                mock_conditions.return_value = False
                with patch('h.notification.reply_template.send_email'
                           ) as mock_mail:
                    rt.send_notifications(event)
                    assert mock_mail.called is False
Exemplo n.º 16
0
def after_request(response):
    if flask.request.method == 'OPTIONS':
        return response

    if 200 <= response.status_code < 300:
        match = re.match(r'^store\.(\w+)_annotation$', flask.request.endpoint)
        if match:
            request = get_current_request()

            action = match.group(1)
            if action == 'delete':
                data = json.loads(flask.request.data)
            else:
                data = json.loads(response.data)

            annotation = wrap_annotation(data)
            event = events.AnnotationEvent(request, annotation, action)

            request.registry.notify(event)
    return response
Exemplo n.º 17
0
def _trigger_event(request, annotation, action):
    """Trigger any callback functions listening for AnnotationEvents"""
    event = events.AnnotationEvent(request, annotation, action)
    request.registry.notify(event)