示例#1
0
    def test_class_parameters_are_used_for_collection_arguments(self):
        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }

        default_get_arguments = {
            'accept': mock.sentinel.accept,
        }

        default_collection_arguments = {
            'cors_origins': mock.sentinel.cors_origins,
        }

        collection_get_arguments = {
            'error_handler': mock.sentinel.error_handler
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            default_get_arguments=default_get_arguments,
            default_collection_arguments=default_collection_arguments,
            collection_get_arguments=collection_get_arguments
        )

        arguments = viewset.collection_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')
        self.assertDictEqual(
            arguments,
            {
                'accept': mock.sentinel.accept,
                'cors_headers': mock.sentinel.cors_headers,
                'cors_origins': mock.sentinel.cors_origins,
                'error_handler': mock.sentinel.error_handler,
            }
        )
示例#2
0
    def test_default_arguments_are_used_for_record_arguments(self):
        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }

        default_get_arguments = {
            'accept': mock.sentinel.accept,
        }

        default_record_arguments = {
            'cors_origins': mock.sentinel.record_cors_origins,
        }

        record_get_arguments = {'error_handler': mock.sentinel.error_handler}

        viewset = ViewSet(default_arguments=default_arguments,
                          default_get_arguments=default_get_arguments,
                          default_record_arguments=default_record_arguments,
                          record_get_arguments=record_get_arguments)

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')

        self.assertDictEqual(
            arguments, {
                'accept': mock.sentinel.accept,
                'cors_headers': mock.sentinel.cors_headers,
                'cors_origins': mock.sentinel.record_cors_origins,
                'error_handler': mock.sentinel.error_handler,
            })
示例#3
0
    def test_class_parameters_overwrite_each_others(self):
        # Some class parameters should overwrite each others.
        # The more specifics should prevail over the more generics.
        # Items annoted with a "<<" are the one that should prevail.
        default_arguments = {
            'cors_origins': mock.sentinel.default_cors_origins,
            'error_handler': mock.sentinel.default_error_handler,
            'cors_headers': mock.sentinel.default_cors_headers,  # <<
        }
        default_record_arguments = {
            'cors_origins': mock.sentinel.default_record_cors_origin,
            'error_handler': mock.sentinel.default_record_error_handler,  # <<
        }

        record_get_arguments = {
            'cors_origins': mock.sentinel.record_get_cors_origin,  # <<
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            default_record_arguments=default_record_arguments,
            record_get_arguments=record_get_arguments,
        )

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')

        self.assertDictEqual(
            arguments, {
                'cors_headers': mock.sentinel.default_cors_headers,
                'error_handler': mock.sentinel.default_record_error_handler,
                'cors_origins': mock.sentinel.record_get_cors_origin,
            })
示例#4
0
    def test_class_parameters_are_used_for_collection_arguments(self):
        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }

        default_get_arguments = {
            'accept': mock.sentinel.accept,
        }

        default_collection_arguments = {
            'cors_origins': mock.sentinel.cors_origins,
        }

        collection_get_arguments = {
            'error_handler': mock.sentinel.error_handler
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            default_get_arguments=default_get_arguments,
            default_collection_arguments=default_collection_arguments,
            collection_get_arguments=collection_get_arguments)

        arguments = viewset.collection_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')
        self.assertDictEqual(
            arguments, {
                'accept': mock.sentinel.accept,
                'cors_headers': mock.sentinel.cors_headers,
                'cors_origins': mock.sentinel.cors_origins,
                'error_handler': mock.sentinel.error_handler,
            })
示例#5
0
    def test_class_parameters_overwrite_each_others(self):
        # Some class parameters should overwrite each others.
        # The more specifics should prevail over the more generics.
        # Items annoted with a "<<" are the one that should prevail.
        default_arguments = {
            'cors_origins': mock.sentinel.default_cors_origins,
            'error_handler': mock.sentinel.default_error_handler,
            'cors_headers': mock.sentinel.default_cors_headers,  # <<
        }
        default_record_arguments = {
            'cors_origins': mock.sentinel.default_record_cors_origin,
            'error_handler': mock.sentinel.default_record_error_handler,  # <<
        }

        record_get_arguments = {
            'cors_origins': mock.sentinel.record_get_cors_origin,  # <<
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            default_record_arguments=default_record_arguments,
            record_get_arguments=record_get_arguments,
        )

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')

        self.assertDictEqual(
            arguments,
            {
                'cors_headers': mock.sentinel.default_cors_headers,
                'error_handler': mock.sentinel.default_record_error_handler,
                'cors_origins': mock.sentinel.record_get_cors_origin,
            }
        )
