Skip to content

Commit a75392e

Browse files
committed
auditing login
1 parent 1acc73c commit a75392e

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

site/src/components/AuditLogRow/AuditLogDescription.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import {
22
MockAuditLog,
33
MockAuditLogWithWorkspaceBuild,
44
MockWorkspaceCreateAuditLogForDifferentOwner,
5+
MockAuditLogSuccessfulLogin,
6+
MockAuditLogUnsuccessfulLoginKnownUser,
7+
MockAuditLogUnsuccessfulLoginUnknownUser,
58
} from "testHelpers/entities"
69
import { AuditLogDescription } from "./AuditLogDescription"
10+
import { AuditLogRow } from "./AuditLogRow"
711
import { render } from "../../testHelpers/renderHelpers"
812
import { screen } from "@testing-library/react"
913

@@ -59,4 +63,22 @@ describe("AuditLogDescription", () => {
5963
),
6064
).toBeDefined()
6165
})
66+
it("renders the correct string for successful login", async () => {
67+
render(<AuditLogRow auditLog={MockAuditLogSuccessfulLogin} />)
68+
expect(getByTextContent(`TestUser logged in`)).toBeDefined()
69+
const statusPill = screen.getByRole("status")
70+
expect(statusPill).toHaveTextContent("201")
71+
})
72+
it("renders the correct string for unsuccessful login for a known user", async () => {
73+
render(<AuditLogRow auditLog={MockAuditLogUnsuccessfulLoginKnownUser} />)
74+
expect(getByTextContent(`TestUser logged in`)).toBeDefined()
75+
const statusPill = screen.getByRole("status")
76+
expect(statusPill).toHaveTextContent("401")
77+
})
78+
it("renders the correct string for unsuccessful login for an unknown user", async () => {
79+
render(<AuditLogRow auditLog={MockAuditLogUnsuccessfulLoginUnknownUser} />)
80+
expect(getByTextContent(`An unknown user logged in`)).toBeDefined()
81+
const statusPill = screen.getByRole("status")
82+
expect(statusPill).toHaveTextContent("401")
83+
})
6284
})

site/src/components/AuditLogRow/AuditLogDescription.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
1212
const { t } = i18next
1313

1414
let target = auditLog.resource_target.trim()
15-
let user = auditLog.user?.username.trim()
15+
let user = auditLog.user
16+
? auditLog.user.username.trim()
17+
: t("auditLog:table.logRow.unknownUser")
1618

1719
if (auditLog.resource_type === "workspace_build") {
1820
// audit logs with a resource_type of workspace build use workspace name as a target
@@ -22,7 +24,7 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
2224
auditLog.additional_fields?.build_reason &&
2325
auditLog.additional_fields?.build_reason !== "initiator"
2426
? t("auditLog:table.logRow.buildReason")
25-
: auditLog.user?.username.trim()
27+
: user
2628
}
2729

2830
// SSH key entries have no links

site/src/components/AuditLogRow/AuditLogRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
9393
className={styles.fullWidth}
9494
>
9595
<UserAvatar
96-
username={auditLog.user?.username ?? ""}
96+
username={auditLog.user?.username ?? "?"}
9797
avatarURL={auditLog.user?.avatar_url}
9898
/>
9999

site/src/i18n/en/auditLog.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"browser": "Browser: ",
1515
"notAvailable": "Not available",
1616
"onBehalfOf": " on behalf of {{owner}}",
17-
"buildReason": "Coder automatically"
17+
"buildReason": "Coder automatically",
18+
"unknownUser": "An unknown user"
1819
}
1920
},
2021
"paywall": {

site/src/testHelpers/entities.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,26 @@ export const MockAuditLogGitSSH: TypesGen.AuditLog = {
11171117
},
11181118
}
11191119

1120+
export const MockAuditLogSuccessfulLogin: TypesGen.AuditLog = {
1121+
...MockAuditLog,
1122+
resource_type: "api_key",
1123+
resource_target: "",
1124+
action: "login",
1125+
status_code: 201,
1126+
description: "{user} logged in",
1127+
}
1128+
1129+
export const MockAuditLogUnsuccessfulLoginKnownUser: TypesGen.AuditLog = {
1130+
...MockAuditLogSuccessfulLogin,
1131+
status_code: 401,
1132+
}
1133+
1134+
export const MockAuditLogUnsuccessfulLoginUnknownUser: TypesGen.AuditLog = {
1135+
...MockAuditLogSuccessfulLogin,
1136+
status_code: 401,
1137+
user: undefined,
1138+
}
1139+
11201140
export const MockWorkspaceQuota: TypesGen.WorkspaceQuota = {
11211141
credits_consumed: 0,
11221142
budget: 100,

0 commit comments

Comments
 (0)