Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 810c121

Browse files
committed
Add test to check if the navbar displays or not the admin menu
1 parent e07ce0d commit 810c121

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { screen, waitFor } from "@testing-library/react"
2+
import React from "react"
3+
import * as API from "../../api"
4+
import { renderWithAuth } from "../../testHelpers"
5+
import { checks } from "../../xServices/auth/authXService"
6+
import { Language as AdminDropdownLanguage } from "../AdminDropdown/AdminDropdown"
7+
import { Navbar } from "./Navbar"
8+
9+
beforeEach(() => {
10+
jest.resetAllMocks()
11+
})
12+
13+
describe("Navbar", () => {
14+
describe("when user has permission to read all users", () => {
15+
it("displays the admin menu", async () => {
16+
const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({
17+
[checks.readAllUsers]: true,
18+
})
19+
20+
renderWithAuth(<Navbar />)
21+
22+
// Wait for the request is done
23+
await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1))
24+
await screen.findByRole("button", { name: AdminDropdownLanguage.menuTitle })
25+
})
26+
})
27+
28+
describe("when user has NO permission to read all users", () => {
29+
it("does not display the admin menu", async () => {
30+
const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({
31+
[checks.readAllUsers]: false,
32+
})
33+
renderWithAuth(<Navbar />)
34+
35+
// Wait for the request is done
36+
await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1))
37+
expect(screen.queryByRole("button", { name: AdminDropdownLanguage.menuTitle })).not.toBeInTheDocument()
38+
})
39+
})
40+
})

site/src/testHelpers/handlers.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { rest } from "msw"
2+
import { permissionsToCheck } from "../xServices/auth/authXService"
23
import * as M from "./entities"
34

45
export const handlers = [
@@ -54,6 +55,17 @@ export const handlers = [
5455
rest.get("/api/v2/users/roles", async (req, res, ctx) => {
5556
return res(ctx.status(200), ctx.json(M.MockSiteRoles))
5657
}),
58+
rest.post("/api/v2/users/:userId/permissions/check", async (req, res, ctx) => {
59+
const permissions = Object.keys(permissionsToCheck)
60+
const response = permissions.reduce((obj, permission) => {
61+
return {
62+
...obj,
63+
[permission]: true,
64+
}
65+
}, {})
66+
67+
return res(ctx.status(200), ctx.json(response))
68+
}),
5769

5870
// workspaces
5971
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {

site/src/xServices/auth/authXService.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ export const Language = {
88
successProfileUpdate: "Updated preferences.",
99
}
1010

11-
const permissionsToCheck = {
12-
readAllUsers: {
11+
export const checks = {
12+
readAllUsers: "readAllUsers",
13+
} as const
14+
15+
export const permissionsToCheck = {
16+
[checks.readAllUsers]: {
1317
object: {
1418
resource_type: "user",
1519
},

0 commit comments

Comments
 (0)