Esempio n. 1
0
    def test_required_method_put(self):
        from djsonapi import api
        from djsonapi import serial

        @api.required_method("PUT", debug=True)
        def view(request, put=None):
            self.assertIsNotNone(put)
            return api.ok(put=put)

        ## test put

        data = {"test": 123}
        json = serial.dumps(data)

        factory = RequestFactory()
        request = factory.put("/", content_type="application/json", data=json)
        response = view(request)
        response_data = serial.loads(response.content)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data, response_data["body"]["put"])

        ## test debug

        factory = RequestFactory()
        request = factory.put("/", content_type="application/json", data="FAILJSON")
        response = view(request)
        response_data = serial.loads(response.content)

        self.assertEqual(response.status_code, 400)
        self.assertFalse(response_data["ok"])
        self.assertIsNotNone(response_data["body"]["exception"])
Esempio n. 2
0
 def wrapper(request, *args, **kwargs):
     # if the request method is acceptable
     if request.method in methods:
         # if its post
         if request.method in FORM_METHOD_TYPES:
             # convert the body into JSON
             if request.body:
                 try:
                     # parse that body
                     post = serial.loads(request.body)
                 except Exception as exc:
                     # unless it doesnt parse
                     if debug:
                         return invalid(
                             "Invalid JSON %s" % request.method,
                             exception=str(exc))
                     else:
                         return invalid(
                             "Invalid JSON %s" % request.method)
             else:
                 post = {}
             # and return the result
             kwarg_name = request.method.lower()
             kwargs[kwarg_name] = post
             return func(request, *args, **kwargs)
         # if its not post, return the function
         return func(request, *args, **kwargs)
     else:
         # invalid request method
         return error405()
Esempio n. 3
0
    def test_error405(self):
        from djsonapi import api
        from djsonapi import serial

        response = api.error405(user=123)
        self.assertEqual(response.status_code, 405)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data["message"], "Method Not Supported")
        self.assertEqual(response_data["body"]["user"], 123)
Esempio n. 4
0
    def test_error405(self):
        from djsonapi import api
        from djsonapi import serial

        response = api.error405(user=123)
        self.assertEqual(response.status_code, 405)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data["message"], "Method Not Supported")
        self.assertEqual(response_data["body"]["user"], 123)
Esempio n. 5
0
    def test_form_valid(self):
        from django import forms
        from djsonapi import api
        from djsonapi import serial

        class SomeForm(forms.Form):
            field = forms.IntegerField()

        @api.required_method("GET", "POST", "PUT")
        @api.post_form(SomeForm)
        def view(request, form=None):
            return api.ok(form=form.cleaned_data if form else None,
                          method=request.method)

        ## Valid

        data = {"field": 123}
        json = serial.dumps(data)

        factory = RequestFactory()
        request = factory.post("/", content_type="application/json", data=json)
        response = view(request)
        response_data = serial.loads(response.content)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_data["body"]["method"], "POST")
        self.assertEqual(response_data["body"]["form"]["field"], data["field"])

        ## Invalid

        data = {"xxx": 123}
        json = serial.dumps(data)

        factory = RequestFactory()
        request = factory.post("/", content_type="application/json", data=json)
        response = view(request)
        response_data = serial.loads(response.content)

        self.assertEqual(response.status_code, 400)
        self.assertIsNotNone(response_data["body"]["errors"]["field"])
Esempio n. 6
0
    def test_form_valid(self):
        from django import forms
        from djsonapi import api
        from djsonapi import serial

        class SomeForm(forms.Form):
            field = forms.IntegerField()

        @api.required_method("GET", "POST", "PUT")
        @api.post_form(SomeForm)
        def view(request, form=None):
            return api.ok(form=form.cleaned_data if form  else None, method=request.method)

        ## Valid

        data = {"field": 123}
        json = serial.dumps(data)

        factory = RequestFactory()
        request = factory.post("/", content_type="application/json", data=json)
        response = view(request)
        response_data = serial.loads(response.content)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_data["body"]["method"], "POST")
        self.assertEqual(response_data["body"]["form"]["field"], data["field"])

        ## Invalid

        data = {"xxx": 123}
        json = serial.dumps(data)

        factory = RequestFactory()
        request = factory.post("/", content_type="application/json", data=json)
        response = view(request)
        response_data = serial.loads(response.content)

        self.assertEqual(response.status_code, 400)
        self.assertIsNotNone(response_data["body"]["errors"]["field"])
Esempio n. 7
0
    def test_catch500(self):
        from djsonapi import api
        from djsonapi import serial

        @api.catch500(log_error=False)
        def raise_exc(request):
            raise Exception("Failure")

        factory = RequestFactory()
        request = factory.get("/")

        response = raise_exc(request)

        self.assertEqual(response.status_code, 500)

        json = serial.loads(response.content)
        self.assertFalse(json["ok"])