示例#6
0
 def test_get_service_name_returns_resource_att_if_not_callable(self):
     viewset = ViewSet()
     resource = mock.MagicMock()
     resource.name = 'fakename'
     self.assertEquals(
         viewset.get_service_name('record', resource),
         'fakename-record')
示例#7
0
 def test_default_arguments_are_copied_before_being_returned(self):
     original_arguments = {}
     viewset = ViewSet(collection_get_arguments=original_arguments,
                       validate_schema_for=())
     arguments = viewset.collection_arguments(mock.sentinel.resource, 'GET')
     self.assertEquals(original_arguments, {})
     self.assertNotEquals(original_arguments, arguments)
示例#8
0
 def test_get_service_name_doesnt_use_callable_as_a_name(self):
     viewset = ViewSet()
     resource = mock.MagicMock()
     resource.name = lambda x: 'should not be called'
     resource.__name__ = "FakeName"
     self.assertEquals(viewset.get_service_name('record', resource),
                       'fakename-record')
示例#9
0
    def test_default_arguments_are_used_for_record_arguments(self):
        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }
        default_record_arguments = {
            'cors_origins': mock.sentinel.record_cors_origins,
        }

        record_get_arguments = {
            'error_handler': mock.sentinel.error_handler
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            default_record_arguments=default_record_arguments,
            record_get_arguments=record_get_arguments
        )

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')

        self.assertDictEqual(
            arguments,
            {
                'cors_headers': mock.sentinel.cors_headers,
                'cors_origins': mock.sentinel.record_cors_origins,
                'error_handler': mock.sentinel.error_handler,
            }
        )
示例#10
0
 def test_get_service_name_doesnt_use_callable_as_a_name(self):
     viewset = ViewSet()
     resource = mock.MagicMock()
     resource.name = lambda x: 'should not be called'
     resource.__name__ = "FakeName"
     self.assertEquals(
         viewset.get_service_name('record', resource),
         'fakename-record')
示例#11
0
 def test_is_endpoint_enabled_returns_true_if_enabled(self):
     viewset = ViewSet()
     config = {
         'record_fake_get_enabled': True
     }
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'get',
                                              config)
     self.assertTrue(is_enabled)
示例#12
0
 def test_is_endpoint_enabled_returns_false_for_delete_if_readonly(self):
     viewset = ViewSet()
     config = {
         'readonly': True
     }
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'delete',
                                              config)
     self.assertFalse(is_enabled)
示例#13
0
 def test_is_endpoint_enabled_returns_true_for_head_if_readonly(self):
     viewset = ViewSet()
     config = {
         'readonly': True
     }
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'head',
                                              config)
     self.assertTrue(is_enabled)
示例#14
0
 def test_schema_is_added_when_uppercase_method_matches(self):
     viewset = ViewSet(
         validate_schema_for=('GET', )
     )
     resource = mock.MagicMock(mapping=colander.SchemaNode(colander.Int()))
     arguments = viewset.collection_arguments(resource, 'get')
     schema = arguments['schema']
     self.assertEquals(schema.children[0], resource.mapping)
示例#15
0
 def test_is_endpoint_enabled_returns_false_if_enabled(self):
     viewset = ViewSet()
     config = {
         'cliquet.record_fake_get_enabled': False
     }
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'get',
                                              config)
     self.assertFalse(is_enabled)
示例#16
0
 def test_default_arguments_are_copied_before_being_returned(self):
     original_arguments = {}
     viewset = ViewSet(
         collection_get_arguments=original_arguments,
         validate_schema_for=()
     )
     arguments = viewset.collection_arguments(mock.sentinel.resource, 'GET')
     self.assertEquals(original_arguments, {})
     self.assertNotEquals(original_arguments, arguments)
示例#17
0
    def test_a_default_schema_is_added_when_method_doesnt_match(self, mocked):
        viewset = ViewSet(validate_schema_for=('GET', ))
        resource = mock.MagicMock()
        mocked.MappingSchema.return_value = mock.sentinel.default_schema

        arguments = viewset.collection_arguments(resource, 'POST')
        self.assertEquals(arguments['schema'], mock.sentinel.default_schema)
        self.assertNotEqual(arguments['schema'], resource.mapping)

        mocked.MappingSchema.assert_called_with(unknown='preserve')
示例#18
0
    def test_a_default_schema_is_added_when_method_doesnt_match(self, mocked):
        viewset = ViewSet(
            validate_schema_for=('GET', )
        )
        resource = mock.MagicMock()
        mocked.MappingSchema.return_value = mock.sentinel.default_schema

        arguments = viewset.collection_arguments(resource, 'POST')
        self.assertEquals(arguments['schema'], mock.sentinel.default_schema)
        self.assertNotEqual(arguments['schema'], resource.mapping)

        mocked.MappingSchema.assert_called_with(unknown='preserve')
