Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion tests/strategy_switch_worker_validation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";

import { __test } from "../web/strategy-switch-console/worker.js";
import worker, { __test } from "../web/strategy-switch-console/worker.js";

const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const indexHtml = readFileSync(resolve(root, "web/strategy-switch-console/index.html"), "utf8");
Expand Down Expand Up @@ -51,6 +51,17 @@ const crossOriginError = captureError(
assert.match(crossOriginError.message, /cross-origin request rejected/);
assert.equal(crossOriginError.status, 403);

const unauthorizedSyncResponse = await worker.fetch(
new Request("https://switch.example/api/internal/sync-account-default", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "{}",
}),
{ STRATEGY_SWITCH_SYNC_TOKEN: "test-sync-token" },
);
assert.equal(unauthorizedSyncResponse.status, 401);
assert.match((await unauthorizedSyncResponse.json()).error, /internal sync token is invalid/);

assert.equal(
await __test.withTimeout(new Promise((resolve) => setTimeout(() => resolve("late"), 25)), 1, "fallback"),
"fallback",
Expand Down
16 changes: 9 additions & 7 deletions web/strategy-switch-console/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@ export default {
async fetch(request, env) {
const url = new URL(request.url);
try {
if (url.pathname === "/login") return startLogin(request, env);
if (url.pathname === "/callback") return finishLogin(request, env);
if (url.pathname === "/admin") return adminPage(request, env);
if (url.pathname === "/login") return await startLogin(request, env);
if (url.pathname === "/callback") return await finishLogin(request, env);
if (url.pathname === "/admin") return await adminPage(request, env);
if (url.pathname === "/api/session") return json(await sessionPayload(request, env));
if (url.pathname === "/api/strategy-profiles") return json(await strategyProfilesPayload(env));
if (url.pathname === "/api/config") return json(await configPayload(request, env));
if (url.pathname === "/api/admin/config" && request.method === "GET") return adminConfigResponse(request, env);
if (url.pathname === "/api/admin/config" && request.method === "GET") {
return await adminConfigResponse(request, env);
}
if (url.pathname === "/api/admin/config" && request.method === "POST") {
return saveAdminConfig(request, env);
return await saveAdminConfig(request, env);
}
if (url.pathname === "/api/internal/sync-account-default" && request.method === "POST") {
return syncAccountDefaultResponse(request, env);
return await syncAccountDefaultResponse(request, env);
}
if (url.pathname === "/api/logout" && request.method === "POST") return logout(request);
if (url.pathname === "/api/switch" && request.method === "POST") return dispatchSwitch(request, env);
if (url.pathname === "/api/switch" && request.method === "POST") return await dispatchSwitch(request, env);
return html(PAGE_HTML);
} catch (error) {
return json({ ok: false, error: error.message || "unexpected error" }, error.status || 500);
Expand Down