Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[40747] Set default scope for notifications to visible

Oliver Günther15394b615abc
diff --git a/app/models/queries/notifications/notification_query.rb b/app/models/queries/notifications/notification_query.rb
index be6587a9091..259a04669e2 100644
--- a/app/models/queries/notifications/notification_query.rb
+++ b/app/models/queries/notifications/notification_query.rb
@@ -34,6 +34,6 @@ class Queries::Notifications::NotificationQuery < Queries::BaseQuery
end
def default_scope
- Notification.recipient(user)
+ Notification.visible(User.current).recipient(user)
end
end
diff --git a/spec/models/queries/notifications/notification_query_spec.rb b/spec/models/queries/notifications/notification_query_spec.rb
index e7dab0b5e4f..acea32fca91 100644
--- a/spec/models/queries/notifications/notification_query_spec.rb
+++ b/spec/models/queries/notifications/notification_query_spec.rb
@@ -29,10 +29,17 @@
require 'spec_helper'
describe Queries::Notifications::NotificationQuery, type: :model do
- shared_let(:recipient) { FactoryBot.create :user }
+ shared_let(:project) { FactoryBot.create :project }
+
+ shared_let(:recipient) { FactoryBot.create :user, member_in_project: project, member_with_permissions: %i[view_work_packages] }
+
+ shared_let(:work_package) { FactoryBot.create :work_package, project: project }
+ shared_let(:notification) { FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package }
let(:instance) { described_class.new(user: recipient) }
- let(:base_scope) { Notification.recipient(recipient) }
+ let(:base_scope) { Notification.visible(recipient).recipient(recipient) }
+
+ current_user { recipient }
context 'without a filter' do
describe '#results' do
@@ -49,9 +56,9 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
- expected = base_scope.where("notifications.read_ian IN ('t')").order(id: :desc)
+ expected = base_scope.merge(Notification.where("notifications.read_ian IN ('t')").order(id: :desc)).to_sql
- expect(instance.results.to_sql).to eql expected.to_sql
+ expect(instance.results.to_sql).to eql expected
end
end
@@ -114,11 +121,7 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
- expected = <<~SQL.squish
- SELECT "notifications".* FROM "notifications"
- WHERE "notifications"."recipient_id" = #{recipient.id}
- ORDER BY "notifications"."read_ian" DESC, "notifications"."id" DESC
- SQL
+ expected = base_scope.merge(Notification.order(read_ian: :desc, id: :desc)).to_sql
expect(instance.results.to_sql).to eql expected
end
@@ -132,11 +135,7 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
- expected = <<~SQL.squish
- SELECT "notifications".* FROM "notifications"
- WHERE "notifications"."recipient_id" = #{recipient.id}
- ORDER BY "notifications"."reason" DESC, "notifications"."id" DESC
- SQL
+ expected = base_scope.merge(Notification.order(reason: :desc, id: :desc)).to_sql
expect(instance.results.to_sql).to eql expected
end
@@ -170,12 +169,11 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
- expected = <<~SQL.squish
- SELECT "notifications"."reason", COUNT(*) FROM "notifications"
- WHERE "notifications"."recipient_id" = #{recipient.id}
- GROUP BY "notifications"."reason"
- ORDER BY "notifications"."reason" ASC
- SQL
+ scope = Notification
+ .group(:reason)
+ .order(reason: :asc)
+ .select(:reason, Arel.sql('COUNT(*)'))
+ expected = base_scope.merge(scope).to_sql
expect(instance.groups.to_sql).to eql expected
end
@@ -189,12 +187,11 @@ describe Queries::Notifications::NotificationQuery, type: :model do
describe '#results' do
it 'is the same as handwriting the query' do
- expected = <<~SQL.squish
- SELECT "notifications"."project_id", COUNT(*) FROM "notifications"
- WHERE "notifications"."recipient_id" = #{recipient.id}
- GROUP BY "notifications"."project_id"
- ORDER BY "notifications"."project_id" ASC
- SQL
+ scope = Notification
+ .group(:project_id)
+ .order(project_id: :asc)
+ .select(:project_id, Arel.sql('COUNT(*)'))
+ expected = base_scope.merge(scope).to_sql
expect(instance.groups.to_sql).to eql expected
end
diff --git a/spec/requests/api/v3/notifications/bulk_read_ian_resource_spec.rb b/spec/requests/api/v3/notifications/bulk_read_ian_resource_spec.rb
index a1837ae444b..77ae690520a 100644
--- a/spec/requests/api/v3/notifications/bulk_read_ian_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/bulk_read_ian_resource_spec.rb
@@ -35,11 +35,16 @@ describe ::API::V3::Notifications::NotificationsAPI,
content_type: :json do
include API::V3::Utilities::PathHelper
- shared_let(:recipient) { FactoryBot.create :user }
+ shared_let(:project) { FactoryBot.create :project }
+
+ shared_let(:recipient) { FactoryBot.create :user, member_in_project: project, member_with_permissions: %i[view_work_packages] }
shared_let(:other_recipient) { FactoryBot.create :user }
- shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient }
- shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient }
- shared_let(:notification3) { FactoryBot.create :notification, recipient: recipient }
+
+ shared_let(:work_package) { FactoryBot.create :work_package, project: project }
+
+ shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package }
+ shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package }
+ shared_let(:notification3) { FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package }
shared_let(:other_user_notification) { FactoryBot.create :notification, recipient: other_recipient }
let(:filters) { nil }
diff --git a/spec/requests/api/v3/notifications/bulk_unread_ian_resource_spec.rb b/spec/requests/api/v3/notifications/bulk_unread_ian_resource_spec.rb
index e8fb036494c..cc93b286b46 100644
--- a/spec/requests/api/v3/notifications/bulk_unread_ian_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/bulk_unread_ian_resource_spec.rb
@@ -35,12 +35,27 @@ describe ::API::V3::Notifications::NotificationsAPI,
content_type: :json do
include API::V3::Utilities::PathHelper
- shared_let(:recipient) { FactoryBot.create :user }
+ shared_let(:project) { FactoryBot.create :project }
+ shared_let(:work_package) { FactoryBot.create :work_package, project: project }
+
+ shared_let(:recipient) { FactoryBot.create :user, member_in_project: project, member_with_permissions: %i[view_work_packages] }
shared_let(:other_recipient) { FactoryBot.create :user }
- shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient, read_ian: true }
- shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient, read_ian: true }
- shared_let(:notification3) { FactoryBot.create :notification, recipient: recipient, read_ian: true }
- shared_let(:other_user_notification) { FactoryBot.create :notification, recipient: other_recipient, read_ian: true }
+ shared_let(:notification1) do
+ FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package, read_ian: true
+ end
+ shared_let(:notification2) do
+ FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package, read_ian: true
+ end
+ shared_let(:notification3) do
+ FactoryBot.create :notification, recipient: recipient, project: project, resource: work_package, read_ian: true
+ end
+ shared_let(:other_user_notification) do
+ FactoryBot.create :notification,
+ recipient: other_recipient,
+ read_ian: true,
+ project: project,
+ resource: work_package
+ end
let(:filters) { nil }
@@ -86,10 +101,10 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
it 'sets the current users`s notifications matching the filter to read' do
- expect(::Notification.where(id: [notification1.id, notification2.id]).pluck(:read_ian))
+ expect(::Notification.where(id: [notification1.id, notification2.id]).order(id: :asc).pluck(:read_ian))
.to all(be_falsey)
- expect(::Notification.where(id: [other_user_notification, notification3.id]).pluck(:read_ian))
+ expect(::Notification.where(id: [other_user_notification.id, notification3.id]).order(id: :asc).pluck(:read_ian))
.to all(be_truthy)
end
end