def test_post_with_collision_two_batch(self): def side_effect(*args, **kwargs): # This could occur if a slightly earlier POST or PUT still had # the database locked during a DB transaction. from django.db.transaction import TransactionManagementError raise TransactionManagementError() child_resource = mock_resource(name='child') root_resource = mock_resource( name='root', child_resource=child_resource, base_regex=r'^api/v2/(?P<base_resource>.*)$') child_resource.allowed_methods.add('POST') child_resource.get.side_effect = AuthorizationError('foo') child_resource.post = side_effect request_data = { "data": [ self._generate_batch_partial( 'post', 'http://localhost:8081/api/v2/child', {'id ': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 409) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child')
def test_method_not_allowed(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$') root_resource.allowed_methods.add('POST') root_resource.get.side_effect = Exception request_data = { "data": [ self._generate_batch_partial('get', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=body) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 405) self.assertEqual(data[0]['allowed'], 'POST')
def test_validation_exception_batch(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$') root_resource.allowed_methods.add('POST') root_resource.post.side_effect = validators.ValidationError( Mock(), { 'class.field': 'broken', }) request_data = { "data": [ self._generate_batch_partial('post', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 400) self.assertEqual(data[0]['validation_errors'], {'class.field': 'broken'})
def test_unauthorized_batch(self): child_resource = mock_resource(name='child') root_resource = mock_resource( name='root', child_resource=child_resource, base_regex=r'^api/v2/(?P<base_resource>.*)$') child_resource.allowed_methods.add('GET') child_resource.get.side_effect = AuthorizationError('foo') request_data = { "data": [ self._generate_batch_partial( 'get', 'http://localhost:8081/api/v2/child', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=body) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 403) self.assertEqual(data[0]['validation_errors'], [u'Modification of field foo not authorized'])
def test_exception_in_batch(self, format_exc): format_exc.return_value = 'some traceback' root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$') root_resource.allowed_methods.add('GET') root_resource.allowed_methods.add('POST') root_resource.get.side_effect = Exception request_data = { "data": [ self._generate_batch_partial('get', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=body) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 500) self.assertEqual(data[0]['data'], {'error': 'some traceback'})
def test_post_batch(self): result = Mock(resource_path='grand_child_path') root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['POST'], result=result) request_data = { "data": [ self._generate_batch_partial( 'post', 'http://localhost:8081/api/v2/child/grandchild', {}) ] } body = json.dumps(request_data) response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=body) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 201) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertEqual(data[0]['location'], 'http://localhost:8081/api/v2/grand_child_path')
def test_one_fails_one_passes(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['PUT'], result={'name': 'value'}) request_data = { "data": [ self._generate_batch_partial( 'put', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345}), self._generate_batch_partial( 'get', 'http://localhost:8081/api/v2/child', {'business_id': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 2) self.assertEqual(data[0]['status'], 200) self.assertEqual(data[1]['status'], 405)
def test_put_key_error_batch(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$' ) root_resource.allowed_methods.add('PUT') root_resource.get.side_effect = KeyError('bad key message') request_data = { "data": [ self._generate_batch_partial( 'put', 'http://localhost:8081/api/v2/', {'business_id': 12345} ) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 400) self.assertEqual(data[0]['validation_errors'], {'missingData': 'bad key message'})
def test_put_nocontent_batch(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['PUT'], result={} ) request_data = { "data": [ self._generate_batch_partial( 'put', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345} ) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 204) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertTrue('data' not in data[0])
def test_method_not_allowed(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$' ) root_resource.allowed_methods.add('POST') root_resource.get.side_effect = Exception request_data = { "data": [ self._generate_batch_partial('get', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=body ) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 405) self.assertEqual(data[0]['allowed'], 'POST')
def test_put_key_error_batch(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$') root_resource.allowed_methods.add('PUT') root_resource.get.side_effect = KeyError('bad key message') request_data = { "data": [ self._generate_batch_partial('put', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 400) self.assertEqual(data[0]['validation_errors'], {'missingData': 'bad key message'})
def test_one_fails_one_passes(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['PUT'], result={'name': 'value'} ) request_data = { "data": [ self._generate_batch_partial( 'put', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345} ), self._generate_batch_partial( 'get', 'http://localhost:8081/api/v2/child', {'business_id': 12345} ) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 2) self.assertEqual(data[0]['status'], 200) self.assertEqual(data[1]['status'], 405)
def test_exception_in_batch(self, format_exc): format_exc.return_value = 'some traceback' root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$' ) root_resource.allowed_methods.add('GET') root_resource.allowed_methods.add('POST') root_resource.get.side_effect = Exception request_data = { "data": [ self._generate_batch_partial('get', 'http://localhost:8081/api/v2/', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=body ) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 500) self.assertEqual(data[0]['data'], {'error': 'some traceback'})
def test_unauthorized_batch(self): child_resource = mock_resource(name='child') root_resource = mock_resource( name='root', child_resource=child_resource, base_regex=r'^api/v2/(?P<base_resource>.*)$' ) child_resource.allowed_methods.add('GET') child_resource.get.side_effect = AuthorizationError('foo') request_data = { "data": [ self._generate_batch_partial('get', 'http://localhost:8081/api/v2/child', {'business_id': 12345}) ] } body = json.dumps(request_data) response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=body ) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 403) self.assertEqual(data[0]['validation_errors'], [u'Modification of field foo not authorized'])
def test_post_batch(self): result = Mock(resource_path='grand_child_path') root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['POST'], result=result ) request_data = { "data": [ self._generate_batch_partial('post', 'http://localhost:8081/api/v2/child/grandchild', {}) ] } body = json.dumps(request_data) response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=body ) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 201) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertEqual(data[0]['location'], 'http://localhost:8081/api/v2/grand_child_path')
def test_get_batch(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['GET'], result={'name': 'value'} ) request_data = { "data": [ self._generate_batch_partial( 'get', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345} ) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 200) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertEqual(data[0]['data'], {u'name': u'value'}) ctx = mock_context() ctx.formatter = JSONFormatter() self.assertEqual(data[0]['etag'], get_sha1(ctx, {u'name': u'value'}))
def test_get_batch(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['GET'], result={'name': 'value'}) request_data = { "data": [ self._generate_batch_partial( 'get', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 200) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertEqual(data[0]['data'], {u'name': u'value'}) ctx = mock_context() ctx.formatter = JSONFormatter() self.assertEqual(data[0]['etag'], get_sha1(ctx, {u'name': u'value'}))
def test_no_post_root_resource(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['GET'], result={'name': 'value'} ) request_data = { "data": [{"method": "get", "uri": "someurl", "body": {}}] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='GET', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 405)
def test_no_post_root_resource(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['GET'], result={'name': 'value'}) request_data = { "data": [{ "method": "get", "uri": "someurl", "body": {} }] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='GET', body=json.dumps(request_data)) self.assertEqual(response.status_code, 405)
def test_post_with_collision_two_batch(self, enter): def side_effect(*args, **kwargs): # This could occur if a slightly earlier POST or PUT still had # the database locked during a DB transaction. from django.db.transaction import TransactionManagementError raise TransactionManagementError() enter.side_effect = side_effect child_resource = mock_resource(name='child') root_resource = mock_resource( name='root', child_resource=child_resource, base_regex=r'^api/v2/(?P<base_resource>.*)$' ) child_resource.allowed_methods.add('POST') child_resource.get.side_effect = AuthorizationError('foo') request_data = { "data": [ self._generate_batch_partial('post', 'http://localhost:8081/api/v2/child', {'id ': 12345}) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 409) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child')
def test_validation_exception_batch(self): root_resource = mock_resource( name='root', base_regex=r'^api/v2/(?P<base_resource>.*)$' ) root_resource.allowed_methods.add('POST') root_resource.post.side_effect = validators.ValidationError( Mock(), { 'class.field': 'broken', } ) request_data = { "data": [ self._generate_batch_partial( 'post', 'http://localhost:8081/api/v2/', {'business_id': 12345} ) ] } response = savory_dispatch_batch( root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data) ) self.assertEqual(response.status_code, 200) data = json.loads(response.content)['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 400) self.assertEqual(data[0]['validation_errors'], {'class.field': 'broken'})
def test_put_nocontent_batch(self): root_resource = self.create_root_resource_with_children( r'^api/v2/(?P<base_resource>.*)$', methods=['PUT'], result={}) request_data = { "data": [ self._generate_batch_partial( 'put', 'http://localhost:8081/api/v2/child/grandchild', {'business_id': 12345}) ] } response = savory_dispatch_batch(root_resource, full_host='localhost:8081', method='POST', body=json.dumps(request_data)) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content) data = response_json['data'] self.assertEqual(len(data), 1) self.assertEqual(data[0]['status'], 204) self.assertEqual(data[0]['uri'], 'http://localhost:8081/api/v2/child/grandchild') self.assertTrue('data' not in data[0])