-
Notifications
You must be signed in to change notification settings - Fork 453
test(e2e): add test for getToken with custom JWT templates
#8053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmilewski
wants to merge
3
commits into
main
Choose a base branch
from
tom/user-4952-test-against-skipcache-and-custom-jwts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or 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,116 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import type { Application } from '../models/application'; | ||
| import { appConfigs } from '../presets'; | ||
| import type { FakeUser } from '../testUtils'; | ||
| import { createTestUtils } from '../testUtils'; | ||
|
|
||
| test.describe('Custom JWT templates with skipCache @nextjs', () => { | ||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| let app: Application; | ||
| let fakeUser: FakeUser; | ||
| let jwtTemplateId: string; | ||
| const jwtTemplateName = `e2e-test-${Date.now()}`; | ||
|
|
||
| test.beforeAll(async () => { | ||
| test.setTimeout(120_000); | ||
|
|
||
| app = await appConfigs.next.appRouter | ||
| .clone() | ||
| .addFile( | ||
| 'src/middleware.ts', | ||
| () => `import { clerkMiddleware } from '@clerk/nextjs/server'; | ||
|
|
||
| export default clerkMiddleware(); | ||
|
|
||
| export const config = { | ||
| matcher: ['/((?!.*\\\\..*|_next).*)', '/', '/(api|trpc)(.*)'], | ||
| }; | ||
| `, | ||
| ) | ||
| .addFile( | ||
| 'src/app/api/custom-jwt/route.ts', | ||
| () => `import { headers } from 'next/headers'; | ||
| import { auth } from '@clerk/nextjs/server'; | ||
|
|
||
| export async function GET() { | ||
| const headersList = await headers(); | ||
| const templateName = headersList.get('x-jwt-template'); | ||
| const skipCache = headersList.get('x-skip-cache') === 'true'; | ||
| const { getToken, userId, sessionId } = await auth(); | ||
| const customToken = await getToken({ template: templateName, skipCache }); | ||
| return Response.json({ | ||
| userId, | ||
| sessionId, | ||
| customToken, | ||
| }); | ||
| }`, | ||
| ) | ||
| .commit(); | ||
|
|
||
| await app.setup(); | ||
| await app.withEnv(appConfigs.envs.withEmailCodes); | ||
| await app.dev(); | ||
|
|
||
| const m = createTestUtils({ app }); | ||
| fakeUser = m.services.users.createFakeUser(); | ||
| await m.services.users.createBapiUser(fakeUser); | ||
|
|
||
| const template = await m.services.clerk.jwtTemplates.create({ | ||
| name: jwtTemplateName, | ||
| claims: { test_claim: 'hello_from_e2e' }, | ||
| lifetime: 60, | ||
| }); | ||
| jwtTemplateId = template.id; | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| const m = createTestUtils({ app }); | ||
| if (jwtTemplateId) { | ||
| await m.services.clerk.jwtTemplates.delete(jwtTemplateId); | ||
| } | ||
| await fakeUser.deleteIfExists(); | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test('getToken with skipCache returns a fresh custom JWT token on each call', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
|
|
||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| const fetchToken = (skipCache: boolean) => | ||
| page.request.get(`${app.serverUrl}/api/custom-jwt`, { | ||
| headers: { | ||
| 'x-jwt-template': jwtTemplateName, | ||
| 'x-skip-cache': String(skipCache), | ||
| }, | ||
| }); | ||
|
|
||
| // Without skipCache | ||
| const res1 = await fetchToken(false); | ||
| expect(res1.status()).toBe(200); | ||
| const body1 = await res1.json(); | ||
| expect(body1.userId).toBeTruthy(); | ||
| expect(body1.sessionId).toBeTruthy(); | ||
| expect(body1.customToken).toBeTruthy(); | ||
|
|
||
| const payload1 = JSON.parse(atob(body1.customToken.split('.')[1])); | ||
| expect(payload1.test_claim).toBe('hello_from_e2e'); | ||
|
|
||
| // With skipCache — should return a valid, freshly issued token | ||
| const res2 = await fetchToken(true); | ||
| expect(res2.status()).toBe(200); | ||
| const body2 = await res2.json(); | ||
| expect(body2.userId).toBeTruthy(); | ||
| expect(body2.sessionId).toBeTruthy(); | ||
| expect(body2.customToken).toBeTruthy(); | ||
|
|
||
| const payload2 = JSON.parse(atob(body2.customToken.split('.')[1])); | ||
| expect(payload2.test_claim).toBe('hello_from_e2e'); | ||
| expect(payload2.iat).toBeGreaterThanOrEqual(payload1.iat); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a strong enough assertion to know that we got a freshly minted token with skipCache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approved, but defer to jacek