2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2016-03-28 07:41:00 -04:00
|
|
|
|
2017-06-29 00:13:10 -04:00
|
|
|
RSpec.describe NotificationSetting do
|
2018-12-03 08:01:30 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
2018-12-03 07:47:26 -05:00
|
|
|
|
2016-03-28 07:41:00 -04:00
|
|
|
describe "Associations" do
|
|
|
|
it { is_expected.to belong_to(:user) }
|
2016-03-28 17:22:28 -04:00
|
|
|
it { is_expected.to belong_to(:source) }
|
2016-03-28 07:41:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Validation" do
|
2017-06-29 00:13:10 -04:00
|
|
|
subject { described_class.new(source_id: 1, source_type: 'Project') }
|
2016-03-28 07:41:00 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
|
|
|
it { is_expected.to validate_presence_of(:level) }
|
2017-07-24 08:33:14 -04:00
|
|
|
|
|
|
|
describe 'user_id' do
|
2017-07-24 12:26:15 -04:00
|
|
|
before do
|
|
|
|
subject.user = create(:user)
|
|
|
|
end
|
2017-07-24 08:33:14 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to([:source_type, :source_id]).with_message(/already exists in source/) }
|
|
|
|
end
|
2016-05-31 16:51:46 -04:00
|
|
|
|
|
|
|
context "events" do
|
|
|
|
let(:user) { create(:user) }
|
2017-06-29 00:13:10 -04:00
|
|
|
let(:notification_setting) { described_class.new(source_id: 1, source_type: 'Project', user_id: user.id) }
|
2016-05-31 16:51:46 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
notification_setting.level = "custom"
|
|
|
|
notification_setting.new_note = "true"
|
|
|
|
notification_setting.new_issue = 1
|
|
|
|
notification_setting.close_issue = "1"
|
|
|
|
notification_setting.merge_merge_request = "t"
|
|
|
|
notification_setting.close_merge_request = "nil"
|
|
|
|
notification_setting.reopen_merge_request = "false"
|
|
|
|
notification_setting.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses boolean before saving" do
|
|
|
|
expect(notification_setting.new_note).to eq(true)
|
|
|
|
expect(notification_setting.new_issue).to eq(true)
|
|
|
|
expect(notification_setting.close_issue).to eq(true)
|
|
|
|
expect(notification_setting.merge_merge_request).to eq(true)
|
2018-12-15 04:06:56 -05:00
|
|
|
expect(notification_setting.close_merge_request).to eq(true)
|
2016-05-31 16:51:46 -04:00
|
|
|
expect(notification_setting.reopen_merge_request).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2020-05-27 17:08:05 -04:00
|
|
|
|
|
|
|
context 'notification_email' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
subject { described_class.new(source_id: 1, source_type: 'Project', user_id: user.id) }
|
|
|
|
|
|
|
|
it 'allows to change email to verified one' do
|
|
|
|
email = create(:email, :confirmed, user: user)
|
|
|
|
|
|
|
|
subject.update(notification_email: email.email)
|
|
|
|
|
|
|
|
expect(subject).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow to change email to not verified one' do
|
|
|
|
email = create(:email, user: user)
|
|
|
|
|
|
|
|
subject.update(notification_email: email.email)
|
|
|
|
|
|
|
|
expect(subject).to be_invalid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to change email to empty one' do
|
|
|
|
subject.update(notification_email: '')
|
|
|
|
|
|
|
|
expect(subject).to be_valid
|
|
|
|
end
|
|
|
|
end
|
2016-03-28 07:41:00 -04:00
|
|
|
end
|
2016-07-07 07:41:48 -04:00
|
|
|
|
|
|
|
describe '#for_projects' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
1.upto(4) do |i|
|
|
|
|
setting = create(:notification_setting, user: user)
|
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
setting.project.update(pending_delete: true) if i.even?
|
2016-07-07 07:41:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes projects pending delete' do
|
|
|
|
expect(user.notification_settings.for_projects).to all(have_attributes(project: an_instance_of(Project)))
|
|
|
|
expect(user.notification_settings.for_projects.map(&:project)).to all(have_attributes(pending_delete: false))
|
|
|
|
end
|
|
|
|
end
|
2017-06-08 12:10:35 -04:00
|
|
|
|
2017-07-28 05:44:33 -04:00
|
|
|
describe '#event_enabled?' do
|
2017-06-08 12:10:35 -04:00
|
|
|
before do
|
|
|
|
subject.update!(user: create(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an event with a matching column name' do
|
|
|
|
it 'returns the value of the column' do
|
2017-07-28 05:44:33 -04:00
|
|
|
subject.update!(new_note: true)
|
2017-06-08 12:10:35 -04:00
|
|
|
|
2017-07-28 05:44:33 -04:00
|
|
|
expect(subject.event_enabled?(:new_note)).to be(true)
|
2017-06-08 12:10:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the column has a nil value' do
|
2017-07-28 05:44:33 -04:00
|
|
|
it 'returns false' do
|
2017-06-08 12:10:35 -04:00
|
|
|
expect(subject.event_enabled?(:new_note)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an event without a matching column name' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(subject.event_enabled?(:foo_event)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
2020-08-17 08:10:12 -04:00
|
|
|
|
|
|
|
describe 'for failed_pipeline' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:column, :expected) do
|
|
|
|
nil | true
|
|
|
|
true | true
|
|
|
|
false | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
subject.update!(failed_pipeline: column)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(subject.event_enabled?(:failed_pipeline)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'for fixed_pipeline' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:column, :expected) do
|
|
|
|
nil | true
|
|
|
|
true | true
|
|
|
|
false | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
subject.update!(fixed_pipeline: column)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(subject.event_enabled?(:fixed_pipeline)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-08 12:10:35 -04:00
|
|
|
end
|
2018-07-10 10:29:27 -04:00
|
|
|
|
2018-07-22 22:42:19 -04:00
|
|
|
describe '.email_events' do
|
|
|
|
subject { described_class.email_events }
|
|
|
|
|
|
|
|
it 'returns email events' do
|
|
|
|
expect(subject).to include(
|
2019-10-17 14:08:05 -04:00
|
|
|
:new_release,
|
2018-07-22 22:42:19 -04:00
|
|
|
:new_note,
|
|
|
|
:new_issue,
|
|
|
|
:reopen_issue,
|
|
|
|
:close_issue,
|
|
|
|
:reassign_issue,
|
|
|
|
:new_merge_request,
|
|
|
|
:reopen_merge_request,
|
|
|
|
:close_merge_request,
|
|
|
|
:reassign_merge_request,
|
2020-09-21 08:09:34 -04:00
|
|
|
:change_reviewer_merge_request,
|
2018-07-22 22:42:19 -04:00
|
|
|
:merge_merge_request,
|
|
|
|
:failed_pipeline,
|
2020-02-27 13:09:21 -05:00
|
|
|
:success_pipeline,
|
2020-07-27 23:09:35 -04:00
|
|
|
:fixed_pipeline,
|
2021-02-19 07:11:06 -05:00
|
|
|
:moved_project,
|
|
|
|
:merge_when_pipeline_succeeds
|
2018-07-22 22:42:19 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes EXCLUDED_WATCHER_EVENTS' do
|
|
|
|
expect(subject).to include(*described_class::EXCLUDED_WATCHER_EVENTS)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#email_events' do
|
|
|
|
let(:source) { build(:group) }
|
|
|
|
|
|
|
|
subject { build(:notification_setting, source: source) }
|
|
|
|
|
|
|
|
it 'calls email_events' do
|
|
|
|
expect(described_class).to receive(:email_events).with(source)
|
|
|
|
subject.email_events
|
2018-07-10 10:29:27 -04:00
|
|
|
end
|
|
|
|
end
|
2021-04-13 14:11:28 -04:00
|
|
|
|
|
|
|
describe '#order_by_id_asc' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:other_project) { create(:project) }
|
|
|
|
let_it_be(:notification_setting_1) { create(:notification_setting, project: project) }
|
|
|
|
let_it_be(:notification_setting_2) { create(:notification_setting, project: other_project) }
|
|
|
|
let_it_be(:notification_setting_3) { create(:notification_setting, project: project) }
|
|
|
|
|
|
|
|
let(:ids) { [notification_setting_1, notification_setting_2, notification_setting_3].map(&:id) }
|
|
|
|
|
|
|
|
subject(:ordered_records) { described_class.where(id: ids, source: project).order_by_id_asc }
|
|
|
|
|
|
|
|
it { is_expected.to eq([notification_setting_1, notification_setting_3]) }
|
|
|
|
end
|
2016-03-28 07:41:00 -04:00
|
|
|
end
|