2020-07-09 17:27:30 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Stub functions that are used by the Amazon API Gateway unit tests.
When tests are run against an actual AWS account, the stubber class does not
set up stubs and passes all calls through to the Boto3 client.
"""
2020-12-28 17:18:39 -08:00
import json
2020-07-09 17:27:30 -07:00
from test_tools . example_stubber import ExampleStubber
class ApiGatewayStubber ( ExampleStubber ) :
"""
A class that implements a variety of stub functions that are used by the
Amazon API Gateway unit tests.
The stubbed functions all expect certain parameters to be passed to them as
part of the tests, and will raise errors when the actual parameters differ from
the expected.
"""
2023-10-18 10:35:05 -07:00
2020-07-09 17:27:30 -07:00
def __init__ ( self , client , use_stubs = True ) :
"""
Initializes the object with a specific client and configures it for
stubbing or AWS passthrough.
:param client: A Boto3 API Gateway client.
:param use_stubs: When True, use stubs to intercept requests. Otherwise,
pass requests through to AWS.
"""
super ( ) . __init__ ( client , use_stubs )
def stub_create_rest_api ( self , api_name , api_id , error_code = None ) :
2023-10-18 10:35:05 -07:00
expected_params = { " name " : api_name }
response = { " id " : api_id }
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" create_rest_api " , expected_params , response , error_code = error_code
)
2020-07-09 17:27:30 -07:00
def stub_get_resources ( self , api_id , resources , error_code = None ) :
2023-10-18 10:35:05 -07:00
expected_params = { " restApiId " : api_id }
response = { " items " : resources }
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" get_resources " , expected_params , response , error_code = error_code
)
2020-07-09 17:27:30 -07:00
def stub_create_resource (
2023-10-18 10:35:05 -07:00
self , api_id , parent_id , path , resource_id , error_code = None
) :
expected_params = { " restApiId " : api_id , " parentId " : parent_id , " pathPart " : path }
response = { " id " : resource_id }
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" create_resource " , expected_params , response , error_code = error_code
)
2020-07-09 17:27:30 -07:00
2023-10-18 10:35:05 -07:00
def stub_put_method ( self , api_id , resource_id , error_code = None , http_method = " ANY " ) :
2020-07-09 17:27:30 -07:00
expected_params = {
2023-10-18 10:35:05 -07:00
" restApiId " : api_id ,
" resourceId " : resource_id ,
" httpMethod " : http_method ,
" authorizationType " : " NONE " ,
}
self . _stub_bifurcator ( " put_method " , expected_params , error_code = error_code )
2020-07-09 17:27:30 -07:00
def stub_put_method_response (
2023-10-18 10:35:05 -07:00
self , api_id , resource_id , response_models , error_code = None , http_method = " ANY "
) :
2020-07-09 17:27:30 -07:00
expected_params = {
2023-10-18 10:35:05 -07:00
" restApiId " : api_id ,
" resourceId " : resource_id ,
" httpMethod " : http_method ,
" statusCode " : " 200 " ,
" responseModels " : response_models ,
}
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" put_method_response " , expected_params , error_code = error_code
)
2020-07-09 17:27:30 -07:00
2020-12-28 17:18:39 -08:00
def stub_put_integration (
2023-10-18 10:35:05 -07:00
self ,
api_id ,
resource_id ,
uri ,
error_code = None ,
http_method = " ANY " ,
integ_type = " AWS_PROXY " ,
integ_method = " POST " ,
integ_role_arn = None ,
integ_templates = None ,
passthrough = None ,
) :
2020-07-09 17:27:30 -07:00
expected_params = {
2023-10-18 10:35:05 -07:00
" restApiId " : api_id ,
" resourceId " : resource_id ,
" httpMethod " : http_method ,
" type " : integ_type ,
" integrationHttpMethod " : integ_method ,
" uri " : uri ,
}
2020-12-28 17:18:39 -08:00
if integ_role_arn is not None :
2023-10-18 10:35:05 -07:00
expected_params [ " credentials " ] = integ_role_arn
2020-12-28 17:18:39 -08:00
if integ_templates is not None :
2023-10-18 10:35:05 -07:00
expected_params [ " requestTemplates " ] = {
" application/json " : json . dumps ( integ_templates )
}
2020-12-28 17:18:39 -08:00
if passthrough is not None :
2023-10-18 10:35:05 -07:00
expected_params [ " passthroughBehavior " ] = passthrough
self . _stub_bifurcator ( " put_integration " , expected_params , error_code = error_code )
2020-07-09 17:27:30 -07:00
def stub_put_integration_response (
2023-10-18 10:35:05 -07:00
self ,
api_id ,
resource_id ,
response_templates ,
error_code = None ,
http_method = " ANY " ,
) :
2020-07-09 17:27:30 -07:00
expected_params = {
2023-10-18 10:35:05 -07:00
" restApiId " : api_id ,
" resourceId " : resource_id ,
" httpMethod " : http_method ,
" statusCode " : " 200 " ,
" responseTemplates " : response_templates ,
}
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" put_integration_response " , expected_params , error_code = error_code
)
2020-07-09 17:27:30 -07:00
def stub_create_deployment ( self , api_id , api_stage , error_code = None ) :
2023-10-18 10:35:05 -07:00
expected_params = { " restApiId " : api_id , " stageName " : api_stage }
2020-07-09 17:27:30 -07:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" create_deployment " , expected_params , error_code = error_code
)
2020-07-09 17:27:30 -07:00
def stub_delete_rest_api ( self , api_id , error_code = None ) :
2023-10-18 10:35:05 -07:00
expected_params = { " restApiId " : api_id }
self . _stub_bifurcator ( " delete_rest_api " , expected_params , error_code = error_code )
2020-12-28 17:18:39 -08:00
def stub_get_rest_apis ( self , rest_apis , error_code = None ) :
expected_params = { }
2023-10-18 10:35:05 -07:00
response = { " items " : rest_apis }
2020-12-28 17:18:39 -08:00
self . _stub_bifurcator (
2023-10-18 10:35:05 -07:00
" get_rest_apis " , expected_params , response , error_code = error_code
)