This commit is contained in:
Bert Blommers 2020-04-14 08:06:00 +01:00
parent 1c96a05314
commit 69f963a3c2
3 changed files with 64 additions and 92 deletions

View File

@ -39,7 +39,7 @@ from .exceptions import (
InvalidRestApiId,
InvalidModelName,
RestAPINotFound,
ModelNotFound
ModelNotFound,
)
STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}"
@ -499,13 +499,15 @@ class RestAPI(BaseModel):
self.resources[child_id] = child
return child
def add_model(self,
name,
description=None,
schema=None,
content_type=None,
cli_input_json=None,
generate_cli_skeleton=None):
def add_model(
self,
name,
description=None,
schema=None,
content_type=None,
cli_input_json=None,
generate_cli_skeleton=None,
):
model_id = create_id()
new_model = Model(
id=model_id,
@ -514,7 +516,8 @@ class RestAPI(BaseModel):
schema=schema,
content_type=content_type,
cli_input_json=cli_input_json,
generate_cli_skeleton=generate_cli_skeleton)
generate_cli_skeleton=generate_cli_skeleton,
)
self.models[name] = new_model
return new_model
@ -670,7 +673,7 @@ class DomainName(BaseModel, dict):
self["generateCliSkeleton"] = kwargs.get("generate_cli_skeleton")
class Model(BaseModel,dict):
class Model(BaseModel, dict):
def __init__(self, id, name, **kwargs):
super(Model, self).__init__()
self["id"] = id
@ -1130,14 +1133,16 @@ class APIGatewayBackend(BaseBackend):
else:
return self.domain_names[domain_name]
def create_model(self,
rest_api_id,
name,
content_type,
description=None,
schema=None,
cli_input_json=None,
generate_cli_skeleton=None):
def create_model(
self,
rest_api_id,
name,
content_type,
description=None,
schema=None,
cli_input_json=None,
generate_cli_skeleton=None,
):
if not rest_api_id:
raise InvalidRestApiId
@ -1151,7 +1156,8 @@ class APIGatewayBackend(BaseBackend):
schema=schema,
content_type=content_type,
cli_input_json=cli_input_json,
generate_cli_skeleton=generate_cli_skeleton)
generate_cli_skeleton=generate_cli_skeleton,
)
return new_model

View File

