Skip to content

Commit dc74ac4

Browse files
committed
test dbauthz
1 parent 9edb384 commit dc74ac4

File tree

4 files changed

+182
-4
lines changed

4 files changed

+182
-4
lines changed

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,6 +4477,169 @@ func (s *MethodTestSuite) TestNotifications() {
44774477
Disableds: []bool{true, false},
44784478
}).Asserts(rbac.ResourceNotificationPreference.WithOwner(user.ID.String()), policy.ActionUpdate)
44794479
}))
4480+
4481+
s.Run("FetchUnreadInboxNotificationsByUserID", s.Subtest(func(db database.Store, check *expects) {
4482+
u := dbgen.User(s.T(), db, database.User{})
4483+
o := dbgen.Organization(s.T(), db, database.Organization{})
4484+
tpl := dbgen.Template(s.T(), db, database.Template{
4485+
OrganizationID: o.ID,
4486+
CreatedBy: u.ID,
4487+
})
4488+
4489+
notifID := uuid.New()
4490+
4491+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4492+
ID: notifID,
4493+
UserID: u.ID,
4494+
TemplateID: tpl.ID,
4495+
Title: "test title",
4496+
Content: "test content notification",
4497+
Icon: "test icon",
4498+
})
4499+
4500+
check.Args(u.ID).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionRead).Returns([]database.NotificationsInbox{notif})
4501+
}))
4502+
4503+
s.Run("FetchInboxNotificationsByUserID", s.Subtest(func(db database.Store, check *expects) {
4504+
u := dbgen.User(s.T(), db, database.User{})
4505+
o := dbgen.Organization(s.T(), db, database.Organization{})
4506+
tpl := dbgen.Template(s.T(), db, database.Template{
4507+
OrganizationID: o.ID,
4508+
CreatedBy: u.ID,
4509+
})
4510+
4511+
notifID := uuid.New()
4512+
4513+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4514+
ID: notifID,
4515+
UserID: u.ID,
4516+
TemplateID: tpl.ID,
4517+
Title: "test title",
4518+
Content: "test content notification",
4519+
Icon: "test icon",
4520+
})
4521+
4522+
check.Args(u.ID).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionRead).Returns([]database.NotificationsInbox{notif})
4523+
}))
4524+
4525+
s.Run("FetchInboxNotificationsByUserIDAndTemplateIDAndTargets", s.Subtest(func(db database.Store, check *expects) {
4526+
u := dbgen.User(s.T(), db, database.User{})
4527+
o := dbgen.Organization(s.T(), db, database.Organization{})
4528+
tpl := dbgen.Template(s.T(), db, database.Template{
4529+
OrganizationID: o.ID,
4530+
CreatedBy: u.ID,
4531+
})
4532+
4533+
notifID := uuid.New()
4534+
4535+
targets := []uuid.UUID{u.ID, tpl.ID}
4536+
4537+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4538+
ID: notifID,
4539+
UserID: u.ID,
4540+
TemplateID: tpl.ID,
4541+
Targets: targets,
4542+
Title: "test title",
4543+
Content: "test content notification",
4544+
Icon: "test icon",
4545+
})
4546+
4547+
check.Args(database.FetchInboxNotificationsByUserIDAndTemplateIDAndTargetsParams{
4548+
UserID: u.ID,
4549+
TemplateID: tpl.ID,
4550+
Targets: []uuid.UUID{u.ID},
4551+
}).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionRead).Returns([]database.NotificationsInbox{notif})
4552+
}))
4553+
4554+
s.Run("FetchUnreadInboxNotificationsByUserIDAndTemplateIDAndTargets", s.Subtest(func(db database.Store, check *expects) {
4555+
u := dbgen.User(s.T(), db, database.User{})
4556+
o := dbgen.Organization(s.T(), db, database.Organization{})
4557+
tpl := dbgen.Template(s.T(), db, database.Template{
4558+
OrganizationID: o.ID,
4559+
CreatedBy: u.ID,
4560+
})
4561+
4562+
notifID := uuid.New()
4563+
4564+
targets := []uuid.UUID{u.ID, tpl.ID}
4565+
4566+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4567+
ID: notifID,
4568+
UserID: u.ID,
4569+
TemplateID: tpl.ID,
4570+
Targets: targets,
4571+
Title: "test title",
4572+
Content: "test content notification",
4573+
Icon: "test icon",
4574+
})
4575+
4576+
check.Args(database.FetchUnreadInboxNotificationsByUserIDAndTemplateIDAndTargetsParams{
4577+
UserID: u.ID,
4578+
TemplateID: tpl.ID,
4579+
Targets: []uuid.UUID{u.ID},
4580+
}).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionRead).Returns([]database.NotificationsInbox{notif})
4581+
}))
4582+
4583+
s.Run("GetInboxNotificationByID", s.Subtest(func(db database.Store, check *expects) {
4584+
u := dbgen.User(s.T(), db, database.User{})
4585+
o := dbgen.Organization(s.T(), db, database.Organization{})
4586+
tpl := dbgen.Template(s.T(), db, database.Template{
4587+
OrganizationID: o.ID,
4588+
CreatedBy: u.ID,
4589+
})
4590+
4591+
notifID := uuid.New()
4592+
4593+
targets := []uuid.UUID{u.ID, tpl.ID}
4594+
4595+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4596+
ID: notifID,
4597+
UserID: u.ID,
4598+
TemplateID: tpl.ID,
4599+
Targets: targets,
4600+
Title: "test title",
4601+
Content: "test content notification",
4602+
Icon: "test icon",
4603+
})
4604+
4605+
check.Args(notifID).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionRead).Returns(notif)
4606+
}))
4607+
4608+
s.Run("InsertInboxNotification", s.Subtest(func(_ database.Store, check *expects) {
4609+
owner := uuid.UUID{}
4610+
check.Args(database.InsertInboxNotificationParams{}).Asserts(rbac.ResourceNotificationInbox.WithOwner(owner.String()), policy.ActionCreate)
4611+
}))
4612+
4613+
s.Run("SetInboxNotificationAsRead", s.Subtest(func(db database.Store, check *expects) {
4614+
u := dbgen.User(s.T(), db, database.User{})
4615+
o := dbgen.Organization(s.T(), db, database.Organization{})
4616+
tpl := dbgen.Template(s.T(), db, database.Template{
4617+
OrganizationID: o.ID,
4618+
CreatedBy: u.ID,
4619+
})
4620+
4621+
notifID := uuid.New()
4622+
4623+
targets := []uuid.UUID{u.ID, tpl.ID}
4624+
readAt := dbtestutil.NowInDefaultTimezone()
4625+
4626+
notif := dbgen.NotificationInbox(s.T(), db, database.InsertInboxNotificationParams{
4627+
ID: notifID,
4628+
UserID: u.ID,
4629+
TemplateID: tpl.ID,
4630+
Targets: targets,
4631+
Title: "test title",
4632+
Content: "test content notification",
4633+
Icon: "test icon",
4634+
})
4635+
4636+
notif.ReadAt = sql.NullTime{Time: readAt, Valid: true}
4637+
4638+
check.Args(database.SetInboxNotificationAsReadParams{
4639+
ID: notifID,
4640+
ReadAt: sql.NullTime{Time: readAt, Valid: true},
4641+
}).Asserts(rbac.ResourceNotificationInbox.WithID(notifID).WithOwner(u.ID.String()), policy.ActionUpdate)
4642+
}))
44804643
}
44814644

