Skip to content

Commit

Permalink
fix(core-webhooks): cast params in condition checks (#2887)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored and faustbrian committed Aug 23, 2019
1 parent a201d64 commit 1016bd5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
72 changes: 72 additions & 0 deletions __tests__/unit/core-webhooks/conditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe("Conditions - between", () => {
max: 2,
}),
).toBeTrue();
expect(
between("1.5", {
min: "1",
max: "2",
}),
).toBeTrue();
});

it("should be false", () => {
Expand All @@ -31,6 +37,12 @@ describe("Conditions - between", () => {
max: 2,
}),
).toBeFalse();
expect(
between("3", {
min: "1",
max: "2",
}),
).toBeFalse();
});
});

Expand All @@ -47,72 +59,116 @@ describe("Conditions - contains", () => {
describe("Conditions - equal", () => {
it("should be true", () => {
expect(eq(1, 1)).toBeTrue();
expect(eq("1", "1")).toBeTrue();
});

it("should be false", () => {
expect(eq(1, 2)).toBeFalse();
expect(eq("1", "2")).toBeFalse();
});
});

describe("Conditions - falsy", () => {
it("should be true", () => {
expect(falsy(false)).toBeTrue();
expect(falsy("false")).toBeTrue();
expect(falsy("FaLsE")).toBeTrue();
});

it("should be false", () => {
expect(falsy(true)).toBeFalse();
expect(falsy("true")).toBeFalse();
expect(falsy("TrUe")).toBeFalse();
});
});

describe("Conditions - greater than", () => {
it("should be true", () => {
expect(gt(2, 1)).toBeTrue();
expect(gt("2", "1")).toBeTrue();
expect(gt("10", "2")).toBeTrue();
});

it("should be false", () => {
expect(gt(1, 2)).toBeFalse();
expect(gt("1", "2")).toBeFalse();
expect(gt("2", "10")).toBeFalse();
expect(gt(undefined, NaN)).toBeFalse();
expect(gt(1, NaN)).toBeFalse();
expect(gt(undefined, 1)).toBeFalse();
expect(gt("null", "NaN")).toBeFalse();
expect(gt("1", "NaN")).toBeFalse();
expect(gt("null", "1")).toBeFalse();
});
});

describe("Conditions - greater than or equal", () => {
it("should be true", () => {
expect(gte(2, 1)).toBeTrue();
expect(gte(2, 2)).toBeTrue();
expect(gte("2", "1")).toBeTrue();
expect(gte("2", "2")).toBeTrue();
});

it("should be false", () => {
expect(gte(1, 2)).toBeFalse();
expect(gte("1", "2")).toBeFalse();
expect(gt(undefined, NaN)).toBeFalse();
expect(gt(1, NaN)).toBeFalse();
expect(gt(undefined, 1)).toBeFalse();
expect(gt("null", "NaN")).toBeFalse();
expect(gt("1", "NaN")).toBeFalse();
expect(gt("null", "1")).toBeFalse();
});
});

describe("Conditions - less than", () => {
it("should be true", () => {
expect(lt(1, 2)).toBeTrue();
expect(lt("1", "2")).toBeTrue();
});

it("should be false", () => {
expect(lt(2, 1)).toBeFalse();
expect(lt("2", "1")).toBeFalse();
expect(gt(undefined, NaN)).toBeFalse();
expect(gt(1, NaN)).toBeFalse();
expect(gt(undefined, 1)).toBeFalse();
expect(gt("null", "NaN")).toBeFalse();
expect(gt("1", "NaN")).toBeFalse();
expect(gt("null", "1")).toBeFalse();
});
});

describe("Conditions - less than or equal", () => {
it("should be true", () => {
expect(lte(1, 2)).toBeTrue();
expect(lte(1, 1)).toBeTrue();
expect(lte("1", "2")).toBeTrue();
expect(lte("1", "1")).toBeTrue();
});

it("should be false", () => {
expect(lte(2, 1)).toBeFalse();
expect(lte("2", "1")).toBeFalse();
expect(gt(undefined, NaN)).toBeFalse();
expect(gt(1, NaN)).toBeFalse();
expect(gt(undefined, 1)).toBeFalse();
expect(gt("null", "NaN")).toBeFalse();
expect(gt("1", "NaN")).toBeFalse();
expect(gt("null", "1")).toBeFalse();
});
});

describe("Conditions - not equal", () => {
it("should be true", () => {
expect(ne(1, 2)).toBeTrue();
expect(ne("1", "2")).toBeTrue();
});

it("should be false", () => {
expect(ne(1, 1)).toBeFalse();
expect(ne("1", "1")).toBeFalse();
});
});

Expand All @@ -124,6 +180,12 @@ describe("Conditions - not-between", () => {
max: 2,
}),
).toBeTrue();
expect(
notBetween("3", {
min: "1",
max: "2",
}),
).toBeTrue();
});

it("should be false", () => {
Expand All @@ -133,6 +195,12 @@ describe("Conditions - not-between", () => {
max: 2,
}),
).toBeFalse();
expect(
notBetween("1.5", {
min: "1",
max: "2",
}),
).toBeFalse();
});
});

Expand All @@ -149,9 +217,13 @@ describe("Conditions - regexp", () => {
describe("Conditions - truthy", () => {
it("should be true", () => {
expect(truthy(true)).toBeTrue();
expect(truthy("true")).toBeTrue();
expect(truthy("TrUe")).toBeTrue();
});

it("should be false", () => {
expect(truthy(false)).toBeFalse();
expect(truthy("false")).toBeFalse();
expect(truthy("FaLsE")).toBeFalse();
});
});
22 changes: 13 additions & 9 deletions packages/core-webhooks/src/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export const between = (actual, expected): boolean => actual > expected.min && actual < expected.max;
import { Utils } from "@arkecosystem/crypto";

const toBoolean = (value): boolean => value.toString().toLowerCase().trim() === "true" ? true : false;

export const between = (actual, expected): boolean => gt(actual, expected.min) && lt(actual, expected.max);
export const contains = (actual, expected): boolean => actual.includes(expected);
export const eq = (actual, expected): boolean => actual === expected;
export const falsy = (actual): boolean => actual === false;
export const gt = (actual, expected): boolean => actual > expected;
export const gte = (actual, expected): boolean => actual >= expected;
export const lt = (actual, expected): boolean => actual < expected;
export const lte = (actual, expected): boolean => actual <= expected;
export const ne = (actual, expected): boolean => actual !== expected;
export const eq = (actual, expected): boolean => JSON.stringify(actual) === JSON.stringify(expected);
export const falsy = (actual): boolean => actual === false || !toBoolean(actual);
export const gt = (actual, expected): boolean => Utils.BigNumber.make(actual).gt(expected);
export const gte = (actual, expected): boolean => Utils.BigNumber.make(actual).gte(expected);
export const lt = (actual, expected): boolean => Utils.BigNumber.make(actual).lt(expected);
export const lte = (actual, expected): boolean => Utils.BigNumber.make(actual).lte(expected);
export const ne = (actual, expected): boolean => !eq(actual, expected);
export const notBetween = (actual, expected): boolean => !between(actual, expected);
export const regexp = (actual, expected): boolean => new RegExp(expected).test(actual);
export const truthy = (actual): boolean => actual === true;
export const truthy = (actual): boolean => actual === true || toBoolean(actual);

0 comments on commit 1016bd5

Please sign in to comment.