@ -16,7 +16,7 @@ from .exceptions import (
InvalidRestApiId,
InvalidModelName,
RestAPINotFound,
ModelNotFound
ModelNotFound,
)
API_KEY_SOURCES = ["AUTHORIZER", "HEADER"]
@ -600,15 +600,13 @@ class APIGatewayResponse(BaseResponse):
),
)
def models(self,request, full_url, headers):
def models(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
rest_api_id = self.path.replace("/restapis/", "", 1).split("/")[0]
try:
if self.method == "GET":
models = self.backend.get_models(
rest_api_id
)
models = self.backend.get_models(rest_api_id)
return 200, {}, json.dumps({"item": models})
elif self.method == "POST":
@ -617,9 +615,7 @@ class APIGatewayResponse(BaseResponse):
schema = self._get_param("schema")
content_type = self._get_param("contentType")
cli_input_json = self._get_param("cliInputJson")
generate_cli_skeleton = self._get_param(
"generateCliSkeleton"
)
generate_cli_skeleton = self._get_param("generateCliSkeleton")
model = self.backend.create_model(
rest_api_id,
name,
@ -627,12 +623,12 @@ class APIGatewayResponse(BaseResponse):
description,
schema,
cli_input_json,
generate_cli_skeleton
generate_cli_skeleton,
)
return 200, {}, json.dumps(model)
except (InvalidRestApiId, InvalidModelName,RestAPINotFound) as error:
except (InvalidRestApiId, InvalidModelName, RestAPINotFound) as error:
return (
error.code,
{},
@ -649,13 +645,14 @@ class APIGatewayResponse(BaseResponse):
model_info = {}
try:
if self.method == "GET":
model_info = self.backend.get_model(
rest_api_id,
model_name
)
model_info = self.backend.get_model(rest_api_id, model_name)
return 200, {}, json.dumps(model_info)
except (ModelNotFound, RestAPINotFound, InvalidRestApiId,
InvalidModelName) as error:
except (
ModelNotFound,
RestAPINotFound,
InvalidRestApiId,
InvalidModelName,
) as error:
return (
error.code,
{},

View File

@ -1550,82 +1550,67 @@ def test_get_domain_name():
@mock_apigateway
def test_create_model():
client = boto3.client("apigateway", region_name="us-west-2")
response = client.create_rest_api(name="my_api",
description="this is my api"
)
response = client.create_rest_api(name="my_api", description="this is my api")
rest_api_id = response["id"]
dummy_rest_api_id = 'a12b3c4d'
dummy_rest_api_id = "a12b3c4d"
model_name = "testModel"
description = "test model"
content_type = 'application/json'
content_type = "application/json"
# success case with valid params
response = client.create_model(
restApiId=rest_api_id,
name=model_name,
description=description,
contentType=content_type
contentType=content_type,
)
response["name"].should.equal(model_name)
response["description"].should.equal(description)
# with an invalid rest_api_id it should throw NotFoundException
with assert_raises(ClientError) as ex:
client.create_model(
client.create_model(
restApiId=dummy_rest_api_id,
name=model_name,
description=description,
contentType=content_type
contentType=content_type,
)
ex.exception.response["Error"]["Message"].should.equal(
"Invalid Rest API Id specified"
)
ex.exception.response["Error"]["Code"].should.equal(
"NotFoundException"
)
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
with assert_raises(ClientError) as ex:
client.create_model(
client.create_model(
restApiId=rest_api_id,
name="",
description=description,
contentType=content_type
contentType=content_type,
)
ex.exception.response["Error"]["Message"].should.equal(
"No Model Name specified"
)
ex.exception.response["Error"]["Code"].should.equal(
"BadRequestException"
)
ex.exception.response["Error"]["Message"].should.equal("No Model Name specified")
ex.exception.response["Error"]["Code"].should.equal("BadRequestException")
@mock_apigateway
def test_get_api_models():
client = boto3.client("apigateway", region_name="us-west-2")
response = client.create_rest_api(
name="my_api",
description="this is my api"
)
response = client.create_rest_api(name="my_api", description="this is my api")
rest_api_id = response["id"]
model_name = "testModel"
description = "test model"
content_type = 'application/json'
content_type = "application/json"
# when no models are present
result = client.get_models(
restApiId=rest_api_id
)
result = client.get_models(restApiId=rest_api_id)
result["items"].should.equal([])
# add a model
client.create_model(
restApiId=rest_api_id,
name=model_name,
description=description,
contentType=content_type
contentType=content_type,
)
# get models after adding
result = client.get_models(
restApiId=rest_api_id
)
result = client.get_models(restApiId=rest_api_id)
result["items"][0]["name"] = model_name
result["items"][0]["description"] = description
@ -1633,60 +1618,44 @@ def test_get_api_models():
@mock_apigateway
def test_get_model_by_name():
client = boto3.client("apigateway", region_name="us-west-2")
response = client.create_rest_api(
name="my_api",
description="this is my api"
)
response = client.create_rest_api(name="my_api", description="this is my api")
rest_api_id = response["id"]
dummy_rest_api_id = 'a12b3c4d'
dummy_rest_api_id = "a12b3c4d"
model_name = "testModel"
description = "test model"
content_type = 'application/json'
content_type = "application/json"
# add a model
client.create_model(
restApiId=rest_api_id,
name=model_name,
description=description,
contentType=content_type
contentType=content_type,
)
# get models after adding
result = client.get_model(
restApiId=rest_api_id, modelName=model_name
)
result = client.get_model(restApiId=rest_api_id, modelName=model_name)
result["name"] = model_name
result["description"] = description
with assert_raises(ClientError) as ex:
client.get_model(
restApiId=dummy_rest_api_id, modelName=model_name
)
client.get_model(restApiId=dummy_rest_api_id, modelName=model_name)
ex.exception.response["Error"]["Message"].should.equal(
"Invalid Rest API Id specified"
)
ex.exception.response["Error"]["Code"].should.equal(
"NotFoundException"
)
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
@mock_apigateway
def test_get_model_with_invalid_name():
client = boto3.client("apigateway", region_name="us-west-2")
response = client.create_rest_api(
name="my_api",
description="this is my api"
)
response = client.create_rest_api(name="my_api", description="this is my api")
rest_api_id = response["id"]
# test with an invalid model name
with assert_raises(ClientError) as ex:
client.get_model(
restApiId=rest_api_id, modelName="fake"
)
client.get_model(restApiId=rest_api_id, modelName="fake")
ex.exception.response["Error"]["Message"].should.equal(
"Invalid Model Name specified"
)
ex.exception.response["Error"]["Code"].should.equal(
"NotFoundException"
)
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
@mock_apigateway