Skip to content

Commit

Permalink
Merge pull request #7662 from artsy/artsyjian/after-update-redir
Browse files Browse the repository at this point in the history
feat: tests for pr#7626 (2FA respect after_update param).
  • Loading branch information
sweir27 authored May 28, 2021
2 parents 3db0607 + 8e138fb commit ab1035f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
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("")
})
})

0 comments on commit ab1035f

Please sign in to comment.