Skip to content

Commit

Permalink
Merge pull request #793 from eisbuk/feature/registeration_code_regex
Browse files Browse the repository at this point in the history
Tolerate variations of registerationCode on self register
  • Loading branch information
fadwamahmoud authored Sep 11, 2023
2 parents fcc9e2c + 98bfdc5 commit 866414c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/functions/src/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CustomerFull,
sanitizeCustomer,
DeliveryQueue,
checkExpected,
} from "@eisbuk/shared";

import { checkRequiredFields, EisbukHttpsError } from "./utils";
Expand Down Expand Up @@ -147,7 +148,7 @@ export const customerSelfRegister = functions
const orgDoc = await orgRef.get();
const orgData = orgDoc.data() as OrganizationData;

if (registrationCode !== orgData.registrationCode) {
if (!checkExpected(registrationCode, orgData.registrationCode || "")) {
throw new EisbukHttpsError(
"unauthenticated",
HTTPSErrors.SelfRegInvalidCode,
Expand Down
38 changes: 37 additions & 1 deletion packages/shared/src/utils/__tests__/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { interpolateText } from "../text";
import { interpolateText, checkExpected } from "../text";

describe("Text utils", () => {
describe("interpolateText", () => {
Expand Down Expand Up @@ -35,5 +35,41 @@ describe("Text utils", () => {
const interpolatedHtml = interpolateText(template, {});
expect(interpolatedHtml).toBe("Ciao");
});

test.only("Should ignore whitespace differences in registration code", async () => {
const cases = [
["A string", "A string!"],
["Look, ma, some spaces!", "LOOKMASOMESPACES"],
["Let's try with apostrophes", "Let s try with apostrophes."],
["On the others side", "On the other's side"],
[
"Nobody: use punctuation; You: expect it",
"Nobody? USE punctuation! You, expect it!",
],
];
for (const [input, expected] of cases) {
const result = checkExpected(input, expected);
if (!result) {
console.log("input", input);
console.log("expected", expected);
}
expect(result).toEqual(true);
}
});

test.only("Should fail when strings are different", async () => {
const cases = [
["A string", "A string! You see?"],
["Look, ma, some spaces!", "LOOKMANOSPACES"],
];
for (const [input, expected] of cases) {
const result = checkExpected(input, expected);
if (result) {
console.log("input", input);
console.log("expected", expected);
}
expect(result).toEqual(false);
}
});
});
});
10 changes: 10 additions & 0 deletions packages/shared/src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ export const interpolateText = (

return template.trim();
};

export const checkExpected = (input: string, expected: string) => {
/* Compares two strings without taking into account differences
in whitespace, punctuation, and capitalization.
If the two strings are equal, returns true, otherwise false.
*/
const sanitizedInput = input.replace(/[\s,;:!?'.-]/g, "").toLowerCase();
const sanitizedExpected = expected.replace(/[\s,;:!?'.-]/g, "").toLowerCase();
return sanitizedInput === sanitizedExpected;
};

0 comments on commit 866414c

Please sign in to comment.