UNPKG

cumulocity-cypress

Version:
120 lines (119 loc) 4.6 kB
import { sanitizeStringifiedObject } from "cumulocity-cypress/shared/util"; import { getAuthOptions, resetClient } from "../utils"; import { isAuthOptions } from "./auth"; const { _ } = Cypress; import { gte } from "semver"; export const defaultLoginOptions = () => { return { disableGainsight: Cypress.env("C8Y_DISABLE_GAINSIGHT") ?? true, hideCookieBanner: Cypress.env("C8Y_HIDE_COOKIEBANNER") ?? true, useSession: gte(Cypress.version, "12.0.0"), validationFn: () => { cy.getCookie("XSRF-TOKEN").should("exist"); cy.getCookie("authorization").should("exist"); }, }; }; Cypress.Commands.add("login", { prevSubject: "optional" }, (...args) => { const auth = getAuthOptions(...args); const consoleProps = { auth: auth || null, arguments: args || null, }; const logger = Cypress.log({ autoEnd: false, name: "login", message: sanitizeStringifiedObject(JSON.stringify(auth)), consoleProps: () => consoleProps, }); if (!auth) { logger.end(); throw new Error("Missing authentication. cy.login() requires authentication. Pass auth using cy.getAuth().login() " + "or cy.useAuth() and make sure a valid auth object was created from environment or arguments."); } (Cypress.isCy(auth) ? auth : cy.wrap(auth, { log: false })).then((auth) => { let options = {}; const lastArg = args.pop(); if (isAuthOptions(lastArg) || !_.isObjectLike(lastArg)) { options = defaultLoginOptions(); } else { options = _.defaults(lastArg, defaultLoginOptions()); } consoleProps.auth = auth || null; consoleProps.options = options || null; const loginRequest = (tenant) => { // cookie banner adds interception, set before login if (options.hideCookieBanner === true) { cy.hideCookieBanner(); } const tenant_id = tenant ? `?tenant_id=${tenant}` : ""; return cy .request({ method: "POST", // tenant_id is optional, if not provided, it will use the tenant from auth object or hostname url: `/tenant/oauth${tenant_id}`, body: { grant_type: "PASSWORD", username: auth?.user, password: auth?.password, tfa_code: auth?.tfa, }, form: true, }) .then((resp) => { expect(resp).to.have.property("headers"); if (options.disableGainsight === true) { cy.disableGainsight(); } }); }; const tenant = auth?.tenant || Cypress.env("C8Y_TENANT"); consoleProps.tenant = tenant || null; const cookieOptions = { domain: undefined, httpOnly: false, secure: false, path: "/", }; if (options.useSession === true) { cy.session(auth?.user || auth, () => { if (auth?.token != null && auth.xsrfToken != null) { cy.setCookie("authorization", auth.token, cookieOptions); cy.setCookie("XSRF-TOKEN", auth.xsrfToken, cookieOptions); } else { loginRequest(tenant); } }, { validate() { if (_.isFunction(options.validationFn)) { options?.validationFn(); } Cypress.env("C8Y_LOGGED_IN_USER", auth.user); Cypress.env("C8Y_LOGGED_IN_USER_ALIAS", auth.userAlias); }, cacheAcrossSpecs: true, }); } else { if (auth?.token != null && auth.xsrfToken != null) { cy.setCookie("authorization", auth.token, cookieOptions); cy.setCookie("XSRF-TOKEN", auth.xsrfToken, cookieOptions); } else { loginRequest(tenant).then(() => { if (_.isFunction(options.validationFn)) { options.validationFn(); } }); Cypress.env("C8Y_LOGGED_IN_USER", auth.user); Cypress.env("C8Y_LOGGED_IN_USER_ALIAS", auth.userAlias); } } resetClient(); }); cy.then(() => { logger.end(); }); });