Skip to content

Add audit links/kira pilot #5156

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 13 commits into from
Dec 2, 2022
Prev Previous commit
Next Next commit
fix/write tests
  • Loading branch information
Kira-Pilot committed Nov 30, 2022
commit d1ba2b92989a66d5bbec584464aef467bb59ffba
25 changes: 15 additions & 10 deletions coderd/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

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

ctx := context.Background()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
user := coderdtest.CreateFirstUser(t, client)

err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{})
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
ResourceID: user.UserID,
})
require.NoError(t, err)

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

ctx := context.Background()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
userResourceID := uuid.New()
var (
ctx = context.Background()
client = coderdtest.New(t, nil)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
)

// Create two logs with "Create"
err := client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
Action: codersdk.AuditActionCreate,
ResourceType: codersdk.ResourceTypeTemplate,
ResourceID: template.ID,
Time: time.Date(2022, 8, 15, 14, 30, 45, 100, time.UTC), // 2022-8-15 14:30:45
})
require.NoError(t, err)
err = client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
Action: codersdk.AuditActionCreate,
ResourceType: codersdk.ResourceTypeUser,
ResourceID: userResourceID,
ResourceID: user.UserID,
Time: time.Date(2022, 8, 16, 14, 30, 45, 100, time.UTC), // 2022-8-16 14:30:45
})
require.NoError(t, err)
Expand All @@ -67,7 +72,7 @@ func TestAuditLogsFilter(t *testing.T) {
err = client.CreateTestAuditLog(ctx, codersdk.CreateTestAuditLogRequest{
Action: codersdk.AuditActionDelete,
ResourceType: codersdk.ResourceTypeUser,
ResourceID: userResourceID,
ResourceID: user.UserID,
Time: time.Date(2022, 8, 15, 14, 30, 45, 100, time.UTC), // 2022-8-15 14:30:45
})
require.NoError(t, err)
Expand Down Expand Up @@ -110,7 +115,7 @@ func TestAuditLogsFilter(t *testing.T) {
},
{
Name: "FilterByResourceID",
SearchQuery: "resource_id:" + userResourceID.String(),
SearchQuery: "resource_id:" + user.UserID.String(),
ExpectedResult: 2,
},
{
Expand Down
49 changes: 49 additions & 0 deletions site/src/components/AuditLogRow/AuditLogDescription.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
MockAuditLog,
MockAuditLogWithWorkspaceBuild,
} from "testHelpers/entities"
import { AuditLogDescription } from "./AuditLogDescription"
import { render } from "../../testHelpers/renderHelpers"
import { screen } from "@testing-library/react"

const getByTextContent = (text: string) => {
return screen.getByText((_, element) => {
const hasText = (element: Element | null) => element?.textContent === text
const elementHasText = hasText(element)
const childrenDontHaveText = Array.from(element?.children || []).every(
(child) => !hasText(child),
)
return elementHasText && childrenDontHaveText
})
}
describe("AuditLogDescription", () => {
it("renders the correct string for a workspace create audit log", async () => {
render(<AuditLogDescription auditLog={MockAuditLog} />)

expect(
getByTextContent("TestUser created workspace bruno-dev"),
).toBeDefined()
})

it("renders the correct string for a workspace_build stop audit log", async () => {
render(<AuditLogDescription auditLog={MockAuditLogWithWorkspaceBuild} />)

expect(
getByTextContent("TestUser stopped build for workspace test2"),
).toBeDefined()
})

it("renders the correct string for a workspace_build audit log with a duplicate word", async () => {
const AuditLogWithRepeat = {
...MockAuditLogWithWorkspaceBuild,
additional_fields: {
workspaceName: "workspace",
},
}
render(<AuditLogDescription auditLog={AuditLogWithRepeat} />)

expect(
getByTextContent("TestUser stopped build for workspace workspace"),
).toBeDefined()
})
})
41 changes: 0 additions & 41 deletions site/src/components/AuditLogRow/AuditLogRow.test.tsx

This file was deleted.

4 changes: 3 additions & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,8 @@ export const MockAuditLog: TypesGen.AuditLog = {
additional_fields: {},
description: "{user} created workspace {target}",
user: MockUser,
resource_link: "",
is_deleted: false,
}

export const MockAuditLog2: TypesGen.AuditLog = {
Expand Down Expand Up @@ -999,7 +1001,7 @@ export const MockAuditLogWithWorkspaceBuild: TypesGen.AuditLog = {
request_id: "61555889-2875-475c-8494-f7693dd5d75b",
action: "stop",
resource_type: "workspace_build",
description: "{user} stopped workspace build for {target}",
description: "{user} stopped build for workspace {target}",
additional_fields: {
workspaceName: "test2",
},
Expand Down