Beispiel #1
0
    def test__serial_error(self):
        """If obj is not serializable, raise an exception."""

        obj = MagicMock()

        # Actual call
        with self.assertRaises(TypeError) as ex:
            JSONSerializer._serial(obj)

        # Asserts
        exception = ex.exception
        self.assertEqual('MagicMock is not JSON serializable', str(exception))
Beispiel #2
0
    def __init__(self, conf):
        self.conf = conf
        self._set_mongodb_up(conf)

        middleware = [
            JSONSerializer(),
            ResponseBuilder(),
        ]
        super(API, self).__init__(request_type=Request, middleware=middleware)

        self.set_error_serializer(exception_serializer)
        self.add_error_handler(Exception, unknown_exception_serializer)

        self.endpoint = conf['api']['endpoint']

        RemoteRunnable.init(self)
        self.add_route(RemoteRunnable.route, RemoteRunnable)

        self.runnables = dict()
        if conf['api'].get('default_actions', True):
            self.load_actions('smapy.actions')

        actions_module = conf['api'].get('actions_module')
        if actions_module:
            self.load_actions(actions_module)

        if conf['api'].get('default_resources', True):
            prefix = conf['api'].get('default_resources_prefix', '')
            self._load_default_resources(prefix)

        resources = conf.get('resources')
        if resources:
            self._load_resources(conf.get('resources', dict()))
Beispiel #3
0
    def test__serial_objectid(self):
        """If obj is an ObjectId, print it as a string."""

        obj = ObjectId('57bee205ab17852928644d3e')

        # Actual call
        serialized = JSONSerializer._serial(obj)

        # Asserts
        self.assertEqual('57bee205ab17852928644d3e', serialized)
Beispiel #4
0
    def test__serial_datetime(self):
        """If obj is a datetime, isoformat it."""

        obj = datetime.datetime(2000, 1, 1)

        # Actual call
        serialized = JSONSerializer._serial(obj)

        # Asserts
        self.assertEqual('2000-01-01T00:00:00', serialized)
Beispiel #5
0
    def test_process_request_content_length_is_0(self):
        """If content_lenght==0 is not given req.body must be an empty dict."""

        # Set up
        req = MagicMock()
        req.content_length = 0
        resp = MagicMock()

        # Actual call
        JSONSerializer().process_request(req, resp)

        # Asserts
        self.assertEqual(dict(), req.body)
Beispiel #6
0
    def test_process_response_string(self):
        """If body is already string do nothing."""

        # Set up
        req = MagicMock()
        resp = MagicMock()
        resp.body = 'a string'
        resource = MagicMock()

        # Actual call
        JSONSerializer().process_response(req, resp, resource)

        # Asserts
        self.assertEqual('a string', resp.body)
Beispiel #7
0
    def test_process_response_empty(self):
        """If body is empty do nothing."""

        # Set up
        req = MagicMock()
        resp = MagicMock()
        resp.body = None
        resource = MagicMock()

        # Actual call
        JSONSerializer().process_response(req, resp, resource)

        # Asserts
        self.assertIsNone(resp.body)
Beispiel #8
0
    def test_process_request_success(self):
        """If body is a valid JSON it must be loaded into req.body."""

        # Set up
        req = MagicMock()
        req.content_length = 100
        req.stream.read.return_value = '{"valid": "JSON"}'.encode('utf-8')
        resp = MagicMock()

        # Actual call
        JSONSerializer().process_request(req, resp)

        # Asserts
        body = {'valid': 'JSON'}
        self.assertEqual(body, req.body)
Beispiel #9
0
    def test_process_response_external(self):
        """If not internal, serialize using the custom serializer."""

        # Set up
        req = MagicMock()
        req.context = {'internal': False}
        resp = MagicMock()
        resp.body = {'a datetime': datetime.datetime(2000, 1, 1)}
        resource = MagicMock()

        # Actual call
        JSONSerializer().process_response(req, resp, resource)

        # Asserts
        expected_body = '{\n    "a datetime": "2000-01-01T00:00:00"\n}'
        self.assertEqual(expected_body, resp.body)
Beispiel #10
0
    def test_process_response_internal(self):
        """If internal, serialize using json_util."""

        # Set up
        req = MagicMock()
        req.context = {'internal': True}
        resp = MagicMock()
        resp.body = {'a datetime': datetime.datetime(2000, 1, 1)}
        resource = MagicMock()

        # Actual call
        JSONSerializer().process_response(req, resp, resource)

        # Asserts
        expected_body = '{\n    "a datetime": {\n        "$date": 946684800000\n    }\n}'
        self.assertEqual(expected_body, resp.body)
Beispiel #11
0
    def test_process_request_malformed_json(self):
        """If body is not a valid JSON an excpetion must be raised."""

        # Set up
        req = MagicMock()
        req.content_length = 100
        req.stream.read.return_value = b'this is not a JSON'
        resp = MagicMock()

        # Actual call
        with self.assertRaises(falcon.HTTPBadRequest) as ex:
            JSONSerializer().process_request(req, resp)

        # Asserts
        exception = ex.exception
        self.assertEqual('Malformed JSON', exception.title)
        self.assertEqual('A valid JSON document is required.',
                         exception.description)
Beispiel #12
0
    def test_process_request_encoding_error(self):
        """If body is not a encoded as UTF-8 an excpetion must be raised."""

        # Set up
        req = MagicMock()
        req.content_length = 100
        req.stream.read.return_value = 'this is not UTF-8'.encode('utf-16')
        resp = MagicMock()

        # Actual call
        with self.assertRaises(falcon.HTTPBadRequest) as ex:
            JSONSerializer().process_request(req, resp)

        # Asserts
        exception = ex.exception
        self.assertEqual('Encoding Error', exception.title)
        self.assertEqual('Request must be encoded as UTF-8.',
                         exception.description)
Beispiel #13
0
    def test_process_request_no_body(self):
        """If body is empty an excpetion must be raised."""

        # Set up
        req = MagicMock()
        req.content_length = 100
        req.stream.read.return_value = None
        resp = MagicMock()

        # Actual call
        with self.assertRaises(falcon.HTTPBadRequest) as ex:
            JSONSerializer().process_request(req, resp)

        # Asserts
        exception = ex.exception
        self.assertEqual('Empty request body', exception.title)
        self.assertEqual('A valid JSON document is required.',
                         exception.description)