Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_handle_not_implemented(self):
self.res.request = FakeHttpRequest('TRACE')
resp = self.res.handle('list')
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 501)
resp_json = json.loads(resp.content.decode('utf-8'))
self.assertEqual(
resp_json['error'], "Unsupported method 'TRACE' for list endpoint.")
self.assertTrue('traceback' in resp_json)
def test_as_list(self):
list_endpoint = DjTestResource.as_list()
req = FakeHttpRequest('GET')
resp = list_endpoint(req)
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 200)
self.assertEqual(json.loads(resp.content.decode('utf-8')), {
'objects': [
{
'author': 'daniel',
'body': 'Hello world!',
'id': 2,
'title': 'First post'
},
{
'author': 'daniel',
'body': 'Stuff here.',
'id': 4,
'title': 'Another'
},
{
'author': 'daniel',
'body': "G'bye!",
},
{
'title': "Hitchhiker's Guide To The Galaxy",
'author': 'Douglas Adams',
'short_desc': "Don't forget your towel.",
'pub_date': '1979',
}
]
self.res.preparer = FieldsPreparer(fields={
'title': 'title',
'author': 'author',
'synopsis': 'short_desc',
})
res = self.res.serialize_list(data)
self.assertEqual(json.loads(res), {
'objects': [
{
'author': 'Carl Sagan',
'synopsis': 'A journey through the stars by an emminent astrophysist.',
'title': 'Cosmos'
},
{
'title': "Hitchhiker's Guide To The Galaxy",
'author': 'Douglas Adams',
'synopsis': "Don't forget your towel.",
},
],
})
# Make sure we don't try to serialize a ``None``, which would fail.
self.assertEqual(self.res.serialize_list(None), '')
def test_as_list(self):
list_endpoint = PyrTestResource.as_list()
req = FakeHttpRequest('GET')
resp = list_endpoint(req)
self.assertEqual(resp.content_type, 'application/json')
self.assertEqual(resp.status_code, 200)
self.assertEqual(json.loads(resp.body.decode('utf-8')), {
'objects': [
{
'id': 2,
'title': 'First post'
},
{
'id': 4,
'title': 'Another'
},
{
'id': 5,
'title': 'Last'
}
def test_as_list(self):
list_endpoint = FlTestResource.as_list()
flask.request = FakeHttpRequest('GET')
with self.app.test_request_context('/whatever/', method='GET'):
resp = list_endpoint()
self.assertEqual(resp.headers['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 200)
self.assertEqual(json.loads(resp.data.decode('utf-8')), {
'objects': [
{
'id': 2,
'title': 'First post'
},
{
'id': 4,
'title': 'Another'
},
{
'id': 5,
'title': 'Last'
}
def test_handle_not_authenticated(self):
# Special-cased above for testing.
self.res.request = FakeHttpRequest('DELETE')
# First with DEBUG on
resp = self.res.handle('list')
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 401)
resp_json = json.loads(resp.content.decode('utf-8'))
self.assertEqual(resp_json['error'], 'Unauthorized.')
self.assertTrue('traceback' in resp_json)
# Now with DEBUG off.
settings.DEBUG = False
self.addCleanup(setattr, settings, 'DEBUG', True)
resp = self.res.handle('list')
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 401)
resp_json = json.loads(resp.content.decode('utf-8'))
self.assertEqual(resp_json, {
'error': 'Unauthorized.',
})
self.assertFalse('traceback' in resp_json)
# Last, with bubble_exceptions.
def test_http404_exception_handling(self):
# Make sure we get a proper Not Found exception rather than a
# generic 500, when code raises a Http404 exception.
res = DjTestResourceHttp404Handling()
res.request = FakeHttpRequest('GET')
settings.DEBUG = False
self.addCleanup(setattr, settings, 'DEBUG', True)
resp = res.handle('detail', 1001)
self.assertEqual(resp['Content-Type'], 'application/json')
self.assertEqual(resp.status_code, 404)
self.assertEqual(json.loads(resp.content.decode('utf-8')), {
'error': 'Model with pk 1001 not found.'
})
def authenticate(self, request):
user = request.params.get('user')
if user == 'friend':
return None
elif user == 'foe':
return Http403('you shall not pass')
elif user == 'exceptional-foe':
raise HttpError(403, 'with exception')
else:
# this is an illegal return value for this function
return 42
def test_serialize_queryset(self):
"""Test queryset serialization"""
Author.objects.all().delete()
a1 = Author.objects.create(name="foo")
a2 = Author.objects.create(name="bar")
qs = Author.objects.all()
_ = list(qs) # force sql query execution
# Check that the same (cached) queryset is used, instead of a clone
with self.assertNumQueries(0):
s = serialize(qs)
self.assertEqual(s,
[
{'name': a1.name, 'id': a1.id},
{'name': a2.name, 'id': a2.id},
]
def test_shallow_foreign_key_serialization(self):
"""Test that foreign key fields are serialized as integer IDs."""
s = serialize(self.books[0])
self.assertEqual(s['author'], self.author.id)