mirror of
https://github.com/coder/coder.git
synced 2026-11-17 17:40:53 +08:00
Fixes #304 Unblocks #298 After logging in, the login flow should redirect to a whatever path is specified by the `?redirect` query parameter. This is important for cases like #298 - where we need to set `?redirect=%2Fcli_auth`, but also really any case where the user is linked and might have to go back to the login screen. The fix is simple - just check if the `redirect` query parameter is set, and if it is, use that as the path to redirect to on success. Also adds a test case - we had one checking that we redirect to the default (root `/`) url, but not one of the `?redirect` param
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import React from "react"
|
|
import singletonRouter from "next/router"
|
|
import mockRouter from "next-router-mock"
|
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"
|
|
|
|
import { SignInForm } from "./SignInForm"
|
|
|
|
describe("SignInForm", () => {
|
|
beforeEach(() => {
|
|
mockRouter.setCurrentUrl("/login")
|
|
})
|
|
|
|
it("renders content", async () => {
|
|
// When
|
|
render(<SignInForm />)
|
|
|
|
// Then
|
|
await screen.findByText("Sign In", { exact: false })
|
|
})
|
|
|
|
it("shows an error message if SignIn fails", async () => {
|
|
// Given
|
|
const loginHandler = (_email: string, _password: string) => Promise.reject("Unacceptable credentials")
|
|
|
|
// When
|
|
// Render the component
|
|
const { container } = render(<SignInForm loginHandler={loginHandler} />)
|
|
const inputs = container.querySelectorAll("input")
|
|
// Set username / password
|
|
fireEvent.change(inputs[0], { target: { value: "test@coder.com" } })
|
|
fireEvent.change(inputs[1], { target: { value: "password" } })
|
|
// Click sign-in
|
|
const elem = await screen.findByText("Sign In")
|
|
act(() => elem.click())
|
|
|
|
// Then
|
|
// Should see an error message
|
|
const errorMessage = await screen.findByText("The username or password is incorrect.")
|
|
expect(errorMessage).toBeDefined()
|
|
})
|
|
|
|
it("redirects when login is complete", async () => {
|
|
// Given
|
|
const loginHandler = (_email: string, _password: string) => Promise.resolve()
|
|
|
|
// When
|
|
// Render the component
|
|
const { container } = render(<SignInForm loginHandler={loginHandler} />)
|
|
// Set user / password
|
|
const inputs = container.querySelectorAll("input")
|
|
fireEvent.change(inputs[0], { target: { value: "test@coder.com" } })
|
|
fireEvent.change(inputs[1], { target: { value: "password" } })
|
|
// Click sign-in
|
|
const elem = await screen.findByText("Sign In")
|
|
act(() => elem.click())
|
|
|
|
// Then
|
|
// Should redirect because login was successful
|
|
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/" }))
|
|
})
|
|
|
|
it("respects ?redirect query parameter when complete", async () => {
|
|
// Given
|
|
const loginHandler = (_email: string, _password: string) => Promise.resolve()
|
|
// Set a path to redirect to after login is successful
|
|
mockRouter.setCurrentUrl("/login?redirect=%2Fsome%2Fother%2Fpath")
|
|
|
|
// When
|
|
// Render the component
|
|
const { container } = render(<SignInForm loginHandler={loginHandler} />)
|
|
// Set user / password
|
|
const inputs = container.querySelectorAll("input")
|
|
fireEvent.change(inputs[0], { target: { value: "test@coder.com" } })
|
|
fireEvent.change(inputs[1], { target: { value: "password" } })
|
|
// Click sign-in
|
|
const elem = await screen.findByText("Sign In")
|
|
act(() => elem.click())
|
|
|
|
// Then
|
|
// Should redirect to /some/other/path because ?redirect was specified and login was successful
|
|
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/some/other/path" }))
|
|
})
|
|
})
|