-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathdynamic-keys.test.ts
97 lines (76 loc) · 3.06 KB
/
dynamic-keys.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { expect, test } from '@playwright/test';
import type { Application } from '../models/application';
import { appConfigs } from '../presets';
import { createTestUtils } from '../testUtils';
test.describe('dynamic keys @nextjs', () => {
test.describe.configure({ mode: 'serial' });
let app: Application;
test.beforeAll(async () => {
app = await appConfigs.next.appRouter
.clone()
.addFile(
'src/middleware.ts',
() => `import { clerkClient, clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import { NextResponse } from 'next/server';
const isProtectedRoute = createRouteMatcher(['/protected']);
const shouldFetchBapi = createRouteMatcher(['/fetch-bapi-from-middleware']);
export default clerkMiddleware(async (auth, request) => {
if (isProtectedRoute(request)) {
await auth.protect();
}
if (shouldFetchBapi(request)){
const client = await clerkClient();
const count = await client.users?.getCount();
if (count){
return NextResponse.redirect(new URL('/users-count', request.url));
}
}
}, {
secretKey: process.env.CLERK_DYNAMIC_SECRET_KEY,
signInUrl: '/foobar'
});
export const config = {
matcher: ['/((?!.*\\\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
};`,
)
.addFile(
'src/app/users-count/page.tsx',
() => `import { clerkClient } from '@clerk/nextjs/server'
export default async function Page(){
const count = await clerkClient().users?.getCount() ?? 0;
return <p>Users count: {count}</p>
}
`,
)
.commit();
await app.setup();
await app.withEnv(appConfigs.envs.withDynamicKeys);
await app.dev();
});
test.afterAll(async () => {
await app.teardown();
});
test('redirects to `signInUrl` on `await auth.protect()`', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.po.expect.toBeSignedOut();
await u.page.goToRelative('/protected');
await u.page.waitForURL(/foobar/);
});
test('resolves auth signature with `secretKey` on `await auth.protect()`', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/page-protected');
await u.page.waitForURL(/foobar/);
});
test('calls `clerkClient` with dynamic keys from application runtime', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/users-count');
await expect(u.page.getByText(/Users count/i)).toBeVisible();
});
test('calls `clerkClient` with dynamic keys from middleware runtime', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/fetch-bapi-from-middleware');
await u.page.waitForAppUrl('/users-count');
await expect(u.page.getByText(/Users count/i)).toBeVisible();
});
});