responding with 400 on FieldError and test added

This commit is contained in:
Manali Latkar
2013-09-20 18:57:39 +05:30
parent c2a76faeb9
commit c676abf61b
2 changed files with 22 additions and 3 deletions

View File

@@ -601,7 +601,7 @@ def search(request):
results = event.search_results(results, when, routing_key_status) results = event.search_results(results, when, routing_key_status)
return rsp(json.dumps(results)) return rsp(json.dumps(results))
except ObjectDoesNotExist: except ObjectDoesNotExist:
return rsp(["The requested object does not exist"]) return error_response(404, 'Not Found', ["The requested object does not exist"])
except FieldError: except FieldError:
return rsp(["The requested field '%s' does not exist for the corresponding object.\n" return error_response(400, 'Bad Request', "The requested field '%s' does not exist for the corresponding object.\n"
"Note: The field names of database are case-sensitive." % field] ) "Note: The field names of database are case-sensitive." % field)

View File

@@ -21,6 +21,7 @@
import datetime import datetime
import decimal import decimal
import json import json
from django.core.exceptions import FieldError
import mox import mox
@@ -1351,6 +1352,24 @@ class StackyServerTestCase(StacktachBaseTestCase):
self._assert_on_search_nova(json_resp, raw1) self._assert_on_search_nova(json_resp, raw1)
self.mox.VerifyAll() self.mox.VerifyAll()
def test_search_with_wrong_field_value_returns_400_error_and_a_message(self):
fake_request = self.mox.CreateMockAnything()
fake_request.GET = {'field': 'tenant', 'value': 'tenant'}
models.RawData.objects.filter(tenant='tenant').AndRaise(FieldError)
self.mox.ReplayAll()
resp = stacky_server.search(fake_request)
self.assertEqual(resp.status_code, 400)
json_resp = json.loads(resp.content)
self.assertEquals(json_resp[0],[u'Error', u'Message'])
self.assertEquals(json_resp[1],
[u'Bad Request', u"The requested field"
u" 'tenant' does not exist for the corresponding object.\nNote: "
u"The field names of database are case-sensitive."])
self.mox.VerifyAll()
def test_model_search_default_limit(self): def test_model_search_default_limit(self):
fake_request = self.mox.CreateMockAnything() fake_request = self.mox.CreateMockAnything()
fake_request.GET = {} fake_request.GET = {}