2016-03-28 07:41:00 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe NotificationSetting, type: :model do
|
|
|
|
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
|
2016-03-29 07:42:48 -04:00
|
|
|
subject { NotificationSetting.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) }
|
2016-03-29 07:42:48 -04:00
|
|
|
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to([:source_id, :source_type]).with_message(/already exists in source/) }
|
2016-05-31 16:51:46 -04:00
|
|
|
|
|
|
|
context "events" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:notification_setting) { NotificationSetting.new(source_id: 1, source_type: 'Project', user_id: user.id) }
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(notification_setting.close_merge_request).to eq(false)
|
|
|
|
expect(notification_setting.reopen_merge_request).to eq(false)
|
|
|
|
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)
|
|
|
|
|
|
|
|
setting.project.update_attributes(pending_delete: true) if i.even?
|
|
|
|
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
|
|
|
|
|
|
|
describe 'event_enabled?' do
|
|
|
|
before do
|
|
|
|
subject.update!(user: create(:user))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an event with a matching column name' do
|
|
|
|
before do
|
|
|
|
subject.update!(events: { new_note: true }.to_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the value of the column' do
|
|
|
|
subject.update!(new_note: false)
|
|
|
|
|
|
|
|
expect(subject.event_enabled?(:new_note)).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the column has a nil value' do
|
|
|
|
it 'returns the value from the events hash' do
|
|
|
|
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 07:41:00 -04:00
|
|
|
end
|