-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: write tests for redact function
- Loading branch information
1 parent
ee4efa0
commit 915baed
Showing
2 changed files
with
216 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,113 @@ | ||
import { assertEquals } from "jsr:@std/assert"; | ||
import { redact, redacted } from "./redact.ts"; | ||
|
||
Deno.test("redact", async ({ step }) => { | ||
await step("redacts string literal from a shallow array", () => { | ||
const data = ["foo", "bar", "baz"]; | ||
redact(data, { values: ["bar"] }); | ||
assertEquals(data, ["foo", redacted, "baz"]); | ||
// deno-lint-ignore-file no-explicit-any | ||
import { assertEquals, assertStrictEquals } from "jsr:@std/assert"; | ||
|
||
import { redact, redacted, secret } from "./redact.ts"; | ||
|
||
Deno.test("redacts string literal from a shallow array", () => { | ||
const data = ["foo", "bar", "baz"]; | ||
assertEquals(redact(data, { values: ["bar"] }), ["foo", redacted, "baz"]); | ||
assertEquals(data, ["foo", redacted, "baz"]); | ||
}); | ||
|
||
Deno.test("redacts string match from a shallow array", () => { | ||
const data = ["foobarbaz"]; | ||
assertEquals(redact(data, { values: ["bar"] }), ["fooredactedbaz"]); | ||
assertEquals(data, ["fooredactedbaz"]); | ||
}); | ||
|
||
Deno.test("redacts regexp match from a shallow array", () => { | ||
const data = ["foo", "bar", "baz"]; | ||
assertEquals(redact(data, { values: [/bar/] }), ["foo", redacted, "baz"]); | ||
assertEquals(data, ["foo", redacted, "baz"]); | ||
}); | ||
|
||
Deno.test("redacts partial regexp match from a shallow array", () => { | ||
const data = ["foo", "barstool", "baz"]; | ||
assertEquals(redact(data, { values: [/bar/] }), [ | ||
"foo", | ||
"redactedstool", | ||
"baz", | ||
]); | ||
assertEquals(data, ["foo", "redactedstool", "baz"]); | ||
}); | ||
|
||
Deno.test("redacts partial string match from a shallow array", () => { | ||
const data = ["foo", "barstool", "baz"]; | ||
assertEquals(redact(data, { values: ["bar"] }), [ | ||
"foo", | ||
"redactedstool", | ||
"baz", | ||
]); | ||
assertEquals(data, ["foo", "redactedstool", "baz"]); | ||
}); | ||
|
||
Deno.test("secret marker", async ({ step }) => { | ||
await step("redacts and clears array when marker is found", () => { | ||
const data: any = ["foo", "bar", "baz"]; | ||
Object.defineProperty(data, secret, {}); | ||
assertStrictEquals(redact(data), redacted); | ||
assertEquals(data, []); | ||
}); | ||
|
||
await step("redacts nested array when marker is found", () => { | ||
const data: any = ["foo", "bar", "baz", ["foo", "bar", "baz"]]; | ||
Object.defineProperty(data[3], secret, {}); | ||
assertEquals(redact(data), ["foo", "bar", "baz", redacted]); | ||
assertEquals(data, ["foo", "bar", "baz", redacted]); | ||
}); | ||
|
||
await step("redacts string match from a shallow array", () => { | ||
const data = ["foobarbaz"]; | ||
redact(data, { values: ["bar"] }); | ||
assertEquals(data, ["fooredactedbaz"]); | ||
await step("redacts and clears object when marker is found", () => { | ||
const data: any = { foo: "bar" }; | ||
Object.defineProperty(data, secret, {}); | ||
assertStrictEquals(redact(data), redacted); | ||
assertEquals(data, {}); | ||
}); | ||
|
||
await step("redacts regexp match from a shallow array", () => { | ||
const data = ["foo", "bar", "baz"]; | ||
redact(data, { values: [/bar/] }); | ||
assertEquals(data, ["foo", redacted, "baz"]); | ||
await step("redacts nested object when marker is found", () => { | ||
const data: any = { foo: "bar", baz: { foo: "bar" } }; | ||
Object.defineProperty(data.baz, secret, {}); | ||
assertEquals(redact(data), { foo: "bar", baz: redacted }); | ||
assertEquals(data, { foo: "bar", baz: redacted }); | ||
}); | ||
}); | ||
|
||
await step("redacts partial regexp match from a shallow array", () => { | ||
const data = ["foo", "barstool", "baz"]; | ||
redact(data, { values: [/bar/] }); | ||
assertEquals(data, ["foo", "redactedstool", "baz"]); | ||
Deno.test("redacts string property from object", () => { | ||
const data: any = { password: "this is secret" }; | ||
assertEquals(redact(data, { properties: ["password"] }), { | ||
password: redacted, | ||
}); | ||
assertEquals(data, { password: redacted }); | ||
}); | ||
|
||
Deno.test("redacts symbol property from object", () => { | ||
const data: any = { [Symbol.for("foo")]: "bar" }; | ||
assertEquals(redact(data, { properties: [Symbol.for("foo")] }), { | ||
[Symbol.for("foo")]: redacted, | ||
}); | ||
assertEquals(data, { [Symbol.for("foo")]: redacted }); | ||
}); | ||
|
||
Deno.test("redacts number property from object", () => { | ||
const data: any = { [0]: "bar" }; | ||
assertEquals(redact(data, { properties: [0] }), { [0]: redacted }); | ||
assertEquals(data, { [0]: redacted }); | ||
}); | ||
|
||
Deno.test("redacts symbol property from array", () => { | ||
const data: any = []; | ||
data[Symbol.for("foo")] = "bar"; | ||
redact(data, { properties: [Symbol.for("foo")] }); | ||
assertEquals(data[Symbol.for("foo")], redacted); | ||
}); | ||
|
||
Deno.test("redacts number property from array", () => { | ||
const data: any = ["foo", "bar"]; | ||
assertEquals(redact(data, { properties: [0] }), [redacted, "bar"]); | ||
assertEquals(data, [redacted, "bar"]); | ||
}); | ||
|
||
Deno.test("redacts whole line regexp match", () => { | ||
const data = ["foo", "bar", "baz"]; | ||
assertEquals(redact(data, { values: [/^bar$/] }), ["foo", redacted, "baz"]); | ||
assertEquals(data, ["foo", redacted, "baz"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters