Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
admin btn
Browse files Browse the repository at this point in the history
  • Loading branch information
SupaJoon committed Sep 12, 2023
1 parent 2d67b79 commit 43bb8b6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 15 deletions.
55 changes: 43 additions & 12 deletions cypress/integration/nav_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ const SPRUCE_URLS = {
userPatches: `/user/${USER_ID}/patches`,
cli: `/preferences/cli`,
};
const legacyBase = "http://localhost:9090";
const LEGACY_URLS = {
version: `/version/${PATCH_ID}`,
userPatches: `/patches/user/${USER_ID}`,
distros: `/distros`,
version: `${legacyBase}/version/${PATCH_ID}`,
userPatches: `${legacyBase}/patches/user/${USER_ID}`,
distros: `${legacyBase}/distros`,
admin: `${legacyBase}/admin`,
};
describe("Nav Bar", () => {
const projectCookie = "mci-project-cookie";

it("Should have a nav bar linking to the proper page on the legacy UI", () => {
cy.visit(SPRUCE_URLS.version);
cy.dataCy("legacy-ui-link").should("exist");
cy.dataCy("legacy-ui-link")
.should("have.attr", "href")
.and("include", LEGACY_URLS.version);
cy.dataCy("legacy-ui-link").should(
"have.attr",
"href",
LEGACY_URLS.version
);
});
it("Navigating to a different page should change the nav link to the legacy UI", () => {
cy.visit(SPRUCE_URLS.version);
Expand All @@ -28,9 +32,11 @@ describe("Nav Bar", () => {
.and("include", LEGACY_URLS.version);
cy.visit(SPRUCE_URLS.userPatches);
cy.dataCy("legacy-ui-link").should("exist");
cy.dataCy("legacy-ui-link")
.should("have.attr", "href")
.and("include", LEGACY_URLS.userPatches);
cy.dataCy("legacy-ui-link").should(
"have.attr",
"href",
LEGACY_URLS.userPatches
);
});
it("Visiting a page with no legacy equivalent should not display a nav link", () => {
cy.visit(SPRUCE_URLS.cli);
Expand All @@ -41,9 +47,7 @@ describe("Nav Bar", () => {
cy.dataCy("legacy_route").should("not.exist");
cy.dataCy("auxiliary-dropdown-link").click();
cy.dataCy("legacy_route").should("exist");
cy.dataCy("legacy_route")
.should("have.attr", "href")
.and("include", LEGACY_URLS.distros);
cy.dataCy("legacy_route").should("have.attr", "href", LEGACY_URLS.distros);
});
it("Nav Dropdown should link to patches page of most recent project if cookie exists", () => {
cy.setCookie(projectCookie, "spruce");
Expand Down Expand Up @@ -100,4 +104,31 @@ describe("Nav Bar", () => {
);
cy.getCookie(projectCookie).should("have.property", "value", "spruce");
});

describe("Admin settings", () => {
it("Should not show Admin button to non-admins", () => {
const userData = {
data: {
user: {
userId: "admin",
displayName: "Evergreen Admin",
emailAddress: "[email protected]",
permissions: {
canUpdateAdminSettings: false,
},
},
},
};
cy.overwriteGQL("User", userData);
cy.visit(SPRUCE_URLS.version);
cy.dataCy("admin-link").should("not.exist");
});

it("Should show Admin button to admins", () => {
cy.visit(SPRUCE_URLS.version);
cy.dataCy("admin-link")
.should("be.visible")
.should("have.attr", "href", LEGACY_URLS.admin);
});
});
});
2 changes: 1 addition & 1 deletion cypress/integration/version/action_buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("Action Buttons", () => {
it("Clicking 'Set Priority' button shows popconfirm with input and toast on success", () => {
const priority = "99";
cy.dataCy("prioritize-patch").click();
cy.dataCy("patch-priority-input").type(priority).type("{enter}");
cy.dataCy("patch-priority-input").type(`${priority}{enter}`);
cy.validateToast("success", priority);
});

Expand Down
12 changes: 12 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GQL_URL, hasOperationName } from "../utils/graphql-test-utils";

const user = {
username: "admin",
password: "password",
Expand Down Expand Up @@ -113,3 +115,13 @@ Cypress.Commands.add(
});
}
);

Cypress.Commands.add("overwriteGQL", (operationName: string, body: any) => {
cy.intercept("POST", GQL_URL, (req) => {
if (hasOperationName(req, operationName)) {
req.reply((res) => {
res.body = body;
});
}
});
});
6 changes: 6 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ declare global {
message?: string,
shouldClose?: boolean
): void;
/**
* Custom command to overwrite a GQL response
* @param operationName - The operation name of the query
* @param body - The replacement response body
*/
overwriteGQL(operationName: string, body: any);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/components/Header/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Link, useParams } from "react-router-dom";
import { useNavbarAnalytics } from "analytics";
import Icon from "components/Icon";
import { CURRENT_PROJECT } from "constants/cookies";
import { wikiUrl } from "constants/externalResources";
import { adminSettingsURL, wikiUrl } from "constants/externalResources";
import { getCommitsRoute, getUserPatchesRoute, routes } from "constants/routes";
import { size } from "constants/tokens";
import { useAuthStateContext } from "context/auth";
Expand All @@ -27,7 +27,7 @@ export const Navbar: React.FC = () => {

const { data: userData } = useQuery<UserQuery>(GET_USER);
const { user } = userData || {};
const { userId } = user || {};
const { permissions, userId } = user || {};

const { projectIdentifier: projectFromUrl } = useParams<{
projectIdentifier: string;
Expand Down Expand Up @@ -82,6 +82,11 @@ export const Navbar: React.FC = () => {
>
My Hosts
</PrimaryLink>
{permissions?.canUpdateAdminSettings && (
<PrimaryLink data-cy="admin-link" to={adminSettingsURL}>
Admin
</PrimaryLink>
)}
<AuxiliaryDropdown projectIdentifier={projectIdentifier} />
</NavActionContainer>
<NavActionContainer>
Expand Down
2 changes: 2 additions & 0 deletions src/constants/externalResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ export const getHoneycombSystemMetricsUrl = (
query
)}&omitMissingValues`;
};

export const adminSettingsURL = `${getUiUrl()}/admin`;
5 changes: 5 additions & 0 deletions src/gql/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,7 @@ export type Permissions = {
__typename?: "Permissions";
canCreateDistro: Scalars["Boolean"]["output"];
canCreateProject: Scalars["Boolean"]["output"];
canUpdateAdminSettings: Scalars["Boolean"]["output"];
distroPermissions: DistroPermissions;
userId: Scalars["String"]["output"];
};
Expand Down Expand Up @@ -8300,6 +8301,10 @@ export type UserQuery = {
displayName: string;
emailAddress: string;
userId: string;
permissions: {
__typename?: "Permissions";
canUpdateAdminSettings: boolean;
};
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/gql/mocks/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const getUserMock: ApolloMock<UserQuery, UserQueryVariables> = {
userId: "admin",
displayName: "Evergreen Admin",
emailAddress: "[email protected]",
permissions: {
canUpdateAdminSettings: true,
},
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions src/gql/queries/get-user.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ query User {
user {
displayName
emailAddress
permissions {
canUpdateAdminSettings
}
userId
}
}

0 comments on commit 43bb8b6

Please sign in to comment.