Skip to content

Commit 8205ab3

Browse files
authored
test(e2e): Testing auth().protect() for unauthenticated users in soft navigations (clerk#3427)
1 parent adb4c53 commit 8205ab3

5 files changed

Lines changed: 41 additions & 2 deletions

File tree

.changeset/wise-chairs-carry.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { auth } from '@clerk/nextjs/server';
2+
3+
export default function Page() {
4+
auth().protect();
5+
return <div>Protected Page</div>;
6+
}

integration/templates/next-app-router/src/app/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SignedIn, SignedOut, SignIn, UserButton, Protect } from '@clerk/nextjs';
2+
import Link from 'next/link';
23

34
export default function Home() {
45
return (
@@ -11,6 +12,11 @@ export default function Home() {
1112
path={'/'}
1213
signUpUrl={'/sign-up'}
1314
/>
15+
<ul>
16+
<li>
17+
<Link href='/page-protected'>Page Protected</Link>
18+
</li>
19+
</ul>
1420
</main>
1521
);
1622
}

integration/testUtils/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const createTestUtils = <
4444
Services = typeof services,
4545
PO = typeof pageObjects,
4646
BH = typeof browserHelpers,
47-
FullReturn = { services: Services; po: PO; tabs: BH; page: EnchancedPage },
47+
FullReturn = { services: Services; po: PO; tabs: BH; page: EnchancedPage; nexJsVersion: string },
4848
OnlyAppReturn = { services: Services },
4949
>(
5050
params: Params,
@@ -95,7 +95,14 @@ export const createTestUtils = <
9595
},
9696
};
9797

98-
return { page, services, po: pageObjects, tabs: browserHelpers } as any;
98+
return {
99+
page,
100+
services,
101+
po: pageObjects,
102+
tabs: browserHelpers,
103+
// eslint-disable-next-line turbo/no-undeclared-env-vars
104+
nexJsVersion: process.env.E2E_NEXTJS_VERSION,
105+
} as any;
99106
};
100107

101108
export { testAgainstRunningApps } from './testAgainstRunningApps';

integration/tests/protect.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withCustomRoles] })('authoriz
6767
test('Protect in RSCs and RCCs as `signed-out user`', async ({ page, context }) => {
6868
const u = createTestUtils({ app, page, context });
6969

70+
// Do not run this part for nextjs v14, the flow is broken in 14.2.3 because vercel removed a header that our page detection logic depends on
71+
if (!u.nexJsVersion.startsWith('14')) {
72+
/**
73+
* Soft navigations
74+
*/
75+
await u.page.goToRelative('/');
76+
await page.getByText('Page Protected').click();
77+
await page.waitForURL('**/sign-in?**');
78+
await u.po.signIn.waitForMounted();
79+
}
80+
81+
/**
82+
* Hard navigations
83+
*/
7084
await u.page.goToRelative('/settings/rsc-protect');
7185
await expect(u.page.getByText(/User is not admin/i)).toBeVisible();
7286
await u.page.goToRelative('/settings/rcc-protect');
@@ -77,6 +91,10 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withCustomRoles] })('authoriz
7791
await expect(u.page.getByText(/User is missing permissions/i)).toBeVisible();
7892
await u.page.goToRelative('/settings/auth-protect');
7993
await u.po.signIn.waitForMounted();
94+
await u.page.goToRelative('/protected');
95+
await u.po.signIn.waitForMounted();
96+
await u.page.goToRelative('/page-protected');
97+
await u.po.signIn.waitForMounted();
8098
});
8199

82100
test('Protect in RSCs and RCCs as `viewer`', async ({ page, context }) => {

0 commit comments

Comments
 (0)