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

Skip to content

Commit 17949db

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

File tree

5 files changed

+91
-106
lines changed

5 files changed

+91
-106
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,22 @@ 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.ResourceNotificationTemplate.Type: {policy.ActionCreate, policy.ActionUpdate, policy.ActionDelete},
254255
}),
255256
Org: map[string][]rbac.Permission{},
256257
User: []rbac.Permission{},

coderd/notifications/manager_test.go

Lines changed: 3 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"

coderd/notifications/metrics_test.go

Lines changed: 14 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,8 @@ 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+
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
40+
_, _, api := coderdtest.NewWithAPI(t, nil)
3941

4042
reg := prometheus.NewRegistry()
4143
metrics := notifications.NewMetrics(reg)
@@ -55,7 +57,7 @@ func TestMetrics(t *testing.T) {
5557
cfg.RetryInterval = serpent.Duration(time.Millisecond * 50)
5658
cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100) // Twice as long as fetch interval to ensure we catch pending updates.
5759

58-
mgr, err := notifications.NewManager(cfg, store, defaultHelpers(), metrics, logger.Named("manager"))
60+
mgr, err := notifications.NewManager(cfg, api.Database, defaultHelpers(), metrics, api.Logger.Named("manager"))
5961
require.NoError(t, err)
6062
t.Cleanup(func() {
6163
assert.NoError(t, mgr.Stop(ctx))
@@ -65,10 +67,10 @@ func TestMetrics(t *testing.T) {
6567
method: handler,
6668
})
6769

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

71-
user := createSampleUser(t, store)
73+
user := createSampleUser(t, api.Database)
7274

7375
// Build fingerprints for the two different series we expect.
7476
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, template.String())
@@ -319,7 +321,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
319321
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
320322
const msgCount = 2
321323
for i := 0; i < msgCount; i++ {
322-
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
324+
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
323325
require.NoError(t, err)
324326
}
325327

@@ -353,7 +355,9 @@ func TestCustomMethodMetricCollection(t *testing.T) {
353355
// UpdateNotificationTemplateMethodByID only makes sense with a real database.
354356
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
355357
}
356-
ctx, logger, store := setup(t)
358+
359+
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
360+
_, _, api := coderdtest.NewWithAPI(t, nil)
357361

358362
var (
359363
reg = prometheus.NewRegistry()
@@ -368,7 +372,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
368372
)
369373

370374
// GIVEN: a template whose notification method differs from the default.
371-
out, err := store.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
375+
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
372376
ID: template,
373377
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
374378
})
@@ -377,7 +381,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
377381

378382
// WHEN: two notifications (each with different templates) are enqueued.
379383
cfg := defaultNotificationsConfig(defaultMethod)
380-
mgr, err := notifications.NewManager(cfg, store, defaultHelpers(), metrics, logger.Named("manager"))
384+
mgr, err := notifications.NewManager(cfg, api.Database, defaultHelpers(), metrics, api.Logger.Named("manager"))
381385
require.NoError(t, err)
382386
t.Cleanup(func() {
383387
assert.NoError(t, mgr.Stop(ctx))
@@ -390,10 +394,10 @@ func TestCustomMethodMetricCollection(t *testing.T) {
390394
customMethod: webhookHandler,
391395
})
392396

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

396-
user := createSampleUser(t, store)
400+
user := createSampleUser(t, api.Database)
397401

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

0 commit comments

Comments
 (0)