-
Notifications
You must be signed in to change notification settings - Fork 3
Handle stale auth better #2342
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
Open
ChristopherChudzicki
wants to merge
5
commits into
main
Choose a base branch
from
cc/handle-expiring-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Handle stale auth better #2342
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dfffd6d
restructure users hooks to use a queries file
ChristopherChudzicki 9317dad
ensure fresh data during auth check
ChristopherChudzicki 21562c5
refactor login url
ChristopherChudzicki 82fd1a9
refetch user data on tab focus
ChristopherChudzicki c4da25f
refactor login url more; fix tests
ChristopherChudzicki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { queryOptions } from "@tanstack/react-query" | ||
import { usersApi } from "../../clients" | ||
|
||
const userKeys = { | ||
root: ["users"], | ||
me: () => [...userKeys.root, "me"], | ||
} | ||
|
||
const userQueries = { | ||
me: () => | ||
queryOptions({ | ||
queryKey: userKeys.me(), | ||
queryFn: () => usersApi.usersMeRetrieve().then((res) => res.data), | ||
/** | ||
* Always refetch on window focus in case the user has logged in or out | ||
* in another tab or window while this tab was inactive. | ||
*/ | ||
refetchOnWindowFocus: "always", | ||
}), | ||
} | ||
|
||
export { userQueries, userKeys } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 48 additions & 30 deletions
78
frontends/main/src/app-pages/ErrorPage/ForbiddenPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,62 @@ | ||
import React from "react" | ||
import { renderWithProviders, screen } from "../../test-utils" | ||
import { HOME } from "@/common/urls" | ||
import { renderWithProviders, screen, waitFor } from "../../test-utils" | ||
import { HOME, login } from "@/common/urls" | ||
import ForbiddenPage from "./ForbiddenPage" | ||
import { setMockResponse, urls } from "api/test-utils" | ||
import { Permission } from "api/hooks/user" | ||
import { setMockResponse, urls, factories } from "api/test-utils" | ||
import { useUserMe } from "api/hooks/user" | ||
import { redirect } from "next/navigation" | ||
|
||
const oldWindowLocation = window.location | ||
const mockedRedirect = jest.mocked(redirect) | ||
|
||
beforeAll(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (window as any).location | ||
const makeUser = factories.user.user | ||
|
||
window.location = Object.defineProperties({} as Location, { | ||
...Object.getOwnPropertyDescriptors(oldWindowLocation), | ||
assign: { | ||
configurable: true, | ||
value: jest.fn(), | ||
}, | ||
test("The ForbiddenPage loads with Correct Title", async () => { | ||
setMockResponse.get(urls.userMe.get(), makeUser({ is_authenticated: true })) | ||
renderWithProviders(<ForbiddenPage />) | ||
await screen.findByRole("heading", { | ||
name: "You do not have permission to access this resource.", | ||
}) | ||
}) | ||
|
||
afterAll(() => { | ||
window.location = oldWindowLocation | ||
test("The ForbiddenPage loads with a link that directs to HomePage", async () => { | ||
setMockResponse.get(urls.userMe.get(), makeUser({ is_authenticated: true })) | ||
renderWithProviders(<ForbiddenPage />) | ||
const homeLink = await screen.findByRole("link", { name: "Home" }) | ||
expect(homeLink).toHaveAttribute("href", HOME) | ||
}) | ||
|
||
test("The ForbiddenPage loads with Correct Title", () => { | ||
setMockResponse.get(urls.userMe.get(), { | ||
[Permission.Authenticated]: true, | ||
}) | ||
renderWithProviders(<ForbiddenPage />) | ||
screen.getByRole("heading", { | ||
name: "You do not have permission to access this resource.", | ||
test("Fetches auth data afresh and redirects unauthenticated users to auth", async () => { | ||
const user = makeUser() | ||
setMockResponse.get(urls.userMe.get(), user) | ||
|
||
const FakeHeader = ({ children }: { children?: React.ReactNode }) => { | ||
const user = useUserMe() | ||
return ( | ||
<div> | ||
{user.data?.first_name} | ||
{children} | ||
</div> | ||
) | ||
} | ||
const { view } = renderWithProviders(<FakeHeader />, { | ||
url: "/foo?cat=meow", | ||
}) | ||
}) | ||
await screen.findByText(user.first_name) | ||
|
||
setMockResponse.get(urls.userMe.get(), makeUser({ is_authenticated: false })) | ||
|
||
test("The ForbiddenPage loads with a link that directs to HomePage", () => { | ||
setMockResponse.get(urls.userMe.get(), { | ||
[Permission.Authenticated]: true, | ||
view.rerender( | ||
<FakeHeader> | ||
<ForbiddenPage /> | ||
</FakeHeader>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(mockedRedirect).toHaveBeenCalledWith( | ||
login({ | ||
pathname: "/foo", | ||
searchParams: new URLSearchParams({ cat: "meow" }), | ||
}), | ||
) | ||
}) | ||
renderWithProviders(<ForbiddenPage />) | ||
const homeLink = screen.getByRole("link", { name: "Home" }) | ||
expect(homeLink).toHaveAttribute("href", HOME) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we set some reasonable
staleTime
? DefaultingrefetchOnWindowFocus
to true here has no effect if the data is never stale.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meant to comment on this... I removed the
false
since it has no effect with staleTime set to Infinity. (refetchOnWindowFocus: false | true
... neither has an effect with infinite staletime, just "always").,