80 lines
4.2 KiB
JavaScript
80 lines
4.2 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const { test } = require("node:test");
|
|
const { loadBackground, response } = require("./support/characterization.cjs");
|
|
const type = "opportunitiesExtension.requestInternalServiceRequest";
|
|
for (const family of ["chrome", "firefox"]) {
|
|
test(`${family}: 401 is returned to the caller without opening tabs`, async () => {
|
|
let count = 0;
|
|
const h = loadBackground({ family, fetch: async () => ++count < 3 ? response({}, 401) : response({ items: [] }) });
|
|
const result = await h.dispatch({ type, srId: "123" });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.httpStatus, 401);
|
|
assert.equal(count, 1);
|
|
assert.equal(h.tabs.length, 0);
|
|
assert.deepEqual(h.removedTabs, []);
|
|
assert.deepEqual(result.debugRequests.map(entry => entry.httpStatus), [401]);
|
|
});
|
|
}
|
|
test("concurrent 401 responses do not cause background tab recovery", async () => {
|
|
let calls = 0;
|
|
const h = loadBackground({ fetch: async url => {
|
|
if (++calls <= 2) return response({}, 401);
|
|
const id = new URL(url).searchParams.get("q");
|
|
return response({ items: [{ ServiceRequest_SRToInternalSR_Tgt: [{ Title: id }] }] });
|
|
} });
|
|
const results = await Promise.all([h.dispatch({ type, srId: "1" }), h.dispatch({ type, srId: "2" })]);
|
|
assert.equal(h.tabs.length, 0);
|
|
assert.deepEqual(h.removedTabs, []);
|
|
assert.ok(results.every(result => result.httpStatus === 401));
|
|
});
|
|
test("background never creates recovery tabs after an error", async () => {
|
|
for (const status of [401, 500]) {
|
|
let count = 0;
|
|
const h = loadBackground({ fetch: async () => response({}, ++count === 1 ? 401 : status) });
|
|
const result = await h.dispatch({ type, srId: "1" });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(count, 1);
|
|
assert.deepEqual(h.removedTabs, []);
|
|
}
|
|
const h = loadBackground({ fetch: async () => response({}, 403) });
|
|
assert.equal((await h.dispatch({ type, srId: "1" })).ok, false);
|
|
assert.equal(h.tabs.length, 0);
|
|
});
|
|
for (const family of ["chrome", "firefox"]) {
|
|
test(`${family}: SR detail GET uses exact scope and Session authentication`, async () => {
|
|
const calls = [];
|
|
const children = [{ SrId: 123, SrNumber: "SR123", Title: "Synthetic" }];
|
|
const h = loadBackground({ family, fetch: async (url, options) => {
|
|
calls.push({ url, options });
|
|
return response({ items: [{ ServiceRequest_SRToInternalSR_Tgt: children }] });
|
|
} });
|
|
const result = await h.dispatch({ type, srId: "987", token: "secret-test" });
|
|
assert.equal(result.ok, true);
|
|
assert.deepEqual(result.items, children);
|
|
const url = new URL(calls[0].url);
|
|
assert.equal(url.hostname, "comcipapic-oalprod.integration.ocp.oraclecloud.com");
|
|
assert.equal(url.searchParams.get("q"), "SrId=987");
|
|
assert.equal(url.searchParams.get("onlyData"), "true");
|
|
assert.ok(url.searchParams.get("fields").includes("ServiceRequest_SRToInternalSR_Tgt:ReportedHours_c"));
|
|
assert.equal(calls[0].options.method, "GET");
|
|
assert.equal(calls[0].options.headers.Authorization, "Session");
|
|
assert.equal(calls[0].options.credentials, "include");
|
|
assert.equal(result.debugRequests[0].requestHeaders.Authorization, "Session");
|
|
assert.ok(!JSON.stringify(result).includes("secret-test"));
|
|
assert.equal(result.debugRequests[0].name, "requestInternalServiceRequest");
|
|
});
|
|
}
|
|
test("SR details distinguish empty collections from errors and validate inputs", async () => {
|
|
for (const body of [{ items: [] }, { items: [{}] }, { items: [{ ServiceRequest_SRToInternalSR_Tgt: [] }] }]) {
|
|
const h = loadBackground({ fetch: async () => response(body) });
|
|
assert.deepEqual((await h.dispatch({ type, srId: "1" })).items, []);
|
|
}
|
|
for (const fetch of [async () => response({}, 403), async () => response({}), async () => response({ items: [{ ServiceRequest_SRToInternalSR_Tgt: {} }] })]) {
|
|
const h = loadBackground({ fetch });
|
|
assert.equal((await h.dispatch({ type, srId: "1", token: "test" })).ok, false);
|
|
}
|
|
const h = loadBackground({ fetch: () => assert.fail("Invalid input must not fetch") });
|
|
assert.equal((await h.dispatch({ type, srId: "1 OR 1=1", token: "test" })).ok, false);
|
|
assert.equal((await h.dispatch({ type })).ok, false);
|
|
});
|