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())
Exemple #2
0
    def handler(self, event, context):
        """
        Request handler method for fetch 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())

        _identifier = event[Constants.event_path_parameters()][
            Constants.event_path_parameter_identifier()]
        _http_method = event[Constants.event_http_method()]

        if _http_method == HttpConstants.http_method_get() and _identifier:
            _ddb_response = self.__retrieve_resource(_identifier)
            if len(_ddb_response[
                    Constants.ddb_response_attribute_name_items()]) == 0:
                return response(http.HTTPStatus.NOT_FOUND,
                                json.dumps(_ddb_response))
            return response(http.HTTPStatus.OK, 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,
        Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): resource[Constants.event_identifier()]}
    }
 def test_app_event_invalid_json_in_body(self):
     from resource_api.modify_resource import app
     event = {
         "httpMethod": "PUT",
         "body": "asdf",
         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.BAD_REQUEST,
                      'HTTP Status code not 400')
Exemple #5
0
 def test_app_missing_env_region(self):
     del os.environ['REGION']
     from resource_api.fetch_resource import app
     _event = {
         Constants.event_http_method(): HttpConstants.http_method_get(),
         Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): self.EXISTING_RESOURCE_IDENTIFIER},
     }
     app._dynamodb = None
     _handler_response = app.handler(_event, None)
     self.assertEqual(_handler_response[Constants.response_status_code()], http.HTTPStatus.INTERNAL_SERVER_ERROR,
                      'HTTP Status code not 500')
Exemple #6
0
    def test_app_missing_env_table(self):
        del os.environ['TABLE_NAME']
        from resource_api.fetch_resource import app
        _event = {
            Constants.event_http_method(): HttpConstants.http_method_get(),
            Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): ''},
        }

        _handler_response = app.handler(_event, None)
        self.assertEqual(_handler_response[Constants.response_status_code()], http.HTTPStatus.INTERNAL_SERVER_ERROR,
                         'HTTP Status code not 500')
Exemple #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)
Exemple #8
0
    def test_handler_retrieve_resource_missing_resource_identifier(self):
        from resource_api.fetch_resource.main.RequestHandler import RequestHandler
        _dynamodb = self.setup_mock_database('eu-west-1',
                                             'testing')
        _request_handler = RequestHandler(_dynamodb)

        _event = {
            Constants.event_http_method(): HttpConstants.http_method_get(),
            Constants.event_path_parameters(): {}
        }

        _handler_retrieve_response = _request_handler.handler(_event, None)

        self.assertEqual(_handler_retrieve_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST,
                         'HTTP Status code not 400')
        remove_mock_database(_dynamodb)
Exemple #9
0
    def test_handler_retrieve_resource_not_found(self):
        from resource_api.fetch_resource.main.RequestHandler import RequestHandler
        _dynamodb = self.setup_mock_database('eu-west-1',
                                             'testing')
        _request_handler = RequestHandler(_dynamodb)

        _event = {
            Constants.event_http_method(): HttpConstants.http_method_get(),
            Constants.event_path_parameters(): {Constants.event_path_parameter_identifier(): 'fbf20333-35a5-4a06-9c58-68ea688a9a8b'}
        }

        _handler_retrieve_response = _request_handler.handler(_event, None)

        self.assertEqual(_handler_retrieve_response[Constants.response_status_code()], http.HTTPStatus.NOT_FOUND,
                         'HTTP Status code not 404')
        remove_mock_database(_dynamodb)