Skip to content

Commit eb635fd

Browse files
committed
chore: add tests for context credentials
1 parent 2fa2a31 commit eb635fd

File tree

1 file changed

+230
-1
lines changed

1 file changed

+230
-1
lines changed

src/main.test.ts

Lines changed: 230 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const key = '54321'
2929
const value = 'some value'
3030
const apiToken = 'some token'
3131
const signedURL = 'https://signed.url/123456789'
32+
const edgeToken = 'some other token'
33+
const edgeURL = 'https://cloudfront.url'
3234

3335
describe('get', () => {
3436
test('Reads from the blob store using API credentials', async () => {
@@ -69,6 +71,37 @@ describe('get', () => {
6971
expect(store.fulfilled).toBeTruthy()
7072
})
7173

74+
test('Reads from the blob store using context credentials', async () => {
75+
const store = new MockFetch()
76+
.get({
77+
headers: { authorization: `Bearer ${edgeToken}` },
78+
response: new Response(value),
79+
url: `${edgeURL}/${siteID}/production/${key}`,
80+
})
81+
.get({
82+
headers: { authorization: `Bearer ${edgeToken}` },
83+
response: new Response(value),
84+
url: `${edgeURL}/${siteID}/production/${key}`,
85+
})
86+
87+
const blobs = new Blobs({
88+
authentication: {
89+
contextURL: edgeURL,
90+
token: edgeToken,
91+
},
92+
fetcher: store.fetcher,
93+
siteID,
94+
})
95+
96+
const string = await blobs.get(key)
97+
expect(string).toBe(value)
98+
99+
const stream = await blobs.get(key, { type: 'stream' })
100+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
101+
102+
expect(store.fulfilled).toBeTruthy()
103+
})
104+
72105
test('Returns `null` when the pre-signed URL returns a 404', async () => {
73106
const store = new MockFetch()
74107
.get({
@@ -93,6 +126,26 @@ describe('get', () => {
93126
expect(store.fulfilled).toBeTruthy()
94127
})
95128

129+
test('Returns `null` when the edge URL returns a 404', async () => {
130+
const store = new MockFetch().get({
131+
headers: { authorization: `Bearer ${edgeToken}` },
132+
response: new Response(null, { status: 404 }),
133+
url: `${edgeURL}/${siteID}/production/${key}`,
134+
})
135+
136+
const blobs = new Blobs({
137+
authentication: {
138+
contextURL: edgeURL,
139+
token: edgeToken,
140+
},
141+
fetcher: store.fetcher,
142+
siteID,
143+
})
144+
145+
expect(await blobs.get(key)).toBeNull()
146+
expect(store.fulfilled).toBeTruthy()
147+
})
148+
96149
test('Throws when the API returns a non-200 status code', async () => {
97150
const store = new MockFetch().get({
98151
headers: { authorization: `Bearer ${apiToken}` },
@@ -141,6 +194,29 @@ describe('get', () => {
141194
expect(store.fulfilled).toBeTruthy()
142195
})
143196

197+
test('Throws when an edge URL returns a non-200 status code', async () => {
198+
const store = new MockFetch().get({
199+
headers: { authorization: `Bearer ${edgeToken}` },
200+
response: new Response(null, { status: 401 }),
201+
url: `${edgeURL}/${siteID}/production/${key}`,
202+
})
203+
204+
const blobs = new Blobs({
205+
authentication: {
206+
contextURL: edgeURL,
207+
token: edgeToken,
208+
},
209+
fetcher: store.fetcher,
210+
siteID,
211+
})
212+
213+
await expect(async () => await blobs.get(key)).rejects.toThrowError(
214+
'get operation has failed: store returned a 401 response',
215+
)
216+
217+
expect(store.fulfilled).toBeTruthy()
218+
})
219+
144220
test('Returns `null` when the blob entry contains an expiry date in the past', async () => {
145221
const store = new MockFetch()
146222
.get({
@@ -225,6 +301,28 @@ describe('set', () => {
225301
expect(store.fulfilled).toBeTruthy()
226302
})
227303

304+
test('Writes to the blob store using context credentials', async () => {
305+
const store = new MockFetch().put({
306+
body: value,
307+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
308+
response: new Response(null),
309+
url: `${edgeURL}/${siteID}/production/${key}`,
310+
})
311+
312+
const blobs = new Blobs({
313+
authentication: {
314+
contextURL: edgeURL,
315+
token: edgeToken,
316+
},
317+
fetcher: store.fetcher,
318+
siteID,
319+
})
320+
321+
await blobs.set(key, value)
322+
323+
expect(store.fulfilled).toBeTruthy()
324+
})
325+
228326
test('Accepts a TTL parameter', async () => {
229327
const ttl = new Date(Date.now() + 15_000)
230328
const store = new MockFetch()
@@ -398,6 +496,30 @@ describe('set', () => {
398496
expect(store.fulfilled).toBeTruthy()
399497
})
400498

499+
test('Throws when the edge URL returns a non-200 status code', async () => {
500+
const store = new MockFetch().put({
501+
body: value,
502+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
503+
response: new Response(null, { status: 401 }),
504+
url: `${edgeURL}/${siteID}/production/${key}`,
505+
})
506+
507+
const blobs = new Blobs({
508+
authentication: {
509+
contextURL: edgeURL,
510+
token: edgeToken,
511+
},
512+
fetcher: store.fetcher,
513+
siteID,
514+
})
515+
516+
await expect(async () => await blobs.set(key, value)).rejects.toThrowError(
517+
'put operation has failed: store returned a 401 response',
518+
)
519+
520+
expect(store.fulfilled).toBeTruthy()
521+
})
522+
401523
test('Throws when the instance is missing required configuration properties', async () => {
402524
const { fetcher } = new MockFetch()
403525

@@ -425,7 +547,7 @@ describe('set', () => {
425547
)
426548
})
427549

428-
test('Retries failed operations', async () => {
550+
test('Retries failed operations when using API credentials', async () => {
429551
const store = new MockFetch()
430552
.put({
431553
headers: { authorization: `Bearer ${apiToken}` },
@@ -477,6 +599,47 @@ describe('set', () => {
477599

478600
expect(store.fulfilled).toBeTruthy()
479601
})
602+
603+
test('Retries failed operations when using context credentials', async () => {
604+
const store = new MockFetch()
605+
.put({
606+
body: value,
607+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
608+
response: new Response(null, { status: 500 }),
609+
url: `${edgeURL}/${siteID}/production/${key}`,
610+
})
611+
.put({
612+
body: value,
613+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
614+
response: new Error('Some network problem'),
615+
url: `${edgeURL}/${siteID}/production/${key}`,
616+
})
617+
.put({
618+
body: value,
619+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
620+
response: new Response(null, { headers: { 'X-RateLimit-Reset': '10' }, status: 429 }),
621+
url: `${edgeURL}/${siteID}/production/${key}`,
622+
})
623+
.put({
624+
body: value,
625+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
626+
response: new Response(null),
627+
url: `${edgeURL}/${siteID}/production/${key}`,
628+
})
629+
630+
const blobs = new Blobs({
631+
authentication: {
632+
contextURL: edgeURL,
633+
token: edgeToken,
634+
},
635+
fetcher: store.fetcher,
636+
siteID,
637+
})
638+
639+
await blobs.set(key, value)
640+
641+
expect(store.fulfilled).toBeTruthy()
642+
})
480643
})
481644

482645
describe('setJSON', () => {
@@ -509,6 +672,28 @@ describe('setJSON', () => {
509672
expect(store.fulfilled).toBeTruthy()
510673
})
511674

675+
test('Writes to the blob store using context credentials', async () => {
676+
const store = new MockFetch().put({
677+
body: JSON.stringify({ value }),
678+
headers: { authorization: `Bearer ${edgeToken}`, 'cache-control': 'max-age=0, stale-while-revalidate=60' },
679+
response: new Response(null),
680+
url: `${edgeURL}/${siteID}/production/${key}`,
681+
})
682+
683+
const blobs = new Blobs({
684+
authentication: {
685+
contextURL: edgeURL,
686+
token: edgeToken,
687+
},
688+
fetcher: store.fetcher,
689+
siteID,
690+
})
691+
692+
await blobs.setJSON(key, { value })
693+
694+
expect(store.fulfilled).toBeTruthy()
695+
})
696+
512697
test('Accepts a TTL parameter', async () => {
513698
const ttl = new Date(Date.now() + 15_000)
514699
const store = new MockFetch()
@@ -567,6 +752,27 @@ describe('delete', () => {
567752
expect(store.fulfilled).toBeTruthy()
568753
})
569754

755+
test('Deletes from the blob store using context credentials', async () => {
756+
const store = new MockFetch().delete({
757+
headers: { authorization: `Bearer ${edgeToken}` },
758+
response: new Response(null),
759+
url: `${edgeURL}/${siteID}/production/${key}`,
760+
})
761+
762+
const blobs = new Blobs({
763+
authentication: {
764+
contextURL: edgeURL,
765+
token: edgeToken,
766+
},
767+
fetcher: store.fetcher,
768+
siteID,
769+
})
770+
771+
await blobs.delete(key)
772+
773+
expect(store.fulfilled).toBeTruthy()
774+
})
775+
570776
test('Throws when the API returns a non-200 status code', async () => {
571777
const store = new MockFetch().delete({
572778
headers: { authorization: `Bearer ${apiToken}` },
@@ -588,6 +794,29 @@ describe('delete', () => {
588794
expect(store.fulfilled).toBeTruthy()
589795
})
590796

797+
test('Throws when the edge URL returns a non-200 status code', async () => {
798+
const store = new MockFetch().delete({
799+
headers: { authorization: `Bearer ${edgeToken}` },
800+
response: new Response(null, { status: 401 }),
801+
url: `${edgeURL}/${siteID}/production/${key}`,
802+
})
803+
804+
const blobs = new Blobs({
805+
authentication: {
806+
contextURL: edgeURL,
807+
token: edgeToken,
808+
},
809+
fetcher: store.fetcher,
810+
siteID,
811+
})
812+
813+
await expect(async () => await blobs.delete(key)).rejects.toThrowError(
814+
'delete operation has failed: store returned a 401 response',
815+
)
816+
817+
expect(store.fulfilled).toBeTruthy()
818+
})
819+
591820
test('Throws when the instance is missing required configuration properties', async () => {
592821
const { fetcher } = new MockFetch()
593822

0 commit comments

Comments
 (0)