-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathnext-quickstart-keyless.test.ts
140 lines (115 loc) · 4.59 KB
/
next-quickstart-keyless.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
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
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
import type { Application } from '../models/application';
import { appConfigs } from '../presets';
import { createTestUtils } from '../testUtils';
const commonSetup = appConfigs.next.appRouterQuickstart.clone();
const mockClaimedInstanceEnvironmentCall = async (page: Page) => {
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 });
});
};
test.describe('Keyless mode @quickstart', () => {
test.describe.configure({ mode: 'serial' });
let app: Application;
let dashboardUrl = 'https://dashboard.clerk.com/';
test.beforeAll(async () => {
app = await commonSetup.commit();
await app.setup();
await app.withEnv(appConfigs.envs.withKeyless);
if (appConfigs.envs.withKeyless.privateVariables.get('CLERK_API_URL')?.includes('clerkstage')) {
dashboardUrl = 'https://dashboard.clerkstage.dev/';
}
await app.dev();
});
test.afterAll(async () => {
await app.teardown();
});
test('Navigates to non existed page (/_not-found) without a infinite redirect loop.', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();
await u.po.keylessPopover.waitForMounted();
const redirectMap = new Map<string, number>();
page.on('request', request => {
const url = request.url();
redirectMap.set(url, (redirectMap.get(url) || 0) + 1);
expect(redirectMap.get(url)).toBeLessThanOrEqual(1);
});
await u.page.goToRelative('/something');
await u.page.waitForAppUrl('/something');
});
test('Toggle collapse popover and claim.', async ({ page, context }) => {
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 = await 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=`;
return (
url.pathname === '/apps/claim/sign-in' &&
url.searchParams.get('sign_in_force_redirect_url')?.startsWith(urlToReturnTo) &&
url.searchParams.get('sign_up_force_redirect_url')?.startsWith(urlToReturnTo)
);
});
});
test('Lands on claimed application with missing explicit keys, expanded by default, click to get keys from dashboard.', async ({
page,
context,
}) => {
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_`);
});
});
test('Claimed application with keys inside .env, on dismiss, keyless prompt is removed.', async ({
page,
context,
}) => {
await mockClaimedInstanceEnvironmentCall(page);
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.po.keylessPopover.waitForMounted();
await expect(await u.po.keylessPopover.promptToUseClaimedKeys()).toBeVisible();
/**
* Copy keys from `.clerk/.tmp/keyless.json to `.env`
*/
await app.keylessToEnv();
/**
* wait a bit for the server to load the new env file
*/
await page.waitForTimeout(5_000);
await page.reload();
await u.po.keylessPopover.waitForMounted();
await u.po.keylessPopover.promptToDismiss().click();
await u.po.keylessPopover.waitForUnmounted();
});
});