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

Skip to content

Commit 148fac8

Browse files
committed
Replacing pg setup with standard coderdtest.New
Signed-off-by: Danny Kopping <[email protected]>
1 parent 6f72588 commit 148fac8

File tree

5 files changed

+119
-107
lines changed

5 files changed

+119
-107
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,23 @@ var (
236236
Identifier: rbac.RoleIdentifier{Name: "system"},
237237
DisplayName: "Coder",
238238
Site: rbac.Permissions(map[string][]policy.Action{
239-
rbac.ResourceWildcard.Type: {policy.ActionRead},
240-
rbac.ResourceApiKey.Type: rbac.ResourceApiKey.AvailableActions(),
241-
rbac.ResourceGroup.Type: {policy.ActionCreate, policy.ActionUpdate},
242-
rbac.ResourceAssignRole.Type: rbac.ResourceAssignRole.AvailableActions(),
243-
rbac.ResourceAssignOrgRole.Type: rbac.ResourceAssignOrgRole.AvailableActions(),
244-
rbac.ResourceSystem.Type: {policy.WildcardSymbol},
245-
rbac.ResourceOrganization.Type: {policy.ActionCreate, policy.ActionRead},
246-
rbac.ResourceOrganizationMember.Type: {policy.ActionCreate},
247-
rbac.ResourceProvisionerDaemon.Type: {policy.ActionCreate, policy.ActionUpdate},
248-
rbac.ResourceProvisionerKeys.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionDelete},
249-
rbac.ResourceUser.Type: rbac.ResourceUser.AvailableActions(),
250-
rbac.ResourceWorkspaceDormant.Type: {policy.ActionUpdate, policy.ActionDelete, policy.ActionWorkspaceStop},
251-
rbac.ResourceWorkspace.Type: {policy.ActionUpdate, policy.ActionDelete, policy.ActionWorkspaceStart, policy.ActionWorkspaceStop, policy.ActionSSH},
252-
rbac.ResourceWorkspaceProxy.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
253-
rbac.ResourceDeploymentConfig.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
239+
rbac.ResourceWildcard.Type: {policy.ActionRead},
240+
rbac.ResourceApiKey.Type: rbac.ResourceApiKey.AvailableActions(),
241+
rbac.ResourceGroup.Type: {policy.ActionCreate, policy.ActionUpdate},
242+
rbac.ResourceAssignRole.Type: rbac.ResourceAssignRole.AvailableActions(),
243+
rbac.ResourceAssignOrgRole.Type: rbac.ResourceAssignOrgRole.AvailableActions(),
244+
rbac.ResourceSystem.Type: {policy.WildcardSymbol},
245+
rbac.ResourceOrganization.Type: {policy.ActionCreate, policy.ActionRead},
246+
rbac.ResourceOrganizationMember.Type: {policy.ActionCreate},
247+
rbac.ResourceProvisionerDaemon.Type: {policy.ActionCreate, policy.ActionUpdate},
248+
rbac.ResourceProvisionerKeys.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionDelete},
249+
rbac.ResourceUser.Type: rbac.ResourceUser.AvailableActions(),
250+
rbac.ResourceWorkspaceDormant.Type: {policy.ActionUpdate, policy.ActionDelete, policy.ActionWorkspaceStop},
251+
rbac.ResourceWorkspace.Type: {policy.ActionUpdate, policy.ActionDelete, policy.ActionWorkspaceStart, policy.ActionWorkspaceStop, policy.ActionSSH},
252+
rbac.ResourceWorkspaceProxy.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
253+
rbac.ResourceDeploymentConfig.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
254+
rbac.ResourceNotificationPreference.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
255+
rbac.ResourceNotificationTemplate.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
254256
}),
255257
Org: map[string][]rbac.Permission{},
256258
User: []rbac.Permission{},

coderd/notifications/manager_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/coder/quartz"
11-
"github.com/coder/serpent"
1210
"github.com/google/uuid"
1311
"github.com/stretchr/testify/assert"
1412
"github.com/stretchr/testify/require"
1513
"golang.org/x/xerrors"
1614

15+
"github.com/coder/quartz"
16+
"github.com/coder/serpent"
17+
1718
"github.com/coder/coder/v2/coderd/coderdtest"
1819
"github.com/coder/coder/v2/coderd/database"
1920
"github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -28,6 +29,8 @@ func TestBufferedUpdates(t *testing.T) {
2829
t.Parallel()
2930

3031
// setup
32+
33+
// nolint:gocritic // Unit test.
3134
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
3235
_, _, api := coderdtest.NewWithAPI(t, nil)
3336

