def decorated(event, context): try: # Create ApiRequest layer and init context # Get DB Session resource = get_resource() req = Request(event=event, aws_request_id=context.aws_request_id, resource=resource) req.set_request_context() # Assign request to context context.request = req # Update logging level if DEBUG_HEADER in config.global_context and config.global_context[ DEBUG_HEADER] == 'true': logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) # Call method and manage session in case of error err, res = func(event, context, resource) except Exception as e: logger.error(e) err, res = e, None err.status_code = '500' return response(err, res)
def test_check_params_with_empty_mandatory(self): req = Request(self.event_params, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') err, res = req.check_params({'foo': ''}, ['foo'], []) assert res is None self.assertEqual('Empty or missing mandatory parameter(s): foo', str(err))
def test_check_params_ignores_extras(self): params = { 'mandatory': 'foo', 'optional': 'bar', 'extra': 'baz', } req = Request(self.event_params, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') err, res = req.check_params(params, ['mandatory'], ['optional']) assert res is not None assert 'extra' not in res
def test_check_params_mandatory_not_empty(self): event = json.loads("""{ "body":null, "queryStringParameters":{ "mandatory1":"", "mandatory2":"bar", "optional1":"baz", "extra1":"quux" } }""") req = Request(event, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') (err, res) = req.get_and_check_params(['mandatory1', 'mandatory2'], ['optional1']) self.assertFalse(res) self.assertEqual('Empty or missing mandatory parameter(s): mandatory1', str(err))
def test_check_params(self): event = json.loads("""{ "body":null, "queryStringParameters":{ "mandatory1":"foo", "mandatory2":"bar", "optional1":"baz", "extra1":"quux" }, "pathParameters":null }""") req = Request(event, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') (err, res) = req.get_and_check_params(['mandatory1', 'mandatory2'], ['optional1']) self.assertTrue(res) self.assertEqual(['mandatory1', 'mandatory2', 'optional1'], sorted(req.params.keys()))
def decorated(event, context): try: resource = get_resource() req = Request(event=event, resource=resource) # Assign request to context context.request = req # Call method and manage session in case of error err, res = func(event, context, resource) except Exception as e: logger.error(e) err, res = e, None err.status_code = '500' return response(err, res)
def test_check_params_with_missing_optional(self): req = Request(self.event_params, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') err, res = req.check_params({}, [], ['optional']) assert res is not None assert err is None
def test_check_params_are_on_path(self): req = Request(self.event_params, '0fa8fc8f-b04a-11e6-b8ed-6dda0ca82a80') (err, res) = req.get_and_check_params(['mandatory1'], []) self.assertTrue(res)