-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathkeylessHelpers.ts
More file actions
138 lines (116 loc) · 4.06 KB
/
keylessHelpers.ts
File metadata and controls
138 lines (116 loc) · 4.06 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
import type { BrowserContext, Page } from '@playwright/test';
import { expect } from '@playwright/test';
import type { Application } from '../models/application';
import { createTestUtils } from './index';
/**
* Mocks the environment API call to return a claimed instance.
* Used in keyless mode tests to simulate an instance that has been claimed.
*/
export const mockClaimedInstanceEnvironmentCall = async (page: Page): Promise<void> => {
await page.route('*/**/v1/environment*', async route => {
const response = await route.fetch();
const json = await response.json();
const newJson = {
...json,
auth_config: {
...json.auth_config,
claimed_at: Date.now(),
},
};
await route.fulfill({ response, json: newJson });
});
};
/**
* Tests that the keyless popover can be toggled and the claim link opens the dashboard.
*/
export async function testToggleCollapsePopoverAndClaim({
page,
context,
app,
dashboardUrl,
}: {
page: Page;
context: BrowserContext;
app: Application;
dashboardUrl: string;
}): Promise<void> {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();
await u.po.keylessPopover.waitForMounted();
expect(await u.po.keylessPopover.isExpanded()).toBe(false);
await u.po.keylessPopover.toggle();
expect(await u.po.keylessPopover.isExpanded()).toBe(true);
const claim = u.po.keylessPopover.promptsToClaim();
const [newPage] = await Promise.all([context.waitForEvent('page'), claim.click()]);
await newPage.waitForLoadState();
await newPage.waitForURL(url => {
const urlToReturnTo = `${dashboardUrl}apps/claim?token=`;
const signUpForceRedirectUrl = url.searchParams.get('sign_up_force_redirect_url');
const signUpForceRedirectUrlCheck =
signUpForceRedirectUrl?.startsWith(urlToReturnTo) ||
(signUpForceRedirectUrl?.startsWith(`${dashboardUrl}prepare-account`) &&
signUpForceRedirectUrl?.includes(encodeURIComponent('apps/claim?token=')));
return (
url.pathname === '/apps/claim/sign-in' &&
url.searchParams.get('sign_in_force_redirect_url')?.startsWith(urlToReturnTo) &&
signUpForceRedirectUrlCheck
);
});
}
/**
* Tests that a claimed application with missing explicit keys shows the popover expanded
* with a prompt to get keys from the dashboard.
*/
export async function testClaimedAppWithMissingKeys({
page,
context,
app,
dashboardUrl,
}: {
page: Page;
context: BrowserContext;
app: Application;
dashboardUrl: string;
}): Promise<void> {
await mockClaimedInstanceEnvironmentCall(page);
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.waitForClerkJsLoaded();
await u.po.keylessPopover.waitForMounted();
expect(await u.po.keylessPopover.isExpanded()).toBe(true);
await expect(u.po.keylessPopover.promptToUseClaimedKeys()).toBeVisible();
const [newPage] = await Promise.all([
context.waitForEvent('page'),
u.po.keylessPopover.promptToUseClaimedKeys().click(),
]);
await newPage.waitForLoadState();
await newPage.waitForURL(url => {
return url.href.startsWith(`${dashboardUrl}sign-in?redirect_url=${encodeURIComponent(dashboardUrl)}apps%2Fapp_`);
});
}
/**
* Tests that the keyless popover is removed after adding keys to .env and restarting the dev server.
*/
export async function testKeylessRemovedAfterEnvAndRestart({
page,
context,
app,
}: {
page: Page;
context: BrowserContext;
app: Application;
}): Promise<void> {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.po.keylessPopover.waitForMounted();
expect(await u.po.keylessPopover.isExpanded()).toBe(false);
// Copy keys from keyless.json to .env
await app.keylessToEnv();
// Restart the dev server to pick up new env vars (Vite doesn't hot-reload .env)
await app.restart();
await u.page.goToAppHome();
// Keyless popover should no longer be present since we now have explicit keys
await u.po.keylessPopover.waitForUnmounted();
}