@@ -97,6 +100,8 @@ func TestBuildPayload(t *testing.T) {
97100
t.Parallel()
98101

99102
// SETUP
103+
104+
// nolint:gocritic // Unit test.
100105
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
101106
_, _, api := coderdtest.NewWithAPI(t, nil)
102107

@@ -152,6 +157,8 @@ func TestStopBeforeRun(t *testing.T) {
152157
t.Parallel()
153158

154159
// SETUP
160+
161+
// nolint:gocritic // Unit test.
155162
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
156163
_, _, api := coderdtest.NewWithAPI(t, nil)
157164

coderd/notifications/metrics_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package notifications_test
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67
"time"
78

@@ -35,7 +36,9 @@ func TestMetrics(t *testing.T) {
3536
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
3637
}
3738

38-
ctx, logger, store := setup(t)
39+
// nolint:gocritic // Unit test.
40+
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
41+
_, _, api := coderdtest.NewWithAPI(t, nil)
3942

4043
reg := prometheus.NewRegistry()
4144
metrics := notifications.NewMetrics(reg)
@@ -55,7 +58,7 @@ func TestMetrics(t *testing.T) {
5558
cfg.RetryInterval = serpent.Duration(time.Millisecond * 50)
5659
cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100) // Twice as long as fetch interval to ensure we catch pending updates.
5760

58-
mgr, err := notifications.NewManager(cfg, store, defaultHelpers(), metrics, logger.Named("manager"))
61+
mgr, err := notifications.NewManager(cfg, api.Database, defaultHelpers(), metrics, api.Logger.Named("manager"))
5962
require.NoError(t, err)
6063
t.Cleanup(func() {
6164
assert.NoError(t, mgr.Stop(ctx))
@@ -65,10 +68,10 @@ func TestMetrics(t *testing.T) {
6568
method: handler,
6669
})
6770

68-
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
71+
enq, err := notifications.NewStoreEnqueuer(cfg, api.Database, defaultHelpers(), api.Logger.Named("enqueuer"), quartz.NewReal())
6972
require.NoError(t, err)
7073

71-
user := createSampleUser(t, store)
74+
user := createSampleUser(t, api.Database)
7275

7376
// Build fingerprints for the two different series we expect.
7477
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, template.String())
@@ -206,6 +209,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
206209
t.Parallel()
207210

208211
// SETUP
212+
// nolint:gocritic // Unit test.
209213
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
210214
_, _, api := coderdtest.NewWithAPI(t, nil)
211215

@@ -282,6 +286,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
282286
t.Parallel()
283287

284288
// SETUP
289+
// nolint:gocritic // Unit test.
285290
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
286291
_, _, api := coderdtest.NewWithAPI(t, nil)
287292

@@ -319,7 +324,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
319324
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
320325
const msgCount = 2
321326
for i := 0; i < msgCount; i++ {
322-
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
327+
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
323328
require.NoError(t, err)
324329
}
325330

@@ -353,7 +358,10 @@ func TestCustomMethodMetricCollection(t *testing.T) {
353358
// UpdateNotificationTemplateMethodByID only makes sense with a real database.
354359
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
355360
}
356-
ctx, logger, store := setup(t)
361+
362+
// nolint:gocritic // Unit test.
363+
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
364+
_, _, api := coderdtest.NewWithAPI(t, nil)
357365

358366
var (
359367
reg = prometheus.NewRegistry()
@@ -368,7 +376,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
368376
)
369377

370378
// GIVEN: a template whose notification method differs from the default.
371-
out, err := store.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
379+
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
372380
ID: template,
373381
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
374382
})
@@ -377,7 +385,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
377385

378386
// WHEN: two notifications (each with different templates) are enqueued.
379387
cfg := defaultNotificationsConfig(defaultMethod)
380-
mgr, err := notifications.NewManager(cfg, store, defaultHelpers(), metrics, logger.Named("manager"))
388+
mgr, err := notifications.NewManager(cfg, api.Database, defaultHelpers(), metrics, api.Logger.Named("manager"))
381389
require.NoError(t, err)
382390
t.Cleanup(func() {
383391
assert.NoError(t, mgr.Stop(ctx))
@@ -390,10 +398,10 @@ func TestCustomMethodMetricCollection(t *testing.T) {
390398
customMethod: webhookHandler,
391399
})
392400

393-
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
401+
enq, err := notifications.NewStoreEnqueuer(cfg, api.Database, defaultHelpers(), api.Logger.Named("enqueuer"), quartz.NewReal())
394402
require.NoError(t, err)
395403

396-
user := createSampleUser(t, store)
404+
user := createSampleUser(t, api.Database)
397405

398406
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
399407
require.NoError(t, err)

0 commit comments

Comments
 (0)