Skip to content

Commit 86082eb

Browse files
committed
test(ui): the live member's API requests carry the member alone
Inside @playwright/test a request context created without a storageState inherits the config's use.storageState, the global setup's admin cookie. The read-only member spec sent the member's Bearer from such a context, so every premise request named two accounts, and since the one-account rule (#31469) the data route answers it 401. memberRequestContext() builds the member's context from the member's own cookie plus its Bearer, and the member's sign-in starts from an empty state. No assertion in the spec moves. Claude-Session: https://claude.ai/code/session_01ADHkmPpwZNVKwJLQGotdJy Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3487ccc commit 86082eb

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎ui/e2e/live/member.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ import { request, expect, type APIRequestContext } from '@playwright/test';
2626
* admin: the better-auth cookie for the API origin and the Bearer token the
2727
* console adapter reads from the APP origin's localStorage. It is returned,
2828
* never written to disk, so no path has to be rooted.
29+
*
30+
* ⛔ Every request context this module builds names its `storageState`. Inside
31+
* `@playwright/test`, a request context created without one inherits the
32+
* config's `use.storageState`, which is the global setup's ADMIN session
33+
* cookie (measured: `playwright.request.newContext()` and the imported
34+
* `request.newContext()` both start with that cookie). A member's Bearer sent
35+
* from such a context is a request signed in as two accounts, which the server
36+
* refuses since objectstack#31469 (`CREDENTIALS_NAME_DIFFERENT_ACCOUNTS`; a data
37+
* route answers its own 401). So the sign-in starts from an empty state, and a
38+
* spec acting as the member over the API uses {@link memberRequestContext}.
2939
*/
3040
const APP = process.env.LIVE_APP_URL || 'http://localhost:5180';
3141
const API = process.env.LIVE_API_URL || 'http://localhost:3000';
@@ -68,7 +78,8 @@ export async function signInMember(admin: APIRequestContext, permissionSet: stri
6878
).toBe('USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL');
6979
}
7080

71-
const ctx = await request.newContext();
81+
// An empty state, not the inherited admin cookie: the sign-in names the member only.
82+
const ctx = await request.newContext({ storageState: { cookies: [], origins: [] } });
7283
try {
7384
const res = await ctx.post(`${API}/api/v1/auth/sign-in/email`, {
7485
data: { email, password: MEMBER_PASSWORD },
@@ -90,3 +101,16 @@ export async function signInMember(admin: APIRequestContext, permissionSet: stri
90101
await ctx.dispose();
91102
}
92103
}
104+
105+
/**
106+
* A request context that acts as `member` and as no one else: the member's own
107+
* session cookie and its Bearer token (objectstack#31827). Its `storageState`
108+
* is named so the config's admin cookie is not inherited (see the module
109+
* comment). The caller disposes it.
110+
*/
111+
export async function memberRequestContext(member: LiveMember): Promise<APIRequestContext> {
112+
return request.newContext({
113+
storageState: { cookies: member.storageState.cookies, origins: [] },
114+
extraHTTPHeaders: { Authorization: `Bearer ${member.token}` },
115+
});
116+
}

‎ui/e2e/live/read-only-member-record.spec.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect, type Page } from '@playwright/test';
2-
import { signInMember } from './member';
2+
import { memberRequestContext, signInMember } from './member';
33
import { writesRecordsOf } from './data-writes';
44

55
/**
@@ -49,7 +49,6 @@ test('a member who may read but not write an inquiry gets a read-only form and n
4949
browser,
5050
page,
5151
request,
52-
playwright,
5352
}) => {
5453
// Three page loads in two browser contexts, one of them cold.
5554
test.setTimeout(120_000);
@@ -66,7 +65,7 @@ test('a member who may read but not write an inquiry gets a read-only form and n
6665
const member = await signInMember(request, 'showcase_manager');
6766

6867
await test.step('premise: the server lets the member read the record and refuses its update', async () => {
69-
const api = await playwright.request.newContext({ extraHTTPHeaders: { Authorization: `Bearer ${member.token}` } });
68+
const api = await memberRequestContext(member);
7069
try {
7170
const read = await api.get(`${API}/api/v1/data/${OBJECT}/${id}`);
7271
expect(read.status(), 'the member reads the admin-owned inquiry').toBe(200);

0 commit comments

Comments
 (0)