def test_helper_response_with_cors(self): _response = response(http.HTTPStatus.OK, 'message') self.assertEqual(_response[Constants.response_status_code()], http.HTTPStatus.OK) self.assertEqual( _response[Constants.response_headers()][ HttpConstants.http_header_access_control_allow_origin()], '*')
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(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')
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 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_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')
def modify_resource(self, modified_resource): ddb_response = self.table.query( KeyConditionExpression=Key(Constants.ddb_field_identifier()).eq( modified_resource[Constants.event_identifier()])) if len(ddb_response[Constants.ddb_response_attribute_name_items()]) == 0: raise ValueError('Resource with identifier ' + modified_resource[Constants.event_identifier()] + ' not found') ddb_response = self.table.put_item(Item=modified_resource) return ddb_response
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_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')
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')
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')
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)
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)
def test_handler_missing_event(self): from resource_api.modify_resource import app dynamodb = self.setup_mock_database('eu-west-1', 'testing') handler_modify_response = app.handler(None, None) self.assertEqual(handler_modify_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400') remove_mock_database(dynamodb)
def test_app_event_empty_body(self): from resource_api.modify_resource import app event = { "httpMethod": "PUT", "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.modify_resource import app app.clear_dynamodb() resource = self.generate_mock_resource() _event = generate_mock_event(HttpConstants.http_method_put(), resource) _handler_response = app.handler(_event, None) self.assertEqual(_handler_response[Constants.response_status_code()], http.HTTPStatus.INTERNAL_SERVER_ERROR, 'HTTP Status code not 500')
def test_handler_missing_resource_in_event_body(self): from resource_api.insert_resource.main.RequestHandler import RequestHandler dynamodb = self.setup_mock_database('eu-west-1', 'testing') request_handler = RequestHandler(dynamodb) event = generate_mock_event(HttpConstants.http_method_post(), None) handler_insert_response = request_handler.handler(event, None) self.assertEqual( handler_insert_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400') remove_mock_database(dynamodb)
def test_handler_retrieve_resource_missing_event(self): from resource_api.fetch_resource.main.RequestHandler import RequestHandler _dynamodb = self.setup_mock_database('eu-west-1', 'testing') _request_handler = RequestHandler(_dynamodb) _handler_retrieve_response = _request_handler.handler(None, None) self.assertEqual(_handler_retrieve_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400') remove_mock_database(_dynamodb)
def test_handler_modify_resource(self): from resource_api.modify_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_put(), resource) handler_modify_response = request_handler.handler(event, None) self.assertEqual(handler_modify_response[Constants.response_status_code()], http.HTTPStatus.OK, 'HTTP Status code not 200') remove_mock_database(dynamodb)
def test_handler_unknown_http_method_in_event(self): from resource_api.modify_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('INVALID_HTTP_METHOD', resource) handler_modify_response = request_handler.handler(event, None) self.assertEqual(handler_modify_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400') 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 test_handler_modify_resource_unknown_resource_identifier_in_event_body(self): from resource_api.modify_resource.main.RequestHandler import RequestHandler dynamodb = self.setup_mock_database('eu-west-1', 'testing') request_handler = RequestHandler(dynamodb) resource = self.generate_mock_resource() resource['identifier'] = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' event = generate_mock_event(HttpConstants.http_method_put(), resource) handler_modify_response = request_handler.handler(event, None) self.assertEqual(handler_modify_response[Constants.response_status_code()], http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400') self.assertEqual(handler_modify_response['body'], 'Resource with identifier xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx not found', 'Did not get expected error message') remove_mock_database(dynamodb)
def handler(event, context): """ Handler method for insert resource function. """ 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_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)
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 generate_mock_event(http_method, resource): body_value = json.dumps(resource) return { Constants.event_http_method(): http_method, Constants.event_body(): body_value, }
def remove_mock_database(dynamodb): dynamodb.Table(os.environ[Constants.env_var_table_name()]).delete()
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 __init__(self, dynamodb=None): self.dynamodb = dynamodb self.table_name = os.environ.get(Constants.env_var_table_name()) self.table: Table = self.dynamodb.Table(self.table_name)
def test_helper_response(self): _response = response(http.HTTPStatus.OK, 'message') self.assertEqual(_response[Constants.response_status_code()], http.HTTPStatus.OK)