Skip to content

feat: ability to activate suspended users #2344

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 4 commits into from
Jun 15, 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
added test
  • Loading branch information
Kira-Pilot committed Jun 15, 2022
commit 88643c67e0373a1a426206d03b78ac0f2a7f5c4b
81 changes: 79 additions & 2 deletions site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GlobalSnackbar } from "../../components/GlobalSnackbar/GlobalSnackbar"
import { Language as ResetPasswordDialogLanguage } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
import { Language as RoleSelectLanguage } from "../../components/RoleSelect/RoleSelect"
import { Language as UsersTableLanguage } from "../../components/UsersTable/UsersTable"
import { MockAuditorRole, MockUser, MockUser2, render } from "../../testHelpers/renderHelpers"
import { MockAuditorRole, MockUser, MockUser2, render, SuspendedMockUser } from "../../testHelpers/renderHelpers"
import { server } from "../../testHelpers/server"
import { permissionsToCheck } from "../../xServices/auth/authXService"
import { Language as usersXServiceLanguage } from "../../xServices/users/usersXService"
Expand Down Expand Up @@ -40,6 +40,35 @@ const suspendUser = async (setupActionSpies: () => void) => {
fireEvent.click(confirmButton)
}

const activateUser = async (setupActionSpies: () => void) => {
// Get the first user in the table
const users = await screen.findAllByText(/.*@coder.com/)
const firstUserRow = users[2].closest("tr")
if (!firstUserRow) {
throw new Error("Error on get the first user row")
}

// Click on the "more" button to display the "Activate" option
const moreButton = within(firstUserRow).getByLabelText("more")
fireEvent.click(moreButton)
const menu = screen.getByRole("menu")
const activateButton = within(menu).getByText(UsersTableLanguage.activateMenuItem)
fireEvent.click(activateButton)

// Check if the confirm message is displayed
const confirmDialog = screen.getByRole("dialog")
expect(confirmDialog).toHaveTextContent(
`${UsersPageLanguage.activateDialogMessagePrefix} ${SuspendedMockUser.username}?`,
)

// Setup spies to check the actions after
setupActionSpies()

// Click on the "Confirm" button
const confirmButton = within(confirmDialog).getByText(UsersPageLanguage.activateDialogAction)
fireEvent.click(confirmButton)
}

const resetUserPassword = async (setupActionSpies: () => void) => {
// Get the first user in the table
const users = await screen.findAllByText(/.*@coder.com/)
Expand Down Expand Up @@ -99,7 +128,7 @@ describe("Users Page", () => {
it("shows users", async () => {
render(<UsersPage />)
const users = await screen.findAllByText(/.*@coder.com/)
expect(users.length).toEqual(2)
expect(users.length).toEqual(3)
})

it("shows 'Create user' button to an authorized user", () => {
Expand Down Expand Up @@ -178,6 +207,54 @@ describe("Users Page", () => {
})
})

describe("activate user", () => {
describe("when user is successfully activated", () => {
it("shows a success message and refreshes the page", async () => {
render(
<>
<UsersPage />
<GlobalSnackbar />
</>,
)

await activateUser(() => {
jest.spyOn(API, "activateUser").mockResolvedValueOnce(SuspendedMockUser)
jest
.spyOn(API, "getUsers")
.mockImplementationOnce(() => Promise.resolve([MockUser, MockUser2, SuspendedMockUser]))
})

// Check if the success message is displayed
await screen.findByText(usersXServiceLanguage.activateUserSuccess)

// Check if the API was called correctly
expect(API.activateUser).toBeCalledTimes(1)
expect(API.activateUser).toBeCalledWith(SuspendedMockUser.id)
})
})
describe("when activation fails", () => {
it("shows an error message", async () => {
render(
<>
<UsersPage />
<GlobalSnackbar />
</>,
)

await activateUser(() => {
jest.spyOn(API, "activateUser").mockRejectedValueOnce({})
})

// Check if the error message is displayed
await screen.findByText(usersXServiceLanguage.activateUserError)

// Check if the API was called correctly
expect(API.activateUser).toBeCalledTimes(1)
expect(API.activateUser).toBeCalledWith(SuspendedMockUser.id)
})
})
})

describe("reset user password", () => {
describe("when it is success", () => {
it("shows a success message", async () => {
Expand Down
10 changes: 10 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ export const MockUser2: TypesGen.User = {
roles: [],
}

export const SuspendedMockUser: TypesGen.User = {
id: "suspended-mock-user",
username: "SuspendedMockUser",
email: "iamsuspendedsad!@coder.com",
created_at: "",
status: "suspended",
organization_ids: ["fc0774ce-cc9e-48d4-80ae-88f7a4d4a8b0"],
roles: [],
}

export const MockOrganization: TypesGen.Organization = {
id: "test-org",
name: "Test Organization",
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const handlers = [

// users
rest.get("/api/v2/users", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockUser, M.MockUser2]))
return res(ctx.status(200), ctx.json([M.MockUser, M.MockUser2, M.SuspendedMockUser]))
}),
rest.post("/api/v2/users", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockUser))
Expand Down