Skip to content

Commit d1ba2b9

Browse files
committed
fix/write tests
1 parent 8177557 commit d1ba2b9

File tree

4 files changed

+67
-52
lines changed

4 files changed

+67
-52
lines changed

coderd/audit_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/google/uuid"
98
"github.com/stretchr/testify/require"
109

1110
"github.com/coder/coder/coderd/coderdtest"
@@ -20,9 +19,11 @@ func TestAuditLogs(t *testing.T) {
2019

2120
ctx := context.Background()
2221
client := coderdtest.New(t, nil)
23-
_ = coderdtest.CreateFirstUser(t, client)
22+
user := coderdtest.CreateFirstUser(t, client)
2423

25-
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{})
24+
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
25+
ResourceID: user.UserID,
26+
})
2627
require.NoError(t, err)
2728

2829
alogs, err := client.AuditLogs(ctx, codersdk.AuditLogsRequest{
@@ -43,22 +44,26 @@ func TestAuditLogsFilter(t *testing.T) {
4344
t.Run("Filter", func(t *testing.T) {
4445
t.Parallel()
4546

46-
ctx := context.Background()
47-
client := coderdtest.New(t, nil)
48-
_ = coderdtest.CreateFirstUser(t, client)
49-
userResourceID := uuid.New()
47+
var (
48+
ctx = context.Background()
49+
client = coderdtest.New(t, nil)
50+
user = coderdtest.CreateFirstUser(t, client)
51+
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
52+
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
53+
)
5054

5155
// Create two logs with "Create"
5256
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
5357
Action: codersdk.AuditActionCreate,
5458
ResourceType: codersdk.ResourceTypeTemplate,
59+
ResourceID: template.ID,
5560
Time: time.Date(2022, 8, 15, 14, 30, 45, 100, time.UTC), // 2022-8-15 14:30:45
5661
})
5762
require.NoError(t, err)
5863
err = client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
5964
Action: codersdk.AuditActionCreate,
6065
ResourceType: codersdk.ResourceTypeUser,
61-
ResourceID: userResourceID,
66+
ResourceID: user.UserID,
6267
Time: time.Date(2022, 8, 16, 14, 30, 45, 100, time.UTC), // 2022-8-16 14:30:45
6368
})
6469
require.NoError(t, err)
@@ -67,7 +72,7 @@ func TestAuditLogsFilter(t *testing.T) {
6772
err = client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
6873
Action: codersdk.AuditActionDelete,
6974
ResourceType: codersdk.ResourceTypeUser,
70-
ResourceID: userResourceID,
75+
ResourceID: user.UserID,
7176
Time: time.Date(2022, 8, 15, 14, 30, 45, 100, time.UTC), // 2022-8-15 14:30:45
7277
})
7378
require.NoError(t, err)
@@ -110,7 +115,7 @@ func TestAuditLogsFilter(t *testing.T) {
110115
},
111116
{
112117
Name: "FilterByResourceID",
113-
SearchQuery: "resource_id:" + userResourceID.String(),
118+
SearchQuery: "resource_id:" + user.UserID.String(),
114119
ExpectedResult: 2,
115120
},
116121
{
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
MockAuditLog,
3+
MockAuditLogWithWorkspaceBuild,
4+
} from "testHelpers/entities"
5+
import { AuditLogDescription } from "./AuditLogDescription"
6+
import { render } from "../../testHelpers/renderHelpers"
7+
import { screen } from "@testing-library/react"
8+
9+
const getByTextContent = (text: string) => {
10+
return screen.getByText((_, element) => {
11+
const hasText = (element: Element | null) => element?.textContent === text
12+
const elementHasText = hasText(element)
13+
const childrenDontHaveText = Array.from(element?.children || []).every(
14+
(child) => !hasText(child),
15+
)
16+
return elementHasText && childrenDontHaveText
17+
})
18+
}
19+
describe("AuditLogDescription", () => {
20+
it("renders the correct string for a workspace create audit log", async () => {
21+
render(<AuditLogDescription auditLog={MockAuditLog} />)
22+
23+
expect(
24+
getByTextContent("TestUser created workspace bruno-dev"),
25+
).toBeDefined()
26+
})
27+
28+
it("renders the correct string for a workspace_build stop audit log", async () => {
29+
render(<AuditLogDescription auditLog={MockAuditLogWithWorkspaceBuild} />)
30+
31+
expect(
32+
getByTextContent("TestUser stopped build for workspace test2"),
33+
).toBeDefined()
34+
})
35+
36+
it("renders the correct string for a workspace_build audit log with a duplicate word", async () => {
37+
const AuditLogWithRepeat = {
38+
...MockAuditLogWithWorkspaceBuild,
39+
additional_fields: {
40+
workspaceName: "workspace",
41+
},
42+
}
43+
render(<AuditLogDescription auditLog={AuditLogWithRepeat} />)
44+
45+
expect(
46+
getByTextContent("TestUser stopped build for workspace workspace"),
47+
).toBeDefined()
48+
})
49+
})

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

site/src/testHelpers/entities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ export const MockAuditLog: TypesGen.AuditLog = {
961961
additional_fields: {},
962962
description: "{user} created workspace {target}",
963963
user: MockUser,
964+
resource_link: "",
965+
is_deleted: false,
964966
}
965967

966968
export const MockAuditLog2: TypesGen.AuditLog = {
@@ -999,7 +1001,7 @@ export const MockAuditLogWithWorkspaceBuild: TypesGen.AuditLog = {
9991001
request_id: "61555889-2875-475c-8494-f7693dd5d75b",
10001002
action: "stop",
10011003
resource_type: "workspace_build",
1002-
description: "{user} stopped workspace build for {target}",
1004+
description: "{user} stopped build for workspace {target}",
10031005
additional_fields: {
10041006
workspaceName: "test2",
10051007
},

0 commit comments

Comments
 (0)