-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathpasskeys.test.ts
More file actions
192 lines (150 loc) · 6.92 KB
/
passkeys.test.ts
File metadata and controls
192 lines (150 loc) · 6.92 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { CDPSession } from '@playwright/test';
import { expect, test } from '@playwright/test';
import { appConfigs } from '../../presets';
import type { FakeUser } from '../../testUtils';
import { createTestUtils, testAgainstRunningApps } from '../../testUtils';
testAgainstRunningApps({ withEnv: [appConfigs.envs.withPasskeys] })('passkeys @tanstack-react-start', ({ app }) => {
test.describe.configure({ mode: 'serial' });
let fakeUser: FakeUser;
let savedCredentials: any[] = [];
test.beforeAll(async () => {
const u = createTestUtils({ app });
fakeUser = u.services.users.createFakeUser();
await u.services.users.createBapiUser(fakeUser);
});
test.afterAll(async () => {
await fakeUser.deleteIfExists();
await app.teardown();
});
const setupVirtualAuthenticator = async (page: any): Promise<{ cdpSession: CDPSession; authenticatorId: string }> => {
// Clerk's isValidBrowser() checks !navigator.webdriver, which is true in Playwright.
// Override it so Clerk detects WebAuthn as supported.
await page.addInitScript(() => {
Object.defineProperty(navigator, 'webdriver', { get: () => false });
});
const cdpSession = await page.context().newCDPSession(page);
await cdpSession.send('WebAuthn.enable');
const { authenticatorId } = await cdpSession.send('WebAuthn.addVirtualAuthenticator', {
options: {
protocol: 'ctap2',
transport: 'internal',
hasResidentKey: true,
hasUserVerification: true,
isUserVerified: true,
},
});
return { cdpSession, authenticatorId };
};
const teardownVirtualAuthenticator = async (cdpSession: CDPSession, authenticatorId: string) => {
await cdpSession.send('WebAuthn.removeVirtualAuthenticator', { authenticatorId });
await cdpSession.send('WebAuthn.disable');
await cdpSession.detach();
};
const dismissOrgDialog = async (page: any) => {
await page.getByRole('button', { name: /I'll remove it myself/i }).click();
};
const openSecurityTabViaUserButton = async (u: ReturnType<typeof createTestUtils>) => {
await u.po.userButton.waitForMounted();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
await u.po.userButton.triggerManageAccount();
await u.po.userProfile.waitForUserProfileModal();
await u.po.userProfile.switchToSecurityTab();
};
test('register a passkey from UserProfile', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const { cdpSession, authenticatorId } = await setupVirtualAuthenticator(page);
await u.po.signIn.goTo();
await u.po.signIn.signInWithEmailAndInstantPassword({
email: fakeUser.email,
password: fakeUser.password,
});
await u.page.goToAppHome();
await dismissOrgDialog(page);
await openSecurityTabViaUserButton(u);
// Click "Add a passkey"
await page.getByRole('button', { name: /add a passkey/i }).click();
// The virtual authenticator auto-responds to navigator.credentials.create()
await expect(page.locator('.cl-profileSectionItem__passkeys')).toBeVisible({ timeout: 10000 });
// Save credentials so the sign-in test can import them into its own virtual authenticator
const { credentials } = await cdpSession.send('WebAuthn.getCredentials', { authenticatorId });
savedCredentials = credentials;
await teardownVirtualAuthenticator(cdpSession, authenticatorId);
});
test('sign in with passkey', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const { cdpSession, authenticatorId } = await setupVirtualAuthenticator(page);
// Import credentials from the register test
for (const credential of savedCredentials) {
await cdpSession.send('WebAuthn.addCredential', { authenticatorId, credential });
}
await u.po.signIn.goTo();
await page.getByRole('link', { name: /use passkey/i }).click();
// The virtual authenticator auto-responds to navigator.credentials.get()
await u.po.expect.toBeSignedIn();
await teardownVirtualAuthenticator(cdpSession, authenticatorId);
});
test('rename a passkey', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const { cdpSession, authenticatorId } = await setupVirtualAuthenticator(page);
await u.po.signIn.goTo();
await u.po.signIn.signInWithEmailAndInstantPassword({
email: fakeUser.email,
password: fakeUser.password,
});
await u.page.goToAppHome();
await dismissOrgDialog(page);
await openSecurityTabViaUserButton(u);
// Register a passkey
const passkeysBefore = await page.locator('.cl-profileSectionItem__passkeys').count();
await page.getByRole('button', { name: /add a passkey/i }).click();
await expect(page.locator('.cl-profileSectionItem__passkeys')).toHaveCount(passkeysBefore + 1, { timeout: 10000 });
// Click three-dots menu on the newly added passkey (last one)
await page
.locator('.cl-profileSectionItem__passkeys')
.last()
.getByRole('button', { name: /open menu/i })
.click();
// Click "Rename"
await page.getByRole('menuitem', { name: /rename/i }).click();
// Enter new name
const newName = 'My Renamed Passkey';
await page.locator('input[name="passkeyName"]').fill(newName);
await page.getByRole('button', { name: /save/i }).click();
// Verify the updated name appears
await expect(page.locator('.cl-profileSectionItem__passkeys').filter({ hasText: newName })).toBeVisible();
// Clean up
await teardownVirtualAuthenticator(cdpSession, authenticatorId);
});
test('remove a passkey', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const { cdpSession, authenticatorId } = await setupVirtualAuthenticator(page);
await u.po.signIn.goTo();
await u.po.signIn.signInWithEmailAndInstantPassword({
email: fakeUser.email,
password: fakeUser.password,
});
await u.page.goToAppHome();
await dismissOrgDialog(page);
await openSecurityTabViaUserButton(u);
// Count existing passkeys before registering a new one
const passkeyItems = page.locator('.cl-profileSectionItem__passkeys');
const countBefore = await passkeyItems.count();
// Register a passkey
await page.getByRole('button', { name: /add a passkey/i }).click();
await expect(passkeyItems).toHaveCount(countBefore + 1, { timeout: 10000 });
// Click three-dots menu on the newly added passkey (last one)
await passkeyItems
.last()
.getByRole('button', { name: /open menu/i })
.click();
// Click "Remove"
await page.getByRole('menuitem', { name: /remove/i }).click();
// Confirm removal
await page.getByRole('button', { name: /remove/i }).click();
// Verify passkey count decreased
await expect(passkeyItems).toHaveCount(countBefore, { timeout: 10000 });
// Clean up
await teardownVirtualAuthenticator(cdpSession, authenticatorId);
});
});