Repository navigation
Expand file tree
/
Copy pathtest_appsync.py
More file actions
2494 lines (2110 loc) · 105 KB
/
Copy pathtest_appsync.py
File metadata and controls
2494 lines (2110 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import io
import json
import os
import urllib.request
import uuid as _uuid_mod
import zipfile
from urllib.parse import quote, urlparse
import boto3
import pytest
from botocore.exceptions import ClientError
_ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
_ENDPOINT_NETLOC = urlparse(_ENDPOINT).netloc
def _client(region):
return boto3.client(
"appsync",
endpoint_url=_ENDPOINT,
region_name=region,
aws_access_key_id="test",
aws_secret_access_key="test",
)
def test_appsync_create_and_list_api():
"""Create a GraphQL API and list it."""
from conftest import make_client
appsync = make_client("appsync")
resp = appsync.create_graphql_api(name="test-api", authenticationType="API_KEY")
api = resp["graphqlApi"]
assert api["name"] == "test-api"
assert api["apiId"]
assert api["authenticationType"] == "API_KEY"
apis = appsync.list_graphql_apis()["graphqlApis"]
assert any(a["apiId"] == api["apiId"] for a in apis)
def test_appsync_graphql_apis_are_region_scoped():
east = _client("us-east-1")
west = _client("us-west-2")
east_api = east.create_graphql_api(
name="regional-api-east", authenticationType="API_KEY"
)["graphqlApi"]
west_api = west.create_graphql_api(
name="regional-api-west", authenticationType="API_KEY"
)["graphqlApi"]
try:
east_ids = {api["apiId"] for api in east.list_graphql_apis()["graphqlApis"]}
west_ids = {api["apiId"] for api in west.list_graphql_apis()["graphqlApis"]}
assert east_api["apiId"] in east_ids
assert east_api["apiId"] not in west_ids
assert west_api["apiId"] in west_ids
assert west_api["apiId"] not in east_ids
assert ":us-east-1:" in east_api["arn"]
assert ":us-west-2:" in west_api["arn"]
# Local data-plane URLs do not encode or sign a region. The API ID
# must select its stored region before resolver execution.
west_key = west.create_api_key(apiId=west_api["apiId"])["apiKey"]["id"]
west_graphql = _appsync_graphql_post(
f"{_ENDPOINT}/v1/apis/{west_api['apiId']}/graphql",
"{ __typename }",
headers={"x-api-key": west_key},
)
assert "errors" not in west_graphql
finally:
east.delete_graphql_api(apiId=east_api["apiId"])
west.delete_graphql_api(apiId=west_api["apiId"])
@pytest.mark.parametrize("credential_location", ["header", "query"])
def test_appsync_signed_graphql_request_preserves_region(credential_location):
east = _client("us-east-1")
api = east.create_graphql_api(
name=f"signed-region-{credential_location}", authenticationType="API_KEY"
)["graphqlApi"]
url = f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql"
headers = {}
credential = "test/20260722/us-west-2/appsync/aws4_request"
if credential_location == "header":
headers["Authorization"] = (
f"AWS4-HMAC-SHA256 Credential={credential}, "
"SignedHeaders=host;x-amz-date, Signature=fake"
)
else:
url = f"{url}?X-Amz-Credential={quote(credential, safe='')}"
try:
with pytest.raises(urllib.error.HTTPError) as exc:
_appsync_graphql_post(url, "{ __typename }", headers=headers)
assert exc.value.code == 404
finally:
east.delete_graphql_api(apiId=api["apiId"])
@pytest.mark.parametrize("api_selector", ["api_key", "single_api_fallback"])
def test_appsync_signed_root_graphql_request_preserves_region(api_selector):
east = _client("us-east-1")
api = east.create_graphql_api(
name=f"signed-root-region-{api_selector}",
authenticationType="API_KEY" if api_selector == "api_key" else "AWS_IAM",
)["graphqlApi"]
url = f"{_ENDPOINT}/graphql"
headers = {
"Host": f"{api['apiId']}.appsync-api.us-east-1.{_ENDPOINT_NETLOC}",
"Authorization": (
"AWS4-HMAC-SHA256 "
"Credential=test/20260722/us-west-2/appsync/aws4_request, "
"SignedHeaders=host;x-amz-date, Signature=fake"
),
}
if api_selector == "api_key":
headers["x-api-key"] = east.create_api_key(apiId=api["apiId"])["apiKey"]["id"]
try:
with pytest.raises(urllib.error.HTTPError) as exc:
_appsync_graphql_post(url, "{ __typename }", headers=headers)
assert exc.value.code == 401
finally:
east.delete_graphql_api(apiId=api["apiId"])
def test_appsync_signed_root_graphql_request_uses_current_region_fallback():
west = _client("us-west-2")
api = west.create_graphql_api(
name="signed-root-current-region-fallback",
authenticationType="AWS_IAM",
)["graphqlApi"]
headers = {
"Host": f"{api['apiId']}.appsync-api.us-west-2.{_ENDPOINT_NETLOC}",
"Authorization": (
"AWS4-HMAC-SHA256 "
"Credential=test/20260722/us-west-2/appsync/aws4_request, "
"SignedHeaders=host;x-amz-date, Signature=fake"
),
}
try:
response = _appsync_graphql_post(
f"{_ENDPOINT}/graphql",
"{ __typename }",
headers=headers,
)
assert "errors" not in response
finally:
west.delete_graphql_api(apiId=api["apiId"])
def test_appsync_graphql_honors_dynamodb_data_source_region():
east_ddb = boto3.client(
"dynamodb",
endpoint_url=_ENDPOINT,
region_name="us-east-1",
aws_access_key_id="test",
aws_secret_access_key="test",
)
west_appsync = _client("us-west-2")
table_name = f"appsync-cross-region-{_uuid_mod.uuid4().hex[:12]}"
east_ddb.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
east_ddb.put_item(
TableName=table_name,
Item={"id": {"S": "item-1"}, "name": {"S": "East item"}},
)
api = west_appsync.create_graphql_api(
name="cross-region-ddb", authenticationType="API_KEY"
)["graphqlApi"]
west_appsync.create_data_source(
apiId=api["apiId"],
name="east-table",
type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": table_name, "awsRegion": "us-east-1"},
)
west_appsync.create_resolver(
apiId=api["apiId"],
typeName="Query",
fieldName="getItem",
dataSourceName="east-table",
)
api_key = west_appsync.create_api_key(apiId=api["apiId"])["apiKey"]["id"]
try:
response = _appsync_graphql_post(
f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
'query { getItem(id: "item-1") { id name } }',
headers={"x-api-key": api_key},
)
assert response["data"]["getItem"] == {
"id": "item-1",
"name": "East item",
}
finally:
west_appsync.delete_graphql_api(apiId=api["apiId"])
east_ddb.delete_table(TableName=table_name)
def test_appsync_legacy_children_restore_beside_parent_api():
from ministack.core.responses import (
AccountScopedDict,
get_account_id,
get_region,
set_request_account_id,
set_request_region,
)
from ministack.services import appsync as service
original_account = get_account_id()
original_region = get_region()
account_id = "111111111111"
boot_region = "us-east-1"
api_region = "us-west-2"
api_id = "legacy-api"
apis = AccountScopedDict()
tags = AccountScopedDict()
set_request_account_id(account_id)
set_request_region(boot_region)
api_arn = f"arn:aws:appsync:{api_region}:{account_id}:apis/{api_id}"
apis[api_id] = {"apiId": api_id, "arn": api_arn}
tags[api_arn] = {"legacy": "true"}
payload = {"apis": apis, "tags": tags}
for key in ("api_keys", "data_sources", "resolvers", "types"):
children = AccountScopedDict()
children[api_id] = {"legacy": key}
payload[key] = children
service.reset()
try:
service.load_persisted_state(payload)
assert service._apis.get_scoped(account_id, api_region, api_id)["arn"] == api_arn
for store in (
service._api_keys,
service._data_sources,
service._resolvers,
service._types,
):
assert store.get_scoped(account_id, api_region, api_id) is not None
assert store.get_scoped(account_id, boot_region, api_id) is None
assert service._tags.get(api_arn) == {"legacy": "true"}
finally:
service.reset()
set_request_account_id(original_account)
set_request_region(original_region)
def test_appsync_reset_clears_all_regions():
from ministack.core.responses import get_region, set_request_region
from ministack.services import appsync as service
original_region = get_region()
regional_stores = (
service._apis,
service._api_keys,
service._data_sources,
service._resolvers,
service._types,
)
service.reset()
try:
for region in ("us-east-1", "us-west-2"):
set_request_region(region)
for store in regional_stores:
store[f"resource-{region}"] = {"region": region}
service._tags["arn:aws:appsync:us-east-1:000000000000:apis/tagged"] = {
"tag": "value"
}
service.reset()
assert all(not store.has_any() for store in regional_stores)
assert not service._tags._data
finally:
service.reset()
set_request_region(original_region)
def test_appsync_get_and_delete_api():
from conftest import make_client
appsync = make_client("appsync")
resp = appsync.create_graphql_api(name="del-api", authenticationType="API_KEY")
api_id = resp["graphqlApi"]["apiId"]
got = appsync.get_graphql_api(apiId=api_id)
assert got["graphqlApi"]["name"] == "del-api"
appsync.delete_graphql_api(apiId=api_id)
from botocore.exceptions import ClientError
with pytest.raises(ClientError):
appsync.get_graphql_api(apiId=api_id)
def test_appsync_api_key_crud():
from conftest import make_client
appsync = make_client("appsync")
api = appsync.create_graphql_api(name="key-api", authenticationType="API_KEY")["graphqlApi"]
key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
assert key["id"]
keys = appsync.list_api_keys(apiId=api["apiId"])["apiKeys"]
assert len(keys) >= 1
appsync.delete_api_key(apiId=api["apiId"], id=key["id"])
def test_appsync_tags_reject_wrong_region_api_arn(appsync):
import boto3
api = appsync.create_graphql_api(name="tag-region-api", authenticationType="API_KEY")["graphqlApi"]
arn_parts = api["arn"].split(":")
wrong_region = "us-west-2" if arn_parts[3] != "us-west-2" else "us-east-2"
arn_parts[3] = wrong_region
wrong_region_arn = ":".join(arn_parts)
regional_appsync = boto3.client(
"appsync",
endpoint_url=_ENDPOINT,
region_name=wrong_region,
aws_access_key_id="test",
aws_secret_access_key="test",
)
with pytest.raises(ClientError) as exc:
regional_appsync.tag_resource(resourceArn=wrong_region_arn, tags={"env": "test"})
assert exc.value.response["Error"]["Code"] == "NotFoundException"
def test_appsync_data_source_crud():
from conftest import make_client
appsync = make_client("appsync")
api = appsync.create_graphql_api(name="ds-api", authenticationType="API_KEY")["graphqlApi"]
ds = appsync.create_data_source(
apiId=api["apiId"], name="myds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "test-table", "awsRegion": "us-east-1"},
)["dataSource"]
assert ds["name"] == "myds"
got = appsync.get_data_source(apiId=api["apiId"], name="myds")
assert got["dataSource"]["name"] == "myds"
appsync.delete_data_source(apiId=api["apiId"], name="myds")
def test_appsync_graphql_create_and_query(ddb):
"""Full AppSync flow: create API + data source + resolver, then execute GraphQL."""
from conftest import make_client
appsync = make_client("appsync")
# Create DynamoDB table
ddb.create_table(
TableName="gql-users",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
# Create API
api = appsync.create_graphql_api(name="gql-test", authenticationType="API_KEY")["graphqlApi"]
api_id = api["apiId"]
# Create API key
key = appsync.create_api_key(apiId=api_id)["apiKey"]
# Create data source
appsync.create_data_source(
apiId=api_id, name="usersDS", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-users", "awsRegion": "us-east-1"},
)
# Create resolvers
appsync.create_resolver(
apiId=api_id, typeName="Mutation", fieldName="createUser",
dataSourceName="usersDS",
)
appsync.create_resolver(
apiId=api_id, typeName="Query", fieldName="getUser",
dataSourceName="usersDS",
)
appsync.create_resolver(
apiId=api_id, typeName="Query", fieldName="listUsers",
dataSourceName="usersDS",
)
# Execute mutation via HTTP
import json as _json
import urllib.request
mutation = _json.dumps({
"query": 'mutation CreateUser { createUser(input: {id: "u1", name: "Alice", email: "[email protected]"}) { id name email } }',
}).encode()
req = urllib.request.Request(
f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
data=mutation,
headers={"Content-Type": "application/json", "x-api-key": key["id"]},
)
with urllib.request.urlopen(req) as r:
resp = _json.loads(r.read())
assert "data" in resp
assert resp["data"]["createUser"]["name"] == "Alice"
# Query
query = _json.dumps({
"query": 'query GetUser { getUser(id: "u1") { id name email } }',
}).encode()
req = urllib.request.Request(
f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
data=query,
headers={"Content-Type": "application/json", "x-api-key": key["id"]},
)
with urllib.request.urlopen(req) as r:
resp = _json.loads(r.read())
assert resp["data"]["getUser"]["name"] == "Alice"
assert resp["data"]["getUser"]["id"] == "u1"
# List
list_q = _json.dumps({
"query": "query ListUsers { listUsers { items { id name } } }",
}).encode()
req = urllib.request.Request(
f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
data=list_q,
headers={"Content-Type": "application/json", "x-api-key": key["id"]},
)
with urllib.request.urlopen(req) as r:
resp = _json.loads(r.read())
items = resp["data"]["listUsers"]["items"]
assert len(items) >= 1
assert any(u["name"] == "Alice" for u in items)
def test_appsync_graphql_update_mutation(ddb):
"""Update an existing item via GraphQL mutation."""
import json as _json
import urllib.request
from conftest import make_client
appsync = make_client("appsync")
try:
ddb.create_table(TableName="gql-update", KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], BillingMode="PAY_PER_REQUEST")
except Exception:
pass
api = appsync.create_graphql_api(name="gql-upd", authenticationType="API_KEY")["graphqlApi"]
key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-update", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="createItem", dataSourceName="ds")
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="updateItem", dataSourceName="ds")
appsync.create_resolver(apiId=api["apiId"], typeName="Query", fieldName="getItem", dataSourceName="ds")
def gql(query):
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps({"query": query}).encode(),
headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
# Create
gql('mutation { createItem(input: {id: "i1", title: "Original"}) { id title } }')
# Update
resp = gql('mutation { updateItem(input: {id: "i1", title: "Updated"}) { id title } }')
assert resp["data"]["updateItem"]["title"] == "Updated"
# Verify via get
resp = gql('query { getItem(id: "i1") { id title } }')
assert resp["data"]["getItem"]["title"] == "Updated"
def test_appsync_graphql_delete_mutation(ddb):
"""Delete an item via GraphQL mutation."""
import json as _json
import urllib.request
from conftest import make_client
appsync = make_client("appsync")
try:
ddb.create_table(TableName="gql-del", KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], BillingMode="PAY_PER_REQUEST")
except Exception:
pass
api = appsync.create_graphql_api(name="gql-del", authenticationType="API_KEY")["graphqlApi"]
key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-del", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="createItem", dataSourceName="ds")
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="deleteItem", dataSourceName="ds")
appsync.create_resolver(apiId=api["apiId"], typeName="Query", fieldName="getItem", dataSourceName="ds")
def gql(query):
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps({"query": query}).encode(),
headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
gql('mutation { createItem(input: {id: "d1", title: "Doomed"}) { id } }')
resp = gql('mutation { deleteItem(input: {id: "d1"}) { id title } }')
assert resp["data"]["deleteItem"]["id"] == "d1"
# Verify deleted
resp = gql('query { getItem(id: "d1") { id } }')
assert resp["data"]["getItem"] is None
def test_appsync_graphql_with_variables():
"""GraphQL query using $variables."""
import json as _json
import urllib.request
from conftest import make_client
appsync = make_client("appsync")
ddb_client = make_client("dynamodb")
try:
ddb_client.create_table(TableName="gql-vars", KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], BillingMode="PAY_PER_REQUEST")
except Exception:
pass
api = appsync.create_graphql_api(name="gql-vars", authenticationType="API_KEY")["graphqlApi"]
key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-vars", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="createItem", dataSourceName="ds")
appsync.create_resolver(apiId=api["apiId"], typeName="Query", fieldName="getItem", dataSourceName="ds")
def gql(query, variables=None):
body = {"query": query}
if variables:
body["variables"] = variables
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps(body).encode(),
headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
gql('mutation { createItem(input: {id: "v1", name: "Var Test"}) { id } }')
resp = gql('query GetItem($id: ID!) { getItem(id: $id) { id name } }', {"id": "v1"})
assert resp["data"]["getItem"]["name"] == "Var Test"
def test_appsync_graphql_nonexistent_item():
"""Query for a non-existent item returns null."""
import json as _json
import urllib.request
from conftest import make_client
appsync = make_client("appsync")
ddb_client = make_client("dynamodb")
try:
ddb_client.create_table(TableName="gql-404", KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}], BillingMode="PAY_PER_REQUEST")
except Exception:
pass
api = appsync.create_graphql_api(name="gql-404", authenticationType="API_KEY")["graphqlApi"]
key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-404", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Query", fieldName="getItem", dataSourceName="ds")
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps({"query": 'query { getItem(id: "ghost") { id } }'}).encode(),
headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
resp = _json.loads(r.read())
assert resp["data"]["getItem"] is None
def test_appsync_graphql_nonexistent_api():
"""Query against a non-existent API returns 404."""
import json as _json
import urllib.request
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/fake-api-id/graphql",
data=_json.dumps({"query": "{ getItem(id: \"1\") { id } }"}).encode(),
headers={"Content-Type": "application/json"})
try:
urllib.request.urlopen(req)
assert False, "Should have failed"
except urllib.error.HTTPError as e:
assert e.code == 404
def test_appsync_graphql_empty_query():
"""Empty query returns 400."""
import json as _json
import urllib.request
from conftest import make_client
appsync = make_client("appsync")
api = appsync.create_graphql_api(name="gql-empty", authenticationType="API_KEY")["graphqlApi"]
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps({"query": ""}).encode(),
headers={"Content-Type": "application/json"})
try:
urllib.request.urlopen(req)
assert False, "Should have failed"
except urllib.error.HTTPError as e:
assert e.code == 400
# ---------------------------------------------------------------------------
# AppSync Lambda resolver event shape — verifies full AppSyncResolverEvent is built.
# ---------------------------------------------------------------------------
def _appsync_lambda_zip(handler_code: str) -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("index.py", handler_code)
return buf.getvalue()
def _appsync_create_lambda(lam, fn_name, handler_code):
try:
lam.delete_function(FunctionName=fn_name)
except Exception:
pass
lam.create_function(
FunctionName=fn_name,
Runtime="python3.12",
Role="arn:aws:iam::000000000000:role/test-role",
Handler="index.handler",
Code={"ZipFile": _appsync_lambda_zip(handler_code)},
)
return lam.get_function(FunctionName=fn_name)["Configuration"]["FunctionArn"]
def _appsync_graphql_post(api_url, query, variables=None, headers=None):
"""Send a GraphQL POST request to the AppSync endpoint."""
payload = {"query": query}
if variables:
payload["variables"] = variables
req = urllib.request.Request(
api_url,
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json", **(headers or {})},
method="POST",
)
with urllib.request.urlopen(req, timeout=10) as r:
return json.loads(r.read())
def _appsync_setup_api_with_lambda_resolver(appsync, lam, fn_name, handler_code):
"""Create an AppSync API with API_KEY auth and a Lambda resolver for 'testField'."""
fn_arn = _appsync_create_lambda(lam, fn_name, handler_code)
api = appsync.create_graphql_api(
name=f"test-api-{fn_name}",
authenticationType="API_KEY",
)
api_id = api["graphqlApi"]["apiId"]
api_key = appsync.create_api_key(apiId=api_id)["apiKey"]["id"]
graphql_url = f"{_ENDPOINT}/v1/apis/{api_id}/graphql"
appsync.create_data_source(
apiId=api_id,
name="LambdaDS",
type="AWS_LAMBDA",
lambdaConfig={"lambdaFunctionArn": fn_arn},
)
appsync.create_resolver(
apiId=api_id,
typeName="Query",
fieldName="testField",
dataSourceName="LambdaDS",
kind="UNIT",
)
return api_id, api_key, graphql_url
_APPSYNC_IDENTITY_PROBE_RESOLVER = (
"def handler(event, ctx):\n"
" return {'hasIdentity': event.get('identity') is not None}\n"
)
def _appsync_setup_lambda_auth_api(appsync, lam, name, authorizer_arn, resolver_code):
"""Create an AWS_LAMBDA-auth API with the given authorizer ARN and a Lambda resolver."""
resolver_arn = _appsync_create_lambda(lam, f"{name}-resolver", resolver_code)
api = appsync.create_graphql_api(
name=name,
authenticationType="AWS_LAMBDA",
lambdaAuthorizerConfig={"authorizerUri": authorizer_arn},
)
api_id = api["graphqlApi"]["apiId"]
graphql_url = f"{_ENDPOINT}/v1/apis/{api_id}/graphql"
appsync.create_data_source(
apiId=api_id, name="LambdaDS", type="AWS_LAMBDA",
lambdaConfig={"lambdaFunctionArn": resolver_arn},
)
appsync.create_resolver(
apiId=api_id, typeName="Query", fieldName="testField",
dataSourceName="LambdaDS", kind="UNIT",
)
return api_id, graphql_url
def _appsync_expect_unauthorized(url, query, headers):
"""A rejected AWS_LAMBDA authorizer must surface as HTTP 401 with an
`UnauthorizedException` errors envelope, per the AppSync Developer Guide."""
import urllib.error
req = urllib.request.Request(
url,
data=json.dumps({"query": query}).encode(),
headers={"Content-Type": "application/json", **(headers or {})},
method="POST",
)
try:
urllib.request.urlopen(req, timeout=10)
except urllib.error.HTTPError as exc:
assert exc.code == 401, f"expected 401, got {exc.code}"
body = json.loads(exc.read())
assert "errors" in body and body["errors"]
assert body["errors"][0]["errorType"] == "UnauthorizedException"
return body
raise AssertionError("expected HTTP 401 but request succeeded")
def test_appsync_lambda_event_field_name(appsync, lam):
"""Lambda event must contain info.fieldName matching the queried GraphQL field."""
handler = (
"def handler(event, ctx):\n"
" return {'fieldName': event.get('info', {}).get('fieldName')}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "field-name-test", handler)
resp = _appsync_graphql_post(url, "{ testField { fieldName } }", headers={"x-api-key": api_key})
assert "errors" not in resp
assert resp["data"]["testField"]["fieldName"] == "testField"
def test_appsync_lambda_event_arguments(appsync, lam):
"""Lambda event must contain parsed arguments from the GraphQL query."""
handler = (
"def handler(event, ctx):\n"
" args = event.get('arguments', {})\n"
" return {'receivedId': args.get('params', {}).get('id', 'missing')}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "args-test", handler)
resp = _appsync_graphql_post(
url, 'query { testField(params: {id: "issuer-abc"}) { receivedId } }',
headers={"x-api-key": api_key},
)
assert "errors" not in resp
assert resp["data"]["testField"]["receivedId"] == "issuer-abc"
def test_appsync_lambda_event_api_key_header(appsync, lam):
"""The x-api-key header must be in event.request.headers so isApiKeyAuthenticated() works."""
handler = (
"def handler(event, ctx):\n"
" headers = event.get('request', {}).get('headers', {})\n"
" return {'hasApiKey': 'x-api-key' in headers}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "api-key-header-test", handler)
resp = _appsync_graphql_post(url, "{ testField { hasApiKey } }", headers={"x-api-key": api_key})
assert "errors" not in resp
assert resp["data"]["testField"]["hasApiKey"] is True
def test_appsync_lambda_event_custom_headers_forwarded(appsync, lam):
"""x-request-id, x-session-id, x-user-id, x-workflow, x-process must be in event.request.headers."""
handler = (
"def handler(event, ctx):\n"
" h = event.get('request', {}).get('headers', {})\n"
" return {\n"
" 'requestId': h.get('x-request-id', ''),\n"
" 'sessionId': h.get('x-session-id', ''),\n"
" 'userId': h.get('x-user-id', ''),\n"
" 'workflow': h.get('x-workflow', ''),\n"
" 'process': h.get('x-process', ''),\n"
" }\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "custom-headers-test", handler)
resp = _appsync_graphql_post(
url, "{ testField { requestId sessionId userId workflow process } }",
headers={
"x-api-key": api_key,
"x-request-id": "req-abc-123",
"x-session-id": "sess-xyz-456",
"x-user-id": "user-789",
"x-workflow": "LOGIN",
"x-process": "OTP_VERIFY",
},
)
assert "errors" not in resp
data = resp["data"]["testField"]
assert data["requestId"] == "req-abc-123"
assert data["sessionId"] == "sess-xyz-456"
assert data["userId"] == "user-789"
assert data["workflow"] == "LOGIN"
assert data["process"] == "OTP_VERIFY"
def test_appsync_lambda_event_no_identity_in_api_key_mode(appsync, lam):
"""In API_KEY auth mode, event.identity must be absent or null."""
handler = (
"def handler(event, ctx):\n"
" return {'hasIdentity': event.get('identity') is not None}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "no-identity-test", handler)
resp = _appsync_graphql_post(url, "{ testField { hasIdentity } }", headers={"x-api-key": api_key})
assert "errors" not in resp
assert resp["data"]["testField"]["hasIdentity"] is False
def test_appsync_lambda_event_identity_from_authorizer(appsync, lam):
"""AWS_LAMBDA auth mode — identity.resolverContext from authorizer is in Lambda event."""
authorizer_code = (
"def handler(event, ctx):\n"
" return {\n"
" 'isAuthorized': True,\n"
" 'resolverContext': {\n"
" 'customId': 'test-user-id',\n"
" 'email': '[email protected]',\n"
" 'cognitoGroups': 'admin',\n"
" }\n"
" }\n"
)
resolver_code = (
"def handler(event, ctx):\n"
" identity = event.get('identity') or {}\n"
" rc = identity.get('resolverContext') or {}\n"
" return {\n"
" 'customId': rc.get('customId', ''),\n"
" 'email': rc.get('email', ''),\n"
" }\n"
)
auth_arn = _appsync_create_lambda(lam, "lambda-authorizer-test", authorizer_code)
resolver_arn = _appsync_create_lambda(lam, "lambda-resolver-test", resolver_code)
api = appsync.create_graphql_api(
name="lambda-auth-api",
authenticationType="AWS_LAMBDA",
lambdaAuthorizerConfig={"authorizerUri": auth_arn},
)
api_id = api["graphqlApi"]["apiId"]
url = f"{_ENDPOINT}/v1/apis/{api_id}/graphql"
appsync.create_data_source(
apiId=api_id, name="LambdaDS", type="AWS_LAMBDA",
lambdaConfig={"lambdaFunctionArn": resolver_arn},
)
appsync.create_resolver(
apiId=api_id, typeName="Query", fieldName="testField",
dataSourceName="LambdaDS", kind="UNIT",
)
resp = _appsync_graphql_post(url, "{ testField { customId email } }",
headers={"Authorization": "Bearer fake-jwt-token"})
assert "errors" not in resp
assert resp["data"]["testField"]["customId"] == "test-user-id"
def test_appsync_lambda_not_found_no_crash(appsync):
"""If Lambda function doesn't exist in ministack, AppSync returns a response (no crash)."""
api = appsync.create_graphql_api(name="lambda-missing-api", authenticationType="API_KEY")
api_id = api["graphqlApi"]["apiId"]
api_key = appsync.create_api_key(apiId=api_id)["apiKey"]["id"]
appsync.create_data_source(
apiId=api_id, name="MissingLambdaDS", type="AWS_LAMBDA",
lambdaConfig={"lambdaFunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:does-not-exist"},
)
appsync.create_resolver(
apiId=api_id, typeName="Query", fieldName="testField",
dataSourceName="MissingLambdaDS", kind="UNIT",
)
resp = _appsync_graphql_post(f"{_ENDPOINT}/v1/apis/{api_id}/graphql", "{ testField }",
headers={"x-api-key": api_key})
assert "data" in resp or "errors" in resp
def test_appsync_lambda_returns_errors(appsync, lam):
"""Lambda returning {errors: ['INTERNAL_SERVER_ERROR']} is passed through correctly."""
handler = (
"def handler(event, ctx):\n"
" return {'errors': ['INTERNAL_SERVER_ERROR'], 'data': None}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "error-response-test", handler)
resp = _appsync_graphql_post(url, "{ testField { id } }", headers={"x-api-key": api_key})
assert resp["data"]["testField"]["errors"] == ["INTERNAL_SERVER_ERROR"]
def test_appsync_lambda_event_source_empty_for_root(appsync, lam):
"""event.source must be {} (empty) for top-level Query fields."""
handler = (
"def handler(event, ctx):\n"
" s = event.get('source')\n"
" return {'sourceIsEmpty': s == {} or s is None}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "source-test", handler)
resp = _appsync_graphql_post(url, "{ testField { sourceIsEmpty } }", headers={"x-api-key": api_key})
assert "errors" not in resp
assert resp["data"]["testField"]["sourceIsEmpty"] is True
def test_appsync_lambda_event_variables_substituted(appsync, lam):
"""Variables in the query must be resolved to values before the event is built."""
handler = (
"def handler(event, ctx):\n"
" args = event.get('arguments', {})\n"
" return {'id': args.get('params', {}).get('id', 'missing')}\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "variables-test", handler)
resp = _appsync_graphql_post(
url, "query GetTest($id: ID!) { testField(params: {id: $id}) { id } }",
variables={"id": "issuer-from-var"},
headers={"x-api-key": api_key},
)
assert "errors" not in resp
assert resp["data"]["testField"]["id"] == "issuer-from-var"
def test_appsync_lambda_unhandled_exception_becomes_error(appsync, lam):
"""A Lambda that raises must yield a GraphQL `errors` entry, not fake `data`."""
handler = (
"def handler(event, ctx):\n"
" raise Exception('boom')\n"
)
_, api_key, url = _appsync_setup_api_with_lambda_resolver(appsync, lam, "raise-test", handler)
resp = _appsync_graphql_post(url, "{ testField { id } }", headers={"x-api-key": api_key})
field = resp.get("data", {}).get("testField")
assert field is None or "errorMessage" not in field
assert "errors" in resp or (isinstance(field, dict) and "errors" in field)
def test_appsync_lambda_authorizer_rejection_returns_unauthorized(appsync, lam):
"""AWS_LAMBDA auth — `isAuthorized:false` must reject with `UnauthorizedException` (HTTP 401)."""
authorizer = (
"def handler(event, ctx):\n"
" return {'isAuthorized': False, 'resolverContext': {'should': 'not-leak'}}\n"
)
auth_arn = _appsync_create_lambda(lam, "authz-reject-test", authorizer)
_, url = _appsync_setup_lambda_auth_api(appsync, lam, "authz-reject-api", auth_arn,
_APPSYNC_IDENTITY_PROBE_RESOLVER)
_appsync_expect_unauthorized(url, "{ testField { hasIdentity } }",
headers={"Authorization": "Bearer fake-jwt"})
def test_appsync_lambda_authorizer_wrong_region_arn_does_not_fallback(appsync, lam):
"""A wrong-region authorizer ARN must not invoke a same-named local Lambda."""
authorizer = (
"def handler(event, ctx):\n"
" return {'isAuthorized': True, 'resolverContext': {'region': 'current'}}\n"
)
auth_arn = _appsync_create_lambda(lam, "authz-wrong-region-test", authorizer)
arn_parts = auth_arn.split(":")
arn_parts[3] = "us-west-2" if arn_parts[3] != "us-west-2" else "us-east-2"
wrong_region_arn = ":".join(arn_parts)
_, url = _appsync_setup_lambda_auth_api(
appsync, lam, "authz-wrong-region-api", wrong_region_arn,
_APPSYNC_IDENTITY_PROBE_RESOLVER,
)
_appsync_expect_unauthorized(url, "{ testField { hasIdentity } }",
headers={"Authorization": "Bearer fake-jwt"})
def test_appsync_lambda_missing_authorizer_returns_unauthorized(appsync, lam):
"""AWS_LAMBDA auth — missing authorizer Lambda must reject with `UnauthorizedException` (HTTP 401)."""
_, url = _appsync_setup_lambda_auth_api(
appsync, lam, "authz-missing-api",
"arn:aws:lambda:us-east-1:000000000000:function:authorizer-does-not-exist",
_APPSYNC_IDENTITY_PROBE_RESOLVER,
)
_appsync_expect_unauthorized(url, "{ testField { hasIdentity } }",
headers={"Authorization": "Bearer fake-jwt"})
def test_appsync_lambda_failing_authorizer_returns_unauthorized(appsync, lam):
"""AWS_LAMBDA auth — raising authorizer must reject with `UnauthorizedException` (HTTP 401)."""
authorizer = (
"def handler(event, ctx):\n"
" raise Exception('authorizer boom')\n"
)
auth_arn = _appsync_create_lambda(lam, "authz-raise-test", authorizer)
_, url = _appsync_setup_lambda_auth_api(appsync, lam, "authz-raise-api", auth_arn,
_APPSYNC_IDENTITY_PROBE_RESOLVER)
_appsync_expect_unauthorized(url, "{ testField { hasIdentity } }",
headers={"Authorization": "Bearer fake-jwt"})
SDL = """type Query {
hello: String
}
schema { query: Query }
"""
def test_appsync_schema_creation_and_introspection():
"""StartSchemaCreation stores the SDL; the status polls to SUCCESS and it reads back."""
from conftest import make_client
appsync = make_client("appsync")
api = appsync.create_graphql_api(name="schema-api", authenticationType="API_KEY")["graphqlApi"]
started = appsync.start_schema_creation(apiId=api["apiId"], definition=SDL.encode())
assert started["status"] in ("PROCESSING", "SUCCESS")
status = appsync.get_schema_creation_status(apiId=api["apiId"])
assert status["status"] == "SUCCESS"
schema = appsync.get_introspection_schema(apiId=api["apiId"], format="SDL")
assert schema["schema"].read().decode() == SDL