Skip to content

Commit 1465ad3

Browse files
committed
chore: add testing
1 parent 44b77b8 commit 1465ad3

File tree

5 files changed

+195
-5
lines changed

5 files changed

+195
-5
lines changed

protection/jwt-claims-dbquery/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ to those matching the regions defined in a JWT claim.
88
Run the [sample operations](operations.graphql):
99

1010
JWT with `regions: IN`.
11+
1112
```
1213
stepzen request -f operations.graphql --operation-name=Customers \
1314
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJJTiJdfQ.hDi3-qaIOSFKzlFvaXwSh0trXC3vjiOehSKE0OxgOdE"
1415
```
1516

16-
1717
JWT with `regions: IN, UK`.
18+
1819
```
1920
stepzen request -f operations.graphql --operation-name=Customers \
20-
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJJTiJdfQ.hDi3-qaIOSFKzlFvaXwSh0trXC3vjiOehSKE0OxgOdE"
21+
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJJTiIsIlVLIl19.CRD85IIMMwjaFebtQ_p3AjSoUM6KtH4gvjcfLQfdmjw"
2122
```
2223

2324
JWT with `regions: US, UK`.
25+
2426
```
2527
stepzen request -f operations.graphql --operation-name=Customers \
2628
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJVUyIsIlVLIl19.pf0-A6TN_hT-ldCvsZyqYGv4Twjm9s6wO1aatCjK9Aw"
2729
```
2830

