Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test harder
  • Loading branch information
johnstcn committed Sep 2, 2025
commit 6a92b2abe60a9d4d2c26ce585bb6907dc4a0bca0
24 changes: 18 additions & 6 deletions coderd/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coderd_test

import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -303,21 +304,32 @@ func TestSessionExpiry(t *testing.T) {

func TestAPIKey_OK(t *testing.T) {
t.Parallel()

// Given: a deployment with auditing enabled
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
auditor := audit.NewMock()
client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
owner := coderdtest.CreateFirstUser(t, client)

auditor.ResetLogs()

// When: an API key is created
res, err := client.CreateAPIKey(ctx, codersdk.Me)
require.NoError(t, err)
require.Greater(t, len(res.Key), 2)
require.True(t, auditor.Contains(t, database.AuditLog{
UserID: owner.UserID,
Action: database.AuditActionCreate,
ResourceType: database.ResourceTypeApiKey,
}))

// Then: an audit log is generated
als := auditor.AuditLogs()
require.Len(t, als, 1)
al := als[0]
assert.Equal(t, owner.UserID, al.UserID)
assert.Equal(t, database.AuditActionCreate, al.Action)
assert.Equal(t, database.ResourceTypeApiKey, al.ResourceType)

// Then: the diff MUST NOT contain the generated key.
raw, err := json.Marshal(al)
require.NoError(t, err)
require.NotContains(t, res.Key, string(raw))
}

func TestAPIKey_Deleted(t *testing.T) {
Expand Down
Loading