Skip to content

Commit

Permalink
test: Add e2e tests Dashboard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-code committed Sep 30, 2024
1 parent 4169bd2 commit 251ba36
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ jobs:
useSession,
checkWorkflows,
rstudioSession,
dashboardV2,
]

steps:
Expand Down
78 changes: 78 additions & 0 deletions cypress-tests/cypress/e2e/dashboardV2.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { getRandomString, validateLogin } from "../support/commands/general";
import { generatorProjectName } from "../support/commands/projects";
import { ProjectIdentifierV2 } from "../support/commands/projectsV2";

const projectTestConfig = {
projectAlreadyExists: false,
projectName: generatorProjectName("dashboardV2"),
};

const prefixProjectTitle = "My Renku Project";
const sessionId = ["dashboardV2", getRandomString()];

describe("Dashboard v2, Authenticated Users", () => {
const projectIdentifier: ProjectIdentifierV2 = {
slug: projectTestConfig.projectName,
id: null,
namespace: null,
};

after(() => {
if (!projectTestConfig.projectAlreadyExists && projectIdentifier.id != null){
cy.deleteProjectFromAPIV2(projectIdentifier);
cy.getProjectByNamespaceAPIV2(projectIdentifier).then((response) => {
expect(response.status).to.equal(404);
});
}

});

beforeEach(() => {
// Restore the session
cy.session(
sessionId,
() => {
cy.robustLogin();
},
validateLogin
);
cy.getUserNamespaceAPIV2().then((namespace) => {
if (namespace) {
projectIdentifier.namespace = namespace;
cy.createProjectIfMissingAPIV2({
visibility: "private",
name: `${prefixProjectTitle} ${projectIdentifier.slug}`,
namespace,
slug: projectIdentifier.slug,
}).then((project) => projectIdentifier.id=project.id)
} else {
cy.log('No user namespace found, project cannot be created.');
}
});
});

it("Can see own project on the dashboard", () => {
cy.visit("v2");
cy.getDataCy("dashboard-project-list").find("a").should("have.length.at.least", 1);
cy.getDataCy("dashboard-project-list").find("a").should("contain.text", `${prefixProjectTitle} ${projectIdentifier.slug}`);
cy.getDataCy("dashboard-project-list").find("a").should("contain.text", projectIdentifier.slug);
});

it("Can find project in the search", () => {
cy.visit("v2");
cy.getDataCy("view-my-projects-btn").click();
cy.getDataCy("search-card").should("have.length.at.least", 1);
cy.getDataCy("search-card").should("contain.text", `${prefixProjectTitle} ${projectIdentifier.slug}`);
});
});

describe("Dashboard v2, Non-Authenticated Users", () => {

it("Cannot see projects and groups on Dashboard when logged out", () => {
cy.visit("v2");
cy.getDataCy("projects-container").contains("No 2.0 projects.");
cy.getDataCy("view-other-projects-btn").should("be.visible");
cy.getDataCy("groups-container").contains("No 2.0 groups.");
cy.getDataCy("view-other-groups-btn").should("be.visible");
});
});
74 changes: 74 additions & 0 deletions cypress-tests/cypress/support/commands/projectsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
export type ProjectIdentifierV2 = {
slug: string;
namespace?: string;
id?: string;
};

interface NewProjectV2Props extends ProjectIdentifierV2 {
visibility?: "public" | "private";
name: string;
}

/** Get the namespace of the logged in user from the API. */
function getUserNamespaceAPIV2(): Cypress.Chainable<string | null> {
return cy.request({ failOnStatusCode: false, method: "GET", url: `api/data/namespaces?minimum_role=owner` })
.then((response) => {
if (response.status === 200) {
const userNamespace = response.body?.filter((namespace) => namespace.namespace_kind === "user");
return userNamespace && userNamespace.length > 0 ? userNamespace[0].slug : null;
}
return null;
});
}

/** Get a project by using only the API. */
function getProjectByNamespaceAPIV2(newProjectProps: ProjectIdentifierV2): Cypress.Chainable<any | null> {
return cy.request({ failOnStatusCode: false, method: "GET", url: `api/data/projects/${newProjectProps.namespace}/${newProjectProps.slug}` });
}

/** Create a project (if the project is missing) by using only the API. */
function createProjectIfMissingAPIV2(newProjectProps: NewProjectV2Props) {
return getProjectByNamespaceAPIV2(newProjectProps)
.then((response) => {
if (response.status != 200) {
return cy.request({
method: "POST",
url: "api/data/projects",
body: newProjectProps,
headers: {
'Content-Type': 'application/json'
}
});
} else {
return response.body;
}
});
}

/** Delete a project by using only the API. */
function deleteProjectFromAPIV2(projectIdentifier: ProjectIdentifierV2) {
return cy.request({
failOnStatusCode: false,
method: "DELETE",
url: `api/data/projects/${projectIdentifier.id}`,
});
}

export default function registerProjectV2Commands() {
Cypress.Commands.add("createProjectIfMissingAPIV2", createProjectIfMissingAPIV2);
Cypress.Commands.add("deleteProjectFromAPIV2", deleteProjectFromAPIV2);
Cypress.Commands.add("getUserNamespaceAPIV2", getUserNamespaceAPIV2);
Cypress.Commands.add("getProjectByNamespaceAPIV2", getProjectByNamespaceAPIV2);
}

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
createProjectIfMissingAPIV2(newProjectProps: NewProjectV2Props);
deleteProjectFromAPIV2(identifier: ProjectIdentifierV2);
getUserNamespaceAPIV2();
getProjectByNamespaceAPIV2(identifier: ProjectIdentifierV2);
}
}
}
2 changes: 2 additions & 0 deletions cypress-tests/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import registerGeneralCommands from "./commands/general";
import registerLoginCommands from "./commands/login";
import registerProjectCommands from "./commands/projects";
import registerSessionCommands from "./commands/sessions";
import registerProjectV2Commands from "./commands/projectsV2";

registerGeneralCommands();
registerLoginCommands();
registerProjectCommands();
registerSessionCommands();
registerDatasetsCommands();
registerProjectV2Commands();

Cypress.on("uncaught:exception", (err) => {
return false;
Expand Down
2 changes: 2 additions & 0 deletions cypress-tests/cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import registerGeneralCommands from "./commands/general";
import registerLoginCommands from "./commands/login";
import registerProjectCommands from "./commands/projects";
import registerSessionCommands from "./commands/sessions";
import registerProjectV2Commands from "./commands/projectsV2";

registerGeneralCommands();
registerLoginCommands();
registerProjectCommands();
registerSessionCommands();
registerDatasetsCommands();
registerProjectV2Commands();

0 comments on commit 251ba36

Please sign in to comment.