2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-06-29 13:53:33 -04:00
|
|
|
import '@cypress/code-coverage/support';
|
2022-05-09 09:42:20 -04:00
|
|
|
import '@applitools/eyes-cypress/commands';
|
2018-10-15 19:52:19 -04:00
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
require('cy-verify-downloads').addCustomCommand();
|
|
|
|
|
2022-07-05 15:58:09 -04:00
|
|
|
const BASE_EXPLORE_URL = '/explore/?form_data=';
|
2022-04-01 04:06:48 -04:00
|
|
|
const TokenName = Cypress.env('TOKEN_NAME');
|
2022-09-12 10:48:36 -04:00
|
|
|
let DASHBOARD_FIXTURES: Record<string, any>[] = [];
|
|
|
|
let CHART_FIXTURES: Record<string, any>[] = [];
|
2018-09-06 17:55:48 -04:00
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add('loadChartFixtures', () =>
|
|
|
|
cy.fixture('charts.json').then(charts => {
|
|
|
|
CHART_FIXTURES = charts;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add('loadDashboardFixtures', () =>
|
|
|
|
cy.fixture('dashboards.json').then(dashboards => {
|
|
|
|
DASHBOARD_FIXTURES = dashboards;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
cy.login();
|
|
|
|
cy.loadChartFixtures();
|
|
|
|
cy.loadDashboardFixtures();
|
2022-10-24 11:25:36 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-09-12 10:48:36 -04:00
|
|
|
cy.cleanDashboards();
|
|
|
|
cy.cleanCharts();
|
|
|
|
});
|
|
|
|
|
2022-10-24 11:25:36 -04:00
|
|
|
Cypress.Commands.add('cleanDashboards', () => {
|
|
|
|
cy.login();
|
2022-09-12 10:48:36 -04:00
|
|
|
cy.getDashboards().then((sampleDashboards?: Record<string, any>[]) => {
|
|
|
|
const deletableDashboards = [];
|
|
|
|
for (let i = 0; i < DASHBOARD_FIXTURES.length; i += 1) {
|
|
|
|
const fixture = DASHBOARD_FIXTURES[i];
|
|
|
|
const isInDb = sampleDashboards?.find(
|
|
|
|
d => d.dashboard_title === fixture.dashboard_title,
|
|
|
|
);
|
|
|
|
if (isInDb) {
|
|
|
|
deletableDashboards.push(isInDb.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deletableDashboards.length) {
|
|
|
|
cy.request({
|
|
|
|
failOnStatusCode: false,
|
|
|
|
method: 'DELETE',
|
|
|
|
url: `api/v1/dashboard/?q=!(${deletableDashboards.join(',')})`,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
}).then(resp => resp);
|
|
|
|
}
|
2022-10-24 11:25:36 -04:00
|
|
|
});
|
|
|
|
});
|
2022-09-12 10:48:36 -04:00
|
|
|
|
2022-10-24 11:25:36 -04:00
|
|
|
Cypress.Commands.add('cleanCharts', () => {
|
|
|
|
cy.login();
|
2022-09-12 10:48:36 -04:00
|
|
|
cy.getCharts().then((sampleCharts?: Record<string, any>[]) => {
|
|
|
|
const deletableCharts = [];
|
|
|
|
for (let i = 0; i < CHART_FIXTURES.length; i += 1) {
|
|
|
|
const fixture = CHART_FIXTURES[i];
|
|
|
|
const isInDb = sampleCharts?.find(
|
|
|
|
c => c.slice_name === fixture.slice_name,
|
|
|
|
);
|
|
|
|
if (isInDb) {
|
|
|
|
deletableCharts.push(isInDb.id);
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 11:25:36 -04:00
|
|
|
if (deletableCharts.length) {
|
2022-09-12 10:48:36 -04:00
|
|
|
cy.request({
|
|
|
|
failOnStatusCode: false,
|
|
|
|
method: 'DELETE',
|
|
|
|
url: `api/v1/chart/?q=!(${deletableCharts.join(',')})`,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
}).then(resp => resp);
|
|
|
|
}
|
2022-10-24 11:25:36 -04:00
|
|
|
});
|
|
|
|
});
|
2022-09-12 10:48:36 -04:00
|
|
|
|
|
|
|
Cypress.Commands.add('getBySel', (selector, ...args) =>
|
|
|
|
cy.get(`[data-test=${selector}]`, ...args),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add('getBySelLike', (selector, ...args) =>
|
|
|
|
cy.get(`[data-test*=${selector}]`, ...args),
|
|
|
|
);
|
2022-05-05 19:39:14 -04:00
|
|
|
|
2021-01-05 20:44:02 -05:00
|
|
|
/* eslint-disable consistent-return */
|
|
|
|
Cypress.on('uncaught:exception', err => {
|
|
|
|
// ignore ResizeObserver client errors, as they are unrelated to operation
|
|
|
|
// and causing flaky test failures in CI
|
|
|
|
if (err.message && /ResizeObserver loop limit exceeded/.test(err.message)) {
|
|
|
|
// returning false here prevents Cypress from failing the test
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-06 08:01:31 -05:00
|
|
|
|
|
|
|
return false; // TODO:@geido remove
|
2021-01-05 20:44:02 -05:00
|
|
|
});
|
|
|
|
/* eslint-enable consistent-return */
|
|
|
|
|
2018-09-06 17:55:48 -04:00
|
|
|
Cypress.Commands.add('login', () => {
|
|
|
|
cy.request({
|
|
|
|
method: 'POST',
|
2018-10-11 17:13:04 -04:00
|
|
|
url: '/login/',
|
2018-09-06 17:55:48 -04:00
|
|
|
body: { username: 'admin', password: 'general' },
|
2019-11-27 17:02:48 -05:00
|
|
|
}).then(response => {
|
2018-09-06 17:55:48 -04:00
|
|
|
expect(response.status).to.eq(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add('preserveLogin', () => {
|
|
|
|
Cypress.Cookies.preserveOnce('session');
|
|
|
|
});
|
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
Cypress.Commands.add('visitChartByName', name => {
|
|
|
|
cy.request(`/chart/api/read?_flt_3_slice_name=${name}`).then(response => {
|
2018-09-06 17:55:48 -04:00
|
|
|
cy.visit(`${BASE_EXPLORE_URL}{"slice_id": ${response.body.pks[0]}}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-10 20:26:35 -05:00
|
|
|
Cypress.Commands.add('visitChartById', chartId =>
|
|
|
|
cy.visit(`${BASE_EXPLORE_URL}{"slice_id": ${chartId}}`),
|
|
|
|
);
|
2018-09-06 17:55:48 -04:00
|
|
|
|
2022-06-30 15:06:51 -04:00
|
|
|
Cypress.Commands.add(
|
|
|
|
'visitChartByParams',
|
|
|
|
(formData: {
|
|
|
|
datasource?: string;
|
|
|
|
datasource_id?: number;
|
|
|
|
datasource_type?: string;
|
|
|
|
[key: string]: unknown;
|
|
|
|
}) => {
|
|
|
|
let datasource_id;
|
|
|
|
let datasource_type;
|
|
|
|
if (formData.datasource_id && formData.datasource_type) {
|
|
|
|
({ datasource_id, datasource_type } = formData);
|
|
|
|
} else {
|
|
|
|
[datasource_id, datasource_type] = formData.datasource?.split('__') || [];
|
|
|
|
}
|
|
|
|
const accessToken = window.localStorage.getItem('access_token');
|
|
|
|
cy.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: 'api/v1/explore/form_data',
|
|
|
|
body: {
|
|
|
|
datasource_id,
|
|
|
|
datasource_type,
|
|
|
|
form_data: JSON.stringify(formData),
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
...(accessToken && {
|
|
|
|
Cookie: `csrf_access_token=${accessToken}`,
|
|
|
|
'X-CSRFToken': accessToken,
|
|
|
|
}),
|
|
|
|
...(TokenName && { Authorization: `Bearer ${TokenName}` }),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
}).then(response => {
|
|
|
|
const formDataKey = response.body.key;
|
2022-07-05 15:58:09 -04:00
|
|
|
const url = `/explore/?form_data_key=${formDataKey}`;
|
2022-06-30 15:06:51 -04:00
|
|
|
cy.visit(url);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2018-09-18 19:27:45 -04:00
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
Cypress.Commands.add('verifySliceContainer', chartSelector => {
|
2018-09-18 19:27:45 -04:00
|
|
|
// After a wait response check for valid slice container
|
2020-11-08 20:53:43 -05:00
|
|
|
cy.get('.slice_container')
|
|
|
|
.should('be.visible')
|
|
|
|
.within(() => {
|
|
|
|
if (chartSelector) {
|
|
|
|
cy.get(chartSelector)
|
|
|
|
.should('be.visible')
|
|
|
|
.then(chart => {
|
|
|
|
expect(chart[0].clientWidth).greaterThan(0);
|
|
|
|
expect(chart[0].clientHeight).greaterThan(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-06-29 13:53:33 -04:00
|
|
|
return cy;
|
2018-09-18 19:27:45 -04:00
|
|
|
});
|
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
Cypress.Commands.add(
|
|
|
|
'verifySliceSuccess',
|
2020-06-29 13:53:33 -04:00
|
|
|
({
|
|
|
|
waitAlias,
|
|
|
|
querySubstring,
|
|
|
|
chartSelector,
|
|
|
|
}: {
|
|
|
|
waitAlias: string;
|
|
|
|
chartSelector: JQuery.Selector;
|
2020-07-01 21:32:27 -04:00
|
|
|
querySubstring?: string | RegExp;
|
2020-06-29 13:53:33 -04:00
|
|
|
}) => {
|
2021-01-25 18:05:43 -05:00
|
|
|
cy.wait(waitAlias).then(({ response }) => {
|
2019-11-27 17:02:48 -05:00
|
|
|
cy.verifySliceContainer(chartSelector);
|
2021-01-25 18:05:43 -05:00
|
|
|
const responseBody = response?.body;
|
|
|
|
if (querySubstring) {
|
2021-01-29 06:12:09 -05:00
|
|
|
const query: string =
|
|
|
|
responseBody.query || responseBody.result[0].query || '';
|
2021-01-25 18:05:43 -05:00
|
|
|
if (querySubstring instanceof RegExp) {
|
|
|
|
expect(query).to.match(querySubstring);
|
|
|
|
} else {
|
|
|
|
expect(query).to.contain(querySubstring);
|
2020-06-29 13:53:33 -04:00
|
|
|
}
|
2021-01-25 18:05:43 -05:00
|
|
|
}
|
2019-11-27 17:02:48 -05:00
|
|
|
});
|
2020-06-29 13:53:33 -04:00
|
|
|
return cy;
|
2019-11-27 17:02:48 -05:00
|
|
|
},
|
|
|
|
);
|
2022-04-01 04:06:48 -04:00
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add('createSampleDashboards', (indexes?: number[]) =>
|
|
|
|
cy.cleanDashboards().then(() => {
|
|
|
|
for (let i = 0; i < DASHBOARD_FIXTURES.length; i += 1) {
|
|
|
|
if (indexes?.includes(i) || !indexes) {
|
|
|
|
cy.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: `/api/v1/dashboard/`,
|
|
|
|
body: DASHBOARD_FIXTURES[i],
|
|
|
|
failOnStatusCode: false,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
});
|
2022-04-01 04:06:48 -04:00
|
|
|
}
|
2022-09-12 10:48:36 -04:00
|
|
|
}
|
2022-04-01 04:06:48 -04:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add('createSampleCharts', (indexes?: number[]) =>
|
|
|
|
cy.cleanCharts().then(() => {
|
|
|
|
for (let i = 0; i < CHART_FIXTURES.length; i += 1) {
|
|
|
|
if (indexes?.includes(i) || !indexes) {
|
|
|
|
cy.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: `/api/v1/chart/`,
|
|
|
|
body: CHART_FIXTURES[i],
|
|
|
|
failOnStatusCode: false,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add(
|
|
|
|
'deleteDashboardByName',
|
|
|
|
(dashboardName: string, failOnStatusCode = false) =>
|
|
|
|
cy.getDashboards().then((sampleDashboards?: Record<string, any>[]) => {
|
|
|
|
const dashboard = sampleDashboards?.find(
|
|
|
|
d => d.dashboard_title === dashboardName,
|
|
|
|
);
|
|
|
|
if (dashboard) {
|
|
|
|
cy.deleteDashboard(dashboard.id, failOnStatusCode);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add(
|
|
|
|
'deleteDashboard',
|
|
|
|
(id: number, failOnStatusCode = false) =>
|
|
|
|
cy
|
|
|
|
.request({
|
|
|
|
failOnStatusCode,
|
|
|
|
method: 'DELETE',
|
|
|
|
url: `api/v1/dashboard/${id}`,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp),
|
2022-04-01 04:06:48 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add('getDashboards', () =>
|
|
|
|
cy
|
|
|
|
.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `api/v1/dashboard/`,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp.body.result),
|
|
|
|
);
|
|
|
|
|
2022-10-20 07:16:29 -04:00
|
|
|
Cypress.Commands.add('getDashboard', (dashboardId: string | number) =>
|
|
|
|
cy
|
|
|
|
.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `api/v1/dashboard/${dashboardId}`,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp.body.result),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add(
|
|
|
|
'updateDashboard',
|
|
|
|
(dashboardId: number, body: Record<string, any>) =>
|
|
|
|
cy
|
|
|
|
.request({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `api/v1/dashboard/${dashboardId}`,
|
|
|
|
body,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp.body.result),
|
|
|
|
);
|
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add('deleteChart', (id: number, failOnStatusCode = false) =>
|
2022-04-01 04:06:48 -04:00
|
|
|
cy
|
|
|
|
.request({
|
2022-09-12 10:48:36 -04:00
|
|
|
failOnStatusCode,
|
2022-04-01 04:06:48 -04:00
|
|
|
method: 'DELETE',
|
|
|
|
url: `api/v1/chart/${id}`,
|
|
|
|
headers: {
|
|
|
|
Cookie: `csrf_access_token=${window.localStorage.getItem(
|
|
|
|
'access_token',
|
|
|
|
)}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
'X-CSRFToken': `${window.localStorage.getItem('access_token')}`,
|
|
|
|
Referer: `${Cypress.config().baseUrl}/`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp),
|
|
|
|
);
|
|
|
|
|
|
|
|
Cypress.Commands.add('getCharts', () =>
|
|
|
|
cy
|
|
|
|
.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: `api/v1/chart/`,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${TokenName}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(resp => resp.body.result),
|
|
|
|
);
|
|
|
|
|
2022-09-12 10:48:36 -04:00
|
|
|
Cypress.Commands.add(
|
|
|
|
'deleteChartByName',
|
|
|
|
(sliceName: string, failOnStatusCode = false) =>
|
|
|
|
cy.getCharts().then((sampleCharts?: Record<string, any>[]) => {
|
|
|
|
const chart = sampleCharts?.find(c => c.slice_name === sliceName);
|
|
|
|
if (chart) {
|
|
|
|
cy.deleteChart(chart.id, failOnStatusCode);
|
2022-04-01 04:06:48 -04:00
|
|
|
}
|
2022-09-12 10:48:36 -04:00
|
|
|
}),
|
2022-04-01 04:06:48 -04:00
|
|
|
);
|