-
Notifications
You must be signed in to change notification settings - Fork 7k
[core] Support token based auth in ray dashboard UI #58368
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
206 changes: 206 additions & 0 deletions
206
python/ray/dashboard/client/src/authentication/TokenAuthenticationDialog.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 |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import React from "react"; | ||
| import "@testing-library/jest-dom"; | ||
| import TokenAuthenticationDialog from "./TokenAuthenticationDialog"; | ||
|
|
||
| describe("TokenAuthenticationDialog", () => { | ||
| const mockOnSubmit = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| mockOnSubmit.mockClear(); | ||
| }); | ||
|
|
||
| it("renders with initial message when no existing token", () => { | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByText("Token Authentication Required"), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText(/token authentication is enabled for this cluster/i), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders with re-authentication message when has existing token", () => { | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={true} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByText("Token Authentication Required"), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText(/authentication token is invalid or has expired/i), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays error message when provided", () => { | ||
| const errorMessage = "Invalid token provided"; | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| error={errorMessage} | ||
| />, | ||
| ); | ||
|
|
||
| expect(screen.getByText(errorMessage)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("calls onSubmit with entered token when submit is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| mockOnSubmit.mockResolvedValue(undefined); | ||
|
|
||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const input = screen.getByLabelText(/authentication token/i); | ||
| await user.type(input, "test-token-123"); | ||
|
|
||
| const submitButton = screen.getByRole("button", { name: /submit/i }); | ||
| await user.click(submitButton); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockOnSubmit).toHaveBeenCalledWith("test-token-123"); | ||
| }); | ||
| }); | ||
|
|
||
| it("calls onSubmit when Enter key is pressed", async () => { | ||
| const user = userEvent.setup(); | ||
| mockOnSubmit.mockResolvedValue(undefined); | ||
|
|
||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const input = screen.getByLabelText(/authentication token/i); | ||
| await user.type(input, "test-token-123{Enter}"); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockOnSubmit).toHaveBeenCalledWith("test-token-123"); | ||
| }); | ||
| }); | ||
|
|
||
| it("disables submit button when token is empty", () => { | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const submitButton = screen.getByRole("button", { name: /submit/i }); | ||
| expect(submitButton).toBeDisabled(); | ||
| }); | ||
|
|
||
| it("enables submit button when token is entered", async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const submitButton = screen.getByRole("button", { name: /submit/i }); | ||
| expect(submitButton).toBeDisabled(); | ||
|
|
||
| const input = screen.getByLabelText(/authentication token/i); | ||
| await user.type(input, "test-token"); | ||
|
|
||
| expect(submitButton).not.toBeDisabled(); | ||
| }); | ||
|
|
||
| it("toggles token visibility when visibility icon is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const input = screen.getByLabelText(/authentication token/i); | ||
| await user.type(input, "secret-token"); | ||
|
|
||
| // Initially should be password type (hidden) | ||
| expect(input).toHaveAttribute("type", "password"); | ||
|
|
||
| // Click visibility toggle | ||
| const toggleButton = screen.getByLabelText(/toggle token visibility/i); | ||
| await user.click(toggleButton); | ||
|
|
||
| // Should now be text type (visible) | ||
| expect(input).toHaveAttribute("type", "text"); | ||
|
|
||
| // Click again to hide | ||
| await user.click(toggleButton); | ||
| expect(input).toHaveAttribute("type", "password"); | ||
| }); | ||
|
|
||
| it("shows loading state during submission", async () => { | ||
| const user = userEvent.setup(); | ||
| // Mock a slow submission | ||
| mockOnSubmit.mockImplementation( | ||
| () => new Promise((resolve) => setTimeout(resolve, 100)), | ||
| ); | ||
|
|
||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={true} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| const input = screen.getByLabelText(/authentication token/i); | ||
| await user.type(input, "test-token"); | ||
|
|
||
| const submitButton = screen.getByRole("button", { name: /submit/i }); | ||
| await user.click(submitButton); | ||
|
|
||
| // Should show validating state | ||
| await waitFor(() => { | ||
| expect(screen.getByText(/validating.../i)).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it("does not render when open is false", () => { | ||
| render( | ||
| <TokenAuthenticationDialog | ||
| open={false} | ||
| hasExistingToken={false} | ||
| onSubmit={mockOnSubmit} | ||
| />, | ||
| ); | ||
|
|
||
| // Dialog should not be visible | ||
| expect( | ||
| screen.queryByText("Token Authentication Required"), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.