def generate_mock_event(http_method, resource):
    body_value = json.dumps(resource)
    return {
        Constants.event_http_method(): http_method,
        Constants.event_body(): body_value,
        Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): resource[Constants.event_identifier()]}
    }
    def handler(self, event, context):
        """
        Request handler method for modify resource function.
        """
        if event is None or Constants.event_path_parameters() not in event:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        if Constants.event_path_parameter_identifier() not in event[Constants.event_path_parameters()]:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        try:
            body = json.loads(event[Constants.event_body()])
        except JSONDecodeError as e:
            return response(http.HTTPStatus.BAD_REQUEST, str(e))

        identifier = event[Constants.event_path_parameters()][Constants.event_path_parameter_identifier()]
        http_method = event[Constants.event_http_method()]

        if http_method == HttpConstants.http_method_put() and body is not None:
            try:
                ddb_response = self.modify_resource(body)
                ddb_response[Constants.event_identifier()] = identifier
                return response(http.HTTPStatus.OK, json.dumps(ddb_response))
            except ValueError as e:
                return response(http.HTTPStatus.BAD_REQUEST, str(e))

        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
 def test_app(self):
     from resource_api.insert_resource import app
     event = {Constants.event_body(): "{}"}
     handler_response = app.handler(event, None)
     self.assertEqual(handler_response[Constants.response_status_code()],
                      http.HTTPStatus.BAD_REQUEST,
                      'HTTP Status code not 400')
Beispiel #4
0
 def test_app_event_invalid_json_in_body(self):
     from resource_api.fetch_resource import app
     event = {
         Constants.event_http_method(): HttpConstants.http_method_get(),
         Constants.event_body(): "asdf"
     }
     handler_response = app.handler(event, None)
     self.assertEqual(handler_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST,
                      'HTTP Status code not 400')
 def test_app_event_empty_body(self):
     from resource_api.insert_resource import app
     event = {
         Constants.event_http_method(): HttpConstants.http_method_post(),
         Constants.event_body(): ""
     }
     handler_response = app.handler(event, None)
     self.assertEqual(handler_response[Constants.response_status_code()],
                      http.HTTPStatus.BAD_REQUEST,
                      'HTTP Status code not 400')
 def test_app_missing_env_region(self):
     del os.environ['REGION']
     from resource_api.insert_resource import app
     app.clear_dynamodb()
     _event = {
         Constants.event_http_method(): HttpConstants.http_method_post(),
         Constants.event_body(): "{}"
     }
     _handler_response = app.handler(_event, None)
     self.assertEqual(_handler_response[Constants.response_status_code()],
                      http.HTTPStatus.INTERNAL_SERVER_ERROR,
                      'HTTP Status code not 500')
Beispiel #7
0
 def test_app(self):
     from resource_api.fetch_resource import app
     dynamodb = self.setup_mock_database('eu-west-1', 'testing')
     _event = {
         Constants.event_http_method(): HttpConstants.http_method_get(),
         Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): self.EXISTING_RESOURCE_IDENTIFIER},
     }
     _handler_response = app.handler(_event, None)
     self.assertEqual(_handler_response[Constants.response_status_code()], http.HTTPStatus.OK,
                      'HTTP Status code not 200')
     _ddb_response = json.loads(_handler_response[Constants.event_body()])
     self.assertEqual(_ddb_response[Constants.ddb_response_attribute_name_count()], 1,
                      'Count is not 1')
     remove_mock_database(dynamodb)
Beispiel #8
0
def handler(event, context):
    """
    Handler method for insert resource function.
    """
    if event is None:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
    if event is None or Constants.event_body() not in event or Constants.event_http_method() not in event:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
    if event[Constants.event_body()] is None or len(event[Constants.event_body()]) is 0:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

    global _dynamodb
    if _dynamodb is None:
        try:
            ddb = DynamoDB()
            _dynamodb = ddb.connect(os.environ[Constants.env_var_region()])
        except Exception as e:
            return response(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))

    try:
        request_handler = RequestHandler(_dynamodb)
    except Exception as e:
        return response(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))
    return request_handler.handler(event, context)
    def test_insert_resource(self):
        from resource_api.insert_resource.main.RequestHandler import RequestHandler
        dynamodb = self.setup_mock_database('eu-west-1', 'testing')
        request_handler = RequestHandler(dynamodb)

        resource = self.generate_mock_resource()
        event = generate_mock_event(HttpConstants.http_method_post(), resource)
        handler_insert_response = request_handler.handler(event, None)

        resource_dict_from_json = json.loads(event[Constants.event_body()])
        resource_inserted = resource_dict_from_json

        self.assertEqual(
            handler_insert_response[Constants.response_status_code()],
            http.HTTPStatus.CREATED, 'HTTP Status code not 201')

        resource_identifier = resource_inserted[Constants.event_identifier()]

        query_results = request_handler.get_table_connection().query(
            KeyConditionExpression=Key(
                Constants.ddb_field_identifier()).eq(resource_identifier),
            ScanIndexForward=True)

        inserted_resource = query_results[
            Constants.ddb_response_attribute_name_items()][0]
        self.assertIsNotNone(
            inserted_resource[Constants.ddb_field_created_date()],
            'Value not persisted as expected')
        self.assertIsNotNone(
            inserted_resource[Constants.ddb_field_modified_date()],
            'Value not persisted as expected')
        self.assertIsNotNone(
            inserted_resource[Constants.ddb_field_entity_description()],
            'Value not persisted as expected')
        self.assertEqual(
            inserted_resource[Constants.ddb_field_modified_date()],
            inserted_resource[Constants.ddb_field_created_date()],
            'Value not persisted as expected')
        self.assertEqual(
            inserted_resource[Constants.ddb_field_entity_description()],
            resource_inserted['entityDescription'],
            'Value not persisted as expected')
        remove_mock_database(dynamodb)
    def handler(self, event, context):
        """
        Request handler method for insert resource function.
        """

        if event is None:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        try:
            body = event[Constants.event_body()]
            print(body)
            body_as_json = json.loads(body)
        except JSONDecodeError as e:
            return response(http.HTTPStatus.BAD_REQUEST, str(e))

        http_method = event[Constants.event_http_method()]

        if http_method == HttpConstants.http_method_post() and body_as_json is not None:
            ddb_response = self.insert_resource(body_as_json)
            return response(http.HTTPStatus.CREATED, json.dumps(ddb_response))

        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
def generate_mock_event(http_method, resource):
    body_value = json.dumps(resource)
    return {
        Constants.event_http_method(): http_method,
        Constants.event_body(): body_value,
    }