123 lines
3.1 KiB
JavaScript
123 lines
3.1 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
|
|
const root = path.resolve(__dirname, "..");
|
|
|
|
function createResponse(body, contentType) {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
statusText: "OK",
|
|
headers: {
|
|
get(name) {
|
|
return name.toLowerCase() === "content-type" ? contentType : null;
|
|
},
|
|
forEach(callback) {
|
|
callback(contentType, "content-type");
|
|
}
|
|
},
|
|
text: async () => body
|
|
};
|
|
}
|
|
|
|
function loadBackground(responses, createdTabs, removedTabs) {
|
|
let listener;
|
|
const source = fs.readFileSync(path.join(root, "src", "background.js"), "utf8");
|
|
const context = {
|
|
chrome: {
|
|
runtime: {
|
|
onMessage: {
|
|
addListener(callback) {
|
|
listener = callback;
|
|
}
|
|
}
|
|
},
|
|
tabs: {
|
|
async create(details) {
|
|
createdTabs.push(details);
|
|
return { id: 27 };
|
|
},
|
|
async remove(tabId) {
|
|
removedTabs.push(tabId);
|
|
}
|
|
}
|
|
},
|
|
fetch: async () => {
|
|
const response = responses.shift();
|
|
|
|
if (!response) {
|
|
throw new Error("Unexpected fetch call.");
|
|
}
|
|
|
|
return response;
|
|
},
|
|
Date,
|
|
Error,
|
|
JSON,
|
|
Map,
|
|
Set,
|
|
Promise,
|
|
String,
|
|
Array,
|
|
Object,
|
|
Number,
|
|
Math,
|
|
setTimeout,
|
|
clearTimeout
|
|
};
|
|
|
|
vm.runInNewContext(source, context);
|
|
return listener;
|
|
}
|
|
|
|
function dispatch(listener) {
|
|
return new Promise((resolve) => {
|
|
assert.equal(listener({ type: "opportunitiesExtension.requestConsumerTokenService" }, {
|
|
tab: { windowId: 1 }
|
|
}, resolve), true);
|
|
});
|
|
}
|
|
|
|
async function run() {
|
|
const createdTabs = [];
|
|
const removedTabs = [];
|
|
const listener = loadBackground([
|
|
createResponse("<html><body>Fallback</body></html>", "text/html"),
|
|
createResponse(JSON.stringify([
|
|
{
|
|
tokenService: "ACCP-TS",
|
|
uiaasHeader: { label: "Account-Ti-Authorization", value: "account-token" }
|
|
},
|
|
{
|
|
tokenService: "OCICONS-TS",
|
|
uiaasHeader: { label: "G2m-Authorization", value: "consumption-token" }
|
|
}
|
|
]), "application/json")
|
|
], createdTabs, removedTabs);
|
|
const response = await dispatch(listener);
|
|
|
|
assert.equal(response.ok, true);
|
|
assert.equal(response.authorizations.accountSummary.value, "account-token");
|
|
assert.equal(response.authorizations.accountPlans.value, "consumption-token");
|
|
assert.equal(createdTabs.length, 1);
|
|
assert.equal(createdTabs[0].active, false);
|
|
assert.equal(
|
|
createdTabs[0].url,
|
|
"https://spa.oracle.com/oalcrm/web/api/g2m-consumer-application/ui/index.html"
|
|
);
|
|
assert.deepEqual(removedTabs, [27]);
|
|
assert.equal(response.debugRequests.length, 2);
|
|
assert.equal(response.debugRequests[0].status, "failed");
|
|
assert.equal(response.debugRequests[0].metadata.sessionBootstrapState, "opened-inactive-tab");
|
|
assert.equal(response.debugRequests[1].metadata.sessionBootstrapState, "closed-after-valid-json");
|
|
|
|
console.log("Consumer token session retry tests passed.");
|
|
}
|
|
|
|
run().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|