BigW Consortium Gitlab

notification_setting_spec.rb 2.83 KB
Newer Older
1 2
require 'rails_helper'

3
RSpec.describe NotificationSetting do
4 5
  describe "Associations" do
    it { is_expected.to belong_to(:user) }
6
    it { is_expected.to belong_to(:source) }
7 8 9
  end

  describe "Validation" do
10
    subject { described_class.new(source_id: 1, source_type: 'Project') }
11 12 13

    it { is_expected.to validate_presence_of(:user) }
    it { is_expected.to validate_presence_of(:level) }
14 15

    describe 'user_id' do
16 17 18
      before do
        subject.user = create(:user)
      end
19 20 21

      it { is_expected.to validate_uniqueness_of(:user_id).scoped_to([:source_type, :source_id]).with_message(/already exists in source/) }
    end
22 23 24

    context "events" do
      let(:user) { create(:user) }
25
      let(:notification_setting) { described_class.new(source_id: 1, source_type: 'Project', user_id: user.id) }
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

      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
47
  end
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

  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
65

66
  describe '#event_enabled?' do
67 68 69 70 71 72
    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
73
        subject.update!(new_note: true)
74

75
        expect(subject.event_enabled?(:new_note)).to be(true)
76 77 78
      end

      context 'when the column has a nil value' do
79
        it 'returns false' do
80 81 82 83 84 85 86 87 88 89 90
          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
91
end