cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
164 lines (163 loc) • 5.37 kB
JavaScript
import { isAbsoluteURL, normalizeUrl } from "../../shared/c8ypact/url";
import { C8yDefaultPact, pactId, validatePactMode, validatePactRecordingMode, } from "../../shared/c8ypact";
import { to_boolean } from "../../shared/util";
import * as pactutils from "./pactutils";
const { _ } = Cypress;
if (_.get(Cypress, "__c8yctrl.initialized") === undefined) {
_.set(Cypress, "__c8yctrl.initialized", true);
Cypress.c8yctrl = {
mode,
recordingMode,
get current() {
return _current;
},
isEnabled,
setCurrent,
resetCurrent,
url,
isRecordingEnabled: () => {
const modeValue = mode();
return isEnabled() && ["recording", "record"].includes(modeValue);
},
isMockingEnabled: () => {
const modeValue = mode();
return isEnabled() && ["mock", "apply"].includes(modeValue);
},
debugLog: false,
};
beforeEach(function () {
let consoleProps = {};
let logger = undefined;
if (Cypress.c8yctrl.debugLog === true) {
consoleProps = {
id: Cypress.c8ypact.getCurrentTestId() || null,
current: Cypress.c8yctrl.current || null,
isEnabled: Cypress.c8yctrl.isEnabled(),
isRecordingEnabled: Cypress.c8yctrl.isRecordingEnabled(),
isMockingEnabled: Cypress.c8yctrl.isMockingEnabled(),
mode: Cypress.c8yctrl.mode() || null,
url: Cypress.c8yctrl.url() || null,
recordingMode: Cypress.c8yctrl.recordingMode(),
debugLog: Cypress.c8yctrl.debugLog,
cypressEnv: Cypress.env(),
};
logger = Cypress.log({
name: "c8yctrl",
displayName: "c8yctrl",
message: `init`,
consoleProps: () => consoleProps,
});
}
if (!isEnabled()) {
logger?.end();
return;
}
try {
validatePactMode(Cypress.env("C8YCTRL_MODE"));
validatePactRecordingMode(Cypress.env("C8YCTRL_RECORDING_MODE"));
}
catch (error) {
logger?.end();
throw error;
}
Cypress.c8yctrl
.setCurrent({
id: Cypress.c8ypact.getCurrentTestId(),
mode: mode(),
recordingMode: recordingMode(),
})
.then((response) => {
consoleProps.setCurrentResponse = response;
consoleProps.current = Cypress.c8yctrl.current || null;
logger?.end();
if (response?.ok === false) {
throw new Error(`Failed to update current test in ${url()}/c8yctrl/current. c8yctrl returned ${response.status} ${response.statusText}.`);
}
});
});
afterEach(() => {
if (!isEnabled())
return;
Cypress.c8yctrl.resetCurrent();
});
}
let _current = null;
const C8yCtrlSetCurrentDefaults = {
clear: false,
id: Cypress.c8ypact.getCurrentTestId(),
mode: "apply",
recordingMode: "append",
};
function mode() {
return pactutils.mode("C8YCTRL_MODE");
}
function recordingMode() {
return pactutils.recordingMode("C8YCTRL_RECORDING_MODE");
}
function isEnabled() {
if (url() == null)
return false;
const ignore = to_boolean(Cypress.env("C8YCTRL_IGNORE"), false);
if (ignore === true)
return false;
return pactutils.isEnabled("C8YCTRL_MODE");
}
function setCurrent(options) {
const o = _.defaults(options ?? {}, {
mode: mode(),
recordingMode: recordingMode(),
id: Cypress.c8ypact.getCurrentTestId(),
clear: recordingMode() === "refresh",
}, C8yCtrlSetCurrentDefaults);
const values = ["recording", "record"];
if (values.includes(Cypress.env("C8YCTRL_MODE"))) {
o.mode = "record";
}
if (options?.id == null && options?.title == null) {
throw new Error("Either id or title must be provided to setCurrent.");
}
const id = pactId(o.id ?? o.title);
let clear = false;
if (o.clear === true ||
o.recordingMode === "refresh" ||
Cypress.c8yctrl.recordingMode() === "refresh") {
clear = true;
}
const parameter = `?mode=${o.mode}&recordingMode=${o.recordingMode}${clear ? "&clear=true" : ""}`;
return cy
.task("c8ypact:fetch", {
url: `${url()}/c8yctrl/current${parameter}&id=${id}`,
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "{}",
})
.then((response) => {
if (response == null || response.ok === false || response.body == null) {
_current = null;
return null;
}
const pact = C8yDefaultPact.from(response.body);
_current = pact;
return response;
});
}
function resetCurrent() {
return cy
.task("c8ypact:fetch", {
url: `${url()}/c8yctrl/current`,
method: "DELETE",
})
.then((response) => {
_current = null;
return cy.wrap(response);
});
}
function url() {
const u = Cypress.env("C8YCTRL_URL");
if (u != null && isAbsoluteURL(u)) {
return normalizeUrl(u);
}
return Cypress.config().baseUrl;
}