From 5ed984bc0d9ac817fa49bee8b2dc6bcf50bee010 Mon Sep 17 00:00:00 2001 From: minnakt Date: Mon, 19 Feb 2024 21:22:11 -0500 Subject: [PATCH] Address feedback * No magic strings * Re-type function parameters * Document user types --- cypress/constants/index.ts | 12 ++++++++++++ .../integration/distroSettings/permissions.ts | 10 ++++++---- cypress/integration/nav_bar.ts | 4 ++-- cypress/support/commands.ts | 13 +++++-------- cypress/support/e2e.ts | 16 +++++++++++----- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/cypress/constants/index.ts b/cypress/constants/index.ts index b93b86707e..2dac31d7ea 100644 --- a/cypress/constants/index.ts +++ b/cypress/constants/index.ts @@ -1,2 +1,14 @@ export const EVG_BASE_URL = "http://localhost:9090"; export const GQL_URL = `${EVG_BASE_URL}/graphql/query`; + +/** + * Defines user credentials that can be used to log in to the application. + * - admin: A user who has admin permissions and is a superuser. + * - privileged: A user who has admin permissions, but is not a superuser. + * - regular: A user who has no admin permissions. + */ +export const users = { + admin: { username: "admin", password: "password" }, + privileged: { username: "privileged", password: "password" }, + regular: { username: "regular", password: "password" }, +}; diff --git a/cypress/integration/distroSettings/permissions.ts b/cypress/integration/distroSettings/permissions.ts index 6313c11cd2..5d3aa573dd 100644 --- a/cypress/integration/distroSettings/permissions.ts +++ b/cypress/integration/distroSettings/permissions.ts @@ -1,10 +1,12 @@ +import { users } from "../../constants"; + describe("distro permissions", () => { beforeEach(() => { cy.logout(); }); it("hides the new distro button when a user cannot create distros", () => { - cy.login({ username: "privileged" }); + cy.login(users.privileged); cy.visit("/distro/rhel71-power8-large/settings/general"); cy.dataCy("new-distro-button").should("not.exist"); cy.dataCy("delete-distro-button").should( @@ -16,7 +18,7 @@ describe("distro permissions", () => { }); it("disables the delete button when user lacks admin permissions", () => { - cy.login({ username: "regular" }); + cy.login(users.regular); cy.visit("/distro/rhel71-power8-large/settings/general"); cy.dataCy("delete-distro-button").should( "have.attr", @@ -26,7 +28,7 @@ describe("distro permissions", () => { }); it("disables fields when user lacks edit permissions", () => { - cy.login({ username: "regular" }); + cy.login(users.regular); cy.visit("/distro/rhel71-power8-large/settings/general"); cy.dataCy("distro-settings-page").within(() => { cy.get('input[type="checkbox"]').should( @@ -39,7 +41,7 @@ describe("distro permissions", () => { }); it("enables fields if user has edit permissions for a particular distro", () => { - cy.login({ username: "regular" }); + cy.login(users.regular); cy.visit("/distro/localhost/settings/general"); cy.dataCy("distro-settings-page").within(() => { cy.get('input[type="checkbox"]').should( diff --git a/cypress/integration/nav_bar.ts b/cypress/integration/nav_bar.ts index 5834132daf..629db209eb 100644 --- a/cypress/integration/nav_bar.ts +++ b/cypress/integration/nav_bar.ts @@ -1,4 +1,4 @@ -import { EVG_BASE_URL } from "../constants"; +import { EVG_BASE_URL, users } from "../constants"; const PATCH_ID = "5e4ff3abe3c3317e352062e4"; const USER_ID = "admin"; @@ -118,7 +118,7 @@ describe("Nav Bar", () => { it("Should not show Admin button to non-admins", () => { cy.logout(); - cy.login({ username: "regular" }); + cy.login(users.regular); cy.visit(SPRUCE_URLS.version); cy.dataCy("user-dropdown-link").click(); cy.dataCy("admin-link").should("not.exist"); diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 70cfafeaeb..45ed7a626c 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -1,4 +1,4 @@ -import { EVG_BASE_URL, GQL_URL } from "../constants"; +import { EVG_BASE_URL, GQL_URL, users } from "../constants"; import { hasOperationName } from "../utils/graphql-test-utils"; type cyGetOptions = Parameters[1]; @@ -33,8 +33,8 @@ Cypress.Commands.add( * `enterLoginCredentials` is a custom command to enter login credentials */ Cypress.Commands.add("enterLoginCredentials", () => { - cy.get("input[name=username]").type("admin"); - cy.get("input[name=password]").type("password"); + cy.get("input[name=username]").type(users.admin.username); + cy.get("input[name=password]").type(users.admin.password); cy.get("button[id=login-submit]").click(); }); @@ -52,13 +52,10 @@ Cypress.Commands.add("getInputByLabel", (label: string | RegExp) => { }); /* login */ -Cypress.Commands.add("login", ({ username = "admin" }) => { +Cypress.Commands.add("login", (user = users.admin) => { cy.getCookie("mci-token").then((c) => { if (!c) { - cy.request("POST", `${EVG_BASE_URL}/login`, { - username, - password: "password", - }); + cy.request("POST", `${EVG_BASE_URL}/login`, user); } }); }); diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts index fb86213906..1498d47cff 100644 --- a/cypress/support/e2e.ts +++ b/cypress/support/e2e.ts @@ -86,11 +86,17 @@ declare global { */ selectLGOption(label: string, option: string | RegExp): void; /** - * Custom command to navigate to login page and login. - * @param username The username of the account to log in to. - * @example cy.login() + * Custom command to navigate to login page and log in. + * @example cy.login({ username: "me", password: "abc" }) + * @example cy.login() // defaults to admin login */ - login({ username }: { username: string }): void; + login({ + password, + username, + }?: { + username: string; + password: string; + }): void; /** * Custom command to log out of the application. * @example cy.logout() @@ -148,7 +154,7 @@ before(() => { (() => { let mutationDispatched: boolean; beforeEach(() => { - cy.login({ username: "admin" }); + cy.login(); cy.setCookie(bannerCookie, "true"); cy.setCookie(CY_DISABLE_COMMITS_WELCOME_MODAL, "true"); cy.setCookie(CY_DISABLE_NEW_USER_WELCOME_MODAL, "true");