mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
59b71f296f
Closes #21440 The `TestDBPurgeAuthorization` test was overfitting by calling each purge method individually, which reimplemented dbpurge logic in the test and created a maintenance burden. When new purge steps are added, they either need to be reflected in the test or there will be a testing blindspot. This change extracts the `doTick` closure into an exported `PurgeTick` function that returns an error, making the core purge logic testable. The test now calls `PurgeTick` directly to exercise the actual dbpurge behavior rather than reimplementing it. Retention values are configured to ensure all purge operations run, so we test RBAC permissions for all code paths. - Tests actual dbpurge behavior instead of reimplementing it - Automatically covers new purge steps when they're added - Still validates that all operations have proper RBAC permissions The test focuses on authorization (checking for RBAC errors) rather than verifying deletion behavior, which is already covered by other tests like `TestDeleteExpiredAPIKeys` and `TestDeleteOldAuditLogs`.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package dbpurge
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
"github.com/coder/quartz"
|
|
)
|
|
|
|
func TestDBPurgeAuthorization(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
rawDB, _ := dbtestutil.NewDB(t)
|
|
|
|
authz := rbac.NewAuthorizer(prometheus.NewRegistry())
|
|
db := dbauthz.New(rawDB, authz, testutil.Logger(t), coderdtest.AccessControlStorePointer())
|
|
|
|
ctx = dbauthz.AsDBPurge(ctx)
|
|
|
|
clk := quartz.NewMock(t)
|
|
now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC)
|
|
clk.Set(now)
|
|
|
|
vals := &codersdk.DeploymentValues{ /* same vals as before */ }
|
|
|
|
inst := &instance{
|
|
logger: testutil.Logger(t),
|
|
vals: vals,
|
|
clk: clk,
|
|
// metrics can be nil in this test
|
|
}
|
|
|
|
err := inst.purgeTick(ctx, db, now)
|
|
require.NoError(t, err)
|
|
}
|