Beispiel #1
0
    def test_replace(self):
        """replace method adds function to patch object"""
        patch = ApiPatch()

        def test_function():
            pass
        patch.replace('test-replace', test_function)

        self.assertEqual(len(patch._actions), 1)
        self.assertEqual(patch._actions[0]['op'], 'replace')
        self.assertEqual(patch._actions[0]['path'], 'test-replace')
        self.assertEqual(patch._actions[0]['handler'], test_function)
Beispiel #2
0
    def test_add(self):
        """add method adds function to patch object"""
        patch = ApiPatch()

        def test_function():
            pass
        patch.add('test-add', test_function)

        self.assertEqual(len(patch._actions), 1)
        self.assertEqual(patch._actions[0]['op'], 'add')
        self.assertEqual(patch._actions[0]['path'], 'test-add')
        self.assertEqual(patch._actions[0]['handler'], test_function)
Beispiel #3
0
    def test_replace(self):
        """replace method adds function to patch object"""
        patch = ApiPatch()

        def mock_function():
            pass

        patch.replace('test-replace', mock_function)

        self.assertEqual(len(patch._actions), 1)
        self.assertEqual(patch._actions[0]['op'], 'replace')
        self.assertEqual(patch._actions[0]['path'], 'test-replace')
        self.assertEqual(patch._actions[0]['handler'], mock_function)
Beispiel #4
0
    def test_add(self):
        """add method adds function to patch object"""
        patch = ApiPatch()

        def mock_function():
            pass

        patch.add('test-add', mock_function)

        self.assertEqual(len(patch._actions), 1)
        self.assertEqual(patch._actions[0]['op'], 'add')
        self.assertEqual(patch._actions[0]['path'], 'test-add')
        self.assertEqual(patch._actions[0]['handler'], mock_function)
Beispiel #5
0
    def test_dispatch_action(self):
        """dispatch_action calls specified actions"""
        patch = ApiPatch()

        mock_target = MockObject(13)

        def action_a(request 37 , target, value):
            self.assertEqual(request, 'request')
            self.assertEqual(target, mock_target)
            return {'a': value * 2, 'b': 111}
Beispiel #6
0
    def test_validate_action(self):
        """validate_action method validates action dict"""
        patch = ApiPatch()

        VALID_ACTIONS = [
            {
                'op': 'add',
                'path': 'test',
                'value': 42
            },
            {
                'op': 'remove',
                'path': 'other-test',
                'value': 'Lorem'
            },
            {
                'op': 'replace',
                'path': 'false-test',
                'value': None
            },
        ]

        for action in VALID_ACTIONS:
            patch.validate_action(action)

        # undefined op
        UNSUPPORTED_ACTIONS = (
            {},
            {
                'op': ''
            },
            {
                'no': 'op'
            },
        )

        for action in UNSUPPORTED_ACTIONS:
            try:
                patch.validate_action(action)
            except InvalidAction as e:
                self.assertEqual(e.args[0], "undefined op")

        # unsupported op
        try:
            patch.validate_action({'op': 'nope'})
        except InvalidAction as e:
            self.assertEqual(e.args[0], '"nope" op is unsupported')

        # op lacking patch
        try:
            patch.validate_action({'op': 'add'})
        except InvalidAction as e:
            self.assertEqual(e.args[0], '"add" op has to specify path')

        # op lacking value
        try:
            patch.validate_action({
                'op': 'add',
                'path': 'yolo',
            })
        except InvalidAction as e:
            self.assertEqual(e.args[0], '"add" op has to specify value')

        # empty value is allowed
        try:
            patch.validate_action({
                'op': 'add',
                'path': 'yolo',
                'value': '',
            })
        except InvalidAction as e:
            self.assertEqual(e.args[0], '"add" op has to specify value')
