MCPcopy Index your code
hub / github.com/coder/coder / TestAPIKey_OK

Function TestAPIKey_OK

coderd/apikey_test.go:446–474  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

444}
445
446func TestAPIKey_OK(t *testing.T) {
447 t.Parallel()
448
449 // Given: a deployment with auditing enabled
450 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
451 defer cancel()
452 auditor := audit.NewMock()
453 client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
454 owner := coderdtest.CreateFirstUser(t, client)
455 auditor.ResetLogs()
456
457 // When: an API key is created
458 res, err := client.CreateAPIKey(ctx, codersdk.Me)
459 require.NoError(t, err)
460 require.Greater(t, len(res.Key), 2)
461
462 // Then: an audit log is generated
463 als := auditor.AuditLogs()
464 require.Len(t, als, 1)
465 al := als[0]
466 assert.Equal(t, owner.UserID, al.UserID)
467 assert.Equal(t, database.AuditActionCreate, al.Action)
468 assert.Equal(t, database.ResourceTypeApiKey, al.ResourceType)
469
470 // Then: the diff MUST NOT contain the generated key.
471 raw, err := json.Marshal(al)
472 require.NoError(t, err)
473 require.NotContains(t, res.Key, string(raw))
474}
475
476func TestAPIKey_Deleted(t *testing.T) {
477 t.Parallel()

Callers

nothing calls this directly

Calls 9

NewMockFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
ResetLogsMethod · 0.80
CreateAPIKeyMethod · 0.80
AuditLogsMethod · 0.45
LenMethod · 0.45
EqualMethod · 0.45
MarshalMethod · 0.45

Tested by

no test coverage detected