109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
executeAuthScopedQuery,
|
|
resolveAuthQueryScope,
|
|
} from "../src/api/auth-query-scope.ts";
|
|
|
|
test("waits for MSAL before an anonymous optional-auth query starts", () => {
|
|
const scope = resolveAuthQueryScope({
|
|
authUserId: null,
|
|
accountKey: null,
|
|
msalConfigured: true,
|
|
msalReady: false,
|
|
});
|
|
|
|
assert.equal(scope.ready, false);
|
|
assert.deepEqual(scope.cacheKey, [null, null]);
|
|
});
|
|
|
|
test("allows an anonymous query once MSAL initialization finishes", () => {
|
|
const scope = resolveAuthQueryScope({
|
|
authUserId: null,
|
|
accountKey: null,
|
|
msalConfigured: true,
|
|
msalReady: true,
|
|
});
|
|
|
|
assert.equal(scope.ready, true);
|
|
});
|
|
|
|
test("allows dev auth during MSAL startup and scopes its cache by user", () => {
|
|
const first = resolveAuthQueryScope({
|
|
authUserId: "dev-user-1",
|
|
accountKey: null,
|
|
msalConfigured: true,
|
|
msalReady: false,
|
|
});
|
|
const second = resolveAuthQueryScope({
|
|
authUserId: "dev-user-2",
|
|
accountKey: null,
|
|
msalConfigured: true,
|
|
msalReady: false,
|
|
});
|
|
|
|
assert.equal(first.ready, true);
|
|
assert.notDeepEqual(first.cacheKey, second.cacheKey);
|
|
});
|
|
|
|
test("separates optional-auth query caches by MSAL account", () => {
|
|
const first = resolveAuthQueryScope({
|
|
authUserId: null,
|
|
accountKey: "account-1",
|
|
msalConfigured: true,
|
|
msalReady: true,
|
|
});
|
|
const second = resolveAuthQueryScope({
|
|
authUserId: null,
|
|
accountKey: "account-2",
|
|
msalConfigured: true,
|
|
msalReady: true,
|
|
});
|
|
|
|
assert.notDeepEqual(first.cacheKey, second.cacheKey);
|
|
});
|
|
|
|
test("keeps the existing dev or anonymous request path when no account exists", async () => {
|
|
let tokenAcquisitions = 0;
|
|
|
|
const result = await executeAuthScopedQuery({
|
|
accountKey: null,
|
|
acquireAccessToken: () => {
|
|
tokenAcquisitions += 1;
|
|
return Promise.resolve("unused-token");
|
|
},
|
|
request: (authorizationHeader) =>
|
|
Promise.resolve(authorizationHeader ?? "dev-or-anonymous"),
|
|
});
|
|
|
|
assert.equal(result, "dev-or-anonymous");
|
|
assert.equal(tokenAcquisitions, 0);
|
|
});
|
|
|
|
test("does not run an anonymous request when account token acquisition fails", async () => {
|
|
let requests = 0;
|
|
|
|
await assert.rejects(
|
|
executeAuthScopedQuery({
|
|
accountKey: "account-1",
|
|
acquireAccessToken: () => Promise.reject(new Error("token failed")),
|
|
request: () => {
|
|
requests += 1;
|
|
return Promise.resolve("must-not-run");
|
|
},
|
|
}),
|
|
/token failed/,
|
|
);
|
|
assert.equal(requests, 0);
|
|
});
|
|
|
|
test("sends an explicit bearer token for an authenticated account", async () => {
|
|
const authorizationHeader = await executeAuthScopedQuery({
|
|
accountKey: "account-1",
|
|
acquireAccessToken: () => Promise.resolve("access-token"),
|
|
request: (header) => Promise.resolve(header),
|
|
});
|
|
|
|
assert.equal(authorizationHeader, "Bearer access-token");
|
|
});
|