Skip to content
Merged
Prev Previous commit
Next Next commit
fixed mistake; wrote tests in penance
  • Loading branch information
Kira-Pilot committed Oct 27, 2022
commit dc7e4a969395fecfc0ab8e3f560d4dcf465d7ea1
3 changes: 1 addition & 2 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export interface AuditLog {
readonly action: AuditAction
readonly diff: AuditDiff
readonly status_code: number
// This is likely an enum in an external package ("encoding/json.RawMessage")
readonly additional_fields: string
readonly additional_fields: Record<string, string>
readonly description: string
readonly user?: User
}
Expand Down
41 changes: 41 additions & 0 deletions site/src/components/AuditLogRow/AuditLogRow.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { readableActionMessage } from "./AuditLogRow"
import {
MockAuditLog,
MockAuditLogWithWorkspaceBuild,
} from "testHelpers/entities"

describe("readableActionMessage()", () => {
it("renders the correct string for a workspaceBuild audit log", async () => {
// When
const friendlyString = readableActionMessage(MockAuditLogWithWorkspaceBuild)

// Then
expect(friendlyString).toBe(
"<strong>TestUser</strong> stopped workspace build for <strong>test2</strong>",
)
})
it("renders the correct string for a workspaceBuild audit log with a duplicate word", async () => {
// When
const AuditLogWithRepeat = {
...MockAuditLogWithWorkspaceBuild,
additional_fields: {
workspaceName: "workspace",
},
}
const friendlyString = readableActionMessage(AuditLogWithRepeat)

// Then
expect(friendlyString).toBe(
"<strong>TestUser</strong> stopped workspace build for <strong>workspace</strong>",
)
})
it("renders the correct string for a workspace audit log", async () => {
// When
const friendlyString = readableActionMessage(MockAuditLog)

// Then
expect(friendlyString).toBe(
"<strong>TestUser</strong> updated workspace <strong>bruno-dev</strong>",
)
})
})
28 changes: 6 additions & 22 deletions site/src/components/AuditLogRow/AuditLogRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,17 @@ import userAgentParser from "ua-parser-js"
import { combineClasses } from "util/combineClasses"
import { AuditLogDiff } from "./AuditLogDiff"

// the BE returns additional_field as a string, since it's stored as JSON but
// technically, it's a map, so we adjust the type here.
type ExtendedAuditLog = Omit<AuditLog, "additional_fields"> & {
additional_fields: Record<string, string>
}
export const readableActionMessage = (auditLog: AuditLog): string => {
let target = auditLog.resource_target.trim()

const readableActionMessage = (auditLog: ExtendedAuditLog) => {
// workspace builds audit logs don't have targets; therefore format them differently
// audit logs with a resource_type of workspace build use workspace name as a target
if (auditLog.resource_type === "workspace_build") {
// remove the "{target}" identifier in the string description as we don't use it
const amendedDescription = auditLog.description.substring(
0,
auditLog.description.lastIndexOf(" "),
)
return amendedDescription
.replace("{user}", `<strong>${auditLog.user?.username}</strong>`)
.replace(
auditLog.additional_fields.workspaceName,
`<strong>${auditLog.additional_fields.workspaceName}</strong>`,
)
target = auditLog.additional_fields.workspaceName.trim()
}

return auditLog.description
.replace("{user}", `<strong>${auditLog.user?.username.trim()}</strong>`)
.replace("{target}", `<strong>${auditLog.resource_target.trim()}</strong>`)
.replace("{target}", `<strong>${target}</strong>`)
}

const httpStatusColor = (httpStatus: number): PaletteIndex => {
Expand Down Expand Up @@ -132,9 +118,7 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
>
<span
dangerouslySetInnerHTML={{
__html: readableActionMessage(
auditLog as unknown as ExtendedAuditLog,
),
__html: readableActionMessage(auditLog),
}}
/>
<span className={styles.auditLogTime}>
Expand Down
7 changes: 5 additions & 2 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ export const MockAuditLog: TypesGen.AuditLog = {
},
},
status_code: 200,
additional_fields: "",
additional_fields: {},
description: "{user} updated workspace {target}",
user: MockUser,
}
Expand Down Expand Up @@ -955,7 +955,10 @@ export const MockAuditLogWithWorkspaceBuild: TypesGen.AuditLog = {
request_id: "61555889-2875-475c-8494-f7693dd5d75b",
action: "stop",
resource_type: "workspace_build",
description: "{user} stopped workspace build for workspace test2",
description: "{user} stopped workspace build for {target}",
additional_fields: {
"workspaceName": "test2"
},
}

export const MockWorkspaceQuota: TypesGen.WorkspaceQuota = {
Expand Down