From 5e957c5ae316a10a44b080ef5e2d2c5be7f7c439 Mon Sep 17 00:00:00 2001 From: Samantha Piatt Date: Thu, 18 Jan 2024 10:33:39 -0500 Subject: [PATCH] Lint fix and unit test. --- .../__tests__/handler-impl.test.js | 58 +++++++++++++++++-- .../open-data-scrape-handler/handler-impl.js | 8 +-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/main/solution/backend/src/lambdas/open-data-scrape-handler/__tests__/handler-impl.test.js b/main/solution/backend/src/lambdas/open-data-scrape-handler/__tests__/handler-impl.test.js index dbd2a0b108..7334656cb3 100644 --- a/main/solution/backend/src/lambdas/open-data-scrape-handler/__tests__/handler-impl.test.js +++ b/main/solution/backend/src/lambdas/open-data-scrape-handler/__tests__/handler-impl.test.js @@ -113,33 +113,83 @@ describe('fetchOpenData', () => { sha: 'abc2', }; + const whitelistStudy = { + name: 'Study 3', + description: 'Example study 3', + tags: ['other-tag'], + resources: [ + { + description: 'Description for Study 3', + arn: 'arn:aws:s3:::study3', + region: 'us-east-1', + type: 'S3 Bucket', + }, + ], + id: 'study-3', + sha: 'abc2', + }; + it('has invalid study (Invalid ARN)', async () => { const fileUrls = ['firstFileUrl']; const requiredTags = ['genetic']; + const studyWhitelist = []; const fetchFile = jest.fn(); fetchFile.mockReturnValueOnce(invalidStudy); - const result = await fetchOpenData({ fileUrls, requiredTags, log: consoleLogger, fetchFile }); + const result = await fetchOpenData({ fileUrls, requiredTags, studyWhitelist, log: consoleLogger, fetchFile }); expect(result).toEqual([]); }); it('has one valid study', async () => { const fileUrls = ['firstFileUrl']; const requiredTags = ['genetic']; + const studyWhitelist = []; const fetchFile = jest.fn(); fetchFile.mockReturnValueOnce(validStudy); - const result = await fetchOpenData({ fileUrls, requiredTags, log: consoleLogger, fetchFile }); + const result = await fetchOpenData({ fileUrls, requiredTags, studyWhitelist, log: consoleLogger, fetchFile }); expect(result).toEqual([validStudyOpenData]); }); it('has one valid study and one invalid study (Invalid ARN)', async () => { - const fileUrls = ['firstFileUrl']; + const fileUrls = ['firstFileUrl', 'fileUrl2']; const requiredTags = ['genetic']; + const studyWhitelist = []; const fetchFile = jest.fn(); fetchFile.mockReturnValueOnce(validStudy).mockReturnValueOnce(invalidStudy); - const result = await fetchOpenData({ fileUrls, requiredTags, log: consoleLogger, fetchFile }); + const result = await fetchOpenData({ fileUrls, requiredTags, studyWhitelist, log: consoleLogger, fetchFile }); expect(result).toEqual([validStudyOpenData]); }); + + it('has whitelisted study is included', async () => { + const fileUrls = ['firstFileUrl', 'fileUrl2', 'fileUrl3']; + const requiredTags = ['genetic']; + const studyWhitelist = ['study-3']; + const fetchFile = jest.fn(); + fetchFile + .mockReturnValueOnce(validStudy) + .mockReturnValueOnce(invalidStudy) + .mockReturnValueOnce(whitelistStudy); + + const result = await fetchOpenData({ fileUrls, requiredTags, studyWhitelist, log: consoleLogger, fetchFile }); + expect(result).toEqual([ + validStudyOpenData, + { + description: 'Example study 3', + id: 'study-3', + name: 'Study 3', + resources: [ + { + arn: 'arn:aws:s3:::study3', + description: 'Description for Study 3', + region: 'us-east-1', + type: 'S3 Bucket', + }, + ], + sha: 'abc2', + tags: ['other-tag'], + }, + ]); + }); }); diff --git a/main/solution/backend/src/lambdas/open-data-scrape-handler/handler-impl.js b/main/solution/backend/src/lambdas/open-data-scrape-handler/handler-impl.js index d6711c2bb3..3876abb240 100644 --- a/main/solution/backend/src/lambdas/open-data-scrape-handler/handler-impl.js +++ b/main/solution/backend/src/lambdas/open-data-scrape-handler/handler-impl.js @@ -203,10 +203,8 @@ const fetchOpenData = async ({ fileUrls, requiredTags, studyWhitelist, log, fetc const validS3Arn = new RegExp(/^arn:aws:s3:.*:.*:.+$/); const filtered = metadata.filter(({ id, tags, resources }) => { return ( - ( - studyWhitelist.includes(id) || - requiredTags.some(filterTag => tags.includes(filterTag)) - ) && resources.every(resource => { + (studyWhitelist.includes(id) || requiredTags.some(filterTag => tags.includes(filterTag))) && + resources.every(resource => { return resource.type === 'S3 Bucket' && validS3Arn.test(resource.arn); }) ); @@ -246,7 +244,7 @@ const fetchAndSaveOpenData = async (fetchDatasetFiles, scrape, log, fetchFile, b requiredTags: scrape.filterTags, studyWhitelist: scrape.studyWhitelist, log, - fetchFile + fetchFile, }); const simplifiedStudyData = openData.map(basicProjection);