44824645
func (s *MethodTestSuite) TestOAuth2ProviderApps() {

coderd/database/dbgen/dbgen.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,21 @@ func OrganizationMember(t testing.TB, db database.Store, orig database.Organizat
450450
return mem
451451
}
452452

453+
func NotificationInbox(t testing.TB, db database.Store, orig database.InsertInboxNotificationParams) database.NotificationsInbox {
454+
notification, err := db.InsertInboxNotification(genCtx, database.InsertInboxNotificationParams{
455+
ID: takeFirst(orig.ID, uuid.New()),
456+
UserID: takeFirst(orig.UserID, uuid.New()),
457+
TemplateID: takeFirst(orig.TemplateID, uuid.New()),
458+
Targets: takeFirstSlice(orig.Targets, []uuid.UUID{}),
459+
Title: takeFirst(orig.Title, testutil.GetRandomName(t)),
460+
Content: takeFirst(orig.Content, testutil.GetRandomName(t)),
461+
Icon: takeFirst(orig.Icon, ""),
462+
CreatedAt: takeFirst(orig.CreatedAt, dbtime.Now()),
463+
})
464+
require.NoError(t, err, "insert notification")
465+
return notification
466+
}
467+
453468
func Group(t testing.TB, db database.Store, orig database.Group) database.Group {
454469
t.Helper()
455470

coderd/database/queries.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/notificationsinbox.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ SELECT * FROM notifications_inbox WHERE user_id = $1 AND read_at IS NULL ORDER B
55
SELECT * FROM notifications_inbox WHERE user_id = $1 ORDER BY created_at DESC;
66

77
-- name: FetchInboxNotificationsByUserIDAndTemplateIDAndTargets :many
8-
SELECT * FROM notifications_inbox WHERE user_id = $1 AND template_id = $2 AND targets @> $3 ORDER BY created_at DESC;
8+
SELECT * FROM notifications_inbox WHERE user_id = $1 AND template_id = $2 AND targets @> COALESCE($3, ARRAY[]::UUID[]) ORDER BY created_at DESC;
99

1010
-- name: FetchUnreadInboxNotificationsByUserIDAndTemplateIDAndTargets :many
11-
SELECT * FROM notifications_inbox WHERE user_id = $1 AND template_id = $2 AND targets @> $3 AND read_at IS NULL ORDER BY created_at DESC;
11+
SELECT * FROM notifications_inbox WHERE user_id = $1 AND template_id = $2 AND targets @> COALESCE($3, ARRAY[]::UUID[]) AND read_at IS NULL ORDER BY created_at DESC;
1212

1313
-- name: GetInboxNotificationByID :one
1414
SELECT * FROM notifications_inbox WHERE id = $1;

0 commit comments

Comments
 (0)