def test_add_resources_add_views(self):
        from pyramid_rest.resource import (
            ResourceConfigurator,
            Resource,
            not_allowed_view,
            )

        dad_index = mock.Mock(spec=not_allowed_view)
        dad_show = mock.Mock(spec=not_allowed_view)

        kid_index = mock.Mock(spec=not_allowed_view)
        kid_show = mock.Mock(spec=not_allowed_view)

        kid = Resource('dad.kid')
        dad = Resource('dad')

        config = self._get_config()

        ru = ResourceConfigurator()
        ru._validate_views_args_name = lambda a: a

        # simulate decorating methods:
        dad.index()(dad_index)
        dad.show()(dad_show)
        kid.index()(kid_index)
        kid.show()(kid_show)

        ru._add(config, dad)
        ru._add(config, kid)

        self.assertEqual(14, config.add_view.call_count)

        # check dad views:

        self._check_add_view(dad_index, 'index', 'GET', 'dad_collection', config.add_view.call_args_list[0])
        self._check_add_view(dad_show, 'show', 'GET', 'dad_item', config.add_view.call_args_list[1])

        self._check_add_not_allowed(not_allowed_view, 'edit', 'GET', 'dad_edit', config.add_view.call_args_list[2])
        self._check_add_not_allowed(not_allowed_view, 'new', 'GET', 'dad_new', config.add_view.call_args_list[3])
        self._check_add_not_allowed(not_allowed_view, 'create', 'POST', 'dad_collection', config.add_view.call_args_list[4])
        self._check_add_not_allowed(not_allowed_view, 'update', 'PUT', 'dad_item', config.add_view.call_args_list[5])
        self._check_add_not_allowed(not_allowed_view, 'delete', 'DELETE', 'dad_item', config.add_view.call_args_list[6])

        # check kid views:

        self._check_add_view(kid_index, 'index', 'GET', 'dad.kid_collection', config.add_view.call_args_list[7])
        self._check_add_view(kid_show, 'show', 'GET', 'dad.kid_item', config.add_view.call_args_list[8])

        self._check_add_not_allowed(not_allowed_view, 'edit', 'GET', 'dad.kid_edit', config.add_view.call_args_list[9])
        self._check_add_not_allowed(not_allowed_view, 'new', 'GET', 'dad.kid_new', config.add_view.call_args_list[10])
        self._check_add_not_allowed(not_allowed_view, 'create', 'POST', 'dad.kid_collection', config.add_view.call_args_list[11])
        self._check_add_not_allowed(not_allowed_view, 'update', 'PUT', 'dad.kid_item', config.add_view.call_args_list[12])
        self._check_add_not_allowed(not_allowed_view, 'delete', 'DELETE', 'dad.kid_item', config.add_view.call_args_list[13])
    def test_add_sub_resource(self):
        from pyramid_rest.resource import ResourceConfigurator, Resource

        dad = Resource('dad')
        kid = Resource('dad.kid')

        config = self._get_config()

        # kid is a sub resource:
        # Resource utility must defer processing until parent resource is added
        ru = ResourceConfigurator()
        ru._add(config, kid)

        self.assertEqual({'dad.kid': kid}, ru.deferred)

        ru._add(config, dad)
Beispiel #3
0
def includeme(config):
    log.info('Including pyramid_rest')

    utility = ResourceConfigurator()

    config.registry.registerUtility(utility)

    config.add_renderer('pyramid_rest_renderer',
                        'pyramid_rest.renderer.Factory')

    config.add_directive('add_resource', utility.add_resource)
    config.add_directive(
        'add_singular_resource',
        functools.partial(utility.add_resource, singular=True),
    )
    config.add_request_method(rest_resource_url, 'rest_resource_url')
    config.add_request_method(rest_resource_path, 'rest_resource_path')

    if asbool(config.registry.settings.get('pyramid_rest.tunneling', 'true')):
        config.add_subscriber_predicate('request_methods',
                                        RequestMethodEventPredicate)

        config.add_subscriber(
            override_request_method,
            NewRequest,
            request_methods=['POST'],
        )

    if asbool(config.registry.settings.get('pyramid_rest.mongo', 'true')):
        config.include('pyramid_rest.mongo')

    config.commit()
    def test_lineage_ids(self):
        from pyramid.config import Configurator
        from pyramid_rest.resource import Resource, ResourceConfigurator

        config = Configurator(settings={})
        ru = ResourceConfigurator()

        dad = Resource('dad')
        kid = Resource('dad.kid')
        grandchild = Resource('dad.kid.grandchild')

        ru._add(config, dad)
        ru._add(config, kid)
        ru._add(config, grandchild)

        self.assertEqual(['dad_id'], dad.lineage_ids)
        self.assertEqual(['dad_id', 'kid_id'], kid.lineage_ids)
        self.assertEqual(['dad_id', 'kid_id', 'grandchild_id'], grandchild.lineage_ids)

        self.assertEqual(['id'], dad.ids)
        self.assertEqual(['dad_id', 'id'], kid.ids)
        self.assertEqual(['dad_id', 'kid_id', 'id'], grandchild.ids)
