def test_should_serialize_to_response(self): request = Request(user=None, request_params=None) request.context_params['entity'] = dict(id='aaaa', name='asdf', is_active=True) request = self.serializer.serialize_detail(request) self.assertEqual(request.context_params['serialized_data'], dict(id='aaaa', name='asdf', is_active=True))
def test_should_serialize_to_response(self): request = Request(user=None, request_params=None) request.context_params['entity'] = dict(name='asdf', is_active=True) request = self.serializer.serialize_detail(request) self.assertEqual(request.context_params['serialized_data'], dict(name='asdf', is_active=True))
def test_should_not_throw_error_on_missing_non_required_fields(self): request = Request(user=None, request_params=None) request.context_params['entity'] = dict(id='aaaa', is_active=True) request = self.serializer.serialize_detail(request) self.assertEqual(request.context_params['serialized_data'], dict(id='aaaa', is_active=True))
def test_should_serialize_list_to_response(self): request = Request(user=None, request_params=None, context_params={}) request.context_params['total_count'] = 10 request.context_params['request_filters'] = {'offset': 0, 'limit': 20} request.context_params['entities'] = \ [dict(name='asdf', is_active=True), dict(name='asdf', is_active=False)] request = self.serializer.serialize_list(request) data = request.context_params['serialized_data']['objects'] self.assertEqual(len(data), 2) self.assertEqual(data[0], dict(name='asdf', is_active=True)) self.assertEqual(data[1], dict(name='asdf', is_active=False))
def test_should_serialize_list_to_response(self): request = Request(user=None, request_params=None, context_params={}) request.context_params['total_count'] = 10 request.context_params['request_filters'] = {'offset': 0, 'limit': 20} request.context_params['entities'] = \ [dict(id='aaaa', name='asdf', is_active=True), dict(id='bbbb', name='asdf', is_active=False)] request = self.serializer.serialize_list(request) data = request.context_params['serialized_data']['objects'] self.assertEqual(len(data), 2) self.assertEqual(data[0], dict(id='aaaa', name='asdf', is_active=True)) self.assertEqual(data[1], dict(id='bbbb', name='asdf', is_active=False))
def test_authentication_for_a_valid_user(self): user = MagicMock() request = Request(user=user, request_params=None) returned_request = default_authentication.authenticate(request) assert_that(returned_request, equal_to(request))
def setUp(self): self.get_obj = MagicMock() self.del_obj = MagicMock() self.get_obj.__name__ = 'get_obj' self.del_obj.__name__ = 'del_obj' self.get_obj.return_value = Request(user=None, request_params=None) self.del_obj.return_value = Response()
def test_authentication_when_no_user(self): request = Request(user=None, request_params=None) response = default_authentication.authenticate(request) assert_that(response.is_success, equal_to(False)) assert_that(response.reason, equal_to(error_types.AuthenticationFailed))
def test_api_method_throws_if_no_response_from_last_handler(self): test_method = PipelineComposer(pipeline=[self.get_obj, self.del_obj]) expected_request = Request( user=None, request_params={}) self.get_obj.return_value = expected_request self.del_obj.return_value = expected_request self.assertRaises(AssertionError, test_method, request=expected_request)
def test_api_method_calls_handler(self): test_api_method = PipelineComposer(pipeline=[self.get_obj]) self.get_obj.return_value = Response() expected_request = Request(user=None, request_params={'somearg1': 1, 'somearg2': 2}) test_api_method(expected_request) self.get_obj.assert_called_with(request=expected_request)
def test_registered_resource_should_be_accessible_to_call(self): test_resource = self.TestResource() # Making an assumption that Resource schema name will be used to # access -> Useful for serialization self.http_api.register_resource('test_objs', test_resource) request = Request(user=None, request_params={}) resource = self.http_api.resolve_resource('test_objs') resource.action1(request=request) self.action1.assert_called_once_with(request=request)
def test_api_method_calls_all_functions_in_pipeline(self): test_api_method = PipelineComposer( pipeline=[self.get_obj, self.del_obj]) expected_request = Request(user=None, request_params={'somearg1': 1, 'somearg2': 2}) test_api_method(expected_request) self.get_obj.assert_called_with( request=expected_request) self.del_obj.assert_called_with( request=expected_request)
def test_api_method_exits_pipeline_on_response(self): test_method = PipelineComposer(pipeline=[self.get_obj, self.del_obj]) expected_response = Response() expected_request = Request(user=None, request_params={'somearg1': 1, 'somearg2': 2}) self.get_obj.return_value = expected_response test_method(expected_request) self.get_obj.assert_called_with( request=expected_request) self.assertEqual(self.del_obj.call_count, 0)
def get_request(data=None, user=None, request_params=None, context_params=None): request_params = request_params or {} context_params = context_params or { 'api_name': 'api', 'api_version': 'v2', 'url': 'asdf/1212', 'endpoint': 'test_endpoint', 'timezone': 'Asia/Calcutta' } return Request(user=user, request_params=request_params, context_params=context_params, data=data)
def build_request(http_request, url, api, request_data, request_body): endpoint = api.resolve_endpoint(url) breadcrumb_filters = metadata_factory.api_breadcrumb_filters(url, endpoint) parent_breadcrumbs = metadata_factory.api_breadcrumbs(url, endpoint) return Request(user=_resolve_user(http_request), request_params=_build_request_params( http_request, breadcrumb_filters), context_params={ 'protocol': 'http', 'url': url, 'api_name': api.name, 'api_version': api.version, 'timezone': conf.settings.TIME_ZONE, 'api_breadcrumbs': parent_breadcrumbs }, data=request_data, request_headers=http_request.META, request_body=request_body)
def test_api_method_throws_for_non_standard_response(self): test_method = PipelineComposer(pipeline=[self.get_obj]) self.get_obj.return_value = object() request = Request(user=None, request_params=None) self.assertRaises(AssertionError, test_method, request)
def test_should_throw_error_on_missing_required_fields(self): request = Request(user=None, request_params=None) request.context_params['entity'] = dict(name='asdf', is_active=True) self.assertRaises(AttributeError, self.serializer.serialize_detail, request)