Revision dc1c2b45

b/snf-astakos-app/astakos/api/projects.py
42 42

  
43 43
from snf_django.lib import api
44 44
from snf_django.lib.api import faults
45
from .util import user_from_token, invert_dict, read_json_body
45
from snf_django.lib.api import utils
46
from .util import user_from_token, invert_dict, check_is_dict
46 47

  
47 48
from astakos.im import functions
48 49
from astakos.im.models import (
......
319 320
@transaction.commit_on_success
320 321
def create_project(request):
321 322
    user = request.user
322
    data = request.body
323
    app_data = json.loads(data)
323
    app_data = utils.get_json_body(request)
324 324
    return submit_new_project(app_data, user)
325 325

  
326 326

  
......
357 357
@transaction.commit_on_success
358 358
def modify_project(request, project_id):
359 359
    user = request.user
360
    data = request.body
361
    app_data = json.loads(data)
360
    app_data = utils.get_json_body(request)
362 361
    return submit_modification(app_data, user, project_id=project_id)
363 362

  
364 363

  
......
548 547
def get_action(actions, input_data):
549 548
    action = None
550 549
    data = None
550
    check_is_dict(input_data)
551 551
    for option in actions.keys():
552 552
        if option in input_data:
553 553
            if action:
......
586 586
@transaction.commit_on_success
587 587
def project_action(request, project_id):
588 588
    user = request.user
589
    data = request.body
590
    input_data = json.loads(data)
589
    input_data = utils.get_json_body(request)
591 590

  
592 591
    func, action_data = get_action(PROJECT_ACTION, input_data)
593 592
    with ExceptionHandler():
......
707 706
@transaction.commit_on_success
708 707
def membership_action(request, memb_id):
709 708
    user = request.user
710
    input_data = read_json_body(request, default={})
709
    input_data = utils.get_json_body(request)
711 710
    func, action_data = get_action(MEMBERSHIP_ACTION, input_data)
712 711
    with ExceptionHandler():
713 712
        func(memb_id, user, reason=action_data)
b/snf-astakos-app/astakos/api/quotas.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from django.utils import simplejson as json
35 34
from django.views.decorators.csrf import csrf_exempt
36 35
from django.http import HttpResponse
37 36
from django.db import transaction
38 37

  
39 38
from snf_django.lib import api
40 39
from snf_django.lib.api.faults import BadRequest, ItemNotFound
40
from snf_django.lib.api import utils
41 41
from django.core.cache import cache
42 42

  
43 43
from astakos.im import settings
......
48 48
import astakos.quotaholder_app.exception as qh_exception
49 49
import astakos.quotaholder_app.callpoint as qh
50 50

  
51
from .util import (json_response, is_integer, are_integer,
51
from .util import (json_response, is_integer, are_integer, check_is_dict,
52 52
                   user_from_token, component_from_token)
53 53

  
54 54

  
......
147 147
@api.api_method(http_method='POST', token_required=True, user_required=False)
148 148
@component_from_token
149 149
def issue_commission(request):
150
    data = request.body
151
    try:
152
        input_data = json.loads(data)
153
    except json.JSONDecodeError:
154
        raise BadRequest("POST data should be in json format.")
150
    input_data = utils.get_json_body(request)
151
    check_is_dict(input_data)
155 152

  
156 153
    client_key = unicode(request.component_instance)
157 154
    provisions = input_data.get('provisions')
......
237 234
@component_from_token
238 235
@transaction.commit_on_success
239 236
def resolve_pending_commissions(request):
240
    data = request.body
241
    try:
242
        input_data = json.loads(data)
243
    except json.JSONDecodeError:
244
        raise BadRequest("POST data should be in json format.")
237
    input_data = utils.get_json_body(request)
238
    check_is_dict(input_data)
245 239

  
246 240
    client_key = unicode(request.component_instance)
247 241
    accept = input_data.get('accept', [])
......
293 287
@component_from_token
294 288
@transaction.commit_on_success
295 289
def serial_action(request, serial):
296
    data = request.body
297
    try:
298
        input_data = json.loads(data)
299
    except json.JSONDecodeError:
300
        raise BadRequest("POST data should be in json format.")
290
    input_data = utils.get_json_body(request)
291
    check_is_dict(input_data)
301 292

  
302 293
    try:
303 294
        serial = int(serial)
b/snf-astakos-app/astakos/api/util.py
81 81
    return response
82 82

  
83 83

  
84
def read_json_body(request, default=None):
85
    body = request.body
86
    if not body and request.method == "GET":
87
        body = request.GET.get("body")
88
    if not body:
89
        return default
90
    try:
91
        return json.loads(body)
92
    except json.JSONDecodeError:
93
        raise faults.BadRequest("Request body should be in json format.")
84
def check_is_dict(obj):
85
    if not isinstance(obj, dict):
86
        raise faults.BadRequest("Request should be a JSON dict")
94 87

  
95 88

  
96 89
def is_integer(x):
b/snf-astakos-app/astakos/im/tests/api.py
415 415
        self.assertEqual(r.status_code, 405)
416 416
        self.assertTrue('Allow' in r)
417 417

  
418
        r = client.post(u('commissions'), "\"\xff\"",
419
                        content_type='application/json', **s1_headers)
420
        self.assertEqual(r.status_code, 400)
421

  
422
        r = client.post(u('commissions'), "\"nodict\"",
423
                        content_type='application/json', **s1_headers)
424
        self.assertEqual(r.status_code, 400)
425

  
426
        r = client.post(u('commissions/' + "123" + '/action'), "\"\xff\"",
427
                        content_type='application/json', **s1_headers)
428
        self.assertEqual(r.status_code, 400)
429

  
430
        r = client.post(u('commissions/' + "123" + '/action'), "\"nodict\"",
431
                        content_type='application/json', **s1_headers)
432
        self.assertEqual(r.status_code, 400)
433

  
418 434

  
419 435
class TokensApiTest(TestCase):
420 436
    def setUp(self):
b/snf-astakos-app/astakos/im/tests/projects.py
620 620
        r = client.get(reverse("api_projects"), filters, **h_owner)
621 621
        self.assertEqual(r.status_code, 400)
622 622

  
623
        r = self.client.post(reverse("api_projects"), "\xff",
624
                             content_type="application/json", **h_owner)
625
        self.assertEqual(r.status_code, 400)
626

  
627
        r = self.client.post(reverse("api_project_action",
628
                                     kwargs={"project_id": "1234"}),
629
                             "\"nondict\"", content_type="application/json",
630
                             **h_owner)
631
        self.assertEqual(r.status_code, 400)
632

  
633
        r = client.get(reverse("api_project",
634
                               kwargs={"project_id": u"πρότζεκτ"}),
635
                       **h_owner)
636
        self.assertEqual(r.status_code, 404)
637

  
623 638

  
624 639
class TestProjects(TestCase):
625 640
    """

Also available in: Unified diff