Commit 30474c51 authored by Mario de la Ossa's avatar Mario de la Ossa

NotificationsController - Handle mising parent notificationsetting

If the parent is missing from base_and_ancestors, we need to gracefully
show a default notification setting
parent 63cea671
......@@ -7,7 +7,9 @@ class UserGroupNotificationSettingsFinder
end
def execute
groups_with_ancestors = Gitlab::ObjectHierarchy.new(groups).base_and_ancestors
# rubocop: disable CodeReuse/ActiveRecord
groups_with_ancestors = Gitlab::ObjectHierarchy.new(Group.where(id: groups.select(:id))).base_and_ancestors
# rubocop: enable CodeReuse/ActiveRecord
@loaded_groups_with_ancestors = groups_with_ancestors.index_by(&:id)
@loaded_notification_settings = user.notification_settings_for_groups(groups_with_ancestors).preload_source_route.index_by(&:source_id)
......@@ -27,6 +29,8 @@ class UserGroupNotificationSettingsFinder
parent_setting = loaded_notification_settings[group.parent_id]
return user.notification_settings.build(source: group) unless parent_setting
if should_copy?(parent_setting)
user.notification_settings.build(source: group) do |ns|
ns.assign_attributes(parent_setting.slice(*NotificationSetting.allowed_fields))
......
---
title: NotificationsController - Handle mising parent notificationsetting
merge_request: 41612
author:
type: fixed
......@@ -71,6 +71,43 @@ RSpec.describe UserGroupNotificationSettingsFinder do
end
end
context 'when the group has parent_id set but that does not belong to any group' do
let_it_be(:group) { create(:group) }
let_it_be(:groups) { [group] }
before do
# Let's set a parent_id for a group that definitely doesn't exist
group.update_columns(parent_id: 19283746)
end
it 'returns a default Global notification setting' do
expect(subject.count).to eq(1)
expect(attributes(&:level)).to eq(['global'])
expect(attributes(&:notification_email)).to eq([nil])
end
end
context 'when the group has a private parent' do
let_it_be(:ancestor) { create(:group, :private) }
let_it_be(:group) { create(:group, :private, parent: ancestor) }
let_it_be(:ancestor_email) { create(:email, :confirmed, email: 'ancestor@example.com', user: user) }
let_it_be(:groups) { [group] }
before do
group.add_reporter(user)
# Adding the user creates a NotificationSetting, so we remove it here
user.notification_settings.where(source: group).delete_all
create(:notification_setting, user: user, source: ancestor, level: 'participating', notification_email: ancestor_email.email)
end
it 'still inherits the notification settings' do
expect(subject.count).to eq(1)
expect(attributes(&:level)).to eq(['participating'])
expect(attributes(&:notification_email)).to eq([ancestor_email.email])
end
end
it 'does not cause an N+1', :aggregate_failures do
parent = create(:group)
child = create(:group, parent: parent)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment