|
| 1 | +import json |
| 2 | +from unittest.case import skipIf |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | + |
| 7 | +from integration.config.service_names import APP_SYNC |
| 8 | +from integration.helpers.base_test import BaseTest |
| 9 | +from integration.helpers.resource import current_region_does_not_support |
| 10 | + |
| 11 | + |
| 12 | +def execute_and_verify_appsync_query(url, api_key, query): |
| 13 | + """ |
| 14 | + Executes a query to an AppSync GraphQLApi. |
| 15 | +
|
| 16 | + Also checks that the response is 200 and does not contain errors before returning. |
| 17 | + """ |
| 18 | + headers = { |
| 19 | + "Content-Type": "application/json", |
| 20 | + "x-api-key": api_key, |
| 21 | + } |
| 22 | + payload = {"query": query} |
| 23 | + |
| 24 | + response = requests.post(url, json=payload, headers=headers) |
| 25 | + response.raise_for_status() |
| 26 | + data = response.json() |
| 27 | + if "errors" in data: |
| 28 | + raise Exception(json.dumps(data["errors"])) |
| 29 | + |
| 30 | + return data |
| 31 | + |
| 32 | + |
| 33 | +@skipIf(current_region_does_not_support([APP_SYNC]), "AppSync is not supported in this testing region") |
| 34 | +@pytest.mark.filterwarnings("ignore::DeprecationWarning") |
| 35 | +class TestGraphQLApiConfiguration(BaseTest): |
| 36 | + def test_api(self): |
| 37 | + file_name = "single/graphqlapi-configuration" |
| 38 | + self.create_and_verify_stack(file_name) |
| 39 | + |
| 40 | + outputs = self.get_stack_outputs() |
| 41 | + |
| 42 | + url = outputs["SuperCoolAPI"] |
| 43 | + api_key = outputs["MyApiKey"] |
| 44 | + |
| 45 | + introspection_disable_api_url = outputs["IntrospectionDisableSuperCoolAPI"] |
| 46 | + introspection_disable_api_key = outputs["IntrospectionDisableSuperCoolAPIMyApiKey"] |
| 47 | + |
| 48 | + book_name = "GoodBook" |
| 49 | + query = f""" |
| 50 | + query MyQuery {{ |
| 51 | + getBook( |
| 52 | + bookName: "{book_name}" |
| 53 | + ) {{ |
| 54 | + id |
| 55 | + bookName |
| 56 | + }} |
| 57 | + }} |
| 58 | + """ |
| 59 | + |
| 60 | + response = execute_and_verify_appsync_query(url, api_key, query) |
| 61 | + self.assertEqual(response["data"]["getBook"]["bookName"], book_name) |
| 62 | + |
| 63 | + introspection_disable_query_response = execute_and_verify_appsync_query( |
| 64 | + introspection_disable_api_url, introspection_disable_api_key, query |
| 65 | + ) |
| 66 | + self.assertEqual(introspection_disable_query_response["data"]["getBook"]["bookName"], book_name) |
| 67 | + |
| 68 | + query_introsepction = """ |
| 69 | + query myQuery { |
| 70 | + __schema { |
| 71 | + types { |
| 72 | + name |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + """ |
| 77 | + |
| 78 | + introspection_query_response = execute_and_verify_appsync_query(url, api_key, query_introsepction) |
| 79 | + self.assertIsNotNone(introspection_query_response["data"]["__schema"]) |
| 80 | + |
| 81 | + # sending introspection query and expecting error as introspection is DISABLED for this API using template file |
| 82 | + with self.assertRaises(Exception): |
| 83 | + execute_and_verify_appsync_query( |
| 84 | + introspection_disable_api_url, introspection_disable_api_key, query_introsepction |
| 85 | + ) |
0 commit comments