Beispiel #5
0
    def test_wrong_parent_id_names(self):
        from pyramid_rest.resource import Resource, ResourceConfigurator

        config = self._get_config()
        ru = ResourceConfigurator()

        # add parents:
        ru._add(config, Resource('application'))
        ru._add(config, Resource('application.user'))

        res = Resource('application.user.message')
        # decorate view function
        res.show()(wrong_ids_show)

        with self.assertRaises(TypeError):
            # simulate venusian callback
            ru._add(config, res)
Beispiel #6
0
    def test_init(self):
        from pyramid_rest.resource import ResourceConfigurator

        ru = ResourceConfigurator()

        self.assertEqual(
            dict(
                index='GET',
                create='POST',
                show='GET',
                update='PUT',
                delete='DELETE',
                new='GET',
                edit='GET',
            ),
            ru.methods,
        )
Beispiel #7
0
    def test_add_sub_resource(self):
        from pyramid_rest.resource import ResourceConfigurator, Resource

        dad = Resource('dad')
        kid = Resource('dad.kid')

        config = self._get_config()

        # kid is a sub resource:
        # Resource utility must defer processing until parent resource is added
        ru = ResourceConfigurator()
        ru._add(config, kid)

        self.assertEqual({'dad.kid': kid}, ru.deferred)

        ru._add(config, dad)
Beispiel #8
0
    def test_lineage_ids(self):
        from pyramid.config import Configurator
        from pyramid_rest.resource import Resource, ResourceConfigurator

        config = Configurator(settings={})
        ru = ResourceConfigurator()

        dad = Resource('dad')
        kid = Resource('dad.kid')
        grandchild = Resource('dad.kid.grandchild')

        ru._add(config, dad)
        ru._add(config, kid)
        ru._add(config, grandchild)

        self.assertEqual(['dad_id'], dad.lineage_ids)
        self.assertEqual(['dad_id', 'kid_id'], kid.lineage_ids)
        self.assertEqual(['dad_id', 'kid_id', 'grandchild_id'],
                         grandchild.lineage_ids)

        self.assertEqual(['id'], dad.ids)
        self.assertEqual(['dad_id', 'id'], kid.ids)
        self.assertEqual(['dad_id', 'kid_id', 'id'], grandchild.ids)
Beispiel #9
0
    def test_add_resources_route(self, m_functools):
        from pyramid_rest.resource import ResourceConfigurator, Resource

        kid = Resource('dad.kid')
        dad = Resource('dad')

        config = self._get_config()

        ru = ResourceConfigurator()

        ru._add(config, dad)

        self.assertEqual({'dad': dad}, ru.resources)
        self.assertEqual({'dad': dad}, ru.parent_resources)

        self.assertEqual(
            mock.call(
                pattern='/dads/new',
                name='dad_new',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[0])
        self.assertEqual(
            mock.call(
                pattern='/dads/{id}',
                name='dad_item',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[1])
        self.assertEqual(
            mock.call(
                pattern='/dads/{id}/edit',
                name='dad_edit',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[2])
        self.assertEqual(
            mock.call(
                pattern='/dads',
                name='dad_collection',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[3])
        ru._add(config, kid)
        self.assertEqual({'dad': dad, 'dad.kid': kid}, ru.resources)
        self.assertEqual({'dad': dad}, ru.parent_resources)

        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/new',
                name='dad.kid_new',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[4])
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/{id}',
                name='dad.kid_item',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[5])
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/{id}/edit',
                name='dad.kid_edit',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[6])
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids',
                name='dad.kid_collection',
                factory=m_functools.partial.return_value,
            ), config.add_route.call_args_list[7])
