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, } )
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, })
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, })
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, })
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, } )
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')
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)
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')
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, } )
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')
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)
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)
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)
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)
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)
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)
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')
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')
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, })
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, } )
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)
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')
def test_get_service_arguments_has_no_factory_by_default(self): viewset = ViewSet() service_arguments = viewset.get_service_arguments() self.assertNotIn('factory', service_arguments)
def test_permission_private_is_set_by_default(self): viewset = ViewSet() args = viewset.collection_arguments(mock.sentinel.resource, 'GET') self.assertEquals(args['permission'], 'private')
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)
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)
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)
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)
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)
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')
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)
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')