Skip to content

fix: disable setup page once setup has been completed #9198

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

Merged
merged 12 commits into from
Aug 22, 2023
49 changes: 41 additions & 8 deletions site/src/pages/SetupPage/SetupPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { fireEvent, screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { rest } from "msw"
import { render } from "testHelpers/renderHelpers"
import {
renderWithAuth,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers"
import { server } from "testHelpers/server"
import { SetupPage } from "./SetupPage"
import { Language as PageViewLanguage } from "./SetupPageView"
Expand All @@ -16,9 +19,13 @@ const fillForm = async ({
email?: string
password?: string
} = {}) => {
const usernameField = screen.getByLabelText(PageViewLanguage.usernameLabel)
const emailField = screen.getByLabelText(PageViewLanguage.emailLabel)
const passwordField = screen.getByLabelText(PageViewLanguage.passwordLabel)
const usernameField = await screen.findByLabelText(
PageViewLanguage.usernameLabel,
)
const emailField = await screen.findByLabelText(PageViewLanguage.emailLabel)
const passwordField = await screen.findByLabelText(
PageViewLanguage.passwordLabel,
)
await userEvent.type(usernameField, username)
await userEvent.type(emailField, email)
await userEvent.type(passwordField, password)
Expand All @@ -35,11 +42,18 @@ describe("Setup Page", () => {
rest.get("/api/v2/users/me", (req, res, ctx) => {
return res(ctx.status(401), ctx.json({ message: "no user here" }))
}),
rest.get("/api/v2/users/first", (req, res, ctx) => {
return res(
ctx.status(404),
ctx.json({ message: "no first user has been created" }),
)
}),
)
})

it("shows validation error message", async () => {
render(<SetupPage />)
renderWithAuth(<SetupPage />, { route: "/setup", path: "/setup" })
await waitForLoaderToBeRemoved()
await fillForm({ email: "test" })
const errorMessage = await screen.findByText(PageViewLanguage.emailInvalid)
expect(errorMessage).toBeDefined()
Expand All @@ -63,22 +77,41 @@ describe("Setup Page", () => {
)
}),
)
render(<SetupPage />)

renderWithAuth(<SetupPage />, { route: "/setup", path: "/setup" })
await waitForLoaderToBeRemoved()
await fillForm()
const errorMessage = await screen.findByText(fieldErrorMessage)
expect(errorMessage).toBeDefined()
})

it("redirects to workspaces page when success", async () => {
render(<SetupPage />)
it("redirects to login if setup has already completed", async () => {
// simulates setup having already been completed
server.use(
rest.get("/api/v2/users/first", (req, res, ctx) => {
return res(ctx.status(404), ctx.json({ message: "hooray, you exist!" }))
}),
)

renderWithAuth(<SetupPage />, { route: "/setup", path: "/setup" })
await waitForLoaderToBeRemoved()
await waitFor(() => expect(window.location).toBeAt("/login"))
})

it("redirects to workspaces page when success", async () => {
// simulates the user will be authenticated
server.use(
rest.get("/api/v2/users/me", (req, res, ctx) => {
return res(ctx.status(200), ctx.json(MockUser))
}),
rest.get("/api/v2/users/first", (req, res, ctx) => {
return res(ctx.status(404), ctx.json({ message: "hooray, you exist!" }))
}),
)

renderWithAuth(<SetupPage />, { route: "/setup", path: "/setup" })
await waitForLoaderToBeRemoved()

await fillForm()
await waitFor(() => expect(window.location).toBeAt("/workspaces"))
})
Expand Down
15 changes: 13 additions & 2 deletions site/src/pages/SetupPage/SetupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Helmet } from "react-helmet-async"
import { pageTitle } from "utils/page"
import { setupMachine } from "xServices/setup/setupXService"
import { SetupPageView } from "./SetupPageView"
import { useNavigate } from "react-router-dom"

export const SetupPage: FC = () => {
const [authState, authSend] = useAuth()
Expand All @@ -23,12 +24,22 @@ export const SetupPage: FC = () => {
},
})
const { error } = setupState.context
const navigate = useNavigate()

useEffect(() => {
// If the user is logged in, navigate to the app
if (authState.matches("signedIn")) {
window.location.assign("/workspaces")
navigate("/", { state: { isRedirect: true } })
}
}, [authState])

// If we've already completed setup, navigate to the login page
if (
!authState.matches("loadingInitialAuthData") &&
!authState.matches("configuringTheFirstUser")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe we could do:

const isSetupComple = !authState.matches("loadingInitialAuthData") &&  !authState.matches("configuringTheFirstUser")

) {
navigate("/login", { state: { isRedirect: true } })
}
}, [authState, navigate])

return (
<>
Expand Down