comparison tests/integration/test_api_gateway.py @ 0:cea9500dca25

first commit
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Tue, 14 Sep 2021 17:46:04 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cea9500dca25
1 import os
2 from unittest import TestCase
3
4 import boto3
5 import requests
6
7 """
8 Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test.
9 """
10
11
12 class TestApiGateway(TestCase):
13 api_endpoint: str
14
15 @classmethod
16 def get_stack_name(cls) -> str:
17 stack_name = os.environ.get("AWS_SAM_STACK_NAME")
18 if not stack_name:
19 raise Exception(
20 "Cannot find env var AWS_SAM_STACK_NAME. \n"
21 "Please setup this environment variable with the stack name where we are running integration tests."
22 )
23
24 return stack_name
25
26 def setUp(self) -> None:
27 """
28 Based on the provided env variable AWS_SAM_STACK_NAME,
29 here we use cloudformation API to find out what the HelloWorldApi URL is
30 """
31 stack_name = TestApiGateway.get_stack_name()
32
33 client = boto3.client("cloudformation")
34
35 try:
36 response = client.describe_stacks(StackName=stack_name)
37 except Exception as e:
38 raise Exception(
39 f"Cannot find stack {stack_name}. \n" f'Please make sure stack with the name "{stack_name}" exists.'
40 ) from e
41
42 stacks = response["Stacks"]
43
44 stack_outputs = stacks[0]["Outputs"]
45 api_outputs = [output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"]
46 self.assertTrue(api_outputs, f"Cannot find output HelloWorldApi in stack {stack_name}")
47
48 self.api_endpoint = api_outputs[0]["OutputValue"]
49
50 def test_api_gateway(self):
51 """
52 Call the API Gateway endpoint and check the response
53 """
54 response = requests.get(self.api_endpoint)
55 self.assertDictEqual(response.json(), {"message": "hello world"})