@@ -87,6 +87,11 @@ interface GetUserVariables {
87
87
id : { id : string ; } ;
88
88
}
89
89
90
+ interface DeleteResponse {
91
+ email_deleteMany : number
92
+ user_deleteMany : number
93
+ }
94
+
90
95
const connectorConfig : ConnectorConfig = {
91
96
location : 'us-west2' ,
92
97
serviceId : 'my-service' ,
@@ -97,14 +102,12 @@ const fredrickUser = { id: fredUser.id, address: '64 Elm St. North', name: 'Fred
97
102
98
103
const jeffUser = { id : 'jeff_id' , address : '99 Oak St.' , name : 'Jeff' }
99
104
100
- const expectedUserIds = [ fredUser . id , jeffUser . id ]
101
-
102
105
const fredEmail = {
103
106
id : 'email_id' ,
104
107
subject : 'free bitcoin inside' ,
105
108
date : '1999-12-31' ,
106
109
text : 'get pranked! LOL!' ,
107
- fromId : fredUser . id
110
+ from : { id : fredUser . id }
108
111
}
109
112
110
113
describe ( 'getDataConnect()' , ( ) => {
@@ -116,13 +119,41 @@ describe('getDataConnect()', () => {
116
119
} ) ;
117
120
} ) ;
118
121
122
+ afterEach ( async ( ) => {
123
+ await cleanupDatabase ( ) ;
124
+ } )
125
+
126
+ beforeEach ( async ( ) => {
127
+ await initializeDatabase ( ) ;
128
+ } ) ;
129
+
130
+ /** initial state of database after calling initializeDatabase() */
131
+ const initialState = { users : [ fredUser , jeffUser ] , emails : [ fredEmail ] } ;
132
+
133
+ /** helper function which sets initial state of the database before each test */
134
+ async function initializeDatabase ( ) : Promise < void > {
135
+ await getDataConnect ( connectorConfig ) . executeGraphql < UserUpsertResponse , unknown > (
136
+ upsertFredUser
137
+ ) ;
138
+ await getDataConnect ( connectorConfig ) . executeGraphql < UserUpsertResponse , unknown > (
139
+ upsertJeffUser
140
+ ) ;
141
+ await getDataConnect ( connectorConfig ) . executeGraphql < EmailUpsertResponse , unknown > (
142
+ upsertFredEmail
143
+ ) ;
144
+ }
145
+
146
+ /** helper function which clears state of the database after each test */
147
+ async function cleanupDatabase ( ) : Promise < void > {
148
+ await getDataConnect ( connectorConfig ) . executeGraphql < DeleteResponse , unknown > ( deleteAll ) ;
149
+ }
119
150
/** @auth (level: PUBLIC) */
120
151
const queryListUsers = 'query ListUsers @auth(level: PUBLIC) { users { id, name, address } }' ;
121
152
/** @auth (level: NO_ACCESS) */
122
153
const queryListEmails =
123
- 'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { name } } }' ;
154
+ 'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { id } } }' ;
124
155
/** no @auth specified - default permissions */
125
- const queryGetUserById = 'query GetUser($id: User_Key!) { user(key: $id) { id name } }' ;
156
+ const queryGetUserById = 'query GetUser($id: User_Key!) { user(key: $id) { id name address } }' ;
126
157
127
158
/** @auth (level: USER) */
128
159
const queryListUsersImpersonation = `
@@ -163,10 +194,16 @@ describe('getDataConnect()', () => {
163
194
subject: "${ fredEmail . subject } ",
164
195
date: "${ fredEmail . date } ",
165
196
text: "${ fredEmail . text } ",
166
- fromId: "${ fredEmail . fromId } "
197
+ fromId: "${ fredEmail . from . id } "
167
198
})
168
199
}` ;
169
200
201
+ /** hardcoded delete all mutation, for cleanup */
202
+ const deleteAll = `mutation delete {
203
+ email_deleteMany(all: true)
204
+ user_deleteMany(all: true)
205
+ }`
206
+
170
207
describe ( 'executeGraphql()' , ( ) => {
171
208
it ( 'executeGraphql() successfully executes a GraphQL mutation' , async ( ) => {
172
209
const fredResponse = await getDataConnect ( connectorConfig ) . executeGraphql < UserUpsertResponse , unknown > (
@@ -188,15 +225,19 @@ describe('getDataConnect()', () => {
188
225
) ;
189
226
//{ data: { email_upsert: { id: 'email_id' } } }
190
227
expect ( emailResponse . data . email_upsert . id ) . to . be . not . empty ;
228
+
229
+ const deleteResponse = await getDataConnect ( connectorConfig ) . executeGraphql < DeleteResponse , unknown > ( deleteAll ) ;
230
+ expect ( deleteResponse . data . email_deleteMany ) . to . be . greaterThan ( 0 ) ;
231
+ expect ( deleteResponse . data . user_deleteMany ) . to . be . greaterThan ( 0 ) ;
191
232
} ) ;
192
233
193
234
it ( 'executeGraphql() successfully executes a GraphQL query' , async ( ) => {
194
235
const resp = await getDataConnect ( connectorConfig )
195
236
. executeGraphql < ListUsersResponse , undefined > ( queryListUsers ) ;
196
237
expect ( resp . data . users ) . to . be . not . empty ;
197
- expect ( resp . data . users . length ) . to . greaterThan ( 1 ) ;
238
+ expect ( resp . data . users . length ) . to . equal ( initialState . users . length ) ;
198
239
resp . data . users . forEach ( ( user ) => {
199
- expect ( expectedUserIds ) . to . include ( user . id ) ;
240
+ expect ( initialState . users ) . to . deep . include ( user ) ;
200
241
} ) ;
201
242
} ) ;
202
243
@@ -205,10 +246,8 @@ describe('getDataConnect()', () => {
205
246
multipleQueries ,
206
247
{ operationName : 'ListEmails' }
207
248
) ;
208
- expect ( resp . data . emails ) . to . be . not . empty ;
209
- expect ( resp . data . emails . length ) . equals ( 1 ) ;
210
- expect ( resp . data . emails [ 0 ] . id ) . to . be . not . undefined ;
211
- expect ( resp . data . emails [ 0 ] . from ?. name ) . to . equal ( fredUser . name ) ;
249
+ expect ( resp . data . emails ) . to . not . be . empty ;
250
+ expect ( resp . data . emails ) . to . deep . equal ( initialState . emails ) ;
212
251
} ) ;
213
252
214
253
it ( 'executeGraphql() should throw for a query error when no variables are provided' , async ( ) => {
@@ -219,11 +258,9 @@ describe('getDataConnect()', () => {
219
258
it ( 'executeGraphql() successfully executes a GraphQL query with variables' , async ( ) => {
220
259
const resp = await getDataConnect ( connectorConfig ) . executeGraphql < GetUserResponse , GetUserVariables > (
221
260
queryGetUserById ,
222
- { variables : { id : { id : fredUser . id } } }
261
+ { variables : { id : { id : initialState . users [ 0 ] . id } } }
223
262
) ;
224
- expect ( resp . data . user . id ) . to . equal ( fredUser . id ) ;
225
- expect ( resp . data . user . name ) . to . equal ( fredUser . name ) ;
226
- expect ( resp . data . user . address ) . to . be . undefined ;
263
+ expect ( resp . data . user ) . to . deep . equal ( initialState . users [ 0 ] ) ;
227
264
} ) ;
228
265
} ) ;
229
266
@@ -232,9 +269,9 @@ describe('getDataConnect()', () => {
232
269
const resp = await getDataConnect ( connectorConfig )
233
270
. executeGraphqlRead < ListUsersResponse , undefined > ( queryListUsers ) ;
234
271
expect ( resp . data . users ) . to . be . not . empty ;
235
- expect ( resp . data . users . length ) . to . be . greaterThan ( 1 ) ;
272
+ expect ( resp . data . users . length ) . to . equal ( initialState . users . length ) ;
236
273
resp . data . users . forEach ( ( user ) => {
237
- expect ( expectedUserIds ) . to . include ( user . id ) ;
274
+ expect ( initialState . users ) . to . deep . include ( user ) ;
238
275
} ) ;
239
276
} ) ;
240
277
@@ -273,9 +310,7 @@ describe('getDataConnect()', () => {
273
310
it ( 'executeGraphqlRead() successfully executes an impersonated query with authenticated claims' , async ( ) => {
274
311
const resp =
275
312
await getDataConnect ( connectorConfig ) . executeGraphqlRead < ListUsersResponse , undefined > (
276
- queryListUsersImpersonation ,
277
- optsAuthorizedFredClaims
278
- ) ;
313
+ queryListUsersImpersonation , optsAuthorizedFredClaims ) ;
279
314
expect ( resp . data . users ) . to . be . not . empty ;
280
315
expect ( resp . data . users . length ) . equals ( 1 ) ;
281
316
expect ( resp . data . users [ 0 ] ) . to . deep . equal ( fredUser ) ;
@@ -290,16 +325,15 @@ describe('getDataConnect()', () => {
290
325
} ) ;
291
326
292
327
it ( 'executeGraphql() successfully executes an impersonated query with authenticated claims' , async ( ) => {
293
- const resp = await getDataConnect ( connectorConfig ) . executeGraphqlRead < ListUsersResponse , undefined > (
328
+ const resp = await getDataConnect ( connectorConfig ) . executeGraphql < ListUsersResponse , undefined > (
294
329
queryListUsersImpersonation , optsAuthorizedFredClaims ) ;
295
330
expect ( resp . data . users ) . to . be . not . empty ;
296
331
expect ( resp . data . users . length ) . equals ( 1 ) ;
297
332
expect ( resp . data . users [ 0 ] ) . to . deep . equal ( fredUser ) ;
298
333
} ) ;
299
334
300
335
it ( 'executeGraphql() should throw for impersonated query with unauthenticated claims' , async ( ) => {
301
- return getDataConnect ( connectorConfig ) . executeGraphql (
302
- queryListUsersImpersonation , optsUnauthorizedClaims )
336
+ return getDataConnect ( connectorConfig ) . executeGraphql ( queryListUsersImpersonation , optsUnauthorizedClaims )
303
337
. should . eventually . be . rejected . and . has . property ( 'code' , 'data-connect/unauthenticated' ) ;
304
338
} ) ;
305
339
@@ -314,10 +348,14 @@ describe('getDataConnect()', () => {
314
348
315
349
it ( 'executeGraphql() successfully executes an impersonated mutation with authenticated claims' ,
316
350
async ( ) => {
317
- const resp = await getDataConnect ( connectorConfig ) . executeGraphql < UserUpdateResponse , undefined > (
351
+ const updateResp = await getDataConnect ( connectorConfig ) . executeGraphql < UserUpdateResponse , undefined > (
318
352
updateFredrickUserImpersonated , optsAuthorizedFredClaims ) ;
319
353
// Fred -> Fredrick
320
- expect ( resp . data . user_update . id ) . equals ( fredUser . id ) ;
354
+ expect ( updateResp . data . user_update . id ) . equals ( fredUser . id ) ;
355
+ const queryResp = await getDataConnect ( connectorConfig ) . executeGraphql < GetUserResponse , GetUserVariables > (
356
+ queryGetUserById , { variables : { id : { id : fredUser . id } } } ) ;
357
+ expect ( queryResp . data . user ) . to . not . be . empty ;
358
+ expect ( queryResp . data . user ) . to . deep . equal ( fredrickUser ) ;
321
359
} ) ;
322
360
323
361
it ( 'executeGraphql() should throw for impersonated mutation with unauthenticated claims' , async ( ) => {
@@ -339,19 +377,19 @@ describe('getDataConnect()', () => {
339
377
const resp = await getDataConnect ( connectorConfig ) . executeGraphql < ListUsersResponse , undefined > (
340
378
queryListUsers , optsAuthorizedFredClaims ) ;
341
379
expect ( resp . data . users ) . to . be . not . empty ;
342
- expect ( resp . data . users . length ) . to . be . greaterThan ( 1 ) ;
380
+ expect ( resp . data . users . length ) . to . equal ( initialState . users . length ) ;
343
381
resp . data . users . forEach ( ( user ) => {
344
- expect ( expectedUserIds ) . to . include ( user . id ) ;
382
+ expect ( initialState . users ) . to . deep . include ( user ) ;
345
383
} ) ;
346
384
} ) ;
347
385
348
386
it ( 'executeGraphql() successfully executes an impersonated query with unauthenticated claims' , async ( ) => {
349
387
const resp = await getDataConnect ( connectorConfig ) . executeGraphql < ListUsersResponse , undefined > (
350
388
queryListUsers , optsUnauthorizedClaims ) ;
351
389
expect ( resp . data . users ) . to . be . not . empty ;
352
- expect ( resp . data . users . length ) . to . be . greaterThan ( 1 ) ;
390
+ expect ( resp . data . users . length ) . to . equal ( initialState . users . length ) ;
353
391
resp . data . users . forEach ( ( user ) => {
354
- expect ( expectedUserIds ) . to . include ( user . id ) ;
392
+ expect ( initialState . users ) . to . deep . include ( user ) ;
355
393
} ) ;
356
394
} ) ;
357
395
@@ -360,9 +398,9 @@ describe('getDataConnect()', () => {
360
398
const resp = await getDataConnect ( connectorConfig ) . executeGraphql < ListUsersResponse , undefined > (
361
399
queryListUsers , optsNonExistingClaims ) ;
362
400
expect ( resp . data . users ) . to . be . not . empty ;
363
- expect ( resp . data . users . length ) . to . be . greaterThan ( 1 ) ;
401
+ expect ( resp . data . users . length ) . to . equal ( initialState . users . length ) ;
364
402
resp . data . users . forEach ( ( user ) => {
365
- expect ( expectedUserIds ) . to . include ( user . id ) ;
403
+ expect ( initialState . users ) . to . deep . include ( user ) ;
366
404
} ) ;
367
405
} ) ;
368
406
} ) ;
0 commit comments