-
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.
- Loading branch information
Showing
17 changed files
with
194 additions
and
322 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
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
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import React from "react" | ||
import LearningPathDetailsPage from "@/app-pages/LearningPathDetailsPage/LearningPathDetailsPage" | ||
import RestrictedRoute from "@/components/RestrictedRoute/RestrictedRoute" | ||
import { Permissions } from "@/common/permissions" | ||
|
||
const Page: React.FC = () => { | ||
return <LearningPathDetailsPage /> | ||
return ( | ||
<RestrictedRoute requires={Permissions.LearningPathEditor}> | ||
<LearningPathDetailsPage /> | ||
</RestrictedRoute> | ||
) | ||
} | ||
|
||
export default Page |
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
81 changes: 81 additions & 0 deletions
81
frontends/main/src/components/ErrorBoundary/ErrorBoundary.tsx
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,81 @@ | ||
"use client" | ||
|
||
import React, { Component } from "react" | ||
import NotFoundPage from "@/app-pages/ErrorPage/NotFoundPage" | ||
import ForbiddenPage from "@/app-pages/ErrorPage/ForbiddenPage" | ||
import { ForbiddenError } from "@/common/permissions" | ||
import { usePathname } from "next/navigation" | ||
|
||
interface ErrorBoundaryProps { | ||
children: React.ReactNode | ||
} | ||
|
||
interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps { | ||
pathname: string | ||
} | ||
|
||
interface ErrorBoundaryHandlerState { | ||
hasError: boolean | ||
error: unknown | ||
previousPathname: string | ||
} | ||
const isForbiddenError = (error: unknown) => error instanceof ForbiddenError | ||
|
||
class ErrorBoundaryHandler extends Component< | ||
ErrorBoundaryHandlerProps, | ||
ErrorBoundaryHandlerState | ||
> { | ||
constructor(props: ErrorBoundaryHandlerProps) { | ||
super(props) | ||
this.state = { | ||
hasError: false, | ||
error: null, | ||
previousPathname: this.props.pathname, | ||
} | ||
} | ||
|
||
static getDerivedStateFromError(error: unknown) { | ||
return { hasError: true, error: error } | ||
} | ||
|
||
static getDerivedStateFromProps( | ||
props: ErrorBoundaryHandlerProps, | ||
state: ErrorBoundaryHandlerState, | ||
): ErrorBoundaryHandlerState | null { | ||
if (props.pathname !== state.previousPathname && state.error) { | ||
return { | ||
error: null, | ||
hasError: false, | ||
previousPathname: props.pathname, | ||
} | ||
} | ||
return { | ||
error: state.error, | ||
hasError: state.hasError, | ||
previousPathname: props.pathname, | ||
} | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
if (isForbiddenError(this.state.error)) { | ||
return <ForbiddenPage /> | ||
} else { | ||
return <NotFoundPage /> | ||
} | ||
} | ||
|
||
return this.props.children | ||
} | ||
} | ||
|
||
export function ErrorBoundary({ | ||
children, | ||
}: ErrorBoundaryProps & { children: React.ReactNode }): JSX.Element { | ||
const pathname = usePathname() | ||
return ( | ||
<ErrorBoundaryHandler pathname={pathname}>{children}</ErrorBoundaryHandler> | ||
) | ||
} | ||
|
||
export default ErrorBoundary |
42 changes: 42 additions & 0 deletions
42
frontends/main/src/components/RestrictedRoute/RestrictedRoute.test.tsx
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,42 @@ | ||
import React from "react" | ||
import { renderWithProviders, screen } from "../../test-utils" | ||
import RestrictedRoute from "./RestrictedRoute" | ||
import { Permissions } from "@/common/permissions" | ||
import { allowConsoleErrors } from "ol-test-utilities" | ||
|
||
test("Renders children if permission check satisfied", () => { | ||
const errors: unknown[] = [] | ||
|
||
renderWithProviders( | ||
<RestrictedRoute requires={Permissions.Authenticated}> | ||
Hello, world! | ||
</RestrictedRoute>, | ||
|
||
{ | ||
user: { [Permissions.Authenticated]: true }, | ||
}, | ||
) | ||
|
||
screen.getByText("Hello, world!") | ||
expect(!errors.length).toBe(true) | ||
}) | ||
|
||
test.each(Object.values(Permissions))( | ||
"Throws error if and only if lacking required permission", | ||
async (permission) => { | ||
// if a user is not authenticated they are redirected to login before an error is thrown | ||
if (permission === Permissions.Authenticated) { | ||
return | ||
} | ||
allowConsoleErrors() | ||
|
||
expect(() => | ||
renderWithProviders( | ||
<RestrictedRoute requires={permission}>Hello, world!</RestrictedRoute>, | ||
{ | ||
user: { [permission]: false }, | ||
}, | ||
), | ||
).toThrow("Not allowed.") | ||
}, | ||
) |
Oops, something went wrong.