Skip to content

Commit acc1f08

Browse files
committed
test dbauthz
1 parent fddf156 commit acc1f08

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

44814644
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy