2016-03-28 11:41:00 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-06-29 04:13:10 +00:00
|
|
|
RSpec.describe NotificationSetting do
|
2016-03-28 11:41:00 +00:00
|
|
|
describe "Associations" do
|
|
|
|
it { is_expected.to belong_to(:user) }
|
2016-03-28 21:22:28 +00:00
|
|
|
it { is_expected.to belong_to(:source) }
|
2016-03-28 11:41:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Validation" do
|
2017-06-29 04:13:10 +00:00
|
|
|
subject { described_class.new(source_id: 1, source_type: 'Project') }
|
2016-03-28 11:41:00 +00:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
|
|
|
it { is_expected.to validate_presence_of(:level) }
|
2017-07-24 12:33:14 +00:00
|
|
|
|
|
|
|
describe 'user_id' do
|
2017-07-24 16:26:15 +00:00
|
|
|
before do
|
|
|
|
subject.user = create(:user)
|
|
|
|
end
|
2017-07-24 12:33:14 +00: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 20:51:46 +00:00
|
|
|
|
|
|
|
context "events" do
|
|
|
|
let(:user) { create(:user) }
|
2017-06-29 04:13:10 +00:00
|
|
|
let(:notification_setting) { described_class.new(source_id: 1, source_type: 'Project', user_id: user.id) }
|
2016-05-31 20:51:46 +00: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-05-01 00:36:18 +00:00
|
|
|
|
|
|
|
# In Rails 5 assigning a value which is not explicitly `true` or `false` ("nil" in this case)
|
|
|
|
# to a boolean column transforms it to `true`.
|
|
|
|
# In Rails 4 it transforms the value to `false` with deprecation warning.
|
|
|
|
# Replace `eq(Gitlab.rails5?)` with `eq(true)` when removing rails5? code.
|
|
|
|
expect(notification_setting.close_merge_request).to eq(Gitlab.rails5?)
|
2016-05-31 20:51:46 +00:00
|
|
|
expect(notification_setting.reopen_merge_request).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2016-03-28 11:41:00 +00:00
|
|
|
end
|
2016-07-07 11:41:48 +00: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 10:43:06 +00:00
|
|
|
setting.project.update(pending_delete: true) if i.even?
|
2016-07-07 11:41:48 +00: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 16:10:35 +00:00
|
|
|
|
2017-07-28 09:44:33 +00:00
|
|
|
describe '#event_enabled?' do
|
2017-06-08 16:10:35 +00: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 09:44:33 +00:00
|
|
|
subject.update!(new_note: true)
|
2017-06-08 16:10:35 +00:00
|
|
|
|
2017-07-28 09:44:33 +00:00
|
|
|
expect(subject.event_enabled?(:new_note)).to be(true)
|
2017-06-08 16:10:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the column has a nil value' do
|
2017-07-28 09:44:33 +00:00
|
|
|
it 'returns false' do
|
2017-06-08 16:10:35 +00: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
|
|
|
|
end
|
2016-03-28 11:41:00 +00:00
|
|
|
end
|