Skip to content

chore: refactor audit page to use window function for count #5133

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 15 commits into from
Nov 21, 2022
Merged
Prev Previous commit
Fix frontend tests
  • Loading branch information
presleyp committed Nov 18, 2022
commit 8769f6210c37c7faffcce9915ce23cf712268f68
2 changes: 1 addition & 1 deletion site/src/components/AuditLogRow/AuditLogRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("readableActionMessage()", () => {

// Then
expect(friendlyString).toBe(
"<strong>TestUser</strong> updated workspace <strong>bruno-dev</strong>",
"<strong>TestUser</strong> created workspace <strong>bruno-dev</strong>",
)
})
})
27 changes: 5 additions & 22 deletions site/src/pages/AuditPage/AuditPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,16 @@ describe("AuditPage", () => {

describe("Filtering", () => {
it("filters by typing", async () => {
const getAuditLogsSpy = jest
.spyOn(API, "getAuditLogs")
.mockResolvedValue({ audit_logs: [MockAuditLog], count: 1 })

render(<AuditPage />)
await waitForLoaderToBeRemoved()

// Reset spy so we can focus on the call with the filter
getAuditLogsSpy.mockReset()
await screen.findByText("updated", { exact: false })

const filterField = screen.getByLabelText("Filter")
const query = "resource_type:workspace action:create"
await userEvent.type(filterField, query)

await waitFor(() =>
expect(getAuditLogsSpy).toBeCalledWith({
limit: 25,
offset: 0,
q: query,
}),
)
await screen.findByText("created", { exact: false })
const editWorkspace = screen.queryByText("updated", { exact: false })
expect(editWorkspace).not.toBeInTheDocument()
})

it("filters by URL", async () => {
Expand All @@ -67,17 +56,11 @@ describe("AuditPage", () => {
})

it("resets page to 1 when filter is changed", async () => {
const getAuditLogsSpy = jest
.spyOn(API, "getAuditLogs")
.mockResolvedValue({ audit_logs: [MockAuditLog], count: 1 })

history.push(`/audit?page=2`)
render(<AuditPage />)

await waitForLoaderToBeRemoved()
getAuditLogsSpy
.mockReset()
.mockResolvedValue({ audit_logs: [MockAuditLog], count: 1 })
const getAuditLogsSpy = jest.spyOn(API, "getAuditLogs")

const filterField = screen.getByLabelText("Filter")
const query = "resource_type:workspace action:create"
Expand Down
4 changes: 3 additions & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,14 +956,16 @@ export const MockAuditLog: TypesGen.AuditLog = {
},
status_code: 200,
additional_fields: {},
description: "{user} updated workspace {target}",
description: "{user} created workspace {target}",
user: MockUser,
}

export const MockAuditLog2: TypesGen.AuditLog = {
...MockAuditLog,
id: "53bded77-7b9d-4e82-8771-991a34d759f9",
action: "write",
time: "2022-05-20T16:45:57.122Z",
description: "{user} updated workspace {target}",
diff: {
workspace_name: {
old: "old-workspace-name",
Expand Down
9 changes: 7 additions & 2 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,16 @@ export const handlers = [

// Audit
rest.get("/api/v2/audit", (req, res, ctx) => {
const filter = req.url.searchParams.get("q") as string
const logs =
filter === "resource_type:workspace action:create"
? [M.MockAuditLog]
: [M.MockAuditLog, M.MockAuditLog2]
return res(
ctx.status(200),
ctx.json({
audit_logs: [M.MockAuditLog, M.MockAuditLog2],
count: 2,
audit_logs: logs,
count: logs.length,
}),
)
}),
Expand Down