Files
plugins/packages/search1api/tests/oauth.test.mjs
T
wang2 6a2ade35ef search1api 0.1.1: Magpie attribution, reused OAuth client, send_feedback through Search1API (#424)
search1api 0.1.1 (fatwang2):
- Sign-ins carry utm_source=magpie and API requests send User-Agent search1api-magpie/0.1.1.
- The registered public OAuth client and its loopback port are saved in search1api-oauth-client.json (mode 600) and reused. A busy port, another issuer, or a sign-in that doesn't finish leads to a new registration.
- A send_feedback tool posts to Search1API's own POST /feedback with the caller's credential, on the same request path as the other tools, including the OAuth refresh on 401. Its description lists what is sent (message, intent, category, request_id, model) and says Search1API stores it on agentback.app, linked to the signed-in account. The plugin contacts no other host. A report that can't be sent returns a note and doesn't fail the task.

Run at the reviewed head d48c8c8 (sandbox HOME/TMPDIR, Bun 1.4.2):
- bun test packages/search1api/tests: 23 pass, 0 fail
- bun scripts/check.mjs search1api: ok
- PR CI (test, on-npm): pass
- grep of the package: the only host is api.search1api.com (plus 127.0.0.1 for the callback). No hard-coded keys. agentback.app appears only in the description, the README and a test.
- Drove the built plugin against my own loopback fake API:
  - OAuth access revoked (401): POST /feedback with the old bearer, then /token refresh_token, then POST /feedback with the new bearer. Got "Feedback recorded (fb_1)" and auth.set with the new token.
  - Bad API key: a 401 gives "not recorded ... (401)" with no refresh, and the error body is not echoed.
  - Body is exactly {message, intent, category, request_id, agent:{name:"Magpie", model}}, with UA search1api-magpie/0.1.1.
  - Unknown args and a non-string model are refused before any request.
- An unauthenticated probe of the real api.search1api.com: POST /feedback gives 401, and an unknown path gives 404. So the endpoint exists.
- The OAuth client reuse (unchanged since 95f236f) was run by hand in the previous review.

Not tried: a real Search1API account (none available).
2026-10-27 15:44:05 +08:00

99 lines
4.4 KiB
JavaScript

import assert from "node:assert/strict";
import { access, writeFile } from "node:fs/promises";
import { createServer } from "node:net";
import { join } from "node:path";
import test from "node:test";
import { startOAuth } from "../src/oauth.ts";
import { fixture } from "./helpers.mjs";
const registrations = (f) => f.calls.filter((call) => call.path === "/register").length;
async function signIn(login) {
await fetch(login.url);
return login.callback();
}
test("browser OAuth uses PKCE S256, public registration, state and issuer checks", async (t) => {
const f = await fixture(t);
const login = await startOAuth(f.baseUrl, 1000);
const authURL = new URL(login.url);
assert.equal(authURL.searchParams.get("code_challenge_method"), "S256");
assert.equal(authURL.searchParams.get("utm_source"), "magpie");
const redirect = new URL(authURL.searchParams.get("redirect_uri"));
redirect.searchParams.set("state", "wrong");
redirect.searchParams.set("code", "wrong");
assert.equal((await fetch(redirect)).status, 400);
redirect.searchParams.set("state", authURL.searchParams.get("state"));
redirect.searchParams.set("iss", "https://wrong.example");
assert.equal((await fetch(redirect)).status, 400);
const browser = await fetch(login.url);
assert.equal(browser.status, 200);
const response = await login.callback();
assert.equal(response.type, "success");
assert.equal(response.access, "test-access-0");
assert.equal(response.refresh, "test-refresh-0");
assert.equal(response.clientId, "test-public-client");
assert.equal(f.state.grants, 1);
assert.equal((await login.callback()).type, "failed");
assert.equal(f.state.grants, 1);
});
test("declined consent and abandoned sign-ins finish without leaking tokens or callback listeners", async (t) => {
const f = await fixture(t);
f.state.decline = true;
const declined = await startOAuth(f.baseUrl, 1000);
await fetch(declined.url);
assert.match((await declined.callback()).error, /declined/);
const abandoned = await startOAuth(f.baseUrl, 40);
assert.match((await abandoned.callback()).error, /timed out/);
const redirect = new URL(abandoned.url).searchParams.get("redirect_uri");
await assert.rejects(fetch(redirect));
});
test("sign-ins reuse the registered client and its callback port", async (t) => {
const f = await fixture(t);
const file = join(f.directory, "search1api-oauth-client.json");
const first = await startOAuth(f.baseUrl, 1000, file);
assert.equal((await signIn(first)).type, "success");
const second = await startOAuth(f.baseUrl, 1000, file);
assert.equal(
new URL(second.url).searchParams.get("redirect_uri"),
new URL(first.url).searchParams.get("redirect_uri"),
);
const result = await signIn(second);
assert.equal(result.type, "success");
assert.equal(result.clientId, "test-public-client");
assert.equal(registrations(f), 1);
assert.equal(f.state.grants, 2);
});
test("a busy callback port or a saved client for another issuer registers anew", async (t) => {
const f = await fixture(t);
const file = join(f.directory, "search1api-oauth-client.json");
const first = await startOAuth(f.baseUrl, 1000, file);
await signIn(first);
const port = new URL(new URL(first.url).searchParams.get("redirect_uri")).port;
const blocker = createServer();
await new Promise((resolve) => blocker.listen(Number(port), "127.0.0.1", resolve));
t.after(() => blocker.close());
const busy = await startOAuth(f.baseUrl, 1000, file);
assert.notEqual(new URL(new URL(busy.url).searchParams.get("redirect_uri")).port, port);
assert.equal((await signIn(busy)).type, "success");
assert.equal(registrations(f), 2);
await writeFile(file, JSON.stringify({ issuer: "https://other.example", clientId: "x", redirectUri: "http://127.0.0.1:1/callback" }));
await signIn(await startOAuth(f.baseUrl, 1000, file));
assert.equal(registrations(f), 3);
});
test("a reused client that does not finish signing in is forgotten", async (t) => {
const f = await fixture(t);
const file = join(f.directory, "search1api-oauth-client.json");
await signIn(await startOAuth(f.baseUrl, 1000, file));
f.state.decline = true;
assert.equal((await signIn(await startOAuth(f.baseUrl, 1000, file))).type, "failed");
await assert.rejects(access(file));
f.state.decline = false;
assert.equal((await signIn(await startOAuth(f.baseUrl, 1000, file))).type, "success");
assert.equal(registrations(f), 2);
});