-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
20 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...e2e/app-dir/forbidden/basic/basic.test.ts → ...r/forbidden/basic/forbidden-basic.test.ts
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/unauthorized/default/app/(group)/group-dynamic/[id]/page.js
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,10 @@ | ||
import { unauthorized } from 'next/navigation' | ||
|
||
export default async function Page(props) { | ||
const params = await props.params | ||
if (params.id === '401') { | ||
unauthorized() | ||
} | ||
|
||
return <p id="page">{`group-dynamic [id]`}</p> | ||
} |
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,3 @@ | ||
export default function RootLayout({ children }) { | ||
return <div className="group-root-layout">{children}</div> | ||
} |
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,28 @@ | ||
'use client' | ||
|
||
import { useState, Suspense } from 'react' | ||
import { unauthorized } from 'next/navigation' | ||
import ForbiddenTrigger from './unauthorized-trigger' | ||
|
||
export default function Root({ children }) { | ||
const [clicked, setClicked] = useState(false) | ||
if (clicked) { | ||
unauthorized() | ||
} | ||
|
||
return ( | ||
<html className="root-layout-html"> | ||
<body> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<ForbiddenTrigger /> | ||
</Suspense> | ||
<button id="trigger-unauthorized" onClick={() => setClicked(true)}> | ||
Click to unauthorized | ||
</button> | ||
{children} | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
||
export const dynamic = 'force-dynamic' |
9 changes: 9 additions & 0 deletions
9
test/e2e/app-dir/unauthorized/default/app/metadata-layout-unauthorized/layout.js
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,9 @@ | ||
import { unauthorized } from 'next/navigation' | ||
|
||
export async function generateMetadata() { | ||
unauthorized() | ||
} | ||
|
||
export default function layout({ children }) { | ||
return children | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/unauthorized/default/app/metadata-layout-unauthorized/page.js
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,3 @@ | ||
export default function page() { | ||
return 'not found metadata page' | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/unauthorized/default/app/navigate-unauthorized/page.js
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,5 @@ | ||
import { unauthorized } from 'next/navigation' | ||
|
||
export default function page() { | ||
unauthorized() | ||
} |
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,22 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<p>hello world</p> | ||
<div> | ||
<Link id="navigate-unauthorized" href="/navigate-unauthorized"> | ||
navigate to page calling unauthorized() | ||
</Link> | ||
</div> | ||
<div> | ||
<Link | ||
id="metadata-layout-unauthorized" | ||
href="/metadata-layout-unauthorized" | ||
> | ||
navigate to layout with metadata API calling unauthorized() | ||
</Link> | ||
</div> | ||
</div> | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/unauthorized/default/app/unauthorized-trigger.js
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,12 @@ | ||
'use client' | ||
|
||
import { useSearchParams, unauthorized } from 'next/navigation' | ||
|
||
export default function ForbiddenTrigger() { | ||
const searchParams = useSearchParams() | ||
|
||
if (searchParams.get('root-unauthorized')) { | ||
unauthorized() | ||
} | ||
return null | ||
} |
79 changes: 79 additions & 0 deletions
79
test/e2e/app-dir/unauthorized/default/unauthorized-default.test.ts
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,79 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { | ||
assertHasRedbox, | ||
assertNoRedbox, | ||
getRedboxDescription, | ||
} from 'next-test-utils' | ||
|
||
describe('app dir - unauthorized with default unauthorized boundary', () => { | ||
const { next, isNextDev, skipped } = nextTestSetup({ | ||
files: __dirname, | ||
skipDeployment: true, | ||
}) | ||
|
||
if (skipped) { | ||
return | ||
} | ||
|
||
// TODO: error unauthorized usage in root layout | ||
it.skip('should error on client unauthorized from root layout in browser', async () => { | ||
const browser = await next.browser('/') | ||
|
||
await browser.elementByCss('#trigger-unauthorized').click() | ||
|
||
if (isNextDev) { | ||
await assertHasRedbox(browser) | ||
expect(await getRedboxDescription(browser)).toMatch( | ||
/unauthorized\(\) is not allowed to use in root layout/ | ||
) | ||
} | ||
}) | ||
|
||
// TODO: error unauthorized usage in root layout | ||
it.skip('should error on server unauthorized from root layout on server-side', async () => { | ||
const browser = await next.browser('/?root-unauthorized=1') | ||
|
||
if (isNextDev) { | ||
await assertHasRedbox(browser) | ||
expect(await getRedboxDescription(browser)).toBe( | ||
'Error: unauthorized() is not allowed to use in root layout' | ||
) | ||
} | ||
}) | ||
|
||
it('should be able to navigate to page calling unauthorized', async () => { | ||
const browser = await next.browser('/') | ||
|
||
await browser.elementByCss('#navigate-unauthorized').click() | ||
await browser.waitForElementByCss('.next-error-h1') | ||
|
||
expect(await browser.elementByCss('h1').text()).toBe('401') | ||
expect(await browser.elementByCss('h2').text()).toBe( | ||
`You're not authorized to access this page.` | ||
) | ||
}) | ||
|
||
it('should be able to navigate to page with calling unauthorized in metadata', async () => { | ||
const browser = await next.browser('/') | ||
|
||
await browser.elementByCss('#metadata-layout-unauthorized').click() | ||
await browser.waitForElementByCss('.next-error-h1') | ||
|
||
expect(await browser.elementByCss('h1').text()).toBe('401') | ||
expect(await browser.elementByCss('h2').text()).toBe( | ||
`You're not authorized to access this page.` | ||
) | ||
}) | ||
|
||
it('should render default unauthorized for group routes if unauthorized is not defined', async () => { | ||
const browser = await next.browser('/group-dynamic/123') | ||
expect(await browser.elementByCss('#page').text()).toBe( | ||
'group-dynamic [id]' | ||
) | ||
|
||
await browser.loadPage(next.url + '/group-dynamic/401') | ||
await assertNoRedbox(browser) | ||
await browser.waitForElementByCss('.group-root-layout') | ||
expect(await browser.elementByCss('.next-error-h1').text()).toBe('401') | ||
}) | ||
}) |