cumulocity-cypress
Version:
Cypress commands for Cumulocity IoT
157 lines (156 loc) • 6.04 kB
JavaScript
import { getAuthType, isAuthOptions, isPactAuthObject, toPactAuthObject, } from "../auth";
import { getCreatedObjectId, toPactRequest, toPactResponse, } from "./c8ypact";
import _ from "lodash";
/**
* Default implementation of C8yPactRecord. Use C8yDefaultPactRecord.from to create
* a C8yPactRecord from a Cypress.Response object or an C8yPactRecord object.
*/
export class C8yDefaultPactRecord {
constructor(requestOrParams, response, options, auth, createdObject, modifiedResponse, id) {
// Handle object parameter style
if (isC8yDefaultPactRecordInit(requestOrParams)) {
const params = requestOrParams;
this.request = params.request;
this.response = params.response;
if (params.options)
this.options = params.options;
if (params.auth)
this.auth = params.auth;
if (params.createdObject)
this.createdObject = params.createdObject;
if (params.modifiedResponse)
this.modifiedResponse = params.modifiedResponse;
if (params.id)
this.id = params.id;
}
else {
// Handle individual parameter style
this.request = requestOrParams;
this.response = response;
if (options)
this.options = options;
if (auth)
this.auth = auth;
if (createdObject)
this.createdObject = createdObject;
if (modifiedResponse)
this.modifiedResponse = modifiedResponse;
if (id)
this.id = id;
}
if (this.request?.method?.toLowerCase() === "post") {
const newId = getCreatedObjectId(this.response);
if (newId) {
this.createdObject = newId;
}
}
}
/**
* Creates a C8yPactRecord from a Cypress.Response or an C8yPactRecord object.
* @param obj The Cypress.Response<any> or C8yPactRecord object.
* @param auth The auth information to use.
* @param client The C8yClient for options and auth information.
* @param id The optional ID for the pact record.
*/
static from(obj, auth, client, id) {
// if (obj == null) return obj;
if ("request" in obj && "response" in obj) {
return new C8yDefaultPactRecord(_.get(obj, "request"), _.get(obj, "response"), _.get(obj, "options") || {}, _.get(obj, "auth"), _.get(obj, "createdObject"), _.get(obj, "modifiedResponse"), id || _.get(obj, "id"));
}
const r = _.cloneDeep(obj);
return new C8yDefaultPactRecord(toPactRequest(r) || {}, toPactResponse(r) || {}, client?._options, isAuthOptions(auth) || isPactAuthObject(auth)
? toPactAuthObject(auth)
: client?._auth
? toPactAuthObject(client?._auth)
: undefined, undefined, undefined, id || _.get(obj, "id"));
}
/**
* Returns the date of the response.
*/
date() {
const date = _.get(this.response, "headers.date");
if ((date && _.isString(date)) || _.isNumber(date) || _.isDate(date)) {
const result = new Date(date);
if (!isNaN(result.getTime())) {
return result;
}
}
return null;
}
/**
* Converts the C8yPactRecord to a Cypress.Response object.
*/
toCypressResponse() {
const result = _.cloneDeep(this.response);
_.extend(result, {
...(result.status && {
isOkStatusCode: result.status > 199 && result.status < 300,
}),
...(this.request.headers && {
requestHeaders: Object.fromEntries(Object.entries(this.request.headers || [])),
}),
...(this.request.url && { url: this.request.url }),
...(result.allRequestResponses && { allRequestResponses: [] }),
...(this.request.body && { requestBody: this.request.body }),
method: this.request.method || this.response.method || "GET",
});
return result;
}
hasRequestHeader(key) {
return Object.keys(this.request.headers ?? {})
.map((k) => k.toLowerCase())
.includes(key?.toLowerCase());
}
authType() {
const type = getAuthType(this.auth);
if (type != null)
return type;
if (this.hasRequestHeader("x-xsrf-token")) {
return "CookieAuth";
}
if (this.hasRequestHeader("authorization")) {
return "BasicAuth";
}
return undefined;
}
}
export function createPactRecord(response, client, options = {}) {
let auth = undefined;
const envUser = options.loggedInUser;
const envAlias = options.loggedInUserAlias;
const envType = options.authType;
const envAuth = {
...(envUser && { user: envUser }),
...(envAlias && { userAlias: envAlias }),
...(envAlias && { type: envType ?? "CookieAuth" }),
};
if (client?._auth) {
// do not pick the password. passwords must not be stored in the pact.
auth = _.defaultsDeep(client._auth, envAuth);
if (client._auth.constructor != null) {
if (!auth) {
auth = { type: client._auth.constructor.name };
}
else {
auth.type = client._auth.constructor.name;
}
}
}
if (!auth && (envUser || envAlias)) {
auth = envAuth;
}
// only store properties that need to be exposed. do not store password.
auth = auth ? toPactAuthObject(auth) : auth;
return C8yDefaultPactRecord.from(response, auth, client, options.id);
}
/**
* Type guard to check if an object is C8yDefaultPactRecordInit
*/
function isC8yDefaultPactRecordInit(obj) {
return (obj &&
typeof obj === "object" &&
"request" in obj &&
"response" in obj &&
obj.request != null &&
obj.response != null);
}