|
| 1 | +import json |
| 2 | +from unittest.case import skipIf |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from integration.config.service_names import APP_SYNC |
| 7 | +from integration.helpers.base_test import BaseTest |
| 8 | +from integration.helpers.resource import current_region_does_not_support |
| 9 | + |
| 10 | + |
| 11 | +def execute_and_verify_appsync_query(url, api_key, query): |
| 12 | + """ |
| 13 | + Executes a query to an AppSync GraphQLApi. |
| 14 | +
|
| 15 | + Also checks that the response is 200 and does not contain errors before returning. |
| 16 | + """ |
| 17 | + headers = { |
| 18 | + "Content-Type": "application/json", |
| 19 | + "x-api-key": api_key, |
| 20 | + } |
| 21 | + payload = {"query": query} |
| 22 | + |
| 23 | + response = requests.post(url, json=payload, headers=headers) |
| 24 | + response.raise_for_status() |
| 25 | + data = response.json() |
| 26 | + if "errors" in data: |
| 27 | + raise Exception(json.dumps(data["errors"])) |
| 28 | + |
| 29 | + return data |
| 30 | + |
| 31 | + |
| 32 | +@skipIf(current_region_does_not_support([APP_SYNC]), "AppSync is not supported in this testing region") |
| 33 | +class TestGraphQLApiPipelineResolver(BaseTest): |
| 34 | + def test_api(self): |
| 35 | + file_name = "combination/graphqlapi_pipeline_resolver" |
| 36 | + self.create_and_verify_stack(file_name) |
| 37 | + |
| 38 | + outputs = self.get_stack_outputs() |
| 39 | + |
| 40 | + author = "AUTHORNAME" |
| 41 | + title = "Our first post!" |
| 42 | + content = "This is our first post." |
| 43 | + |
| 44 | + query = f""" |
| 45 | + mutation addPost {{ |
| 46 | + addPost( |
| 47 | + author: "{author}" |
| 48 | + title: "{title}" |
| 49 | + content: "{content}" |
| 50 | + ) {{ |
| 51 | + id |
| 52 | + author |
| 53 | + title |
| 54 | + content |
| 55 | + ups |
| 56 | + downs |
| 57 | + version |
| 58 | + }} |
| 59 | + }} |
| 60 | + """ |
| 61 | + |
| 62 | + url = outputs["SuperCoolAPI"] |
| 63 | + api_key = outputs["MyApiKey"] |
| 64 | + |
| 65 | + response = execute_and_verify_appsync_query(url, api_key, query) |
| 66 | + |
| 67 | + add_post = response["data"]["addPost"] |
| 68 | + |
| 69 | + self.assertEqual(add_post["author"], author) |
| 70 | + self.assertEqual(add_post["title"], title) |
| 71 | + self.assertEqual(add_post["content"], content) |
| 72 | + self.assertEqual(add_post["ups"], 1) |
| 73 | + self.assertEqual(add_post["downs"], 0) |
| 74 | + self.assertEqual(add_post["version"], 1) |
| 75 | + |
| 76 | + post_id = add_post["id"] |
| 77 | + query = f""" |
| 78 | + query getPost {{ |
| 79 | + getPost(id:"{post_id}") {{ |
| 80 | + id |
| 81 | + author |
| 82 | + title |
| 83 | + content |
| 84 | + ups |
| 85 | + downs |
| 86 | + version |
| 87 | + }} |
| 88 | + }} |
| 89 | + """ |
| 90 | + |
| 91 | + response = execute_and_verify_appsync_query(url, api_key, query) |
| 92 | + |
| 93 | + get_post = response["data"]["getPost"] |
| 94 | + |
| 95 | + self.assertEqual(get_post["author"], author) |
| 96 | + self.assertEqual(get_post["title"], title) |
| 97 | + self.assertEqual(get_post["content"], content) |
| 98 | + self.assertEqual(get_post["ups"], 1) |
| 99 | + self.assertEqual(get_post["downs"], 0) |
| 100 | + self.assertEqual(get_post["version"], 1) |
| 101 | + self.assertEqual(get_post["id"], post_id) |
0 commit comments