From 77dfe8582a2752fd1ebc3e6d98a8f673b441b8a2 Mon Sep 17 00:00:00 2001 From: Wan <495709+wa0x6e@users.noreply.github.com> Date: Tue, 3 Oct 2023 01:20:44 +0900 Subject: [PATCH] chore: make cache optional (#193) * fix: do not process if cache is not configured * fix: fix test to not fail if cache credentials are missing --- src/aws.ts | 1 + test/e2e/proxy.test.ts | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/aws.ts b/src/aws.ts index a419989..924d4b0 100644 --- a/src/aws.ts +++ b/src/aws.ts @@ -46,6 +46,7 @@ export async function get(key) { } export async function remove(key) { + if (!client) return false; try { return await client.deleteObject({ Bucket: process.env.AWS_BUCKET_NAME, diff --git a/test/e2e/proxy.test.ts b/test/e2e/proxy.test.ts index 387323a..06837bf 100644 --- a/test/e2e/proxy.test.ts +++ b/test/e2e/proxy.test.ts @@ -14,28 +14,36 @@ describe('GET /ipfs/:cid', () => { }); describe('when the file is cached', () => { - const cachedContent = { status: 'CACHED' }; + if (process.env.AWS_REGION) { + const cachedContent = { status: 'CACHED' }; - it('returns the cache file', async () => { - await set(cid, cachedContent); - const response = await request(HOST).get(path); + it('returns the cache file', async () => { + await set(cid, cachedContent); + const response = await request(HOST).get(path); - expect(response.body).toEqual(cachedContent); - expect(response.statusCode).toBe(200); - expect(response.headers['content-type']).toBe('application/json; charset=utf-8'); - expect(await get(cid)).toEqual(cachedContent); - }); + expect(response.body).toEqual(cachedContent); + expect(response.statusCode).toBe(200); + expect(response.headers['content-type']).toBe('application/json; charset=utf-8'); + expect(await get(cid)).toEqual(cachedContent); + }); + } else { + it.todo('needs to set AWS credentials to test the cache'); + } }); describe('when the file is not cached', () => { - it('returns the file and caches it', async () => { - const response = await request(HOST).get(path); - - expect(response.body).toEqual(content); - expect(response.statusCode).toBe(200); - expect(response.headers['content-type']).toBe('application/json; charset=utf-8'); - expect(await get(cid)).toEqual(response.body); - }); + if (process.env.AWS_REGION) { + it('returns the file and caches it', async () => { + const response = await request(HOST).get(path); + + expect(response.body).toEqual(content); + expect(response.statusCode).toBe(200); + expect(response.headers['content-type']).toBe('application/json; charset=utf-8'); + expect(await get(cid)).toEqual(response.body); + }); + } else { + it.todo('needs to set AWS credentials to test the cache'); + } }); it('returns a 415 error when not a JSON file', async () => {