Skip to content

Commit

Permalink
refactor: use cy.session for anonymous user checks (#3802)
Browse files Browse the repository at this point in the history
Remove the `logout()` command and use `cy.session()` instead. This makes the tests less flaky.
  • Loading branch information
leafty authored and Panaetius committed Oct 17, 2024
1 parent be37421 commit a44c930
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
17 changes: 11 additions & 6 deletions cypress-tests/cypress/e2e/privateProject.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ describe("Basic public project functionality", () => {
},
validateLogin
);
cy.createProjectIfMissing({templateName: "Python", ...projectIdentifier, visibility: "private"});
cy.createProjectIfMissing({
templateName: "Python",
...projectIdentifier,
visibility: "private",
});
cy.visitAndLoadProject(projectIdentifier);
});

Expand All @@ -50,11 +54,11 @@ describe("Basic public project functionality", () => {
.should("be.checked");
cy.searchForProject(projectIdentifier, true);

// logout and search for the project and log back in
cy.logout();
// Check as an anonymous user
cy.session(["anonymous", getRandomString()], () => {});
cy.visit("/");
cy.get("#nav-hamburger").should("be.visible").click();
cy.searchForProject(projectIdentifier, false);
cy.robustLogin();
});

it("Can always search for project after changing the visibility", () => {
Expand Down Expand Up @@ -86,10 +90,11 @@ describe("Basic public project functionality", () => {

// Search the project as both logged in and logged out.
cy.searchForProject(projectIdentifier, true);
cy.logout();
// Check as an anonymous user
cy.session(["anonymous", getRandomString()], () => {});
cy.visit("/");
cy.get("#nav-hamburger").should("be.visible").click();
cy.searchForProject(projectIdentifier, false);
cy.robustLogin();
});

it("Deleting the project removes it from the search page", () => {
Expand Down
6 changes: 3 additions & 3 deletions cypress-tests/cypress/e2e/publicProject.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ describe("Basic public project functionality", () => {
cy.waitMetadataIndexing();
cy.searchForProject(projectIdentifier);

// logout and search for the project and log back in
cy.logout();
// Check as an anonymous user
cy.session(["anonymous", getRandomString()], () => {});
cy.visit("/");
cy.get("#nav-hamburger").should("be.visible").click();
cy.searchForProject(projectIdentifier);
cy.robustLogin();
});

it("Can see overview content and check the clone URLs", () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress-tests/cypress/e2e/useSession.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("Basic public project functionality", () => {

it("Start a new session as anonymous user.", () => {
// Do not re-use the logged-in session
cy.session("anonymous", () => {});
cy.session(["anonymous", getRandomString()], () => {});

// Log out and go to the project again
cy.visit("/");
Expand Down
23 changes: 8 additions & 15 deletions cypress-tests/cypress/support/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const renkuLogin = (credentials: { username: string; password: string }[]) => {
cy.wrap(credentials, { log: false }).each((credential: {password: string, username: string}) => {
cy.get("#username").type(credential.username);
cy.get("#password").type(credential.password, { log: false });
cy.get("#kc-login").click()
})
cy.wrap(credentials, { log: false }).each(
(credential: { password: string; username: string }) => {
cy.get("#username").type(credential.username);
cy.get("#password").type(credential.password, { log: false });
cy.get("#kc-login").click();
}
);
cy.url().then((url) => {
const parsedUrl = new URL(url);
if (
Expand Down Expand Up @@ -93,7 +95,7 @@ function registerAndVerify(props: RegisterAndVerifyProps) {
expect(["/", ""]).to.include(loc.pathname);
expect(loc.search).to.eq("");
expect(loc.hostname).to.eq(baseURL.hostname);
})
});
cy.get("header").should("be.visible");
cy.get("footer").should("be.visible");
// If we send a request to the user endpoint on Gitlab too quickly after we log in then
Expand Down Expand Up @@ -137,15 +139,7 @@ function robustLogin(props?: RobustLoginProps) {
);
}

function logout() {
cy.get("#profile-dropdown").should("be.visible").click();
cy.get("#logout-link").should("be.visible").click();
// Make sure we fully log out
cy.wait(15_000);
}

export default function registerLoginCommands() {
Cypress.Commands.add("logout", logout);
Cypress.Commands.add("renkuLogin", renkuLogin);
Cypress.Commands.add("register", register);
Cypress.Commands.add("registerAndVerify", registerAndVerify);
Expand All @@ -156,7 +150,6 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
logout();
renkuLogin(credentials: { username: string; password: string }[]);
register(
email: string,
Expand Down

0 comments on commit a44c930

Please sign in to comment.