-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #592 from CBIIT/PBAC/navbar-fixes
Refactor Login Flow & Fix Navbar Button Styling
- Loading branch information
Showing
7 changed files
with
133 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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,84 @@ | ||
import { render } from "@testing-library/react"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import LoginController from "./Controller"; | ||
|
||
jest.mock("../../env", () => ({ | ||
...process.env, | ||
REACT_APP_NIH_AUTHORIZE_URL: "https://mock-sso-url", | ||
REACT_APP_NIH_CLIENT_ID: "mock-client-id", | ||
REACT_APP_NIH_REDIRECT_URL: "mock-redirect-url", | ||
})); | ||
|
||
const mockUsePageTitle = jest.fn(); | ||
jest.mock("../../hooks/usePageTitle", () => ({ | ||
...jest.requireActual("../../hooks/usePageTitle"), | ||
__esModule: true, | ||
default: (p) => mockUsePageTitle(p), | ||
})); | ||
|
||
describe("LoginController", () => { | ||
beforeEach(() => { | ||
Object.defineProperty(window, "location", { | ||
value: { | ||
href: "/", | ||
}, | ||
writable: true, | ||
}); | ||
}); | ||
|
||
it("should set the page title to 'Login'", () => { | ||
render(<LoginController />, { wrapper: MemoryRouter }); | ||
|
||
expect(mockUsePageTitle).toHaveBeenCalledWith("Login"); | ||
}); | ||
|
||
it("should render a loader while redirecting to the NIH SSO login page", () => { | ||
const { getByTestId } = render(<LoginController />, { wrapper: MemoryRouter }); | ||
|
||
expect(getByTestId("login-flow-loader")).toBeVisible(); | ||
}); | ||
|
||
it("should redirect to the SSO login page with the correct params", () => { | ||
render(<LoginController />, { wrapper: MemoryRouter }); | ||
|
||
expect(window.location.href).toContain("https://mock-sso-url"); | ||
expect(window.location.href).toContain("client_id=mock-client-id"); | ||
expect(window.location.href).toContain("redirect_uri=mock-redirect-url"); | ||
expect(window.location.href).toContain("response_type=code"); | ||
expect(window.location.href).toContain("scope=openid+email+profile"); | ||
expect(window.location.href).toContain("prompt=login"); | ||
expect(window.location.href).not.toContain("state"); | ||
}); | ||
|
||
it.each<string>(["/data-submissions", "/users/uuid-1234", "mock-redirect-state"])( | ||
"should append the redirect state to the redirect URL if it exists as a valid string (%s)", | ||
(redirectState) => { | ||
render(<LoginController />, { | ||
wrapper: ({ children }) => ( | ||
<MemoryRouter initialEntries={["/login", { state: { redirectState } }]}> | ||
{children} | ||
</MemoryRouter> | ||
), | ||
}); | ||
|
||
const serializedRedirectState = encodeURIComponent(redirectState); | ||
|
||
expect(window.location.href).toContain(`state=${serializedRedirectState}`); | ||
} | ||
); | ||
|
||
it.each([null, undefined, 1234, {}, [], ""])( | ||
"should not append the redirect state to the redirect URL if it is not a valid string (%s)", | ||
(redirectState) => { | ||
render(<LoginController />, { | ||
wrapper: ({ children }) => ( | ||
<MemoryRouter initialEntries={["/login", { state: { redirectState } }]}> | ||
{children} | ||
</MemoryRouter> | ||
), | ||
}); | ||
|
||
expect(window.location.href).not.toContain("state"); | ||
} | ||
); | ||
}); |
This file contains 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,33 @@ | ||
import { useLocation } from "react-router-dom"; | ||
import env from "../../env"; | ||
import usePageTitle from "../../hooks/usePageTitle"; | ||
import SuspenseLoader from "../../components/SuspenseLoader"; | ||
|
||
/** | ||
* Handles the NIH SSO redirect to the login page. | ||
* | ||
* @returns The LoginController component | ||
*/ | ||
const LoginController = () => { | ||
usePageTitle("Login"); | ||
|
||
const { state } = useLocation(); | ||
|
||
const params = new URLSearchParams({ | ||
client_id: env.REACT_APP_NIH_CLIENT_ID, | ||
redirect_uri: env.REACT_APP_NIH_REDIRECT_URL, | ||
response_type: "code", | ||
scope: "openid email profile", | ||
prompt: "login", | ||
}); | ||
|
||
if (typeof state?.redirectState === "string" && !!state.redirectState) { | ||
params.append("state", state.redirectState); | ||
} | ||
|
||
window.location.href = `${env.REACT_APP_NIH_AUTHORIZE_URL}?${params?.toString()}`; | ||
|
||
return <SuspenseLoader data-testid="login-flow-loader" fullscreen />; | ||
}; | ||
|
||
export default LoginController; |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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