From 6985c53eeafbc520b3b1fa35aa5495a08d5fd24b Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 28 May 2021 15:53:32 -0400 Subject: [PATCH 1/3] feat: tests for url util --- src/v2/Utils/__tests__/url.jest.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/v2/Utils/__tests__/url.jest.ts diff --git a/src/v2/Utils/__tests__/url.jest.ts b/src/v2/Utils/__tests__/url.jest.ts new file mode 100644 index 00000000000..d25eb8099b0 --- /dev/null +++ b/src/v2/Utils/__tests__/url.jest.ts @@ -0,0 +1,20 @@ +import { getURLHost } from "../url" + +describe("getURLHost", () => { + it("returns host for url with host", () => { + const url = "https://cms.artsy.net" + expect(getURLHost(url)).toEqual("cms.artsy.net") + }) + it("returns host for url with host and path", () => { + const url = "https://cms.artsy.net/foo" + expect(getURLHost(url)).toEqual("cms.artsy.net") + }) + it("returns empty string for empty string", () => { + const url = "" + expect(getURLHost(url)).toEqual("") + }) + it("returns empty string for non-url", () => { + const url = "!@#123" + expect(getURLHost(url)).toEqual("") + }) +}) From 730ed92c99d017e156908e8e308182f0e381bc6d Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 28 May 2021 15:54:56 -0400 Subject: [PATCH 2/3] feat: tests for TwoFactorAuthentication afterUpdateRedirect and redirectMessage helpers --- .../__tests__/helpers.jest.ts | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts diff --git a/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts b/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts new file mode 100644 index 00000000000..d0ebadb1c3a --- /dev/null +++ b/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts @@ -0,0 +1,74 @@ +import { afterUpdateRedirect, redirectMessage } from "../helpers" +const { location: originalLocation } = window + +describe("afterUpdateRedirect", () => { + beforeEach(() => { + delete window.location + }) + afterEach(() => { + window.location = originalLocation + }) + + it("returns host for trusted domain", () => { + const search = "?after_update=https%3A%2F%2Fcms.artsy.net" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("https://cms.artsy.net") + }) + it("returns host and path for trusted domain", () => { + const search = "?after_update=https%3A%2F%2Fcms.artsy.net%2Fauth%2Fartsy" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("https://cms.artsy.net/auth/artsy") + }) + it("returns / for untrusted domains", () => { + const search = "?after_update=https%3A%2F%2Fcms.evil.net" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("/") + }) + it("returns / for relative root", () => { + const search = "?after_update=/" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("/") + }) + it("returns /foo for relative foo", () => { + const search = "?after_update=/foo" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("/foo") + }) + it("returns empty string for for no after_update param", () => { + const search = "?foo=bar" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("") + }) + it("returns empty string for for no search string at all", () => { + const search = "" + window.location = { search } as any + expect(afterUpdateRedirect()).toEqual("") + }) +}) + +describe("redirectMessage", () => { + it("returns redir message for host", () => { + const url = "https://cms.artsy.net" + expect(redirectMessage(url)).toEqual( + "You will be redirected to: cms.artsy.net" + ) + }) + it("returns redir message for host and path", () => { + const url = "https://cms.artsy.net/auth/artsy" + expect(redirectMessage(url)).toEqual( + "You will be redirected to: cms.artsy.net" + ) + }) + it("returns empty string for relative /", () => { + const url = "/" + expect(redirectMessage(url)).toEqual("") + }) + it("returns empty string for relative /foo", () => { + const url = "/" + expect(redirectMessage(url)).toEqual("") + }) + it("returns empty string for empty url", () => { + const url = "" + expect(redirectMessage(url)).toEqual("") + }) +}) From 8e138fb2f01be527aa336252458d0d04cbaa428e Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 28 May 2021 16:08:28 -0400 Subject: [PATCH 3/3] fix: to get around typescript error: The operand of a delete operator must be optional. --- .../TwoFactorAuthentication/__tests__/helpers.jest.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts b/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts index d0ebadb1c3a..d278cfb50a1 100644 --- a/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts +++ b/src/v2/Components/UserSettings/TwoFactorAuthentication/__tests__/helpers.jest.ts @@ -1,8 +1,10 @@ import { afterUpdateRedirect, redirectMessage } from "../helpers" + const { location: originalLocation } = window describe("afterUpdateRedirect", () => { beforeEach(() => { + // @ts-expect-error STRICT_NULL_CHECK delete window.location }) afterEach(() => {