Ejemplo n.º 1
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])
Ejemplo n.º 2
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)
Ejemplo n.º 3
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])