You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is very important that the library is able to return some sort of response that let's the user know they submitted an invalid request by raising a BadRequestError and returning a 400 and some sort of description explaining why.
Through testing post_relationship, I found that we are not raising a BadRequestError when we send a payload where the data is not an array of dictionaries.
For example:
def test_post_relationship_when_data_has_invalid_format(self):
"""Post relationship when data is not an array of dictionaries returns 400.
A BadRequestError is raised.
"""
user = models.User(
first='Sally', last='Smith',
password='password', username='SallySmith1')
self.session.add(user)
blog_post = models.Post(
title='This Is A Title', content='This is the content',
author_id=user.id, author=user)
self.session.add(blog_post)
comment = models.Comment(
content='This is the first comment',
author_id=user.id, author=user)
self.session.add(comment)
self.session.commit()
payload = {
'data': ['test']
}
with self.assertRaises(errors.BadRequestError) as error:
models.serializer.post_relationship(
self.session, payload, 'posts', blog_post.id, 'comments')
self.assertEqual(
error.exception.detail, 'data must be an array of objects')
self.assertEqual(error.exception.status_code, 400)
This test fails because there is no check for this in place in post_relationship. Thus when I try to run this test a AttributeError occurs. Specifically the AttributeError is occuring because we are failing to check
if not isinstance(item, dict):
raise BadRequestError('data must be an array of objects')
at the beginning of the for loop below:
for item in json_data['data']:
if {'type', 'id'} != set(item.keys()):
raise BadRequestError(
'{} must have type and id keys'.format(relationship.key))
Because we do not have this check a AttributeError occurs because the item does not have any keys when trying to execute item.keys().
This is an easy fix that would provide an informative error message to users who send badly formatted payloads.
The text was updated successfully, but these errors were encountered:
It is very important that the library is able to return some sort of response that let's the user know they submitted an invalid request by raising a BadRequestError and returning a 400 and some sort of description explaining why.
Through testing post_relationship, I found that we are not raising a BadRequestError when we send a payload where the data is not an array of dictionaries.
For example:
This test fails because there is no check for this in place in post_relationship. Thus when I try to run this test a AttributeError occurs. Specifically the AttributeError is occuring because we are failing to check
if not isinstance(item, dict):
raise BadRequestError('data must be an array of objects')
at the beginning of the for loop below:
Because we do not have this check a AttributeError occurs because the item does not have any keys when trying to execute item.keys().
This is an easy fix that would provide an informative error message to users who send badly formatted payloads.
The text was updated successfully, but these errors were encountered: