TestAIProviderAuditDiff exercises the full HTTP -> enterprise auditor -> Postgres write path for AI provider updates. The mock auditor used elsewhere returns an empty diff, so this is the only place that proves changed properties land in the audit_logs row.
(t *testing.T)
| 26 | // elsewhere returns an empty diff, so this is the only place that |
| 27 | // proves changed properties land in the audit_logs row. |
| 28 | func TestAIProviderAuditDiff(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | db, ps := dbtestutil.NewDB(t) |
| 32 | auditor := entaudit.NewAuditor( |
| 33 | db, |
| 34 | entaudit.DefaultFilter, |
| 35 | backends.NewPostgres(db, true), |
| 36 | ) |
| 37 | |
| 38 | ownerClient, _ := coderdenttest.New(t, &coderdenttest.Options{ |
| 39 | AuditLogging: true, |
| 40 | Options: &coderdtest.Options{ |
| 41 | Database: db, |
| 42 | Pubsub: ps, |
| 43 | Auditor: auditor, |
| 44 | }, |
| 45 | LicenseOptions: &coderdenttest.LicenseOptions{ |
| 46 | Features: license.Features{ |
| 47 | codersdk.FeatureAuditLog: 1, |
| 48 | }, |
| 49 | }, |
| 50 | }) |
| 51 | |
| 52 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) |
| 53 | defer cancel() |
| 54 | |
| 55 | //nolint:gocritic // Owner role is the audience for this endpoint. |
| 56 | provider, err := ownerClient.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ |
| 57 | Type: codersdk.AIProviderTypeOpenAI, |
| 58 | Name: "audit-target", |
| 59 | DisplayName: "Audit Target", |
| 60 | Enabled: true, |
| 61 | BaseURL: "https://api.openai.com/v1", |
| 62 | }) |
| 63 | require.NoError(t, err) |
| 64 | |
| 65 | newDisplay := "Renamed" |
| 66 | newURL := "https://api.openai.com/v2" |
| 67 | disabled := false |
| 68 | _, err = ownerClient.UpdateAIProvider(ctx, provider.Name, codersdk.UpdateAIProviderRequest{ |
| 69 | DisplayName: &newDisplay, |
| 70 | BaseURL: &newURL, |
| 71 | Enabled: &disabled, |
| 72 | }) |
| 73 | require.NoError(t, err) |
| 74 | |
| 75 | rows, err := db.GetAuditLogsOffset( |
| 76 | dbauthz.AsSystemRestricted(ctx), |
| 77 | database.GetAuditLogsOffsetParams{ |
| 78 | ResourceType: string(database.ResourceTypeAIProvider), |
| 79 | LimitOpt: 10, |
| 80 | }, |
| 81 | ) |
| 82 | require.NoError(t, err) |
| 83 | require.Len(t, rows, 2, "expected one create and one update audit row") |
| 84 | |
| 85 | // GetAuditLogsOffset returns entries sorted by time in descending order. |
nothing calls this directly
no test coverage detected