Esempio n. 8
0
    def test_required_methods_get(self):
        from djsonapi import api
        from djsonapi import serial

        @api.required_method("GET")
        def view(request, post=None):
            return api.ok(post=post)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json", data="")
        response = view(request)
        not_found = object()
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["post"], None)
Esempio n. 9
0
    def test_required_methods_post_invalid_json(self):
        from djsonapi import api
        from djsonapi import serial

        @api.required_method("POST")
        def view(request, post=None):
            pass

        factory = RequestFactory()
        request = factory.post("/", content_type="application/json", data="garbage23*(%*@")
        response = view(request)
        self.assertEqual(response.status_code, 400)
        response_data = serial.loads(response.content)
        not_found = object()
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data.get("message", not_found), "Invalid JSON POST")
        self.assertEqual(response_data.get("body", not_found), not_found)
Esempio n. 10
0
    def test_catch500(self):
        from djsonapi import api
        from djsonapi import serial

        @api.catch500(log_error=False)
        def raise_exc(request):
            raise Exception("Failure")

        factory = RequestFactory()
        request = factory.get("/")

        response = raise_exc(request)

        self.assertEqual(response.status_code, 500)

        json = serial.loads(response.content)
        self.assertFalse(json["ok"])
Esempio n. 11
0
    def test_required_methods_get(self):
        from djsonapi import api
        from djsonapi import serial

        @api.required_method("GET")
        def view(request, post=None):
            return api.ok(post=post)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json", data="")
        response = view(request)
        not_found = object()
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["post"], None)
Esempio n. 12
0
    def test_required_methods_post_invalid_json(self):
        from djsonapi import api
        from djsonapi import serial

        @api.required_method("POST")
        def view(request, post=None):
            pass

        factory = RequestFactory()
        request = factory.post("/",
                               content_type="application/json",
                               data="garbage23*(%*@")
        response = view(request)
        self.assertEqual(response.status_code, 400)
        response_data = serial.loads(response.content)
        not_found = object()
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data.get("message", not_found),
                         "Invalid JSON POST")
        self.assertEqual(response_data.get("body", not_found), not_found)
Esempio n. 13
0
    def test_login_required_fail_url(self):
        from djsonapi import api
        from djsonapi import serial

        class AnonUser:
            def is_authenticated(self):
                return False

        @api.login_required(login_url="/abc123/")
        def view(request):
            pass

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        request.user = AnonUser()
        response = view(request)
        self.assertEqual(response.status_code, 401)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data["message"], "Unauthorized")
        self.assertEqual(response_data["body"]["login_url"], "/abc123/")
Esempio n. 14
0
    def test_login_required_fail_url(self):
        from djsonapi import api
        from djsonapi import serial

        class AnonUser:
            def is_authenticated(self):
                return False

        @api.login_required(login_url="/abc123/")
        def view(request):
            pass

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        request.user = AnonUser()
        response = view(request)
        self.assertEqual(response.status_code, 401)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], False)
        self.assertEqual(response_data["message"], "Unauthorized")
        self.assertEqual(response_data["body"]["login_url"], "/abc123/")
Esempio n. 15
0
    def test_post_form_get(self):
        from django import forms
        from djsonapi import api
        from djsonapi import serial

        class TestForm(forms.Form):
            field = forms.IntegerField()

        @api.required_method("GET", "POST")
        @api.post_form(lambda request, data: TestForm(data=data))
        def view(request, form=None):
            return api.ok(data=form.cleaned_data if form else None)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        response = view(request)
        not_found = object()
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["data"], None)
Esempio n. 16
0
    def test_login_required_ok(self):
        from djsonapi import api
        from djsonapi import serial

        class User:
            def is_authenticated(self):
                return True

        @api.login_required()
        def view(request):
            return api.ok(user=123)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        request.user = User()
        response = view(request)
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        not_found = object()
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["user"], 123)
Esempio n. 17
0
    def test_post_form_get(self):
        from django import forms
        from djsonapi import api
        from djsonapi import serial

        class TestForm(forms.Form):
            field = forms.IntegerField()

        @api.required_method("GET", "POST")
        @api.post_form(lambda request, data: TestForm(data=data))
        def view(request, form=None):
            return api.ok(data=form.cleaned_data if form else None)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        response = view(request)
        not_found = object()
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["data"], None)
Esempio n. 18
0
    def test_login_required_ok(self):
        from djsonapi import api
        from djsonapi import serial

        class User:
            def is_authenticated(self):
                return True

        @api.login_required()
        def view(request):
            return api.ok(user=123)

        factory = RequestFactory()
        request = factory.get("/", content_type="application/json")
        request.user = User()
        response = view(request)
        self.assertEqual(response.status_code, 200)
        response_data = serial.loads(response.content)
        not_found = object()
        self.assertEqual(response_data["ok"], True)
        self.assertEqual(response_data.get("message", not_found), not_found)
        self.assertEqual(response_data["body"]["user"], 123)