def test_patch_body_json_data_drop_dict(self): """ Tests for dicts being removed if no ``'id'`` key exists """ factory = RequestFactory() data = { 'string': 'Stringvalue', 'dict': { 'some': 'value', } } json_data = json.dumps(data) request = factory.post('/', data=json_data, content_type='text/json; charset=UTF-8') patch_body_json_data(request) query = QueryDict('string=Stringvalue') self.assertEqual(request._post, query)
def test_patch_body_json_data_replace_dict(self): """ Tests for dicts being replaced by its ``'id'`` if present """ factory = RequestFactory() data = { 'string': 'Stringvalue', 'dict': { 'some': 'value', 'id': 42, } } json_data = json.dumps(data) request = factory.post('/', data=json_data, content_type='text/json; charset=UTF-8') patch_body_json_data(request) query = QueryDict('string=Stringvalue&dict=42') self.assertEqual(request._post, query)
def test_patch_body_json_data(self): """ Tests for null values being converted to an empty string in JSON POST data """ factory = RequestFactory() data = { 'string': 'Stringvalue', 'int': 42, 'float': 13.37, 'none': None, } json_data = json.dumps(data) request = factory.post('/', data=json_data, content_type='text/json; charset=UTF-8') patch_body_json_data(request) query = QueryDict('string=Stringvalue&int=42&float=13.37&none') self.assertEqual(request._post, query)
def login_api(request, authentication_form=AuthenticationForm): """ Logs the user specified by the `authentication_form` in. """ if request.method == "POST": request = patch_body_json_data(request) # TODO: Django<=1.5: Django 1.6 removed the cookie check in favor of CSRF request.session.set_test_cookie() form = authentication_form(request, data=request.POST) if form.is_valid(): auth_login(request, form.get_user()) return JSONResponse({}) else: return JSONResponse(form.errors, status=401) else: return JSONResponse({}, status=405) # Method not allowed