2021-04-12 13:32:25 +00:00
|
|
|
import "@testing-library/cypress/add-commands";
|
2021-03-30 19:56:20 +00:00
|
|
|
|
|
|
|
// ***********************************************
|
|
|
|
// This example commands.js shows you how to
|
|
|
|
// create various custom commands and overwrite
|
|
|
|
// existing commands.
|
|
|
|
//
|
|
|
|
// For more comprehensive examples of custom
|
|
|
|
// commands please read more here:
|
|
|
|
// https://on.cypress.io/custom-commands
|
|
|
|
// ***********************************************
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a parent command --
|
|
|
|
// Cypress.Commands.add("login", (email, password) => { ... })
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a child command --
|
|
|
|
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This is a dual command --
|
|
|
|
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// -- This will overwrite an existing command --
|
|
|
|
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
Cypress.Commands.add("setup", () => {
|
|
|
|
cy.exec("make e2e-reset-db e2e-setup", { timeout: 20000 });
|
2021-03-30 19:56:20 +00:00
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
Cypress.Commands.add("login", (username, password) => {
|
|
|
|
username ||= "test";
|
|
|
|
password ||= "admin123#";
|
|
|
|
cy.request("POST", "/api/v1/fleet/login", { username, password }).then(
|
|
|
|
(resp) => {
|
|
|
|
window.localStorage.setItem("KOLIDE::auth_token", resp.body.token);
|
|
|
|
}
|
|
|
|
);
|
2021-03-30 19:56:20 +00:00
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
Cypress.Commands.add("logout", () => {
|
2021-03-30 19:56:20 +00:00
|
|
|
cy.request({
|
2021-04-12 13:32:25 +00:00
|
|
|
url: "/api/v1/fleet/logout",
|
|
|
|
method: "POST",
|
2021-03-30 19:56:20 +00:00
|
|
|
body: {},
|
|
|
|
auth: {
|
2021-04-12 13:32:25 +00:00
|
|
|
bearer: window.localStorage.getItem("KOLIDE::auth_token"),
|
2021-03-30 19:56:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
Cypress.Commands.add("setupSSO", (enable_idp_login = false) => {
|
2021-03-30 19:56:20 +00:00
|
|
|
const body = {
|
|
|
|
sso_settings: {
|
|
|
|
enable_sso: true,
|
|
|
|
enable_sso_idp_login: enable_idp_login,
|
2021-04-12 13:32:25 +00:00
|
|
|
entity_id: "https://localhost:8080",
|
|
|
|
idp_name: "SimpleSAML",
|
|
|
|
issuer_uri: "http://localhost:8080/simplesaml/saml2/idp/SSOService.php",
|
|
|
|
metadata_url: "http://localhost:9080/simplesaml/saml2/idp/metadata.php",
|
2021-03-30 19:56:20 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
cy.request({
|
2021-04-12 13:32:25 +00:00
|
|
|
url: "/api/v1/fleet/config",
|
|
|
|
method: "PATCH",
|
2021-03-30 19:56:20 +00:00
|
|
|
body,
|
|
|
|
auth: {
|
2021-04-12 13:32:25 +00:00
|
|
|
bearer: window.localStorage.getItem("KOLIDE::auth_token"),
|
2021-03-30 19:56:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
Cypress.Commands.add("loginSSO", () => {
|
2021-03-30 19:56:20 +00:00
|
|
|
// Note these requests set cookies that are required for the SSO flow to
|
|
|
|
// work properly. This is handled automatically by the browser.
|
|
|
|
cy.request({
|
2021-04-12 13:32:25 +00:00
|
|
|
method: "GET",
|
|
|
|
url:
|
|
|
|
"http://localhost:9080/simplesaml/saml2/idp/SSOService.php?spentityid=https://localhost:8080",
|
2021-03-30 19:56:20 +00:00
|
|
|
followRedirect: false,
|
|
|
|
}).then((firstResponse) => {
|
|
|
|
const redirect = firstResponse.headers.location;
|
|
|
|
|
|
|
|
cy.request({
|
2021-04-12 13:32:25 +00:00
|
|
|
method: "GET",
|
2021-03-30 19:56:20 +00:00
|
|
|
url: redirect,
|
|
|
|
followRedirect: false,
|
|
|
|
}).then((secondResponse) => {
|
2021-04-12 13:32:25 +00:00
|
|
|
const el = document.createElement("html");
|
2021-03-30 19:56:20 +00:00
|
|
|
el.innerHTML = secondResponse.body;
|
2021-04-12 13:32:25 +00:00
|
|
|
const authState = el.getElementsByTagName("input").namedItem("AuthState")
|
|
|
|
.defaultValue;
|
2021-03-30 19:56:20 +00:00
|
|
|
|
|
|
|
cy.request({
|
2021-04-12 13:32:25 +00:00
|
|
|
method: "POST",
|
2021-03-30 19:56:20 +00:00
|
|
|
url: redirect,
|
|
|
|
body: `username=user1&password=user1pass&AuthState=${authState}`,
|
|
|
|
form: true,
|
|
|
|
followRedirect: false,
|
|
|
|
}).then((finalResponse) => {
|
|
|
|
el.innerHTML = finalResponse.body;
|
2021-04-12 13:32:25 +00:00
|
|
|
const saml = el.getElementsByTagName("input").namedItem("SAMLResponse")
|
|
|
|
.defaultValue;
|
2021-03-30 19:56:20 +00:00
|
|
|
|
|
|
|
// Load the callback URL with the response from the IdP
|
|
|
|
cy.visit({
|
2021-04-12 13:32:25 +00:00
|
|
|
url: "/api/v1/fleet/sso/callback",
|
|
|
|
method: "POST",
|
2021-03-30 19:56:20 +00:00
|
|
|
body: {
|
|
|
|
SAMLResponse: saml,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-04-29 17:10:25 +00:00
|
|
|
|
|
|
|
Cypress.Commands.add("getEmails", () => {
|
|
|
|
return cy
|
|
|
|
.request("http://localhost:8025/api/v2/messages")
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.status).to.eq(200);
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
});
|