Skip to content

feat: Add permissions for links #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test to check if the navbar displays or not the admin menu
  • Loading branch information
BrunoQuaresma committed May 13, 2022
commit ee4b6f6316a9d98575f83211a22d0e8cd0b6c347
40 changes: 40 additions & 0 deletions site/src/components/Navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { screen, waitFor } from "@testing-library/react"
import React from "react"
import * as API from "../../api"
import { renderWithAuth } from "../../testHelpers"
import { checks } from "../../xServices/auth/authXService"
import { Language as AdminDropdownLanguage } from "../AdminDropdown/AdminDropdown"
import { Navbar } from "./Navbar"

beforeEach(() => {
jest.resetAllMocks()
})

describe("Navbar", () => {
describe("when user has permission to read all users", () => {
it("displays the admin menu", async () => {
const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({
[checks.readAllUsers]: true,
})

renderWithAuth(<Navbar />)

// Wait for the request is done
await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1))
await screen.findByRole("button", { name: AdminDropdownLanguage.menuTitle })
})
})

describe("when user has NO permission to read all users", () => {
it("does not display the admin menu", async () => {
const checkUserPermissionsSpy = jest.spyOn(API, "checkUserPermissions").mockResolvedValueOnce({
[checks.readAllUsers]: false,
})
renderWithAuth(<Navbar />)

// Wait for the request is done
await waitFor(() => expect(checkUserPermissionsSpy).toBeCalledTimes(1))
expect(screen.queryByRole("button", { name: AdminDropdownLanguage.menuTitle })).not.toBeInTheDocument()
})
})
})
12 changes: 12 additions & 0 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rest } from "msw"
import { permissionsToCheck } from "../xServices/auth/authXService"
import * as M from "./entities"

export const handlers = [
Expand Down Expand Up @@ -54,6 +55,17 @@ export const handlers = [
rest.get("/api/v2/users/roles", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockSiteRoles))
}),
rest.post("/api/v2/users/:userId/permissions/check", async (req, res, ctx) => {
const permissions = Object.keys(permissionsToCheck)
const response = permissions.reduce((obj, permission) => {
return {
...obj,
[permission]: true,
}
}, {})

return res(ctx.status(200), ctx.json(response))
}),

// workspaces
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {
Expand Down
8 changes: 6 additions & 2 deletions site/src/xServices/auth/authXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ export const Language = {
successProfileUpdate: "Updated preferences.",
}

const permissionsToCheck = {
readAllUsers: {
export const checks = {
readAllUsers: "readAllUsers",
} as const

export const permissionsToCheck = {
[checks.readAllUsers]: {
object: {
resource_type: "user",
},
Expand Down