Beispiel #7
0
    def test_dispatch(self):
        """dispatch calls actions and returns response"""
        patch = ApiPatch()

        def action_error(request, target, value):
            if value == '404':
                raise Http404()
            if value == 'perm':
                raise PermissionDenied("yo ain't doing that!")

        patch.replace('error', action_error)

        def action_mutate(request, target, value):
            return {'value': value * 2}

        patch.replace('mutate', action_mutate)

        # dispatch requires list as an argument
        response = patch.dispatch(MockRequest({}), {})
        self.assertEqual(response.status_code, 400)

        self.assertEqual(response.data['detail'],
                         "PATCH request should be list of operations")

        # valid dispatch
        response = patch.dispatch(
            MockRequest([
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 2,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 6,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 7,
                },
            ]), MockObject(13))

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(response.data['detail']), 3)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(response.data['detail'][2], 'ok')
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 14)

        # invalid action in dispatch
        response = patch.dispatch(
            MockRequest([
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 2,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 6,
                },
                {
                    'op': 'replace',
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 7,
                },
            ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 3)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(response.data['detail'][2],
                         '"replace" op has to specify path')
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 12)

        # action in dispatch raised 404
        response = patch.dispatch(
            MockRequest([
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 2,
                },
                {
                    'op': 'replace',
                    'path': 'error',
                    'value': '404',
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 6,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 7,
                },
            ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 2)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], "NOT FOUND")
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 4)

        # action in dispatch raised perm denied
        response = patch.dispatch(
            MockRequest([
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 2,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 6,
                },
                {
                    'op': 'replace',
                    'path': 'mutate',
                    'value': 9,
                },
                {
                    'op': 'replace',
                    'path': 'error',
                    'value': 'perm',
                },
            ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 4)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(response.data['detail'][2], 'ok')
        self.assertEqual(response.data['detail'][3], "yo ain't doing that!")
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 18)
Beispiel #8
0
    def test_dispatch(self):
        """dispatch calls actions and returns response"""
        patch = ApiPatch()

        def action_error(request, target, value):
            if value == '404':
                raise Http404()
            if value == 'perm':
                raise PermissionDenied("yo ain't doing that!")
        patch.replace('error', action_error)

        def action_mutate(request, target, value):
            return {'value': value * 2}
        patch.replace('mutate', action_mutate)

        # dispatch requires list as an argument
        response = patch.dispatch(MockRequest({}), {})
        self.assertEqual(response.status_code, 400)

        self.assertEqual(
            response.data['detail'],
            "PATCH request should be list of operations")

        # valid dispatch
        response = patch.dispatch(MockRequest([
            {'op': 'replace', 'path': 'mutate', 'value': 2},
            {'op': 'replace', 'path': 'mutate', 'value': 6},
            {'op': 'replace', 'path': 'mutate', 'value': 7},
        ]), MockObject(13))

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(response.data['detail']), 3)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(response.data['detail'][2], 'ok')
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 14)

        # invalid action in dispatch
        response = patch.dispatch(MockRequest([
            {'op': 'replace', 'path': 'mutate', 'value': 2},
            {'op': 'replace', 'path': 'mutate', 'value': 6},
            {'op': 'replace'},
            {'op': 'replace', 'path': 'mutate', 'value': 7},
        ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 3)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(
            response.data['detail'][2], '"replace" op has to specify path')
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 12)

        # action in dispatch raised 404
        response = patch.dispatch(MockRequest([
            {'op': 'replace', 'path': 'mutate', 'value': 2},
            {'op': 'replace', 'path': 'error', 'value': '404'},
            {'op': 'replace', 'path': 'mutate', 'value': 6},
            {'op': 'replace', 'path': 'mutate', 'value': 7},
        ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 2)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(
            response.data['detail'][1], "NOT FOUND")
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 4)

        # action in dispatch raised perm denied
        response = patch.dispatch(MockRequest([
            {'op': 'replace', 'path': 'mutate', 'value': 2},
            {'op': 'replace', 'path': 'mutate', 'value': 6},
            {'op': 'replace', 'path': 'mutate', 'value': 9},
            {'op': 'replace', 'path': 'error', 'value': 'perm'},
        ]), MockObject(13))

        self.assertEqual(response.status_code, 400)

        self.assertEqual(len(response.data['detail']), 4)
        self.assertEqual(response.data['detail'][0], 'ok')
        self.assertEqual(response.data['detail'][1], 'ok')
        self.assertEqual(response.data['detail'][2], 'ok')
        self.assertEqual(
            response.data['detail'][3], "yo ain't doing that!")
        self.assertEqual(response.data['id'], 13)
        self.assertEqual(response.data['value'], 18)
                                         remove_participant)
from misago.threads.permissions import (
    allow_add_participant, allow_add_participants, allow_approve_thread,
    allow_change_best_answer, allow_change_owner, allow_edit_thread,
    allow_pin_thread, allow_hide_thread, allow_mark_as_best_answer,
    allow_mark_best_answer, allow_move_thread, allow_remove_participant,
    allow_see_post, allow_start_thread, allow_unhide_thread,
    allow_unmark_best_answer)
from misago.threads.serializers import ThreadParticipantSerializer
from misago.threads.validators import validate_title

PATCH_LIMIT = settings.MISAGO_THREADS_PER_PAGE + settings.MISAGO_THREADS_TAIL

UserModel = get_user_model()

thread_patch_dispatcher = ApiPatch()


def patch_acl(request, thread, value):
    """useful little op that updates thread acl to current state"""
    if value:
        add_acl(request.user, thread)
        return {'acl': thread.acl}
    else:
        return {'acl': None}


thread_patch_dispatcher.add('acl', patch_acl)


def patch_title(request, thread, value):
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext as _

from misago.acl import add_acl
from misago.core.apipatch import ApiPatch
from misago.threads.moderation import posts as moderation
from misago.threads.permissions import allow_hide_event, allow_unhide_event


event_patch_dispatcher = ApiPatch()


def patch_acl(request, event, value):
    """useful little op that updates event acl to current state"""
    if value:
        add_acl(request.user, event)
        return {'acl': event.acl}
    else:
        return {'acl': None}


event_patch_dispatcher.add('acl', patch_acl)


def patch_is_hidden(request, event, value):
    if value:
        allow_hide_event(request.user, event)
        moderation.hide_post(request.user, event)
    else:
        allow_unhide_event(request.user, event)
        moderation.unhide_post(request.user, event)
from misago.threads.moderation import threads as moderation
from misago.threads.participants import (
    add_participant, change_owner, make_participants_aware, remove_participant)
from misago.threads.permissions import (
    allow_add_participant, allow_add_participants, allow_approve_thread, allow_change_owner, allow_edit_thread,
    allow_pin_thread, allow_hide_thread, allow_move_thread, allow_remove_participant, allow_start_thread,
    allow_unhide_thread)
from misago.threads.serializers import ThreadParticipantSerializer
from misago.threads.validators import validate_title


PATCH_LIMIT = settings.MISAGO_THREADS_PER_PAGE + settings.MISAGO_THREADS_TAIL

UserModel = get_user_model()

thread_patch_dispatcher = ApiPatch()


def patch_acl(request, thread, value):
    """useful little op that updates thread acl to current state"""
    if value:
        add_acl(request.user, thread)
        return {'acl': thread.acl}
    else:
        return {'acl': None}


thread_patch_dispatcher.add('acl', patch_acl)


def patch_title(request, thread, value):
Beispiel #12
0
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.categories.models import Category
from misago.categories.permissions import (
    allow_see_category, allow_browse_category)
from misago.categories.serializers import CategorySerializer
from misago.core.apipatch import ApiPatch
from misago.core.shortcuts import get_int_or_404, get_object_or_404

from misago.threads.moderation import threads as moderation
from misago.threads.permissions import allow_start_thread
from misago.threads.utils import add_categories_to_threads


thread_patch_endpoint = ApiPatch()


def patch_weight(request, thread, value):
    message = _("You don't have permission to change this thread's weight.")
    if not thread.acl.get('can_pin'):
        raise PermissionDenied(message)
    elif thread.weight > thread.acl.get('can_pin'):
        raise PermissionDenied(message)

    if value == 2:
        if thread.acl.get('can_pin') == 2:
            moderation.pin_thread_globally(request.user, thread)
        else:
            raise PermissionDenied(
                _("You don't have permission to pin this thread globally."))
Beispiel #13
0
    def test_validate_action(self):
        """validate_action method validates action dict"""
        patch = ApiPatch()

        VALID_ACTIONS = (
            {'op': 'add', 'path': 'test', 'value': 42},
            {'op': 'remove', 'path': 'other-test', 'value': 'Lorem'},
            {'op': 'replace', 'path': 'false-test', 'value': None},
        )

        for action in VALID_ACTIONS:
            patch.validate_action(action)

        # undefined op
        UNSUPPORTED_ACTIONS = (
            {},
            {'op': ''},
            {'no': 'op'},
        )

        for action in UNSUPPORTED_ACTIONS:
            try:
                patch.validate_action(action)
            except InvalidAction as e:
                self.assertEqual(e.args[0], u"undefined op")

        # unsupported op
        try:
            patch.validate_action({'op': 'nope'})
        except InvalidAction as e:
            self.assertEqual(e.args[0], u'"nope" op is unsupported')

        # op lacking patch
        try:
            patch.validate_action({'op': 'add'})
        except InvalidAction as e:
            self.assertEqual(e.args[0], u'"add" op has to specify path')

        # op lacking value
        try:
            patch.validate_action({'op': 'add', 'path': 'yolo'})
        except InvalidAction as e:
            self.assertEqual(e.args[0], u'"add" op has to specify value')

        # empty value is allowed
        try:
            patch.validate_action({'op': 'add', 'path': 'yolo', 'value': ''})
        except InvalidAction as e:
            self.assertEqual(e.args[0], u'"add" op has to specify value')
Beispiel #14
0
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.core.apipatch import ApiPatch
from ...moderation import posts as moderation
from ...permissions.threads import allow_approve_post, allow_hide_post, allow_protect_post, allow_unhide_post


post_patch_dispatcher = ApiPatch()


def patch_acl(request, post, value):
    """useful little op that updates post acl to current state"""
    if value:
        add_acl(request.user, post)
        return {'acl': post.acl}
    else:
        return {'acl': None}
post_patch_dispatcher.add('acl', patch_acl)


def patch_is_protected(request, post, value):
    allow_protect_post(request.user, post)
    if value:
        moderation.protect_post(request.user, post)
    else:
        moderation.unprotect_post(request.user, post)
    return {'is_protected': post.is_protected}
post_patch_dispatcher.replace('is-protected', patch_is_protected)
Beispiel #15
0
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.categories.models import Category
from misago.categories.permissions import allow_browse_category, allow_see_category
from misago.categories.serializers import CategorySerializer
from misago.core.apipatch import ApiPatch
from misago.core.shortcuts import get_int_or_404, get_object_or_404

from ...moderation import threads as moderation
from ...permissions import allow_start_thread
from ...utils import add_categories_to_items
from ...validators import validate_title


thread_patch_dispatcher = ApiPatch()


def patch_acl(request, thread, value):
    """useful little op that updates thread acl to current state"""
    if value:
        add_acl(request.user, thread)
        return {'acl': thread.acl}
    else:
        return {'acl': None}
thread_patch_dispatcher.add('acl', patch_acl)


def patch_title(request, thread, value):
    try:
        value_cleaned = six.text_type(value).strip()
Beispiel #16
0
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.core.apipatch import ApiPatch
from ...moderation import posts as moderation

event_patch_dispatcher = ApiPatch()


def patch_acl(request, event, value):
    """useful little op that updates event acl to current state"""
    if value:
        add_acl(request.user, event)
        return {'acl': event.acl}
    else:
        return {'acl': None}


event_patch_dispatcher.add('acl', patch_acl)


def patch_is_hidden(request, event, value):
    if event.acl.get('can_hide'):
        if value:
            moderation.hide_post(request.user, event)
        else:
            moderation.unhide_post(request.user, event)

        return {'is_hidden': event.is_hidden}
    else:
Beispiel #17
0
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.categories.models import Category
from misago.categories.permissions import (
    allow_see_category, allow_browse_category)
from misago.categories.serializers import CategorySerializer
from misago.core.apipatch import ApiPatch
from misago.core.shortcuts import get_int_or_404, get_object_or_404

from misago.threads.moderation import threads as moderation
from misago.threads.utils import add_categories_to_threads


thread_patch_endpoint = ApiPatch()


def patch_weight(request, thread, value):
    message = _("You don't have permission to change this thread's weight.")
    if not thread.acl.get('can_pin'):
        raise PermissionDenied(message)
    elif thread.weight > thread.acl.get('can_pin'):
        raise PermissionDenied(message)

    if value == 2:
        if thread.acl.get('can_pin') == 2:
            moderation.pin_thread_globally(request.user, thread)
        else:
            raise PermissionDenied(
                _("You don't have permission to pin this thread globally."))
Beispiel #18
0
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.core.apipatch import ApiPatch
from ...moderation import posts as moderation
from ...permissions.threads import allow_approve_post, allow_hide_post, allow_protect_post, allow_unhide_post

post_patch_dispatcher = ApiPatch()


def patch_acl(request, post, value):
    """useful little op that updates post acl to current state"""
    if value:
        add_acl(request.user, post)
        return {'acl': post.acl}
    else:
        return {'acl': None}


post_patch_dispatcher.add('acl', patch_acl)


def patch_is_protected(request, post, value):
    allow_protect_post(request.user, post)
    if value:
        moderation.protect_post(request.user, post)
    else:
        moderation.unprotect_post(request.user, post)
    return {'is_protected': post.is_protected}
Beispiel #19
0
    def test_dispatch_action(self):
        """dispatch_action calls specified actions"""
        patch = ApiPatch()

        mock_target = MockObject(13)

        def action_a(request, target, value):
            self.assertEqual(request, 'request')
            self.assertEqual(target, mock_target)
            return {'a': value * 2, 'b': 111}

        patch.replace('abc', action_a)

        def action_b(request, target, value):
            self.assertEqual(request, 'request')
            self.assertEqual(target, mock_target)
            return {'b': value * 10}

        patch.replace('abc', action_b)

        def action_fail(request, target, value):
            self.fail("unrequired action was called")

        patch.add('c', action_fail)
        patch.remove('c', action_fail)
        patch.replace('c', action_fail)

        patch_dict = {'id': 123}

        patch.dispatch_action(patch_dict, 'request', mock_target, {
            'op': 'replace',
            'path': 'abc',
            'value': 5,
        })

        self.assertEqual(len(patch_dict), 3)
        self.assertEqual(patch_dict['id'], 123)
        self.assertEqual(patch_dict['a'], 10)
        self.assertEqual(patch_dict['b'], 50)
Beispiel #20
0
from django.utils import six
from django.utils.translation import gettext as _

from misago.acl import add_acl
from misago.categories.models import Category
from misago.categories.permissions import allow_browse_category, allow_see_category
from misago.categories.serializers import CategorySerializer
from misago.core.apipatch import ApiPatch
from misago.core.shortcuts import get_int_or_404, get_object_or_404

from ...moderation import threads as moderation
from ...permissions import allow_start_thread
from ...utils import add_categories_to_threads
from ...validators import validate_title

thread_patch_dispatcher = ApiPatch()


def patch_acl(request, thread, value):
    """useful little op that updates thread acl to current state"""
    if value:
        add_acl(request.user, thread)
        return {'acl': thread.acl}
    else:
        return {'acl': None}


thread_patch_dispatcher.add('acl', patch_acl)


def patch_title(request, thread, value):
Beispiel #21
0
    def test_dispatch_action(self):
        """dispatch_action calls specified actions"""
        patch = ApiPatch()

        mock_target = MockObject(13)

        def action_a(request, target, value):
            self.assertEqual(request, 'request')
            self.assertEqual(target, mock_target)
            return {'a': value * 2, 'b': 111}
        patch.replace('abc', action_a)

        def action_b(request, target, value):
            self.assertEqual(request, 'request')
            self.assertEqual(target, mock_target)
            return {'b': value * 10}
        patch.replace('abc', action_b)

        def action_fail(request, target, value):
            self.fail("unrequired action was called")

        patch.add('c', action_fail)
        patch.remove('c', action_fail)
        patch.replace('c', action_fail)

        patch_dict = {
            'id': 123
        }

        patch.dispatch_action(patch_dict, 'request', mock_target, {
            'op': 'replace',
            'path': 'abc',
            'value': 5,
        })

        self.assertEqual(len(patch_dict), 3)
        self.assertEqual(patch_dict['id'], 123)
        self.assertEqual(patch_dict['a'], 10)
        self.assertEqual(patch_dict['b'], 50)