Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
chore: added more detailed example for test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
YEOWEIHNGWHYELAB committed Feb 23, 2024
1 parent 0ca892c commit f8e46f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/components/Login/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ describe("decodeQr", () => {
it("should fail for old, deprecated QR codes", () => {
expect.assertions(2);
const code = "1e4457bc-f7d0-4329-a344-f0e3c75d8dd4";
expect(() => decodeQr(code)).toThrow("Invalid format");
expect(() => decodeQr(code)).toThrow("No endpoint");
const url = "https://example.com";
expect(() => decodeQr(url)).toThrow("Invalid format");
expect(() => decodeQr(url)).toThrow("No endpoint");
});

it("should decode the correct qr code and return endpoint and key", () => {
expect.assertions(4);
const code = `{"key": "1e4457bc-f7d0-4329-a344-f0e3c75d8dd4","endpoint": "https://somewhere.com"}`;
const code = `{"key": "1e4457bc-f7d0-4329-a344-f0e3c75d8dd4","endpoint": "https://endpoint.com"}`;
const url =
"https://somewhere.com?key=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpoint=https://somewhere.com";
"https://this-is-the-base-url.com?key=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpoint=https://endpoint.com";
const jsonParsed = decodeQr(code);
const urlParsed = decodeQr(url);
expect(jsonParsed.endpoint).toBe("https://somewhere.com");
expect(jsonParsed.endpoint).toBe("https://endpoint.com");
expect(jsonParsed.key).toBe("1e4457bc-f7d0-4329-a344-f0e3c75d8dd4");
expect(urlParsed.endpoint).toBe("https://somewhere.com");
expect(urlParsed.endpoint).toBe("https://endpoint.com");
expect(urlParsed.key).toBe("1e4457bc-f7d0-4329-a344-f0e3c75d8dd4");
});

Expand All @@ -31,7 +31,7 @@ describe("decodeQr", () => {
expect(() => decodeQr(missingEndpointJSON)).toThrow("No endpoint");

const syntaxError = `xxx{"key": "1e4457bc-f7d0-4329-a344-f0e3c75d8dd4","endpoint": "https://somewhere.com"}`;
expect(() => decodeQr(syntaxError)).toThrow("Invalid format");
expect(() => decodeQr(syntaxError)).toThrow("No endpoint");

const jsonNoEndPoint = `{"key": "1e4457bc-f7d0-4329-a344-f0e3c75d8dd4"}`;
expect(() => decodeQr(jsonNoEndPoint)).toThrow("No endpoint");
Expand All @@ -40,11 +40,11 @@ describe("decodeQr", () => {
expect(() => decodeQr(jsonNoKey)).toThrow("No key");

const missingKeyURL =
"http:example.com?keys=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpoint=https://somewhere.com";
"http://example.com?keys=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpoint=https://somewhere.com";
expect(() => decodeQr(missingKeyURL)).toThrow("No key");

const missingEndpointURL =
"http:example.com?key=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpointed=https://somewhere.com";
"http://example.com?key=1e4457bc-f7d0-4329-a344-f0e3c75d8dd4&endpointed=https://somewhere.com";
expect(() => decodeQr(missingEndpointURL)).toThrow("No endpoint");

const urlNoKey = "https://example.com?endpoint=example";
Expand Down
2 changes: 0 additions & 2 deletions src/components/Login/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export const decodeQr = (code: string): DecodedQrResponse => {
key: params.key as string,
};
}
if (!parsedCode.endpoint && !parsedCode.key)
throw new AuthInvalidError("Invalid format");
if (!parsedCode.endpoint) throw new AuthInvalidError("No endpoint");
if (!parsedCode.key) throw new AuthInvalidError("No key");
return { endpoint: parsedCode.endpoint, key: parsedCode.key };
Expand Down

0 comments on commit f8e46f4

Please sign in to comment.