saleor-app-sdk-REDIS_APL/src/handlers/next/validate-allow-saleor-urls.test.ts
Lukasz Ostrowski 67cded2e2a
Add optional URL protection to createRegisterHandler (#148)
* validateAllowSaleorUrls impl

* Implement in handler and add test

* update codeowners

* Apply suggestions from code review

Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>

* Rename param

Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2023-01-13 17:29:38 +01:00

42 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { validateAllowSaleorUrls } from "./validate-allow-saleor-urls";
const saleorCloudUrlMock = "https://my-shop.saleor.cloud/graphql/";
const onPremiseSaleorUrlMock = "https://my-shop-123.aws-services.com/graphql/";
const saleorCloudRegexValidator = (url: string) => /https:\/\/.*.saleor.cloud\/graphql\//.test(url);
describe("validateAllowSaleorUrls", () => {
it("Passes any URL if allow list is empty", () => {
expect(validateAllowSaleorUrls(saleorCloudUrlMock, [])).toBe(true);
expect(validateAllowSaleorUrls(onPremiseSaleorUrlMock, [])).toBe(true);
});
it("Passes only for URL that was exactly matched in provided allow list array", () => {
expect(validateAllowSaleorUrls(saleorCloudUrlMock, [saleorCloudUrlMock])).toBe(true);
expect(validateAllowSaleorUrls(onPremiseSaleorUrlMock, [saleorCloudUrlMock])).toBe(false);
});
it("Validates against custom function provided to allow list", () => {
expect(validateAllowSaleorUrls(saleorCloudUrlMock, [saleorCloudRegexValidator])).toBe(true);
expect(validateAllowSaleorUrls(onPremiseSaleorUrlMock, [saleorCloudRegexValidator])).toBe(
false
);
});
it("Validates against more than one argument in allow list", () => {
expect(
validateAllowSaleorUrls(saleorCloudUrlMock, [
saleorCloudRegexValidator,
onPremiseSaleorUrlMock,
])
).toBe(true);
expect(
validateAllowSaleorUrls(onPremiseSaleorUrlMock, [
saleorCloudRegexValidator,
onPremiseSaleorUrlMock,
])
).toBe(true);
});
});