Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tests for pr#7626 (2FA respect after_update param). #7662

Merged
merged 3 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { afterUpdateRedirect, redirectMessage } from "../helpers"

const { location: originalLocation } = window

describe("afterUpdateRedirect", () => {
beforeEach(() => {
// @ts-expect-error STRICT_NULL_CHECK
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("")
})
})
20 changes: 20 additions & 0 deletions src/v2/Utils/__tests__/url.jest.ts
Original file line number Diff line number Diff line change
@@ -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("")
})
})