|
1 | | -import { test, expect, Page, APIRequestContext, Browser } from '@playwright/test'; |
| 1 | +import { test, expect, Page, APIRequestContext, APIResponse, Browser } from '@playwright/test'; |
2 | 2 | import { PageSelectors, SidebarSelectors } from '../../support/selectors'; |
3 | 3 | import { generateRandomEmail, setupPageErrorHandling, TestConfig } from '../../support/test-config'; |
4 | 4 | import { signInAndWaitForApp } from '../../support/auth-flow-helpers'; |
@@ -35,6 +35,82 @@ async function getAuthToken(page: Page): Promise<string> { |
35 | 35 | return page.evaluate(() => localStorage.getItem('af_auth_token') || ''); |
36 | 36 | } |
37 | 37 |
|
| 38 | +async function expectApiSuccess<T = unknown>(response: APIResponse, action: string): Promise<T> { |
| 39 | + let body: { code?: number; data?: T; message?: string } | null = null; |
| 40 | + |
| 41 | + try { |
| 42 | + body = await response.json(); |
| 43 | + } catch { |
| 44 | + const text = await response.text().catch(() => ''); |
| 45 | + |
| 46 | + throw new Error(`${action} failed: HTTP ${response.status()} ${text}`); |
| 47 | + } |
| 48 | + |
| 49 | + if (!response.ok() || body?.code !== 0) { |
| 50 | + throw new Error(`${action} failed: HTTP ${response.status()} ${JSON.stringify(body)}`); |
| 51 | + } |
| 52 | + |
| 53 | + return body.data as T; |
| 54 | +} |
| 55 | + |
| 56 | +async function guestCanAccessSharedPage( |
| 57 | + request: APIRequestContext, |
| 58 | + guestAuthToken: string, |
| 59 | + workspaceId: string, |
| 60 | + viewId: string, |
| 61 | +): Promise<boolean> { |
| 62 | + const response = await request.get(`${TestConfig.apiUrl}/api/workspace/${workspaceId}/page-view/${viewId}`, { |
| 63 | + headers: { Authorization: `Bearer ${guestAuthToken}` }, |
| 64 | + failOnStatusCode: false, |
| 65 | + }); |
| 66 | + const body = await response.json().catch(() => null) as { code?: number; data?: { view?: unknown } } | null; |
| 67 | + |
| 68 | + return response.ok() && body?.code === 0 && Boolean(body?.data?.view); |
| 69 | +} |
| 70 | + |
| 71 | +async function joinSharedWorkspaceIfInviteCodeRequired( |
| 72 | + request: APIRequestContext, |
| 73 | + guestAuthToken: string, |
| 74 | + workspaceId: string, |
| 75 | + viewId: string, |
| 76 | + guestEmail: string, |
| 77 | +) { |
| 78 | + if (await guestCanAccessSharedPage(request, guestAuthToken, workspaceId, viewId)) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const codesResponse = await request.get( |
| 83 | + `${TestConfig.apiUrl}/api/sharing/workspace/${workspaceId}/view/${viewId}/guest-invite-codes`, |
| 84 | + { headers: { Authorization: `Bearer ${guestAuthToken}` } }, |
| 85 | + ); |
| 86 | + const codesData = await expectApiSuccess<{ codes: Array<{ code: string; email: string }> }>( |
| 87 | + codesResponse, |
| 88 | + 'List guest invite codes', |
| 89 | + ); |
| 90 | + const inviteCode = codesData.codes.find((item) => item.email.toLowerCase() === guestEmail.toLowerCase())?.code; |
| 91 | + |
| 92 | + if (!inviteCode) { |
| 93 | + throw new Error(`Guest cannot access shared page and no guest invite code was found for ${guestEmail}`); |
| 94 | + } |
| 95 | + |
| 96 | + const joinResponse = await request.post( |
| 97 | + `${TestConfig.apiUrl}/api/sharing/workspace/${workspaceId}/join-by-guest-invite-code`, |
| 98 | + { |
| 99 | + headers: { Authorization: `Bearer ${guestAuthToken}`, 'Content-Type': 'application/json' }, |
| 100 | + data: { code: inviteCode }, |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + await expectApiSuccess(joinResponse, 'Join shared workspace by guest invite code'); |
| 105 | + |
| 106 | + await expect |
| 107 | + .poll( |
| 108 | + () => guestCanAccessSharedPage(request, guestAuthToken, workspaceId, viewId), |
| 109 | + { timeout: 30000, intervals: [1000, 2000, 3000] }, |
| 110 | + ) |
| 111 | + .toBe(true); |
| 112 | +} |
| 113 | + |
38 | 114 | async function createPageWithContent(page: Page, title: string, content: string): Promise<string> { |
39 | 115 | const viewId = await createDocumentPageAndNavigate(page); |
40 | 116 |
|
@@ -86,7 +162,7 @@ async function shareViewWithGuest( |
86 | 162 | data: { view_id: viewId, emails: [guestEmail], access_level: 50, auto_confirm: true }, |
87 | 163 | }, |
88 | 164 | ); |
89 | | - if (!response.ok()) throw new Error(`Failed to share view: ${response.status()}`); |
| 165 | + await expectApiSuccess(response, 'Share view with guest'); |
90 | 166 | } |
91 | 167 |
|
92 | 168 | async function getUserProfile(request: APIRequestContext, authToken: string) { |
@@ -119,7 +195,11 @@ async function waitForEditorContent(page: Page, maxAttempts = 3): Promise<void> |
119 | 195 | // ── Setup: Annie creates and shares a page ─────────────────────────── |
120 | 196 |
|
121 | 197 | async function annieCreatesAndSharesPage( |
122 | | - browser: Browser, request: APIRequestContext, ownerEmail: string, guestEmail: string, |
| 198 | + browser: Browser, |
| 199 | + request: APIRequestContext, |
| 200 | + ownerEmail: string, |
| 201 | + guestEmail: string, |
| 202 | + guestAuthToken: string, |
123 | 203 | ): Promise<{ ownerWorkspaceId: string; sharedViewId: string; directLink: string }> { |
124 | 204 | const ownerContext = await browser.newContext(); |
125 | 205 | const ownerPage = await ownerContext.newPage(); |
@@ -147,6 +227,14 @@ async function annieCreatesAndSharesPage( |
147 | 227 |
|
148 | 228 | await shareViewWithGuest(request, ownerToken, ownerWorkspaceId, sharedViewId, guestEmail); |
149 | 229 | testLog.info(`Annie shared page with guest (auto_confirm=true)`); |
| 230 | + await joinSharedWorkspaceIfInviteCodeRequired( |
| 231 | + request, |
| 232 | + guestAuthToken, |
| 233 | + ownerWorkspaceId, |
| 234 | + sharedViewId, |
| 235 | + guestEmail, |
| 236 | + ); |
| 237 | + testLog.info('Guest access to Annie workspace confirmed'); |
150 | 238 |
|
151 | 239 | const directLink = `/app/${ownerWorkspaceId}/${sharedViewId}`; |
152 | 240 | await ownerContext.close(); |
@@ -191,7 +279,7 @@ test.describe('Shared View Cross-Workspace Routing', () => { |
191 | 279 |
|
192 | 280 | // And: Annie creates a page in her workspace and shares it with Nathan |
193 | 281 | const { ownerWorkspaceId, directLink } = await annieCreatesAndSharesPage( |
194 | | - browser, request, ownerEmail, guestEmail, |
| 282 | + browser, request, ownerEmail, guestEmail, guestToken, |
195 | 283 | ); |
196 | 284 | expect(ownerWorkspaceId).not.toBe(guestWorkspaceId); |
197 | 285 | testLog.info(`Direct link to Annie's page: ${directLink}`); |
@@ -257,7 +345,7 @@ test.describe('Shared View Cross-Workspace Routing', () => { |
257 | 345 |
|
258 | 346 | // And: Annie creates a page and shares it with Nathan |
259 | 347 | const { ownerWorkspaceId, directLink } = await annieCreatesAndSharesPage( |
260 | | - browser, request, ownerEmail, guestEmail, |
| 348 | + browser, request, ownerEmail, guestEmail, guestToken, |
261 | 349 | ); |
262 | 350 | expect(guestWorkspaceId).not.toBe(ownerWorkspaceId); |
263 | 351 | testLog.info(`Annie's direct link: ${directLink}`); |
@@ -301,7 +389,7 @@ test.describe('Shared View Cross-Workspace Routing', () => { |
301 | 389 |
|
302 | 390 | // And: Annie creates a page and shares it with Nathan |
303 | 391 | const { sharedViewId } = await annieCreatesAndSharesPage( |
304 | | - browser, request, ownerEmail, guestEmail, |
| 392 | + browser, request, ownerEmail, guestEmail, guestToken, |
305 | 393 | ); |
306 | 394 |
|
307 | 395 | // When: Nathan navigates using his OWN workspace ID with the shared view ID |
|
0 commit comments