2020-07-09 17:27:30 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Unit tests for api_gateway_rest.py functions.
"""
import boto3
from botocore . exceptions import ClientError
import pytest
import api_gateway_rest
2023-10-18 10:35:05 -07:00
@pytest.mark.parametrize (
" error_code,stop_on_method " ,
[
( None , None ) ,
( " TestException " , " stub_create_rest_api " ) ,
( " TestException " , " stub_get_resources " ) ,
( " TestException " , " stub_create_resource " ) ,
( " TestException " , " stub_put_method " ) ,
( " TestException " , " stub_put_integration " ) ,
( " TestException " , " stub_create_deployment " ) ,
( " TestException " , " stub_add_permission " ) ,
] ,
)
2020-09-15 18:20:16 -07:00
def test_create_rest_api (
2023-10-18 10:35:05 -07:00
make_stubber , make_unique_name , stub_runner , error_code , stop_on_method
) :
apig_client = boto3 . client ( " apigateway " )
2020-07-09 17:27:30 -07:00
apig_stubber = make_stubber ( apig_client )
2023-10-18 10:35:05 -07:00
lambda_client = boto3 . client ( " lambda " )
2020-07-09 17:27:30 -07:00
lambda_stubber = make_stubber ( lambda_client )
2023-10-18 10:35:05 -07:00
api_name = make_unique_name ( " api- " )
api_base_path = " test-path "
api_stage = " test "
account_id = " 123456789012 "
lambda_func_arn = " aws;arn:lambda:::function/test-func "
rest_api_id = " rest-id "
root_id = " root-id "
resource_id = " resource-id "
lambda_uri = (
f " arn:aws:apigateway: { apig_client . meta . region_name } : "
f " lambda:path/2015-03-31/functions/ { lambda_func_arn } /invocations "
)
apig_source_arn = (
f " arn:aws:execute-api: { apig_client . meta . region_name } : "
f " { account_id } : { rest_api_id } /*/*/ { api_base_path } "
)
2020-07-09 17:27:30 -07:00
2020-09-15 18:20:16 -07:00
with stub_runner ( error_code , stop_on_method ) as runner :
runner . add ( apig_stubber . stub_create_rest_api , api_name , rest_api_id )
runner . add (
2023-10-18 10:35:05 -07:00
apig_stubber . stub_get_resources ,
rest_api_id ,
[
{ " id " : " not-the-root-id " , " path " : " /not-the-root " } ,
{ " id " : root_id , " path " : " / " } ,
] ,
)
runner . add (
apig_stubber . stub_create_resource ,
rest_api_id ,
root_id ,
api_base_path ,
resource_id ,
)
2020-09-15 18:20:16 -07:00
runner . add ( apig_stubber . stub_put_method , rest_api_id , resource_id )
runner . add (
2023-10-18 10:35:05 -07:00
apig_stubber . stub_put_integration , rest_api_id , resource_id , lambda_uri
)
2020-09-15 18:20:16 -07:00
runner . add ( apig_stubber . stub_create_deployment , rest_api_id , api_stage )
runner . add (
2023-10-18 10:35:05 -07:00
lambda_stubber . stub_add_permission ,
lambda_func_arn ,
" lambda:InvokeFunction " ,
" apigateway.amazonaws.com " ,
apig_source_arn ,
)
2020-07-09 17:27:30 -07:00
if error_code is None :
got_api_id = api_gateway_rest . create_rest_api (
2023-10-18 10:35:05 -07:00
apig_client ,
api_name ,
api_base_path ,
api_stage ,
account_id ,
lambda_client ,
lambda_func_arn ,
)
2020-07-09 17:27:30 -07:00
assert got_api_id == rest_api_id
else :
with pytest . raises ( ClientError ) as exc_info :
api_gateway_rest . create_rest_api (
2023-10-18 10:35:05 -07:00
apig_client ,
api_name ,
api_base_path ,
api_stage ,
account_id ,
lambda_client ,
lambda_func_arn ,
)
assert exc_info . value . response [ " Error " ] [ " Code " ] == error_code
2020-07-09 17:27:30 -07:00
2023-10-18 10:35:05 -07:00
@pytest.mark.parametrize ( " error_code " , [ None , " TestException " ] )
2020-07-09 17:27:30 -07:00
def test_delete_rest_api ( make_stubber , error_code ) :
2023-10-18 10:35:05 -07:00
apig_client = boto3 . client ( " apigateway " )
2020-07-09 17:27:30 -07:00
apig_stubber = make_stubber ( apig_client )
2023-10-18 10:35:05 -07:00
api_id = " test-id "
2020-07-09 17:27:30 -07:00
apig_stubber . stub_delete_rest_api ( api_id , error_code = error_code )
if error_code is None :
api_gateway_rest . delete_rest_api ( apig_client , api_id )
else :
with pytest . raises ( ClientError ) as exc_info :
api_gateway_rest . delete_rest_api ( apig_client , api_id )
2023-10-18 10:35:05 -07:00
assert exc_info . value . response [ " Error " ] [ " Code " ] == error_code