Skip to content

Commit b8fa378

Browse files
committed
Add basic auditXService code
1 parent f037aad commit b8fa378

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

site/src/api/api.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import axios, { AxiosRequestHeaders } from "axios"
22
import dayjs from "dayjs"
3+
import { MockAuditLog } from "testHelpers/entities"
34
import * as Types from "./types"
45
import { WorkspaceBuildTransition } from "./types"
56
import * as TypesGen from "./typesGenerated"
7+
import { User } from "./typesGenerated"
68

79
const CONTENT_TYPE_JSON: AxiosRequestHeaders = {
810
"Content-Type": "application/json",
@@ -383,3 +385,32 @@ export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
383385
const response = await axios.get("/api/v2/entitlements")
384386
return response.data
385387
}
388+
389+
interface AuditLog {
390+
readonly id: string
391+
readonly request_id: string
392+
readonly time: string
393+
readonly organization_id: string
394+
// Named type "net/netip.Addr" unknown, using "any"
395+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
396+
readonly ip: any
397+
readonly user_agent: string
398+
readonly resource_type: any
399+
readonly resource_id: string
400+
readonly resource_target: string
401+
readonly action: any
402+
readonly diff: any
403+
readonly status_code: number
404+
// This is likely an enum in an external package ("encoding/json.RawMessage")
405+
readonly additional_fields: any
406+
readonly description: string
407+
readonly user?: User
408+
// This is likely an enum in an external package ("encoding/json.RawMessage")
409+
readonly resource: any
410+
}
411+
412+
export const getAuditLogs = async (): Promise<AuditLog[]> => {
413+
return [MockAuditLog]
414+
// const response = await axios.get("/api/v2/audit")
415+
// return response.data
416+
}

site/src/testHelpers/entities.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ export const makeMockApiError = ({
637637
detail,
638638
validations,
639639
}: {
640-
message?: string
641-
detail?: string
640+
message?: ""
641+
detail?: ""
642642
validations?: FieldError[]
643643
}) => ({
644644
response: {
@@ -684,3 +684,22 @@ export const MockEntitlementsWithAuditLog: TypesGen.Entitlements = {
684684
},
685685
},
686686
}
687+
688+
export const MockAuditLog = {
689+
id: "fbd2116a-8961-4954-87ae-e4575bd29ce0",
690+
request_id: "53bded77-7b9d-4e82-8771-991a34d759f9",
691+
time: "2022-05-19T16:45:57.122Z",
692+
organization_id: "fc0774ce-cc9e-48d4-80ae-88f7a4d4a8b0",
693+
ip: "127.0.0.1",
694+
user_agent: "browser",
695+
resource_type: "organization",
696+
resource_id: "ef8d1cf4-82de-4fd9-8980-047dad6d06b5",
697+
resource_target: "Bruno's Org",
698+
action: "create",
699+
diff: {},
700+
status_code: 200,
701+
additional_fields: {},
702+
description: "Colin Adler updated the organization Bruno's Org",
703+
user: MockUser,
704+
resource: MockOrganization,
705+
}

site/src/testHelpers/handlers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,9 @@ export const handlers = [
147147
rest.get("/api/v2/entitlements", (req, res, ctx) => {
148148
return res(ctx.status(200), ctx.json(M.MockEntitlements))
149149
}),
150+
151+
// Audit
152+
rest.get("/api/v2/audit", (req, res, ctx) => {
153+
return res(ctx.status(200), ctx.json(M.MockAuditLog))
154+
}),
150155
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { getAuditLogs } from "api/api"
2+
import { assign, createMachine } from "xstate"
3+
4+
type AuditLogs = Awaited<ReturnType<typeof getAuditLogs>>
5+
6+
export const auditMachine = createMachine(
7+
{
8+
id: "auditMachine",
9+
schema: {
10+
context: {} as { auditLogs: AuditLogs },
11+
services: {} as {
12+
loadAuditLogs: {
13+
data: AuditLogs
14+
}
15+
},
16+
},
17+
tsTypes: {} as import("./auditXService.typegen").Typegen0,
18+
states: {
19+
loadingLogs: {
20+
invoke: {
21+
src: "loadAuditLogs",
22+
onDone: {
23+
target: "success",
24+
actions: ["assignAuditLogs"],
25+
},
26+
},
27+
},
28+
success: {
29+
type: "final",
30+
},
31+
},
32+
},
33+
{
34+
actions: {
35+
assignAuditLogs: assign({
36+
auditLogs: (_, event) => event.data,
37+
}),
38+
},
39+
services: {
40+
loadAuditLogs: () => getAuditLogs(),
41+
},
42+
},
43+
)

0 commit comments

Comments
 (0)