From b8f62b075dd6362ea05a3a628b17378f698b8918 Mon Sep 17 00:00:00 2001 From: William Valencia Date: Wed, 13 Nov 2024 17:39:00 -0500 Subject: [PATCH 1/4] CMR-10146: Making sure validBbox compares numbers and not strings --- src/middleware/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/middleware/index.ts b/src/middleware/index.ts index 760d9626..6f26b251 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -206,8 +206,8 @@ const validBbox = (bbox: string | number[]) => { validLat(swLat) && validLon(neLon) && validLat(neLat) && - swLat <= neLat && - swLon <= neLon + Number(swLat) <= Number(neLat) && + Number(swLon) <= Number(neLon) ); }; From 1a682dda0d96826bacfedaa1c427fc86bb5988c3 Mon Sep 17 00:00:00 2001 From: William Valencia Date: Wed, 13 Nov 2024 17:47:56 -0500 Subject: [PATCH 2/4] CMR-10146: Adding comments --- src/middleware/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/middleware/index.ts b/src/middleware/index.ts index 6f26b251..24301954 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -206,6 +206,7 @@ const validBbox = (bbox: string | number[]) => { validLat(swLat) && validLon(neLon) && validLat(neLat) && + // Ensure that number comparisons are used instead of string comparisons Number(swLat) <= Number(neLat) && Number(swLon) <= Number(neLon) ); From 59fe6dc4914a6d8cd5a1fe268212802249b2f87d Mon Sep 17 00:00:00 2001 From: William Valencia Date: Thu, 14 Nov 2024 14:48:02 -0500 Subject: [PATCH 3/4] CMR-10146: Adjusting the bbox types and adding tests --- src/middleware/__tests__/index.spec.ts | 35 ++++++++++++++++++++++++++ src/middleware/index.ts | 14 +++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/middleware/__tests__/index.spec.ts diff --git a/src/middleware/__tests__/index.spec.ts b/src/middleware/__tests__/index.spec.ts new file mode 100644 index 00000000..edd402a1 --- /dev/null +++ b/src/middleware/__tests__/index.spec.ts @@ -0,0 +1,35 @@ +import chai from "chai"; +const { expect } = chai; + +import { validBbox } from "../index"; + +describe("validBBOX", () => { + describe("when bbox is a string", () => { + it("returns a valid bbox", async () => { + const bbox = "-122.09,39.89,-122.03,39.92" + expect(validBbox(bbox)).to.equal(true); + }); + }); + + describe("when bbox is a string array", () => { + it("returns a valid bbox", async () => { + const bbox = ["-122.09","39.89","-122.03","39.92"] + expect(validBbox(bbox)).to.equal(true); + }); + }); + + describe("when bbox is an invalid string array with negative numbers", () => { + it("returns an invalid bbox", async () => { + const bbox = ["-122.03","39.89","-122.09","39.92"] + expect(validBbox(bbox)).to.equal(false); + }); + }); + + describe("when bbox is a number array", () => { + it("returns a valid bbox", async () => { + const bbox = [ -122.09, 39.89, -122.03, 39.92 ] + expect(validBbox(bbox)).to.equal(true); + }); + }); + +}); \ No newline at end of file diff --git a/src/middleware/index.ts b/src/middleware/index.ts index 24301954..dd7212f2 100644 --- a/src/middleware/index.ts +++ b/src/middleware/index.ts @@ -190,7 +190,10 @@ const validLat = (lat: number) => inclusiveBetween(lat, -90.0, 90.0); const validLon = (lon: number) => inclusiveBetween(lon, -180.0, 180.0); -const validBbox = (bbox: string | number[]) => { +// validBbox should be able to accept a comma separated string, a string array and +// a number array. We noticed the string array should be accepted when running the +// R Stac script. +export const validBbox = (bbox: string | string[] | number[]) => { const parsedBbox = typeof bbox === "string" ? parseOrdinateString(bbox) : bbox; if (parsedBbox.length !== 4 && parsedBbox.length !== 6) return false; @@ -201,11 +204,12 @@ const validBbox = (bbox: string | number[]) => { } else { [swLon, swLat, , neLon, neLat] = parsedBbox; } + return ( - validLon(swLon) && - validLat(swLat) && - validLon(neLon) && - validLat(neLat) && + validLon(Number(swLon)) && + validLat(Number(swLat)) && + validLon(Number(neLon)) && + validLat(Number(neLat)) && // Ensure that number comparisons are used instead of string comparisons Number(swLat) <= Number(neLat) && Number(swLon) <= Number(neLon) From f452115c9462599fba41c7eac3eaae9d9a54f86b Mon Sep 17 00:00:00 2001 From: William Valencia Date: Thu, 14 Nov 2024 14:53:50 -0500 Subject: [PATCH 4/4] CMR-10146: Making test prettier --- src/middleware/__tests__/index.spec.ts | 43 +++++++++++++------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/middleware/__tests__/index.spec.ts b/src/middleware/__tests__/index.spec.ts index edd402a1..d8d7e2f3 100644 --- a/src/middleware/__tests__/index.spec.ts +++ b/src/middleware/__tests__/index.spec.ts @@ -4,32 +4,31 @@ const { expect } = chai; import { validBbox } from "../index"; describe("validBBOX", () => { - describe("when bbox is a string", () => { - it("returns a valid bbox", async () => { - const bbox = "-122.09,39.89,-122.03,39.92" - expect(validBbox(bbox)).to.equal(true); - }); + describe("when bbox is a string", () => { + it("returns a valid bbox", async () => { + const bbox = "-122.09,39.89,-122.03,39.92"; + expect(validBbox(bbox)).to.equal(true); }); + }); - describe("when bbox is a string array", () => { - it("returns a valid bbox", async () => { - const bbox = ["-122.09","39.89","-122.03","39.92"] - expect(validBbox(bbox)).to.equal(true); - }); + describe("when bbox is a string array", () => { + it("returns a valid bbox", async () => { + const bbox = ["-122.09", "39.89", "-122.03", "39.92"]; + expect(validBbox(bbox)).to.equal(true); }); + }); - describe("when bbox is an invalid string array with negative numbers", () => { - it("returns an invalid bbox", async () => { - const bbox = ["-122.03","39.89","-122.09","39.92"] - expect(validBbox(bbox)).to.equal(false); - }); + describe("when bbox is an invalid string array with negative numbers", () => { + it("returns an invalid bbox", async () => { + const bbox = ["-122.03", "39.89", "-122.09", "39.92"]; + expect(validBbox(bbox)).to.equal(false); }); + }); - describe("when bbox is a number array", () => { - it("returns a valid bbox", async () => { - const bbox = [ -122.09, 39.89, -122.03, 39.92 ] - expect(validBbox(bbox)).to.equal(true); - }); + describe("when bbox is a number array", () => { + it("returns a valid bbox", async () => { + const bbox = [-122.09, 39.89, -122.03, 39.92]; + expect(validBbox(bbox)).to.equal(true); }); - -}); \ No newline at end of file + }); +});