Beispiel #10
0
    def test_add_resources_add_views(self):
        from pyramid_rest.resource import (
            ResourceConfigurator,
            Resource,
            not_allowed_view,
        )

        dad_index = mock.Mock(spec=not_allowed_view)
        dad_show = mock.Mock(spec=not_allowed_view)

        kid_index = mock.Mock(spec=not_allowed_view)
        kid_show = mock.Mock(spec=not_allowed_view)

        kid = Resource('dad.kid')
        dad = Resource('dad')

        config = self._get_config()

        ru = ResourceConfigurator()
        ru._validate_views_args_name = lambda a: a

        # simulate decorating methods:
        dad.index()(dad_index)
        dad.show()(dad_show)
        kid.index()(kid_index)
        kid.show()(kid_show)

        ru._add(config, dad)
        ru._add(config, kid)

        self.assertEqual(14, config.add_view.call_count)

        # check dad views:

        self._check_add_view(dad_index, 'index', 'GET', 'dad_collection',
                             config.add_view.call_args_list[0])
        self._check_add_view(dad_show, 'show', 'GET', 'dad_item',
                             config.add_view.call_args_list[1])

        self._check_add_not_allowed(not_allowed_view, 'edit', 'GET',
                                    'dad_edit',
                                    config.add_view.call_args_list[2])
        self._check_add_not_allowed(not_allowed_view, 'new', 'GET', 'dad_new',
                                    config.add_view.call_args_list[3])
        self._check_add_not_allowed(not_allowed_view, 'create', 'POST',
                                    'dad_collection',
                                    config.add_view.call_args_list[4])
        self._check_add_not_allowed(not_allowed_view, 'update', 'PUT',
                                    'dad_item',
                                    config.add_view.call_args_list[5])
        self._check_add_not_allowed(not_allowed_view, 'delete', 'DELETE',
                                    'dad_item',
                                    config.add_view.call_args_list[6])

        # check kid views:

        self._check_add_view(kid_index, 'index', 'GET', 'dad.kid_collection',
                             config.add_view.call_args_list[7])
        self._check_add_view(kid_show, 'show', 'GET', 'dad.kid_item',
                             config.add_view.call_args_list[8])

        self._check_add_not_allowed(not_allowed_view, 'edit', 'GET',
                                    'dad.kid_edit',
                                    config.add_view.call_args_list[9])
        self._check_add_not_allowed(not_allowed_view, 'new', 'GET',
                                    'dad.kid_new',
                                    config.add_view.call_args_list[10])
        self._check_add_not_allowed(not_allowed_view, 'create', 'POST',
                                    'dad.kid_collection',
                                    config.add_view.call_args_list[11])
        self._check_add_not_allowed(not_allowed_view, 'update', 'PUT',
                                    'dad.kid_item',
                                    config.add_view.call_args_list[12])
        self._check_add_not_allowed(not_allowed_view, 'delete', 'DELETE',
                                    'dad.kid_item',
                                    config.add_view.call_args_list[13])
Beispiel #11
0
    def test_add_resources_route(self, m_functools):
        from pyramid_rest.resource import ResourceConfigurator, Resource

        kid = Resource('dad.kid')
        dad = Resource('dad')

        config = self._get_config()

        ru = ResourceConfigurator()

        ru._add(config, dad)

        self.assertEqual({'dad': dad}, ru.resources)
        self.assertEqual({'dad': dad}, ru.parent_resources)

        self.assertEqual(
            mock.call(
                pattern='/dads/new',
                name='dad_new',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[0]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads/{id}',
                name='dad_item',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[1]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads/{id}/edit',
                name='dad_edit',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[2]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads',
                name='dad_collection',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[3]
            )
        ru._add(config, kid)
        self.assertEqual({'dad': dad, 'dad.kid': kid}, ru.resources)
        self.assertEqual({'dad': dad}, ru.parent_resources)

        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/new',
                name='dad.kid_new',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[4]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/{id}',
                name='dad.kid_item',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[5]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids/{id}/edit',
                name='dad.kid_edit',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[6]
            )
        self.assertEqual(
            mock.call(
                pattern='/dads/{dad_id}/kids',
                name='dad.kid_collection',
                factory=m_functools.partial.return_value,
                ),
            config.add_route.call_args_list[7]
            )