Skip to content

Commit 1ff5872

Browse files
authored
chore(e2e): Add e2e tests for has() with JWT v2 format (#5659)
1 parent ae3c818 commit 1ff5872

File tree

10 files changed

+419
-0
lines changed

10 files changed

+419
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export function Conditionals({
2+
hasImpersonationRead,
3+
hasMagicLinksCreate,
4+
hasMagicLinksRead,
5+
hasImpersonationManage,
6+
hasAdminRole,
7+
hasManagerRole,
8+
hasImpersonationReaderRole,
9+
role,
10+
hasImpersonationFeature,
11+
hasMagicLinksFeature,
12+
}: {
13+
hasImpersonationRead: boolean;
14+
hasMagicLinksCreate: boolean;
15+
hasMagicLinksRead: boolean;
16+
hasImpersonationManage: boolean;
17+
hasAdminRole: boolean;
18+
hasManagerRole: boolean;
19+
hasImpersonationReaderRole: boolean;
20+
role: string | null | undefined;
21+
hasImpersonationFeature: boolean;
22+
hasMagicLinksFeature: boolean;
23+
}) {
24+
return (
25+
<>
26+
<pre>
27+
{`has({ permission: "org:impersonation:read" }) -> `}
28+
{hasImpersonationRead ? 'true' : 'false'}
29+
</pre>
30+
31+
<pre>
32+
{`has({ permission: "org:magic_links:create" }) -> `}
33+
{hasMagicLinksCreate ? 'true' : 'false'}
34+
</pre>
35+
36+
<pre>
37+
{`has({ permission: "org:magic_links:read" }) -> `}
38+
{hasMagicLinksRead ? 'true' : 'false'}
39+
</pre>
40+
41+
<pre>
42+
{`has({ permission: "org:impersonation:manage" }) -> `}
43+
{hasImpersonationManage ? 'true' : 'false'}
44+
</pre>
45+
46+
<pre>
47+
{`has({ role: "org:admin" }) -> `}
48+
{hasAdminRole ? 'true' : 'false'}
49+
</pre>
50+
51+
<pre>
52+
{`has({ role: "org:manager" }) -> `}
53+
{hasManagerRole ? 'true' : 'false'}
54+
</pre>
55+
56+
<pre>
57+
{`has({ role: "org:impersonation_reader" }) -> `}
58+
{hasImpersonationReaderRole ? 'true' : 'false'}
59+
</pre>
60+
61+
<pre>
62+
{`role -> `}
63+
{role}
64+
</pre>
65+
66+
<pre>
67+
{`has({ feature: "org:impersonation" }) -> `}
68+
{hasImpersonationFeature ? 'true' : 'false'}
69+
</pre>
70+
71+
<pre>
72+
{`has({ feature: "org:magic_links" }) -> `}
73+
{hasMagicLinksFeature ? 'true' : 'false'}
74+
</pre>
75+
</>
76+
);
77+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
import { useAuth } from '@clerk/nextjs';
3+
import { Conditionals } from '../conditionals';
4+
5+
export default function Page() {
6+
const { has, orgRole } = useAuth();
7+
8+
if (!has) {
9+
return <div>Loading...</div>;
10+
}
11+
12+
return (
13+
<>
14+
<h1>Has Client</h1>
15+
<Conditionals
16+
hasImpersonationRead={has({ permission: 'org:impersonation:read' })}
17+
hasMagicLinksCreate={has({ permission: 'org:magic_links:create' })}
18+
hasMagicLinksRead={has({ permission: 'org:magic_links:read' })}
19+
hasImpersonationManage={has({ permission: 'org:impersonation:manage' })}
20+
hasAdminRole={has({ role: 'org:admin' })}
21+
hasManagerRole={has({ role: 'org:manager' })}
22+
hasImpersonationReaderRole={has({ role: 'org:impersonation_reader' })}
23+
role={orgRole}
24+
hasImpersonationFeature={has({ feature: 'org:impersonation' })}
25+
hasMagicLinksFeature={has({ feature: 'org:magic_links' })}
26+
/>
27+
</>
28+
);
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { auth } from '@clerk/nextjs/server';
2+
import { Conditionals } from '../conditionals';
3+
4+
export default async function Page() {
5+
const { has, orgRole } = await auth();
6+
7+
return (
8+
<>
9+
<h1>Has Server</h1>
10+
<Conditionals
11+
hasImpersonationRead={has({ permission: 'org:impersonation:read' })}
12+
hasMagicLinksCreate={has({ permission: 'org:magic_links:create' })}
13+
hasMagicLinksRead={has({ permission: 'org:magic_links:read' })}
14+
hasImpersonationManage={has({ permission: 'org:impersonation:manage' })}
15+
hasAdminRole={has({ role: 'org:admin' })}
16+
hasManagerRole={has({ role: 'org:manager' })}
17+
hasImpersonationReaderRole={has({ role: 'org:impersonation_reader' })}
18+
role={orgRole}
19+
hasImpersonationFeature={has({ feature: 'org:impersonation' })}
20+
hasMagicLinksFeature={has({ feature: 'org:magic_links' })}
21+
/>
22+
</>
23+
);
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
import { useAuth } from '@clerk/nextjs';
3+
import { Conditionals } from '../conditionals';
4+
5+
export function SSR() {
6+
const { has, orgRole } = useAuth();
7+
8+
if (!has) {
9+
return <div>Loading...</div>;
10+
}
11+
12+
return (
13+
<>
14+
<h1>Has SSR</h1>
15+
<Conditionals
16+
hasImpersonationRead={has({ permission: 'org:impersonation:read' })}
17+
hasMagicLinksCreate={has({ permission: 'org:magic_links:create' })}
18+
hasMagicLinksRead={has({ permission: 'org:magic_links:read' })}
19+
hasImpersonationManage={has({ permission: 'org:impersonation:manage' })}
20+
hasAdminRole={has({ role: 'org:admin' })}
21+
hasManagerRole={has({ role: 'org:manager' })}
22+
hasImpersonationReaderRole={has({ role: 'org:impersonation_reader' })}
23+
role={orgRole}
24+
hasImpersonationFeature={has({ feature: 'org:impersonation' })}
25+
hasMagicLinksFeature={has({ feature: 'org:magic_links' })}
26+
/>
27+
</>
28+
);
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ClerkProvider } from '@clerk/nextjs';
2+
import { SSR } from './client';
3+
4+
export default function Page() {
5+
return (
6+
<ClerkProvider dynamic>
7+
<SSR />
8+
</ClerkProvider>
9+
);
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ClientJWT } from '../client-jwt';
2+
import { ServerJWT } from '../server-jwt';
3+
4+
export default function Layout({ children }: React.PropsWithChildren) {
5+
return (
6+
<div
7+
style={{
8+
display: 'flex',
9+
width: '100%',
10+
}}
11+
>
12+
<aside
13+
style={{
14+
flex: '0 0 300px',
15+
}}
16+
>
17+
<ServerJWT />
18+
<ClientJWT />
19+
</aside>
20+
<div style={{ flex: '1 1 auto' }}>{children}</div>
21+
</div>
22+
);
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Home() {
2+
return (
3+
<div>
4+
<main />
5+
</div>
6+
);
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use client';
2+
3+
import { useAuth } from '@clerk/nextjs';
4+
5+
export function ClientJWT() {
6+
const { sessionClaims } = useAuth();
7+
return (
8+
<>
9+
<h1>Client JWT</h1>
10+
<pre
11+
data-testid='client-jwt'
12+
style={{
13+
maxWidth: '32rem', // equivalent to max-w-lg
14+
textWrap: 'wrap',
15+
wordBreak: 'break-word',
16+
}}
17+
>
18+
{JSON.stringify(sessionClaims, null, 4)}
19+
</pre>
20+
</>
21+
);
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { auth } from '@clerk/nextjs/server';
2+
3+
export async function ServerJWT() {
4+
return (
5+
<>
6+
<h1>Server JWT</h1>
7+
<pre data-testid='server-jwt'>{JSON.stringify((await auth()).sessionClaims, null, 4)}</pre>
8+
</>
9+
);
10+
}

0 commit comments

Comments
 (0)