2931
JWT with `regions: US, UK` and user supplied filter
32+
3033
```
3134
stepzen request -f operations.graphql --operation-name=Customers \
3235
--var f='{"city": {"eq":"London"}}' \

protection/jwt-claims-dbquery/operations.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
query Customers($f:CustomerFilter) {
2-
customers(filter:$f) {
1+
query Customers($f: CustomerFilter) {
2+
customers(filter: $f) {
33
id
44
name
55
city

protection/jwt-claims-dbquery/paging.graphql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ input CustomerFilter {
3434
city: StringFilter
3535
}
3636

37+
type _RegionsList {
38+
regions: [String]!
39+
}
40+
3741
extend type Query {
3842
# customers is the exposed field that limits the caller to regions
3943
# based upon the regions claim in the request's JWT.
@@ -52,7 +56,15 @@ extend type Query {
5256
)
5357

5458
# extracts the regions visible to the request from the JWT.
55-
_regions: [String]! @value(script: { src: "$jwt.regions" })
59+
_regions: _RegionsList
60+
@value(
61+
script: {
62+
src: """
63+
{"regions": `$jwt`.regions }
64+
"""
65+
language: JSONATA
66+
}
67+
)
5668

5769
# this flattens the customer connection pagination structure
5870
# into a simple list of Customer objects.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
const fs = require("fs");
2+
const path = require("node:path");
3+
const {
4+
deployAndRun,
5+
runtests,
6+
GQLHeaders,
7+
endpoint,
8+
getTestDescription,
9+
} = require("../../../tests/gqltest.js");
10+
11+
testDescription = getTestDescription("snippets", __dirname);
12+
13+
const requestsFile = path.join(path.dirname(__dirname), "operations.graphql");
14+
const requests = fs.readFileSync(requestsFile, "utf8").toString();
15+
16+
describe(testDescription, function () {
17+
// just deploy
18+
deployAndRun(__dirname, [], undefined);
19+
20+
// and then run with various JWTs
21+
runtests(
22+
"regions-in",
23+
endpoint,
24+
new GQLHeaders().withToken(
25+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJJTiJdfQ.hDi3-qaIOSFKzlFvaXwSh0trXC3vjiOehSKE0OxgOdE"
26+
),
27+
[
28+
{
29+
label: "customers",
30+
query: requests,
31+
operationName: "Customers",
32+
expected: {
33+
customers: [
34+
{
35+
id: "10",
36+
name: "Salma Khan ",
37+
city: "Delhi ",
38+
region: "IN ",
39+
},
40+
],
41+
},
42+
},
43+
]
44+
);
45+
runtests(
46+
"regions-in-uk",
47+
endpoint,
48+
new GQLHeaders().withToken(
49+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJJTiIsIlVLIl19.CRD85IIMMwjaFebtQ_p3AjSoUM6KtH4gvjcfLQfdmjw"
50+
),
51+
[
52+
{
53+
label: "customers",
54+
query: requests,
55+
operationName: "Customers",
56+
expected: {
57+
customers: [
58+
{
59+
id: "3",
60+
name: "Salim Ali ",
61+
city: "London ",
62+
region: "UK ",
63+
},
64+
{
65+
id: "4",
66+
name: "Jane Xiu ",
67+
city: "Edinburgh ",
68+
region: "UK ",
69+
},
70+
{
71+
id: "10",
72+
name: "Salma Khan ",
73+
city: "Delhi ",
74+
region: "IN ",
75+
},
76+
],
77+
},
78+
},
79+
]
80+
);
81+
runtests(
82+
"regions-us-uk",
83+
endpoint,
84+
new GQLHeaders().withToken(
85+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1IiwicmVnaW9ucyI6WyJVUyIsIlVLIl19.pf0-A6TN_hT-ldCvsZyqYGv4Twjm9s6wO1aatCjK9Aw"
86+
),
87+
[
88+
{
89+
label: "customers",
90+
query: requests,
91+
operationName: "Customers",
92+
expected: {
93+
customers: [
94+
{
95+
id: "1",
96+
name: "Lucas Bill ",
97+
city: "Boston ",
98+
region: "US ",
99+
},
100+
{
101+
id: "2",
102+
name: "Mandy Jones ",
103+
city: "Round Rock ",
104+
region: "US ",
105+
},
106+
{
107+
id: "3",
108+
name: "Salim Ali ",
109+
city: "London ",
110+
region: "UK ",
111+
},
112+
{
113+
id: "4",
114+
name: "Jane Xiu ",
115+
city: "Edinburgh ",
116+
region: "UK ",
117+
},
118+
{
119+
id: "5",
120+
name: "John Doe ",
121+
city: "Miami ",
122+
region: "US ",
123+
},
124+
{
125+
id: "6",
126+
name: "Jane Smith ",
127+
city: "San Francisco ",
128+
region: "US ",
129+
},
130+
{
131+
id: "7",
132+
name: "Sandeep Bhushan ",
133+
city: "New York ",
134+
region: "US ",
135+
},
136+
{
137+
id: "8",
138+
name: "George Han ",
139+
city: "Seattle ",
140+
region: "US ",
141+
},
142+
{
143+
id: "9",
144+
name: "Asha Kumari ",
145+
city: "Chicago ",
146+
region: "US ",
147+
},
148+
],
149+
},
150+
},
151+
{
152+
label: "customers-filter",
153+
query: requests,
154+
operationName: "Customers",
155+
variables: {
156+
f: { city: { eq: "London" } },
157+
},
158+
expected: {
159+
customers: [
160+
{
161+
id: "3",
162+
name: "Salim Ali ",
163+
city: "London ",
164+
region: "UK ",
165+
},
166+
],
167+
},
168+
},
169+
]
170+
);
171+
});

tests/gqltest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require("node:path");
55

66
const {
77
runtests,
8+
GQLHeaders,
89
} = require('gqltest/packages/gqltest/gqltest.js');
910

1011
const stepzen = require("gqltest/packages/gqltest/stepzen.js");
@@ -59,5 +60,8 @@ function getTestDescription(testRoot, fullDirName) {
5960

6061
exports.deployAndRun = deployAndRun;
6162
exports.getTestDescription = getTestDescription;
63+
exports.endpoint = endpoint;
6264

65+
exports.GQLHeaders = GQLHeaders;
66+
exports.runtests = runtests;
6367
exports.stepzen = stepzen;

0 commit comments

Comments
 (0)