# # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 # # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # import os from unittest import TestCase import boto3 import requests """ Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test. """ class TestApiGateway(TestCase): api_endpoint: str @classmethod def get_stack_name(cls) -> str: stack_name = os.environ.get("AWS_SAM_STACK_NAME") if not stack_name: raise Exception( "Cannot find env var AWS_SAM_STACK_NAME. \n" "Please setup this environment variable with the stack name where we are running integration tests." ) return stack_name def setUp(self) -> None: """ Based on the provided env variable AWS_SAM_STACK_NAME, here we use cloudformation API to find out what the HelloWorldApi URL is """ stack_name = TestApiGateway.get_stack_name() client = boto3.client("cloudformation") try: response = client.describe_stacks(StackName=stack_name) except Exception as e: raise Exception( f"Cannot find stack {stack_name}. \n" f'Please make sure stack with the name "{stack_name}" exists.' ) from e stacks = response["Stacks"] stack_outputs = stacks[0]["Outputs"] api_outputs = [ output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"] self.assertTrue( api_outputs, f"Cannot find output HelloWorldApi in stack {stack_name}") self.api_endpoint = api_outputs[0]["OutputValue"] def test_api_gateway(self): """ Call the API Gateway endpoint and check the response """ response = requests.get(self.api_endpoint) self.assertDictEqual(response.json(), {"message": "hello world"})