示例#19
0
    def test_service_arguments_arent_inherited_by_record_arguments(self):
        service_arguments = {
            'description': 'The little book of calm',
        }

        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }

        viewset = ViewSet(default_arguments=default_arguments,
                          service_arguments=service_arguments,
                          default_record_arguments={},
                          record_get_arguments={})

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')
        self.assertDictEqual(arguments, {
            'cors_headers': mock.sentinel.cors_headers,
        })
示例#20
0
    def test_service_arguments_arent_inherited_by_record_arguments(self):
        service_arguments = {
            'description': 'The little book of calm',
        }

        default_arguments = {
            'cors_headers': mock.sentinel.cors_headers,
        }

        viewset = ViewSet(
            default_arguments=default_arguments,
            service_arguments=service_arguments,
            default_record_arguments={},
            record_get_arguments={}
        )

        arguments = viewset.record_arguments(mock.MagicMock, 'get')
        arguments.pop('schema')
        self.assertDictEqual(
            arguments,
            {
                'cors_headers': mock.sentinel.cors_headers,
            }
        )
示例#21
0
 def test_get_service_arguments_ignore_factory_if_not_exists(self):
     viewset = ViewSet()  # Don't provide a factory here.
     service_arguments = viewset.get_service_arguments()
     self.assertNotIn("factory", service_arguments)
示例#22
0
 def test_get_service_name_returns_resource_att_if_not_callable(self):
     viewset = ViewSet()
     resource = mock.MagicMock()
     resource.name = 'fakename'
     self.assertEquals(viewset.get_service_name('record', resource),
                       'fakename-record')
示例#23
0
 def test_get_service_arguments_has_no_factory_by_default(self):
     viewset = ViewSet()
     service_arguments = viewset.get_service_arguments()
     self.assertNotIn('factory', service_arguments)
示例#24
0
 def test_permission_private_is_set_by_default(self):
     viewset = ViewSet()
     args = viewset.collection_arguments(mock.sentinel.resource, 'GET')
     self.assertEquals(args['permission'], 'private')
示例#25
0
 def test_is_endpoint_enabled_returns_true_if_enabled(self):
     viewset = ViewSet()
     config = {'record_fake_get_enabled': True}
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'get',
                                              config)
     self.assertTrue(is_enabled)
示例#26
0
 def test_is_endpoint_enabled_returns_false_for_delete_if_readonly(self):
     viewset = ViewSet()
     config = {'readonly': True}
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'delete',
                                              config)
     self.assertFalse(is_enabled)
示例#27
0
 def test_is_endpoint_enabled_returns_true_for_head_if_readonly(self):
     viewset = ViewSet()
     config = {'readonly': True}
     is_enabled = viewset.is_endpoint_enabled('record', 'fake', 'head',
                                              config)
     self.assertTrue(is_enabled)
示例#28
0
 def test_arguments_are_merged_on_initialization(self):
     viewset = ViewSet(collection_path=mock.sentinel.collection_path)
     self.assertEquals(viewset.collection_path,
                       mock.sentinel.collection_path)
示例#29
0
 def test_get_service_arguments_has_no_factory_by_default(self):
     viewset = ViewSet()
     service_arguments = viewset.get_service_arguments()
     self.assertNotIn('factory', service_arguments)
示例#30
0
 def test_permission_private_is_set_by_default(self):
     viewset = ViewSet()
     args = viewset.collection_arguments(mock.sentinel.resource, 'GET')
     self.assertEquals(args['permission'], 'private')
示例#31
0
 def test_schema_is_added_when_uppercase_method_matches(self):
     viewset = ViewSet(validate_schema_for=('GET', ))
     resource = mock.MagicMock(mapping=colander.SchemaNode(colander.Int()))
     arguments = viewset.collection_arguments(resource, 'get')
     schema = arguments['schema']
     self.assertEquals(schema.children[0], resource.mapping)
示例#32
0
 def test_get_service_name_returns_the_viewset_name_if_defined(self):
     viewset = ViewSet(name='fakename')
     self.assertEquals(
         viewset.get_service_name('record', mock.MagicMock),
         'fakename-record')
示例#33
0
 def test_get_service_arguments_returns_factory_if_exists(self):
     viewset = ViewSet(factory=mock.sentinel.factory)
     service_arguments = viewset.get_service_arguments()
     self.assertIn("factory", service_arguments)
     self.assertEquals(service_arguments["factory"], mock.sentinel.factory)
示例#34
0
 def test_get_service_name_returns_the_viewset_name_if_defined(self):
     viewset = ViewSet(name='fakename')
     self.assertEquals(viewset.get_service_name('record', mock.MagicMock),
                       'fakename-record')