backend/test/utils/sanitizer.utils.test.ts
Fred Boniface 2df7c4b068 Run Prettier
Signed-off-by: Fred Boniface <fred@fjla.uk>
2023-09-28 21:48:27 +01:00

63 lines
2.0 KiB
TypeScript

import { getDomainFromEmail } from "../../src/utils/sanitizer.utils";
import { removeNonNumeric } from "../../src/utils/sanitizer.utils";
import { removeNonAlpha } from "../../src/utils/sanitizer.utils";
import { removeNonAlphanumeric } from "../../src/utils/sanitizer.utils";
describe("Sanitize Email", () => {
const inputs = [
"this+is+an-_email@example.com",
'"unusual email"@example.com',
"(brackets)addr@example.com",
"I%Have{Special}%Characters@example.com",
"Basic.address@example.com",
`"very.(),:;<>[]\".VERY.\"very\ \"very\".unusual"@example.com`,
"THIS_EMAIL_CONTAINS_CAPITALISED_DOMAIN@EXAMPLE.COM",
];
const expectedOutput = "example.com";
for (const addr of inputs) {
test(`Should return only domain: ${addr}`, () => {
expect(getDomainFromEmail(addr)).toEqual(expectedOutput);
});
}
});
describe("Remove non-numeric", () => {
const inputs = ["abc123", "<%43)($£@:}jfkd4"];
const expectedOutputs = ["123", "434"];
for (const key in inputs) {
const input = inputs[key];
const desired = expectedOutputs[key];
test(`Should return only numbers: ${key}`, () => {
expect(removeNonNumeric(input)).toEqual(desired);
});
}
});
describe("Remove non-Alpha", () => {
const inputs = ["DROP/*comment*/sampletable", "10; DROP TABLE members /*"];
const outputs = ["DROPcommentsampletable", "DROPTABLEmembers"];
for (const key in inputs) {
const input = inputs[key];
const desired = outputs[key];
test(`Should return with only letters: ${key}`, () => {
expect(removeNonAlpha(input)).toEqual(desired);
});
}
});
describe("Remove non-alphanumeric", () => {
const inputs = [
"DROP/*comment*/sampletable",
"10; DROP TABLE members /*",
"1F44",
];
const outputs = ["DROPcommentsampletable", "10DROPTABLEmembers", "1F44"];
for (const key in inputs) {
const input = inputs[key];
const desired = outputs[key];
test(`Should return with only alphanumeric: ${key}`, () => {
expect(removeNonAlphanumeric(input)).toEqual(desired);
});
}
});