Skip to content

Commit a5caadd

Browse files
committed
added: integration tests for appsync configuration
1 parent 43d8fa6 commit a5caadd

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[
2+
{
3+
"LogicalResourceId": "SuperCoolAPI",
4+
"ResourceType": "AWS::AppSync::GraphQLApi"
5+
},
6+
{
7+
"LogicalResourceId": "SuperCoolAPICloudWatchRole",
8+
"ResourceType": "AWS::IAM::Role"
9+
},
10+
{
11+
"LogicalResourceId": "SuperCoolAPISchema",
12+
"ResourceType": "AWS::AppSync::GraphQLSchema"
13+
},
14+
{
15+
"LogicalResourceId": "SuperCoolAPIQuerygetBook",
16+
"ResourceType": "AWS::AppSync::Resolver"
17+
},
18+
{
19+
"LogicalResourceId": "SuperCoolAPINoneDataSource",
20+
"ResourceType": "AWS::AppSync::DataSource"
21+
},
22+
{
23+
"LogicalResourceId": "SuperCoolAPIprocessQuery",
24+
"ResourceType": "AWS::AppSync::FunctionConfiguration"
25+
},
26+
{
27+
"LogicalResourceId": "SuperCoolAPIMyApiKey",
28+
"ResourceType": "AWS::AppSync::ApiKey"
29+
},
30+
{
31+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPI",
32+
"ResourceType": "AWS::AppSync::GraphQLApi"
33+
},
34+
{
35+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPICloudWatchRole",
36+
"ResourceType": "AWS::IAM::Role"
37+
},
38+
{
39+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPISchema",
40+
"ResourceType": "AWS::AppSync::GraphQLSchema"
41+
},
42+
{
43+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPIQuerygetBook",
44+
"ResourceType": "AWS::AppSync::Resolver"
45+
},
46+
{
47+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPINoneDataSource",
48+
"ResourceType": "AWS::AppSync::DataSource"
49+
},
50+
{
51+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPIprocessQuery",
52+
"ResourceType": "AWS::AppSync::FunctionConfiguration"
53+
},
54+
{
55+
"LogicalResourceId": "IntrospectionDisableSuperCoolAPIMyApiKey",
56+
"ResourceType": "AWS::AppSync::ApiKey"
57+
}
58+
]
59+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Transform: AWS::Serverless-2016-10-31
2+
Resources:
3+
SuperCoolAPI:
4+
Type: AWS::Serverless::GraphQLApi
5+
Properties:
6+
SchemaInline: |
7+
type Book {
8+
bookName: String
9+
id: ID
10+
}
11+
type Query { getBook(bookName: String): Book }
12+
OwnerContact: blah-blah
13+
Auth:
14+
Type: API_KEY
15+
ApiKeys:
16+
MyApiKey: {}
17+
Functions:
18+
processQuery:
19+
Runtime:
20+
Name: APPSYNC_JS
21+
Version: 1.0.0
22+
DataSource: NONE
23+
InlineCode: |
24+
import { util } from '@aws-appsync/utils';
25+
26+
export function request(ctx) {
27+
const id = util.autoId();
28+
return { payload: { ...ctx.args, id } };
29+
}
30+
31+
export function response(ctx) {
32+
return ctx.result;
33+
}
34+
Resolvers:
35+
Query:
36+
getBook:
37+
Pipeline:
38+
- processQuery
39+
40+
IntrospectionDisableSuperCoolAPI:
41+
Type: AWS::Serverless::GraphQLApi
42+
Properties:
43+
SchemaInline: |
44+
type Book {
45+
bookName: String
46+
id: ID
47+
}
48+
type Query { getBook(bookName: String): Book }
49+
OwnerContact: blah-blah
50+
IntrospectionConfig: DISABLED
51+
QueryDepthLimit: 10
52+
ResolverCountLimit: 100
53+
Auth:
54+
Type: API_KEY
55+
ApiKeys:
56+
MyApiKey: {}
57+
Functions:
58+
processQuery:
59+
Runtime:
60+
Name: APPSYNC_JS
61+
Version: 1.0.0
62+
DataSource: NONE
63+
InlineCode: |
64+
import { util } from '@aws-appsync/utils';
65+
66+
export function request(ctx) {
67+
const id = util.autoId();
68+
return { payload: { ...ctx.args, id } };
69+
}
70+
71+
export function response(ctx) {
72+
return ctx.result;
73+
}
74+
Resolvers:
75+
Query:
76+
getBook:
77+
Pipeline:
78+
- processQuery
79+
Outputs:
80+
SuperCoolAPI:
81+
Description: AppSync API
82+
Value: !GetAtt SuperCoolAPI.GraphQLUrl
83+
MyApiKey:
84+
Description: API Id
85+
Value: !GetAtt SuperCoolAPIMyApiKey.ApiKey
86+
IntrospectionDisableSuperCoolAPI:
87+
Description: AppSync API
88+
Value: !GetAtt IntrospectionDisableSuperCoolAPI.GraphQLUrl
89+
IntrospectionDisableSuperCoolAPIMyApiKey:
90+
Description: API Id
91+
Value: !GetAtt IntrospectionDisableSuperCoolAPIMyApiKey.ApiKey
92+
93+
Metadata:
94+
SamTransformTest: true
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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(introspection_disable_api_url, introspection_disable_api_key, query)
64+
self.assertEqual(introspection_disable_query_response["data"]["getBook"]["bookName"], book_name)
65+
66+
query_introsepction = f"""
67+
query myQuery {{
68+
__schema {{
69+
types {{
70+
name
71+
}}
72+
}}
73+
}}
74+
"""
75+
76+
introspection_query_response = execute_and_verify_appsync_query(url, api_key, query_introsepction)
77+
self.assertIsNotNone(introspection_query_response["data"]["__schema"])
78+
79+
# sending introspection query and expecting error as introspection is DISABLED for this API using template file
80+
with self.assertRaises(Exception):
81+
execute_and_verify_appsync_query(introspection_disable_api_url, introspection_disable_api_key, query_introsepction)

0 commit comments

Comments
 (0)