(t *testing.T)
| 444 | } |
| 445 | |
| 446 | func 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 | |
| 476 | func TestAPIKey_Deleted(t *testing.T) { |
| 477 | t.Parallel() |
nothing calls this directly
no test coverage detected