Skip to content

refactor: Refactor auth provider #5782

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 18 commits into from
Jan 20, 2023
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
Fix Audit Log page test
  • Loading branch information
BrunoQuaresma committed Jan 19, 2023
commit 6442b3a93925e723b5d67d7ca12eea478989b716
23 changes: 2 additions & 21 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ export const withDefaultFeatures = (
return fs as TypesGen.Entitlements["features"]
}

// defaultEntitlements has a default set of disabled functionality.
export const defaultEntitlements = (): TypesGen.Entitlements => {
return {
features: withDefaultFeatures({}),
has_license: false,
errors: [],
warnings: [],
experimental: false,
trial: false,
}
}

// Always attach CSRF token to all requests.
// In puppeteer the document is undefined. In those cases, just
// do nothing.
Expand Down Expand Up @@ -625,15 +613,8 @@ export const putWorkspaceExtension = async (
}

export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
try {
const response = await axios.get("/api/v2/entitlements")
return response.data
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
return defaultEntitlements()
}
throw error
}
const response = await axios.get("/api/v2/entitlements")
return response.data
}

export const getExperiments = async (): Promise<TypesGen.Experiment[]> => {
Expand Down
56 changes: 44 additions & 12 deletions site/src/pages/AuditPage/AuditPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
import { screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import * as API from "api/api"
import { rest } from "msw"
import {
history,
renderWithAuth,
MockAuditLog,
MockAuditLog2,
render,
waitForLoaderToBeRemoved,
MockEntitlementsWithAuditLog,
} from "testHelpers/renderHelpers"
import { server } from "testHelpers/server"

import * as CreateDayString from "util/createDayString"
import AuditPage from "./AuditPage"

interface RenderPageOptions {
filter?: string
page?: number
}

const renderPage = async ({ filter, page }: RenderPageOptions = {}) => {
let route = "/audit"
const params = new URLSearchParams()

if (filter) {
params.set("filter", filter)
}

if (page) {
params.set("page", page.toString())
}

if (Array.from(params).length > 0) {
route += `?${params.toString()}`
}

renderWithAuth(<AuditPage />, {
route,
path: "/audit",
})
await waitForLoaderToBeRemoved()
}

describe("AuditPage", () => {
beforeEach(() => {
// Mocking the dayjs module within the createDayString file
const mock = jest.spyOn(CreateDayString, "createDayString")
mock.mockImplementation(() => "a minute ago")

// Mock the entitlements
server.use(
rest.get("/api/v2/entitlements", (req, res, ctx) => {
return res(ctx.status(200), ctx.json(MockEntitlementsWithAuditLog))
}),
)
})

it("shows the audit logs", async () => {
// When
render(<AuditPage />)
await renderPage()

// Then
await screen.findByTestId(`audit-log-row-${MockAuditLog.id}`)
Expand All @@ -29,8 +67,7 @@ describe("AuditPage", () => {

describe("Filtering", () => {
it("filters by typing", async () => {
render(<AuditPage />)
await waitForLoaderToBeRemoved()
await renderPage()
await screen.findByText("updated", { exact: false })

const filterField = screen.getByLabelText("Filter")
Expand All @@ -47,19 +84,14 @@ describe("AuditPage", () => {
.mockResolvedValue({ audit_logs: [MockAuditLog], count: 1 })

const query = "resource_type:workspace action:create"
history.push(`/audit?filter=${encodeURIComponent(query)}`)
render(<AuditPage />)

await waitForLoaderToBeRemoved()
await renderPage({ filter: query })

expect(getAuditLogsSpy).toBeCalledWith({ limit: 25, offset: 0, q: query })
})

it("resets page to 1 when filter is changed", async () => {
history.push(`/audit?page=2`)
render(<AuditPage />)
await renderPage({ page: 2 })

await waitForLoaderToBeRemoved()
const getAuditLogsSpy = jest.spyOn(API, "getAuditLogs")

const filterField = screen.getByLabelText("Filter")
Expand Down