ToolJet/cypress-tests/cypress/commands/apiCommands.js

494 lines
15 KiB
JavaScript
Raw Normal View History

const envVar = Cypress.env("environment");
Cypress.Commands.add("apiCreateApp", (appName = "testApp") => {
cy.window({ log: false }).then((win) => {
win.localStorage.setItem("walkthroughCompleted", "true");
});
cy.getCookie("tj_auth_token", { log: false }).then((cookie) => {
Cypress.env("authToken", `tj_auth_token=${cookie.value}`);
cy.request({
method: "POST",
2024-12-04 06:47:53 +00:00
url: `${Cypress.env("server_host")}/api/apps`,
headers: {
"Tj-Workspace-Id": Cypress.env("workspaceId"),
2023-12-21 10:09:47 +00:00
Cookie: `tj_auth_token = ${cookie.value}`,
},
body: {
type: "front-end",
name: appName,
is_maintenance_on: false,
organization_id: "",
user_id: "",
created_at: "",
updated_at: "",
id: "",
is_public: null,
workflow_enabled: false,
creation_mode: "DEFAULT",
},
}).then((response) => {
{
log: false;
}
expect(response.status).to.equal(201);
Cypress.env("appId", response.allRequestResponses[0]["Response Body"].id);
2025-10-03 16:20:25 +00:00
Cypress.env(
"user_id",
response.allRequestResponses[0]["Response Body"].user_id
);
Cypress.log({
name: "App create",
displayName: "APP CREATED",
message: `: ${response.body.name}`,
});
});
});
});
2025-11-04 07:43:27 +00:00
Cypress.Commands.add("apiDeleteApp", (appId = Cypress.env("appId")) => {
cy.getCookie("tj_auth_token", { log: false }).then((cookie) => {
Cypress.env("authToken", `tj_auth_token=${cookie.value}`);
cy.request(
{
method: "DELETE",
2025-11-07 07:31:27 +00:00
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
2025-11-04 07:43:27 +00:00
headers: {
"Tj-Workspace-Id": Cypress.env("workspaceId"),
Cookie: Cypress.env("authToken"),
},
2025-11-04 07:43:27 +00:00
},
{ log: false }
).then((response) => {
expect(response.status).to.equal(200);
Cypress.log({
name: "App Delete",
displayName: "APP DELETED",
2025-11-07 07:31:27 +00:00
message: `:${appId}`,
});
});
2025-11-04 07:43:27 +00:00
});
});
Cypress.Commands.add(
"openApp",
(
slug = "",
workspaceId = Cypress.env("workspaceId"),
appId = Cypress.env("appId"),
componentSelector = "[data-cy='drag-and-drop-a-component-label']"
) => {
cy.intercept("GET", "/api/apps/*").as("getAppData");
cy.window({ log: false }).then((win) => {
win.localStorage.setItem("walkthroughCompleted", "true");
});
cy.visit(`/${workspaceId}/apps/${appId}/${slug}`);
cy.wait("@getAppData").then((interception) => {
const responseData = interception.response.body;
2025-04-25 08:26:18 +00:00
Cypress.env("editingVersionId", responseData.editing_version.id);
Cypress.env("environmentId", responseData.editorEnvironment.id);
});
cy.get(componentSelector, { timeout: 10000 });
}
);
Cypress.Commands.add("apiAddQuery", (queryName, query, dataQueryId) => {
2023-12-21 10:09:47 +00:00
cy.getCookie("tj_auth_token").then((cookie) => {
const headers = {
"Tj-Workspace-Id": Cypress.env("workspaceId"),
Cookie: `tj_auth_token=${cookie.value}`,
};
cy.apiGetAppData(Cypress.env("appId")).then((appData) => {
const editingVersionId = appData.editing_version.id;
cy.request({
method: "PATCH",
url: `${Cypress.env("server_host")}/api/data-queries/${dataQueryId}/versions/${editingVersionId}`,
headers: headers,
body: {
name: queryName,
options: {
mode: "sql",
transformationLanguage: "javascript",
enableTransformation: false,
query: query,
},
2023-12-21 10:09:47 +00:00
},
}).then((patchResponse) => {
expect(patchResponse.status).to.equal(200);
});
2023-12-21 10:09:47 +00:00
});
});
});
Cypress.Commands.add(
"apiAddQueryToApp",
2025-10-15 09:03:22 +00:00
({ queryName, options, dataSourceName, dsKind }) => {
cy.getCookie("tj_auth_token", { log: false }).then((cookie) => {
2025-09-16 15:54:44 +00:00
const authToken = cookie?.value;
const workspaceId = Cypress.env("workspaceId");
const appId = Cypress.env("appId");
2025-09-16 15:54:44 +00:00
const commonHeaders = {
"tj-workspace-id": workspaceId,
Cookie: `tj_auth_token=${authToken}; app_id=${appId}`,
};
cy.request({
method: "GET",
2024-12-04 06:47:53 +00:00
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
2025-09-16 15:54:44 +00:00
headers: commonHeaders,
}).then((appResponse) => {
const editingVersionId = appResponse.body.editing_version.id;
2025-09-16 15:54:44 +00:00
const currentEnvironmentId = appResponse.body.editorEnvironment.id;
Cypress.env("version-id", editingVersionId);
2025-09-16 15:54:44 +00:00
Cypress.env("environmentVersion-id", currentEnvironmentId);
cy.request({
2025-09-16 15:54:44 +00:00
method: "GET",
url: `${Cypress.env("server_host")}/api/data-sources/${workspaceId}/environments/${currentEnvironmentId}/versions/${editingVersionId}`,
headers: commonHeaders,
}).then((dsResponse) => {
const dataSource = dsResponse.body.data_sources.find(
2025-10-15 09:03:22 +00:00
(ds) => ds.name === dataSourceName
2025-09-16 15:54:44 +00:00
);
const dataSourceID = dataSource.id;
2025-10-15 09:03:22 +00:00
Cypress.env(`${dataSourceName}`, dataSourceID);
2025-09-16 15:54:44 +00:00
cy.request({
method: "POST",
url: `${Cypress.env("server_host")}/api/data-queries/data-sources/${dataSourceID}/versions/${editingVersionId}`,
headers: {
"Content-Type": "application/json",
"tj-workspace-id": workspaceId,
Cookie: `tj_auth_token=${authToken}; app_id=${appId}`,
},
body: {
app_id: appId,
app_version_id: editingVersionId,
name: queryName,
kind: dsKind,
options: options,
data_source_id: dataSourceID,
plugin_id: null,
},
}).then((queryResponse) => {
expect(queryResponse.status).to.eq(201);
Cypress.env("query-id", queryResponse.body.id);
Cypress.log({
name: "apiAddQueryToApp",
displayName: "QUERY CREATED",
message: `${queryName} (${dsKind})`,
});
});
});
});
});
}
);
Cypress.Commands.add(
"apiAddComponentToApp",
(
appName,
componentName,
layoutConfig = {},
componentType = "Text",
componentValue = "default"
) => {
cy.getAppId(appName).then((appId) => {
const defaultLayout = {
desktop: { top: 90, left: 9, width: 6, height: 40 },
mobile: { top: 90, left: 9, width: 6, height: 40 },
};
2024-12-04 06:47:53 +00:00
const layouts = {
desktop: { ...defaultLayout.desktop, ...layoutConfig.desktop },
mobile: { ...defaultLayout.mobile, ...layoutConfig.mobile },
};
2024-12-04 06:47:53 +00:00
cy.getCookie("tj_auth_token", { log: false }).then((cookie) => {
Cypress.env("authToken", `tj_auth_token=${cookie.value}`);
2024-12-04 06:47:53 +00:00
cy.request({
method: "GET",
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
headers: {
"Tj-Workspace-Id": Cypress.env("workspaceId"),
Cookie: `tj_auth_token=${cookie.value}`,
},
}).then((response) => {
expect(response.status).to.eq(200);
2024-12-04 06:47:53 +00:00
const { id: editingVersionId, home_page_id: homePageId } =
response.body.editing_version;
const componentId = crypto.randomUUID
? crypto.randomUUID()
: require("uuid").v4();
let finalProperties = {};
if (componentType === "Text") {
finalProperties = {
text: { value: `${componentValue}` },
};
} else if (componentType === "TextInput") {
finalProperties = {
value: { value: `${componentValue}` },
};
}
const requestBody = {
is_user_switched_version: false,
pageId: homePageId,
diff: {
[componentId]: {
name: componentName,
layouts: layouts,
type: componentType,
properties: finalProperties,
},
},
};
cy.request({
method: "POST",
url: `${Cypress.env("server_host")}/api/v2/apps/${appId}/versions/${editingVersionId}/components`,
headers: {
"Content-Type": "application/json",
"Tj-Workspace-Id": Cypress.env("workspaceId"),
Cookie: `tj_auth_token=${cookie.value}`,
},
body: requestBody,
}).then((postResponse) => {
expect(postResponse.status).to.eq(201);
cy.log(`Component ${componentId} added successfully`);
});
});
});
});
}
);
Cypress.Commands.add("apiMakeAppPublic", (appId = Cypress.env("appId")) => {
cy.getAuthHeaders().then((headers) => {
cy.request({
method: "PUT",
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
headers: headers,
body: {
app: { is_public: true },
},
log: false,
}).then((response) => {
expect(response.status).to.equal(200);
});
});
});
Cypress.Commands.add("apiReleaseApp", (appName) => {
cy.getAppId(appName).then((appId) => {
cy.getAuthHeaders().then((headers) => {
cy.request({
method: "GET",
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
headers,
})
.then((response) => {
expect(response.status).to.eq(200);
const editingVersionId = response.body.editing_version.id;
cy.request({
method: "PUT",
url: `${Cypress.env("server_host")}/api/apps/${appId}/release`,
headers: headers,
body: {
versionToBeReleased: editingVersionId,
},
});
})
.then((res) => {
expect(res.status).to.eq(200);
});
});
});
});
Cypress.Commands.add("apiAddAppSlug", (appName, slug) => {
cy.getAppId(appName).then((appId) => {
cy.getAuthHeaders().then((headers) => {
cy.request({
method: "PUT",
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
headers: headers,
body: {
app: {
slug: slug,
},
},
}).then((response) => {
expect(response.status).to.eq(200);
cy.log("App slug updated successfully");
});
});
});
});
Cypress.Commands.add("apiGetAppData", (appId = Cypress.env("appId")) => {
cy.getAuthHeaders().then((headers) => {
cy.request({
method: "GET",
url: `${Cypress.env("server_host")}/api/apps/${appId}`,
headers: headers,
}).then((response) => {
expect(response.status).to.equal(200);
return response.body;
});
});
});
Cypress.Commands.add("apiRunQuery", () => {
cy.getCookie("tj_auth_token", { log: false }).then((cookie) => {
const authToken = cookie?.value;
const workspaceId = Cypress.env("workspaceId");
const appId = Cypress.env("appId");
const queryId = Cypress.env("query-id");
const versionId = Cypress.env("version-id");
const currentEnvironmentId = Cypress.env("environmentVersion-id");
cy.request({
method: "POST",
url: `${Cypress.env("server_host")}/api/data-queries/${queryId}/versions/${versionId}/run/${currentEnvironmentId}`,
headers: {
"Content-Type": "application/json",
"tj-workspace-id": workspaceId,
Cookie: `tj_auth_token=${authToken}; app_id=${appId}`,
},
body: {},
}).then((runResponse) => {
expect(runResponse.status).to.eq(201);
Cypress.log({
name: "apiRunQuery",
displayName: "QUERY RUN",
message: `Ran query ${queryId} (version ${versionId})`,
});
});
});
2025-09-16 15:45:28 +00:00
});
2025-10-03 16:20:25 +00:00
Cypress.Commands.add("apiUpdateGlobalSettings", (globalSettings) => {
cy.getCookie("tj_auth_token")
.should("exist")
.then((cookie) => {
return cy
.request({
method: "PUT",
2025-10-06 04:58:28 +00:00
url: `${Cypress.env("server_host")}/api/v2/apps/${Cypress.env("appId")}/versions/${Cypress.env("editingVersionId")}/global_settings`,
2025-10-03 16:20:25 +00:00
body: { globalSettings },
headers: {
"Content-Type": "application/json",
"tj-workspace-id": Cypress.env("workspaceId") || "",
Cookie: `tj_auth_token=${cookie.value}`,
},
failOnStatusCode: false,
})
.then((response) => {
expect(response.status, "update global settings status").to.eq(200);
return response.body;
});
});
});
Cypress.Commands.add(
"apiPromoteAppVersion",
2025-11-07 07:31:27 +00:00
(
targetEnvId = Cypress.env("environmentId"),
appId = Cypress.env("appId")
) => {
2025-10-03 16:20:25 +00:00
cy.getCookie("tj_auth_token").then((cookie) => {
cy.request({
method: "PUT",
url: `${Cypress.env("server_host")}/api/v2/apps/${appId}/versions/${Cypress.env("editingVersionId")}/promote`,
2025-10-03 16:20:25 +00:00
body: { currentEnvironmentId: targetEnvId },
headers: {
"Content-Type": "application/json",
"Tj-Workspace-Id": Cypress.env("workspaceId"),
Cookie: `tj_auth_token=${cookie.value}`,
},
}).then((response) => {
expect(response.status, "Promote app version status").to.eq(200);
const editorEnv = response.body.editorEnvironment;
if (editorEnv.name === "staging") {
Cypress.env("stagingEnvId", editorEnv.id);
} else if (editorEnv.name === "production") {
Cypress.env("productionEnvId", editorEnv.id);
}
Cypress.log({
name: "App promoted",
message: `Environment: ${editorEnv.name} (${editorEnv.id})`,
});
return response.body;
});
});
}
);
2025-10-26 12:50:13 +00:00
Cypress.Commands.add("apiGetAppIdByName", (appName) => {
return cy.getAuthHeaders().then((headers) => {
return cy
.request({
method: "GET",
url: `${Cypress.env("server_host")}/api/apps`,
headers: headers,
log: false,
})
.then((response) => {
expect(response.status).to.equal(200);
const app = response.body.apps.find((app) => app.name === appName);
expect(app, `App with name "${appName}" not found`).to.exist;
return app.id;
});
});
2025-11-04 07:43:27 +00:00
});
Feature/draft versioning support (#14284) * added app_versions fields * added data migration for backward compatibility * added ce specific logic * fixed ce migration (need to dev-test) * moved to data migration * migration changes * added endpoint to create draft version * backend changes * added draft to app_import scenario * added version description * minor changes (needs improvement) * fixed breaking dropdown in editor * updated submodule commits * revert package.json * revert ui not used changes * submodule changes * reverting non used files * ui changes * ui changes * ui changes * ui changes * ui changes * copywriting changes * ui changes * ui changes * edit version modal changes * ui integration changes * added button solid and removed unused css * removed commented code from create version modal * updated button size to use large * ui changes * draft version modal changes * added sub-module commits to main * draft version endpoint changes * ui changes for draft version modal * fix breaking ui * ui changes for banner * minor ui changes * remove scss changes from themes file * removed unused components (cleanup) * removed unused components (pr cleanup) * draft version changes * create version modal changes * canvas banner fixes * comment creation logic * refactor: version dropdown * update endpoint changes * fix: promote logic * update submodule * fix: released version and create version modal * fix draft version creation * minor ui changes * minor backend fixes * tooltip changes * added all components in same folder * added minor comments * import fixes * refactor files * fix: overlay issues * fix: on version creation * fix ce bugs * bug fixes * bug fixes * bug fixes * bug fixes * base merge * feat: draft versioning support with UI enhancements and backend adjustments - Updated AppCanvas to conditionally render AppCanvasBanner based on edit mode. - Enhanced CreateDraftVersionModal to handle version selection and creation logic. - Modified CreateVersionModal to streamline version creation process and handle commits. - Improved ReleaseConfirmation to include development versions in release context. - Refactored CreateDraftButton and VersionDropdownItem for better UI consistency and dark mode support. - Updated VersionManagerDropdown to manage draft versions and improve version selection logic. - Enhanced version switcher and promote/release buttons with dark mode styling. - Adjusted server-side features and constants to support new draft versioning capabilities. - Updated styles across components for better visual consistency and responsiveness. * minor fixes * rebase * merge base * update submodule * Add data-cy attribute for draft version components * Update cypress test cases for draft version feature * Update failing test cases * Update draft version test cases * Skip older flow * migration changes * migration fixes * Update the failed test cases * removed multiple api calls * fix: version set on draft creation * fixes * fix: version update on save version * fixes * name fix * fix version lock banner styling * bump version to 3.20.50-lts across all components --------- Co-authored-by: Vijaykant Yadav <[email protected]> Co-authored-by: ajith-k-v <[email protected]> Co-authored-by: gsmithun4 <[email protected]>
2025-12-05 16:43:00 +00:00
Cypress.Commands.add(
"apiPublishDraftVersion",
(
versionName,
versionDescription = "",
appId = Cypress.env("appId"),
editingVersionId = Cypress.env("editingVersionId")
) => {
cy.getCookie("tj_auth_token").then((cookie) => {
cy.request({
method: "PUT",
url: `${Cypress.env("server_host")}/api/v2/apps/${appId}/versions/${editingVersionId}`,
body: {
is_user_switched_version: false,
name: versionName,
description: versionDescription,
status: "PUBLISHED",
},
headers: {
"Content-Type": "application/json",
"tj-workspace-id": Cypress.env("workspaceId"),
Cookie: `tj_auth_token=${cookie.value}`,
},
}).then((response) => {
expect(response.status, "Publish draft version status").to.eq(200);
Cypress.log({
name: "Version published",
displayName: "VERSION PUBLISHED",
message: `Version: ${versionName} (${editingVersionId})`,
});
return response.body;
});
});
}
);