@forge/storage 1.5.13 → 1.5.14-experimental-6adbba3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,609 +43,670 @@ describe('GlobalStorage', () => {
43
43
  __getAppAri: jest.fn().mockReturnValue(contextAri)
44
44
  };
45
45
  });
46
- describe('get', () => {
47
- it('should call the storage API, passing the provided key and returning the stored value', async () => {
48
- const apiClientMock = getApiClientMock({
49
- data: {
50
- appStoredEntity: {
51
- value: 'testValue'
52
- }
53
- }
54
- });
55
- const globalStorage = getStorage(apiClientMock);
56
- const returnedValue = await globalStorage.get('testKey');
57
- verifyApiClientCalledWith(apiClientMock, {
58
- contextAri,
59
- key: 'testKey',
60
- encrypted: false
61
- });
62
- expect(returnedValue).toEqual('testValue');
63
- });
64
- it('should call the storage API, passing the provided key and returning undefined if the key doesnt exist', async () => {
65
- const apiClientMock = getApiClientMock({
66
- data: {
67
- appStoredEntity: {
68
- value: null
69
- }
70
- }
71
- });
72
- const globalStorage = getStorage(apiClientMock);
73
- const returnedValue = await globalStorage.get('testKey');
74
- verifyApiClientCalledWith(apiClientMock, {
75
- contextAri,
76
- key: 'testKey',
77
- encrypted: false
78
- });
79
- expect(returnedValue).toEqual(undefined);
80
- });
81
- it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
82
- const apiClientMock = getApiClientMock({
83
- data: {
84
- appStoredEntity: {
85
- value: 0
86
- }
87
- }
88
- });
89
- const globalStorage = getStorage(apiClientMock);
90
- const returnedValue = await globalStorage.get('testKey');
91
- verifyApiClientCalledWith(apiClientMock, {
92
- contextAri,
93
- key: 'testKey',
94
- encrypted: false
95
- });
96
- expect(returnedValue).toEqual(0);
97
- });
98
- it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
99
- const apiClientMock = getApiClientMock({
100
- data: {
101
- appStoredEntity: {
102
- value: ''
103
- }
104
- }
105
- });
106
- const globalStorage = getStorage(apiClientMock);
107
- const returnedValue = await globalStorage.get('testKey');
108
- verifyApiClientCalledWith(apiClientMock, {
109
- contextAri,
110
- key: 'testKey',
111
- encrypted: false
112
- });
113
- expect(returnedValue).toEqual('');
114
- });
115
- it('should throw an error with the returned status for non-200 status codes', async () => {
116
- const apiClientMock = getApiClientMock(undefined, 400);
117
- const globalStorage = getStorage(apiClientMock);
118
- const response = globalStorage.get('testKey');
119
- expect(apiClientMock).toHaveBeenCalled();
120
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
121
- });
122
- it('should throw an error with the returned error message for failed responses', async () => {
123
- const apiClientMock = getApiClientMock({
124
- errors: [INVALID_CURSOR_ERROR]
125
- }, 200);
126
- const globalStorage = getStorage(apiClientMock);
127
- const response = globalStorage.get('testKey');
128
- expect(apiClientMock).toHaveBeenCalled();
129
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
130
- });
131
- it('should throw an error if the response is not a valid JSON', async () => {
132
- const apiClientMock = getApiClientMockInvalidJson('test', 200);
133
- const globalStorage = getStorage(apiClientMock);
134
- const response = globalStorage.get('testKey');
135
- expect(apiClientMock).toHaveBeenCalled();
136
- await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
137
- });
138
- });
139
- describe('get secret', () => {
140
- it('should call the storage API, passing the provided key and returning the stored value', async () => {
141
- const apiClientMock = getApiClientMock({
142
- data: {
143
- appStoredEntity: {
144
- value: 'testValue'
145
- }
146
- }
147
- });
148
- const globalStorage = getStorage(apiClientMock);
149
- const returnedValue = await globalStorage.getSecret('testKey');
150
- verifyApiClientCalledWith(apiClientMock, {
151
- contextAri,
152
- key: 'testKey',
153
- encrypted: true
154
- });
155
- expect(returnedValue).toEqual('testValue');
156
- });
157
- });
158
- describe('set', () => {
159
- it('should call the storage API, passing the provided key and value', async () => {
160
- const apiClientMock = getApiClientMock({
161
- data: {
162
- appStorage: {
163
- setAppStoredEntity: {
164
- success: true
46
+ describe('Untyped entities', () => {
47
+ describe('get', () => {
48
+ it('should call the storage API, passing the provided key and returning the stored value', async () => {
49
+ const apiClientMock = getApiClientMock({
50
+ data: {
51
+ appStoredEntity: {
52
+ value: 'testValue'
165
53
  }
166
54
  }
167
- }
168
- });
169
- const globalStorage = getStorage(apiClientMock);
170
- await globalStorage.set('testKey', 'testValue');
171
- verifyApiClientCalledWith(apiClientMock, {
172
- input: {
55
+ });
56
+ const globalStorage = getStorage(apiClientMock);
57
+ const returnedValue = await globalStorage.get('testKey');
58
+ verifyApiClientCalledWith(apiClientMock, {
173
59
  contextAri,
174
60
  key: 'testKey',
175
- value: 'testValue',
176
61
  encrypted: false
177
- }
178
- });
179
- });
180
- it('should throw an error if the storage API returns successful = false', async () => {
181
- const apiClientMock = getApiClientMock({
182
- data: {
183
- appStorage: {
184
- setAppStoredEntity: {
185
- success: false,
186
- errors: [INVALID_CURSOR_ERROR]
187
- }
188
- }
189
- }
190
- });
191
- const globalStorage = getStorage(apiClientMock);
192
- const response = globalStorage.set('testKey', 'testValue');
193
- expect(apiClientMock).toHaveBeenCalled();
194
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
195
- });
196
- it('should throw an error if the storage API returns a non 200 status code', async () => {
197
- const apiClientMock = getApiClientMockInvalidJson('', 400);
198
- const globalStorage = getStorage(apiClientMock);
199
- const response = globalStorage.set('testKey', 'testValue');
200
- expect(apiClientMock).toHaveBeenCalled();
201
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
202
- });
203
- it('should throw a 500 error if success=false but no errors were returned', async () => {
204
- const apiClientMock = getApiClientMock({
205
- data: {
206
- appStorage: {
207
- setAppStoredEntity: {
208
- success: false
62
+ });
63
+ expect(returnedValue).toEqual('testValue');
64
+ });
65
+ it('should call the storage API, passing the provided key and returning undefined if the key doesnt exist', async () => {
66
+ const apiClientMock = getApiClientMock({
67
+ data: {
68
+ appStoredEntity: {
69
+ value: null
209
70
  }
210
71
  }
211
- }
212
- });
213
- const globalStorage = getStorage(apiClientMock);
214
- await expect(globalStorage.set('testKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
215
- verifyApiClientCalledWith(apiClientMock, {
216
- input: {
72
+ });
73
+ const globalStorage = getStorage(apiClientMock);
74
+ const returnedValue = await globalStorage.get('testKey');
75
+ verifyApiClientCalledWith(apiClientMock, {
217
76
  contextAri,
218
77
  key: 'testKey',
219
- value: 'testValue',
220
78
  encrypted: false
221
- }
222
- });
223
- });
224
- });
225
- describe('set secret', () => {
226
- it('should call the storage API, passing the provided key and value', async () => {
227
- const apiClientMock = getApiClientMock({
228
- data: {
229
- appStorage: {
230
- setAppStoredEntity: {
231
- success: true
79
+ });
80
+ expect(returnedValue).toEqual(undefined);
81
+ });
82
+ it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
83
+ const apiClientMock = getApiClientMock({
84
+ data: {
85
+ appStoredEntity: {
86
+ value: 0
232
87
  }
233
88
  }
234
- }
235
- });
236
- const globalStorage = getStorage(apiClientMock);
237
- await globalStorage.setSecret('testKey', 'testValue');
238
- verifyApiClientCalledWith(apiClientMock, {
239
- input: {
89
+ });
90
+ const globalStorage = getStorage(apiClientMock);
91
+ const returnedValue = await globalStorage.get('testKey');
92
+ verifyApiClientCalledWith(apiClientMock, {
240
93
  contextAri,
241
94
  key: 'testKey',
242
- value: 'testValue',
243
- encrypted: true
244
- }
245
- });
246
- });
247
- });
248
- describe('delete', () => {
249
- it('should call the storage API, passing the provided key', async () => {
250
- const apiClientMock = getApiClientMock({
251
- data: {
252
- appStorage: {
253
- deleteAppStoredEntity: {
254
- success: true
95
+ encrypted: false
96
+ });
97
+ expect(returnedValue).toEqual(0);
98
+ });
99
+ it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
100
+ const apiClientMock = getApiClientMock({
101
+ data: {
102
+ appStoredEntity: {
103
+ value: ''
255
104
  }
256
105
  }
257
- }
258
- });
259
- const globalStorage = getStorage(apiClientMock);
260
- await globalStorage.delete('testKey');
261
- verifyApiClientCalledWith(apiClientMock, {
262
- input: {
106
+ });
107
+ const globalStorage = getStorage(apiClientMock);
108
+ const returnedValue = await globalStorage.get('testKey');
109
+ verifyApiClientCalledWith(apiClientMock, {
263
110
  contextAri,
264
111
  key: 'testKey',
265
112
  encrypted: false
266
- }
113
+ });
114
+ expect(returnedValue).toEqual('');
115
+ });
116
+ it('should throw an error with the returned status for non-200 status codes', async () => {
117
+ const apiClientMock = getApiClientMock(undefined, 400);
118
+ const globalStorage = getStorage(apiClientMock);
119
+ const response = globalStorage.get('testKey');
120
+ expect(apiClientMock).toHaveBeenCalled();
121
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
122
+ });
123
+ it('should throw an error with the returned error message for failed responses', async () => {
124
+ const apiClientMock = getApiClientMock({
125
+ errors: [INVALID_CURSOR_ERROR]
126
+ }, 200);
127
+ const globalStorage = getStorage(apiClientMock);
128
+ const response = globalStorage.get('testKey');
129
+ expect(apiClientMock).toHaveBeenCalled();
130
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
131
+ });
132
+ it('should throw an error if the response is not a valid JSON', async () => {
133
+ const apiClientMock = getApiClientMockInvalidJson('test', 200);
134
+ const globalStorage = getStorage(apiClientMock);
135
+ const response = globalStorage.get('testKey');
136
+ expect(apiClientMock).toHaveBeenCalled();
137
+ await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
138
+ });
139
+ });
140
+ describe('set', () => {
141
+ it('should call the storage API, passing the provided key and value', async () => {
142
+ const apiClientMock = getApiClientMock({
143
+ data: {
144
+ appStorage: {
145
+ setAppStoredEntity: {
146
+ success: true
147
+ }
148
+ }
149
+ }
150
+ });
151
+ const globalStorage = getStorage(apiClientMock);
152
+ await globalStorage.set('testKey', 'testValue');
153
+ verifyApiClientCalledWith(apiClientMock, {
154
+ input: {
155
+ contextAri,
156
+ key: 'testKey',
157
+ value: 'testValue',
158
+ encrypted: false
159
+ }
160
+ });
161
+ });
162
+ it('should throw an error if the storage API returns successful = false', async () => {
163
+ const apiClientMock = getApiClientMock({
164
+ data: {
165
+ appStorage: {
166
+ setAppStoredEntity: {
167
+ success: false,
168
+ errors: [INVALID_CURSOR_ERROR]
169
+ }
170
+ }
171
+ }
172
+ });
173
+ const globalStorage = getStorage(apiClientMock);
174
+ const response = globalStorage.set('testKey', 'testValue');
175
+ expect(apiClientMock).toHaveBeenCalled();
176
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
177
+ });
178
+ it('should throw an error if the storage API returns a non 200 status code', async () => {
179
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
180
+ const globalStorage = getStorage(apiClientMock);
181
+ const response = globalStorage.set('testKey', 'testValue');
182
+ expect(apiClientMock).toHaveBeenCalled();
183
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
184
+ });
185
+ it('should throw a 500 error if success=false but no errors were returned', async () => {
186
+ const apiClientMock = getApiClientMock({
187
+ data: {
188
+ appStorage: {
189
+ setAppStoredEntity: {
190
+ success: false
191
+ }
192
+ }
193
+ }
194
+ });
195
+ const globalStorage = getStorage(apiClientMock);
196
+ await expect(globalStorage.set('testKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
197
+ verifyApiClientCalledWith(apiClientMock, {
198
+ input: {
199
+ contextAri,
200
+ key: 'testKey',
201
+ value: 'testValue',
202
+ encrypted: false
203
+ }
204
+ });
267
205
  });
268
206
  });
269
- it('should throw an error if the storage API returns successful = false', async () => {
270
- const apiClientMock = getApiClientMock({
271
- data: {
272
- appStorage: {
273
- deleteAppStoredEntity: {
274
- success: false,
275
- errors: [INVALID_CURSOR_ERROR]
207
+ describe('delete', () => {
208
+ it('should call the storage API, passing the provided key', async () => {
209
+ const apiClientMock = getApiClientMock({
210
+ data: {
211
+ appStorage: {
212
+ deleteAppStoredEntity: {
213
+ success: true
214
+ }
276
215
  }
277
216
  }
278
- }
217
+ });
218
+ const globalStorage = getStorage(apiClientMock);
219
+ await globalStorage.delete('testKey');
220
+ verifyApiClientCalledWith(apiClientMock, {
221
+ input: {
222
+ contextAri,
223
+ key: 'testKey',
224
+ encrypted: false
225
+ }
226
+ });
227
+ });
228
+ it('should throw an error if the storage API returns successful = false', async () => {
229
+ const apiClientMock = getApiClientMock({
230
+ data: {
231
+ appStorage: {
232
+ deleteAppStoredEntity: {
233
+ success: false,
234
+ errors: [INVALID_CURSOR_ERROR]
235
+ }
236
+ }
237
+ }
238
+ });
239
+ const globalStorage = getStorage(apiClientMock);
240
+ const response = globalStorage.delete('testKey');
241
+ expect(apiClientMock).toHaveBeenCalled();
242
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
243
+ });
244
+ it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
245
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
246
+ const globalStorage = getStorage(apiClientMock);
247
+ const response = globalStorage.delete('testKey');
248
+ expect(apiClientMock).toHaveBeenCalled();
249
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
250
+ });
251
+ });
252
+ describe('bulkSet', () => {
253
+ it('should call the storage API for bulkSet and return savedKeys', async () => {
254
+ const apiClientMock = getApiClientMock({
255
+ data: {
256
+ appStorage: {
257
+ setAppStoredEntities: {
258
+ success: true,
259
+ savedKeys: ['testKey'],
260
+ failedKeys: []
261
+ }
262
+ }
263
+ }
264
+ });
265
+ const globalStorage = getStorage(apiClientMock);
266
+ await globalStorage.bulkSet([
267
+ {
268
+ key: 'testKey',
269
+ value: 'testValue'
270
+ }
271
+ ]);
272
+ verifyApiClientCalledWith(apiClientMock, {
273
+ input: {
274
+ contextAri,
275
+ entities: [
276
+ {
277
+ key: 'testKey',
278
+ value: 'testValue'
279
+ }
280
+ ],
281
+ encrypted: false
282
+ }
283
+ });
284
+ });
285
+ it('should throw an error if the storage API returns successful = false', async () => {
286
+ const apiClientMock = getApiClientMock({
287
+ data: {
288
+ appStorage: {
289
+ setAppStoredEntities: {
290
+ success: false,
291
+ errors: [INVALID_CURSOR_ERROR]
292
+ }
293
+ }
294
+ }
295
+ });
296
+ const globalStorage = getStorage(apiClientMock);
297
+ const response = globalStorage.bulkSet([
298
+ {
299
+ key: 'testKey',
300
+ value: 'testValue'
301
+ }
302
+ ]);
303
+ expect(apiClientMock).toHaveBeenCalled();
304
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
279
305
  });
280
- const globalStorage = getStorage(apiClientMock);
281
- const response = globalStorage.delete('testKey');
282
- expect(apiClientMock).toHaveBeenCalled();
283
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
284
- });
285
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
286
- const apiClientMock = getApiClientMockInvalidJson('', 400);
287
- const globalStorage = getStorage(apiClientMock);
288
- const response = globalStorage.delete('testKey');
289
- expect(apiClientMock).toHaveBeenCalled();
290
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
291
306
  });
292
307
  });
293
- describe('delete secret', () => {
294
- it('should call the storage API, passing the provided key', async () => {
295
- const apiClientMock = getApiClientMock({
296
- data: {
297
- appStorage: {
298
- deleteAppStoredEntity: {
299
- success: true
308
+ describe('Secret storage', () => {
309
+ describe('get secret', () => {
310
+ it('should call the storage API, passing the provided key and returning the stored value', async () => {
311
+ const apiClientMock = getApiClientMock({
312
+ data: {
313
+ appStoredEntity: {
314
+ value: 'testValue'
300
315
  }
301
316
  }
302
- }
303
- });
304
- const globalStorage = getStorage(apiClientMock);
305
- await globalStorage.deleteSecret('testKey');
306
- verifyApiClientCalledWith(apiClientMock, {
307
- input: {
317
+ });
318
+ const globalStorage = getStorage(apiClientMock);
319
+ const returnedValue = await globalStorage.getSecret('testKey');
320
+ verifyApiClientCalledWith(apiClientMock, {
308
321
  contextAri,
309
322
  key: 'testKey',
310
323
  encrypted: true
311
- }
312
- });
313
- });
314
- });
315
- describe('getEntity', () => {
316
- it('should call the storage API, passing the provided entity name and entity key and returning the stored value', async () => {
317
- const apiClientMock = getApiClientMock({
318
- data: {
319
- appStoredCustomEntity: {
320
- value: 'testValue'
324
+ });
325
+ expect(returnedValue).toEqual('testValue');
326
+ });
327
+ });
328
+ describe('set secret', () => {
329
+ it('should call the storage API, passing the provided key and value', async () => {
330
+ const apiClientMock = getApiClientMock({
331
+ data: {
332
+ appStorage: {
333
+ setAppStoredEntity: {
334
+ success: true
335
+ }
336
+ }
321
337
  }
322
- }
323
- });
324
- const globalStorage = getStorage(apiClientMock);
325
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
326
- verifyApiClientCalledWith(apiClientMock, {
327
- contextAri,
328
- entityName: 'testEntityName',
329
- key: 'testEntityKey'
330
- });
331
- expect(returnedValue).toEqual('testValue');
332
- });
333
- it('should call the storage API, passing the provided entity key and returning undefined if the key doesnt exist', async () => {
334
- const apiClientMock = getApiClientMock({
335
- data: {
336
- appStoredCustomEntity: {
337
- value: null
338
+ });
339
+ const globalStorage = getStorage(apiClientMock);
340
+ await globalStorage.setSecret('testKey', 'testValue');
341
+ verifyApiClientCalledWith(apiClientMock, {
342
+ input: {
343
+ contextAri,
344
+ key: 'testKey',
345
+ value: 'testValue',
346
+ encrypted: true
338
347
  }
339
- }
348
+ });
340
349
  });
341
- const globalStorage = getStorage(apiClientMock);
342
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
343
- verifyApiClientCalledWith(apiClientMock, {
344
- contextAri,
345
- entityName: 'testEntityName',
346
- key: 'testEntityKey'
347
- });
348
- expect(returnedValue).toEqual(undefined);
349
350
  });
350
- it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
351
- const apiClientMock = getApiClientMock({
352
- data: {
353
- appStoredCustomEntity: {
354
- value: 0
351
+ describe('delete secret', () => {
352
+ it('should call the storage API, passing the provided key', async () => {
353
+ const apiClientMock = getApiClientMock({
354
+ data: {
355
+ appStorage: {
356
+ deleteAppStoredEntity: {
357
+ success: true
358
+ }
359
+ }
355
360
  }
356
- }
357
- });
358
- const globalStorage = getStorage(apiClientMock);
359
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
360
- verifyApiClientCalledWith(apiClientMock, {
361
- contextAri,
362
- entityName: 'testEntityName',
363
- key: 'testEntityKey'
364
- });
365
- expect(returnedValue).toEqual(0);
366
- });
367
- it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
368
- const apiClientMock = getApiClientMock({
369
- data: {
370
- appStoredCustomEntity: {
371
- value: ''
361
+ });
362
+ const globalStorage = getStorage(apiClientMock);
363
+ await globalStorage.deleteSecret('testKey');
364
+ verifyApiClientCalledWith(apiClientMock, {
365
+ input: {
366
+ contextAri,
367
+ key: 'testKey',
368
+ encrypted: true
372
369
  }
373
- }
374
- });
375
- const globalStorage = getStorage(apiClientMock);
376
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
377
- verifyApiClientCalledWith(apiClientMock, {
378
- contextAri,
379
- entityName: 'testEntityName',
380
- key: 'testEntityKey'
370
+ });
381
371
  });
382
- expect(returnedValue).toEqual('');
383
- });
384
- it('should throw an error with the returned status for non-200 status codes', async () => {
385
- const apiClientMock = getApiClientMock(undefined, 400);
386
- const globalStorage = getStorage(apiClientMock);
387
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
388
- expect(apiClientMock).toHaveBeenCalled();
389
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
390
- });
391
- it('should throw an error with the returned error message for failed responses', async () => {
392
- const apiClientMock = getApiClientMock({
393
- errors: [INVALID_CURSOR_ERROR]
394
- }, 200);
395
- const globalStorage = getStorage(apiClientMock);
396
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
397
- expect(apiClientMock).toHaveBeenCalled();
398
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
399
- });
400
- it('should throw an error if the response is not a valid JSON', async () => {
401
- const apiClientMock = getApiClientMockInvalidJson('test', 200);
402
- const globalStorage = getStorage(apiClientMock);
403
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
404
- expect(apiClientMock).toHaveBeenCalled();
405
- await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
406
372
  });
407
373
  });
408
- describe('setEntity', () => {
409
- it('should call the storage API, passing the provided entity name, entity key and value', async () => {
410
- const apiClientMock = getApiClientMock({
411
- data: {
412
- appStorageCustomEntity: {
413
- setAppStoredCustomEntity: {
414
- success: true
374
+ describe('Custom entities', () => {
375
+ describe('getEntity', () => {
376
+ it('should call the storage API, passing the provided entity name and entity key and returning the stored value', async () => {
377
+ const apiClientMock = getApiClientMock({
378
+ data: {
379
+ appStoredCustomEntity: {
380
+ value: 'testValue'
415
381
  }
416
382
  }
417
- }
418
- });
419
- const globalStorage = getStorage(apiClientMock);
420
- await globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
421
- verifyApiClientCalledWith(apiClientMock, {
422
- input: {
383
+ });
384
+ const globalStorage = getStorage(apiClientMock);
385
+ const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
386
+ verifyApiClientCalledWith(apiClientMock, {
423
387
  contextAri,
424
388
  entityName: 'testEntityName',
425
- key: 'testEntityKey',
426
- value: 'testValue'
427
- }
428
- });
429
- });
430
- it('should throw an error if the storage API returns successful = false', async () => {
431
- const apiClientMock = getApiClientMock({
432
- data: {
433
- appStorageCustomEntity: {
434
- setAppStoredCustomEntity: {
435
- success: false,
436
- errors: [INVALID_CURSOR_ERROR]
389
+ key: 'testEntityKey'
390
+ });
391
+ expect(returnedValue).toEqual('testValue');
392
+ });
393
+ it('should call the storage API, passing the provided entity key and returning undefined if the key doesnt exist', async () => {
394
+ const apiClientMock = getApiClientMock({
395
+ data: {
396
+ appStoredCustomEntity: {
397
+ value: null
437
398
  }
438
399
  }
439
- }
440
- });
441
- const globalStorage = getStorage(apiClientMock);
442
- const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
443
- expect(apiClientMock).toHaveBeenCalled();
444
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
445
- });
446
- it('should throw an error if the storage API returns a non 200 status code', async () => {
447
- const apiClientMock = getApiClientMockInvalidJson('', 400);
448
- const globalStorage = getStorage(apiClientMock);
449
- const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
450
- expect(apiClientMock).toHaveBeenCalled();
451
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
452
- });
453
- it('should throw a 500 error if success=false but no errors were returned', async () => {
454
- const apiClientMock = getApiClientMock({
455
- data: {
456
- appStorageCustomEntity: {
457
- setAppStoredCustomEntity: {
458
- success: false
400
+ });
401
+ const globalStorage = getStorage(apiClientMock);
402
+ const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
403
+ verifyApiClientCalledWith(apiClientMock, {
404
+ contextAri,
405
+ entityName: 'testEntityName',
406
+ key: 'testEntityKey'
407
+ });
408
+ expect(returnedValue).toEqual(undefined);
409
+ });
410
+ it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
411
+ const apiClientMock = getApiClientMock({
412
+ data: {
413
+ appStoredCustomEntity: {
414
+ value: 0
459
415
  }
460
416
  }
461
- }
462
- });
463
- const globalStorage = getStorage(apiClientMock);
464
- await expect(globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
465
- verifyApiClientCalledWith(apiClientMock, {
466
- input: {
417
+ });
418
+ const globalStorage = getStorage(apiClientMock);
419
+ const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
420
+ verifyApiClientCalledWith(apiClientMock, {
467
421
  contextAri,
468
422
  entityName: 'testEntityName',
469
- key: 'testEntityKey',
470
- value: 'testValue'
471
- }
472
- });
473
- });
474
- });
475
- describe('deleteEntity', () => {
476
- it('should call the storage API, passing the provided entity name and key', async () => {
477
- const apiClientMock = getApiClientMock({
478
- data: {
479
- appStorageCustomEntity: {
480
- deleteAppStoredCustomEntity: {
481
- success: true
423
+ key: 'testEntityKey'
424
+ });
425
+ expect(returnedValue).toEqual(0);
426
+ });
427
+ it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
428
+ const apiClientMock = getApiClientMock({
429
+ data: {
430
+ appStoredCustomEntity: {
431
+ value: ''
482
432
  }
483
433
  }
484
- }
485
- });
486
- const globalStorage = getStorage(apiClientMock);
487
- await globalStorage.deleteEntity('testEntityName', 'testEntityKey');
488
- verifyApiClientCalledWith(apiClientMock, {
489
- input: {
434
+ });
435
+ const globalStorage = getStorage(apiClientMock);
436
+ const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
437
+ verifyApiClientCalledWith(apiClientMock, {
490
438
  contextAri,
491
439
  entityName: 'testEntityName',
492
440
  key: 'testEntityKey'
493
- }
494
- });
495
- });
496
- it('should throw an error if the storage API returns successful = false', async () => {
497
- const apiClientMock = getApiClientMock({
498
- data: {
499
- appStorageCustomEntity: {
500
- deleteAppStoredCustomEntity: {
501
- success: false,
502
- errors: [INVALID_CURSOR_ERROR]
441
+ });
442
+ expect(returnedValue).toEqual('');
443
+ });
444
+ it('should throw an error with the returned status for non-200 status codes', async () => {
445
+ const apiClientMock = getApiClientMock(undefined, 400);
446
+ const globalStorage = getStorage(apiClientMock);
447
+ const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
448
+ expect(apiClientMock).toHaveBeenCalled();
449
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
450
+ });
451
+ it('should throw an error with the returned error message for failed responses', async () => {
452
+ const apiClientMock = getApiClientMock({
453
+ errors: [INVALID_CURSOR_ERROR]
454
+ }, 200);
455
+ const globalStorage = getStorage(apiClientMock);
456
+ const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
457
+ expect(apiClientMock).toHaveBeenCalled();
458
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
459
+ });
460
+ it('should throw an error if the response is not a valid JSON', async () => {
461
+ const apiClientMock = getApiClientMockInvalidJson('test', 200);
462
+ const globalStorage = getStorage(apiClientMock);
463
+ const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
464
+ expect(apiClientMock).toHaveBeenCalled();
465
+ await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
466
+ });
467
+ });
468
+ describe('setEntity', () => {
469
+ it('should call the storage API, passing the provided entity name, entity key and value', async () => {
470
+ const apiClientMock = getApiClientMock({
471
+ data: {
472
+ appStorageCustomEntity: {
473
+ setAppStoredCustomEntity: {
474
+ success: true
475
+ }
503
476
  }
504
477
  }
505
- }
506
- });
507
- const globalStorage = getStorage(apiClientMock);
508
- const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
509
- expect(apiClientMock).toHaveBeenCalled();
510
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
511
- });
512
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
513
- const apiClientMock = getApiClientMockInvalidJson('', 400);
514
- const globalStorage = getStorage(apiClientMock);
515
- const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
516
- expect(apiClientMock).toHaveBeenCalled();
517
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
518
- });
519
- });
520
- describe('list', () => {
521
- it('should call the storage API with the provided parameters', async () => {
522
- const apiClientMock = getApiClientMock({
523
- data: {
524
- appStoredEntities: {
525
- edges: [
526
- { node: { key: 'key1', value: 'testValue' }, cursor: 'cursor1' },
527
- { node: { key: 'key2', value: 'testValue' }, cursor: 'cursor2' }
528
- ]
478
+ });
479
+ const globalStorage = getStorage(apiClientMock);
480
+ await globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
481
+ verifyApiClientCalledWith(apiClientMock, {
482
+ input: {
483
+ contextAri,
484
+ entityName: 'testEntityName',
485
+ key: 'testEntityKey',
486
+ value: 'testValue'
529
487
  }
530
- }
531
- });
532
- const globalStorage = getStorage(apiClientMock);
533
- const where = [
534
- {
535
- field: 'key',
536
- condition: 'STARTS_WITH',
537
- value: 'test'
538
- }
539
- ];
540
- const cursor = 'cursor';
541
- const limit = 10;
542
- const response = await globalStorage.list({ where, cursor, limit });
543
- verifyApiClientCalledWith(apiClientMock, {
544
- contextAri,
545
- where,
546
- cursor,
547
- limit
548
- }, gql_queries_1.UntypedQueries.listQuery(contextAri, {}).query);
549
- expect(response).toEqual(expect.objectContaining({
550
- results: [
551
- { key: 'key1', value: 'testValue' },
552
- { key: 'key2', value: 'testValue' }
553
- ],
554
- nextCursor: 'cursor2'
555
- }));
556
- });
557
- it('should query the appStoredEntitiesForCleanup endpoint given process.env.IS_CLEANUP_FUNCTION is set to true', async () => {
558
- process.env.IS_CLEANUP_FUNCTION = 'true';
559
- const apiClientMock = getApiClientMock({
560
- data: {
561
- appStoredEntitiesForCleanup: {
562
- edges: [
563
- { node: { key: 'key1', value: 'testValue' }, cursor: 'cursor1' },
564
- { node: { key: 'key2', value: 'testValue' }, cursor: 'cursor2' }
565
- ]
488
+ });
489
+ });
490
+ it('should throw an error if the storage API returns successful = false', async () => {
491
+ const apiClientMock = getApiClientMock({
492
+ data: {
493
+ appStorageCustomEntity: {
494
+ setAppStoredCustomEntity: {
495
+ success: false,
496
+ errors: [INVALID_CURSOR_ERROR]
497
+ }
498
+ }
566
499
  }
567
- }
568
- });
569
- const globalStorage = getStorage(apiClientMock);
570
- const where = [
571
- {
572
- field: 'key',
573
- condition: 'STARTS_WITH',
574
- value: 'test'
575
- }
576
- ];
577
- const cursor = 'cursor';
578
- const limit = 10;
579
- const response = await globalStorage.list({ where, cursor, limit });
580
- verifyApiClientCalledWith(apiClientMock, {
581
- contextAri,
582
- where,
583
- cursor,
584
- limit
585
- }, gql_queries_1.UntypedQueries.listQueryForCleanup(contextAri, {}).query);
586
- expect(response).toEqual(expect.objectContaining({
587
- results: [
588
- { key: 'key1', value: 'testValue' },
589
- { key: 'key2', value: 'testValue' }
590
- ],
591
- nextCursor: 'cursor2'
592
- }));
593
- process.env.IS_CLEANUP_FUNCTION = '';
594
- });
595
- it('should use default values', async () => {
596
- const apiClientMock = getApiClientMock({
597
- data: {
598
- appStoredEntities: {
599
- edges: []
500
+ });
501
+ const globalStorage = getStorage(apiClientMock);
502
+ const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
503
+ expect(apiClientMock).toHaveBeenCalled();
504
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
505
+ });
506
+ it('should throw an error if the storage API returns a non 200 status code', async () => {
507
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
508
+ const globalStorage = getStorage(apiClientMock);
509
+ const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
510
+ expect(apiClientMock).toHaveBeenCalled();
511
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
512
+ });
513
+ it('should throw a 500 error if success=false but no errors were returned', async () => {
514
+ const apiClientMock = getApiClientMock({
515
+ data: {
516
+ appStorageCustomEntity: {
517
+ setAppStoredCustomEntity: {
518
+ success: false
519
+ }
520
+ }
600
521
  }
601
- }
602
- });
603
- const globalStorage = getStorage(apiClientMock);
604
- await globalStorage.list({});
605
- verifyApiClientCalledWith(apiClientMock, {
606
- contextAri,
607
- where: null,
608
- cursor: null,
609
- limit: null
610
- }, gql_queries_1.UntypedQueries.listQuery(contextAri, {}).query);
611
- });
612
- it('should handle an empty result set', async () => {
613
- const apiClientMock = getApiClientMock({
614
- data: {
615
- appStoredEntities: {
616
- edges: []
522
+ });
523
+ const globalStorage = getStorage(apiClientMock);
524
+ await expect(globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
525
+ verifyApiClientCalledWith(apiClientMock, {
526
+ input: {
527
+ contextAri,
528
+ entityName: 'testEntityName',
529
+ key: 'testEntityKey',
530
+ value: 'testValue'
617
531
  }
618
- }
532
+ });
619
533
  });
620
- const globalStorage = getStorage(apiClientMock);
621
- const where = [
622
- {
623
- field: 'key',
624
- condition: 'STARTS_WITH',
625
- value: 'test'
626
- }
627
- ];
628
- const response = await globalStorage.list({ where });
629
- expect(response).toEqual(expect.objectContaining({
630
- results: [],
631
- nextCursor: undefined
632
- }));
633
534
  });
634
- it('should throw an error if the storage API returns an error', async () => {
635
- const apiClientMock = getApiClientMock({
636
- errors: [INVALID_CURSOR_ERROR]
535
+ describe('deleteEntity', () => {
536
+ it('should call the storage API, passing the provided entity name and key', async () => {
537
+ const apiClientMock = getApiClientMock({
538
+ data: {
539
+ appStorageCustomEntity: {
540
+ deleteAppStoredCustomEntity: {
541
+ success: true
542
+ }
543
+ }
544
+ }
545
+ });
546
+ const globalStorage = getStorage(apiClientMock);
547
+ await globalStorage.deleteEntity('testEntityName', 'testEntityKey');
548
+ verifyApiClientCalledWith(apiClientMock, {
549
+ input: {
550
+ contextAri,
551
+ entityName: 'testEntityName',
552
+ key: 'testEntityKey'
553
+ }
554
+ });
555
+ });
556
+ it('should throw an error if the storage API returns successful = false', async () => {
557
+ const apiClientMock = getApiClientMock({
558
+ data: {
559
+ appStorageCustomEntity: {
560
+ deleteAppStoredCustomEntity: {
561
+ success: false,
562
+ errors: [INVALID_CURSOR_ERROR]
563
+ }
564
+ }
565
+ }
566
+ });
567
+ const globalStorage = getStorage(apiClientMock);
568
+ const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
569
+ expect(apiClientMock).toHaveBeenCalled();
570
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
571
+ });
572
+ it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
573
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
574
+ const globalStorage = getStorage(apiClientMock);
575
+ const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
576
+ expect(apiClientMock).toHaveBeenCalled();
577
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
578
+ });
579
+ });
580
+ describe('list', () => {
581
+ it('should call the storage API with the provided parameters', async () => {
582
+ const apiClientMock = getApiClientMock({
583
+ data: {
584
+ appStoredEntities: {
585
+ edges: [
586
+ { node: { key: 'key1', value: 'testValue' }, cursor: 'cursor1' },
587
+ { node: { key: 'key2', value: 'testValue' }, cursor: 'cursor2' }
588
+ ]
589
+ }
590
+ }
591
+ });
592
+ const globalStorage = getStorage(apiClientMock);
593
+ const where = [
594
+ {
595
+ field: 'key',
596
+ condition: 'STARTS_WITH',
597
+ value: 'test'
598
+ }
599
+ ];
600
+ const cursor = 'cursor';
601
+ const limit = 10;
602
+ const response = await globalStorage.list({ where, cursor, limit });
603
+ verifyApiClientCalledWith(apiClientMock, {
604
+ contextAri,
605
+ where,
606
+ cursor,
607
+ limit
608
+ }, gql_queries_1.UntypedQueries.listQuery(contextAri, {}).query);
609
+ expect(response).toEqual(expect.objectContaining({
610
+ results: [
611
+ { key: 'key1', value: 'testValue' },
612
+ { key: 'key2', value: 'testValue' }
613
+ ],
614
+ nextCursor: 'cursor2'
615
+ }));
616
+ });
617
+ it('should query the appStoredEntitiesForCleanup endpoint given process.env.IS_CLEANUP_FUNCTION is set to true', async () => {
618
+ process.env.IS_CLEANUP_FUNCTION = 'true';
619
+ const apiClientMock = getApiClientMock({
620
+ data: {
621
+ appStoredEntitiesForCleanup: {
622
+ edges: [
623
+ { node: { key: 'key1', value: 'testValue' }, cursor: 'cursor1' },
624
+ { node: { key: 'key2', value: 'testValue' }, cursor: 'cursor2' }
625
+ ]
626
+ }
627
+ }
628
+ });
629
+ const globalStorage = getStorage(apiClientMock);
630
+ const where = [
631
+ {
632
+ field: 'key',
633
+ condition: 'STARTS_WITH',
634
+ value: 'test'
635
+ }
636
+ ];
637
+ const cursor = 'cursor';
638
+ const limit = 10;
639
+ const response = await globalStorage.list({ where, cursor, limit });
640
+ verifyApiClientCalledWith(apiClientMock, {
641
+ contextAri,
642
+ where,
643
+ cursor,
644
+ limit
645
+ }, gql_queries_1.UntypedQueries.listQueryForCleanup(contextAri, {}).query);
646
+ expect(response).toEqual(expect.objectContaining({
647
+ results: [
648
+ { key: 'key1', value: 'testValue' },
649
+ { key: 'key2', value: 'testValue' }
650
+ ],
651
+ nextCursor: 'cursor2'
652
+ }));
653
+ process.env.IS_CLEANUP_FUNCTION = '';
654
+ });
655
+ it('should use default values', async () => {
656
+ const apiClientMock = getApiClientMock({
657
+ data: {
658
+ appStoredEntities: {
659
+ edges: []
660
+ }
661
+ }
662
+ });
663
+ const globalStorage = getStorage(apiClientMock);
664
+ await globalStorage.list({});
665
+ verifyApiClientCalledWith(apiClientMock, {
666
+ contextAri,
667
+ where: null,
668
+ cursor: null,
669
+ limit: null
670
+ }, gql_queries_1.UntypedQueries.listQuery(contextAri, {}).query);
671
+ });
672
+ it('should handle an empty result set', async () => {
673
+ const apiClientMock = getApiClientMock({
674
+ data: {
675
+ appStoredEntities: {
676
+ edges: []
677
+ }
678
+ }
679
+ });
680
+ const globalStorage = getStorage(apiClientMock);
681
+ const where = [
682
+ {
683
+ field: 'key',
684
+ condition: 'STARTS_WITH',
685
+ value: 'test'
686
+ }
687
+ ];
688
+ const response = await globalStorage.list({ where });
689
+ expect(response).toEqual(expect.objectContaining({
690
+ results: [],
691
+ nextCursor: undefined
692
+ }));
693
+ });
694
+ it('should throw an error if the storage API returns an error', async () => {
695
+ const apiClientMock = getApiClientMock({
696
+ errors: [INVALID_CURSOR_ERROR]
697
+ });
698
+ const globalStorage = getStorage(apiClientMock);
699
+ const response = globalStorage.list({});
700
+ expect(apiClientMock).toHaveBeenCalled();
701
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
702
+ });
703
+ it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
704
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
705
+ const globalStorage = getStorage(apiClientMock);
706
+ const response = globalStorage.list({});
707
+ expect(apiClientMock).toHaveBeenCalled();
708
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
637
709
  });
638
- const globalStorage = getStorage(apiClientMock);
639
- const response = globalStorage.list({});
640
- expect(apiClientMock).toHaveBeenCalled();
641
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
642
- });
643
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
644
- const apiClientMock = getApiClientMockInvalidJson('', 400);
645
- const globalStorage = getStorage(apiClientMock);
646
- const response = globalStorage.list({});
647
- expect(apiClientMock).toHaveBeenCalled();
648
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
649
710
  });
650
711
  });
651
712
  describe('listCustomEntities', () => {