MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / runBearerScoping

Function runBearerScoping

e2e/desktop/local-auth-mcp.test.ts:209–255  ·  view source on GitHub ↗
(runDir: string)

Source from the content-addressed store, hash-verified

207);
208
209const runBearerScoping = async (runDir: string) => {
210 const home = mkdtempSync(join(tmpdir(), "executor-desktop-scope-"));
211 const app = await launchDesktop(home, runDir);
212 try {
213 const page = await app.firstWindow({ timeout: 120_000 });
214 await page.getByText("Settings").first().waitFor({ timeout: 120_000 });
215
216 const { origin } = readSidecar(home);
217 const gatedUrl = `${origin}/api/scope`;
218
219 // The app's OWN window: the main process injects the bearer → not 401.
220 const mainStatus = await page.evaluate(
221 (url) =>
222 fetch(url)
223 .then((r) => r.status)
224 .catch(() => -1),
225 gatedUrl,
226 );
227 expect(mainStatus, "app window: injected bearer reaches the gated endpoint").not.toBe(401);
228
229 // A non-app BrowserWindow (what an OAuth popup is) in the SAME session: its
230 // requests to the sidecar must NOT get the bearer auto-attached → 401.
231 const popupStatus = await app.evaluate(
232 async ({ BrowserWindow }, { origin: o, gatedUrl: g }) => {
233 const popup = new BrowserWindow({
234 show: false,
235 webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true },
236 });
237 // Load the SPA (served unauthenticated) so the fetch has the right origin.
238 await popup.loadURL(`${o}/`);
239 const status = await popup.webContents.executeJavaScript(
240 `fetch(${JSON.stringify(g)}).then((r) => r.status).catch(() => -1)`,
241 );
242 popup.destroy();
243 return status as number;
244 },
245 { origin, gatedUrl },
246 );
247 expect(popupStatus, "popup webContents: no bearer injected → gated endpoint rejects it").toBe(
248 401,
249 );
250
251 await page.screenshot({ path: join(runDir, "01-bearer-scoping.png") });
252 } finally {
253 await closeWithVideo(app, runDir, home);
254 }
255};

Callers 1

Calls 5

readSidecarFunction · 0.85
closeWithVideoFunction · 0.85
launchDesktopFunction · 0.70
destroyMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected