notification_service_spec.rb 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require 'spec_helper'

describe NotificationService do
  let(:notification) { NotificationService.new }

  describe 'Keys' do
    describe :new_key do
      let(:key) { create(:personal_key) }

      it { notification.new_key(key).should be_true }

      it 'should sent email to key owner' do
        Notify.should_receive(:new_ssh_key_email).with(key.id)
        notification.new_key(key)
      end
    end
  end

19
  describe 'Notes' do
20 21
    context 'issue note' do
      let(:issue) { create(:issue, assignee: create(:user)) }
22
      let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@mention referenced') }
23

24 25
      before do
        build_team(note.project)
26 27
      end

28 29 30 31 32
      describe :new_note do
        it do
          should_email(@u_watcher.id)
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)
33
          should_email(@u_mentioned.id)
34 35 36 37 38 39 40 41 42 43 44 45 46
          should_not_email(note.author_id)
          should_not_email(@u_participating.id)
          should_not_email(@u_disabled.id)
          notification.new_note(note)
        end

        def should_email(user_id)
          Notify.should_receive(:note_issue_email).with(user_id, note.id)
        end

        def should_not_email(user_id)
          Notify.should_not_receive(:note_issue_email).with(user_id, note.id)
        end
47
      end
48
    end
49

50 51 52 53 54
    context 'commit note' do
      let(:note) { create :note_on_commit }

      before do
        build_team(note.project)
55 56
      end

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      describe :new_note do
        it do
          should_email(@u_watcher.id)
          should_not_email(note.author_id)
          should_not_email(@u_participating.id)
          should_not_email(@u_disabled.id)
          notification.new_note(note)
        end

        it do
          create(:note_on_commit,
                 author: @u_participating,
                 project_id: note.project_id,
                 commit_id: note.commit_id)

          should_email(@u_watcher.id)
          should_email(@u_participating.id)
          should_not_email(note.author_id)
          should_not_email(@u_disabled.id)
          notification.new_note(note)
        end

        def should_email(user_id)
          Notify.should_receive(:note_commit_email).with(user_id, note.id)
        end

        def should_not_email(user_id)
          Notify.should_not_receive(:note_commit_email).with(user_id, note.id)
        end
86 87 88 89
      end
    end
  end

90 91 92
  describe 'Issues' do
    let(:issue) { create :issue, assignee: create(:user) }

93 94 95 96
    before do
      build_team(issue.project)
    end

97
    describe :new_issue do
98 99 100 101 102 103 104 105 106 107 108 109 110 111
      it do
        should_email(issue.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.new_issue(issue, @u_disabled)
      end

      def should_email(user_id)
        Notify.should_receive(:new_issue_email).with(user_id, issue.id)
      end

      def should_not_email(user_id)
        Notify.should_not_receive(:new_issue_email).with(user_id, issue.id)
112 113 114 115
      end
    end

    describe :reassigned_issue do
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      it 'should email new assignee' do
        should_email(issue.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)

        notification.reassigned_issue(issue, @u_disabled)
      end

      def should_email(user_id)
        Notify.should_receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id)
      end

      def should_not_email(user_id)
        Notify.should_not_receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id)
131 132 133 134 135
      end
    end

    describe :close_issue do
      it 'should sent email to issue assignee and issue author' do
136 137 138 139 140 141 142 143 144 145
        should_email(issue.assignee_id)
        should_email(issue.author_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)

        notification.close_issue(issue, @u_disabled)
      end

      def should_email(user_id)
146
        Notify.should_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
147 148 149
      end

      def should_not_email(user_id)
150
        Notify.should_not_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
151 152 153
      end
    end
  end
154 155 156 157

  describe 'Merge Requests' do
    let(:merge_request) { create :merge_request, assignee: create(:user) }

158 159 160 161
    before do
      build_team(merge_request.project)
    end

162
    describe :new_merge_request do
163 164 165 166 167 168 169 170 171
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.new_merge_request(merge_request, @u_disabled)
      end

      def should_email(user_id)
172
        Notify.should_receive(:new_merge_request_email).with(user_id, merge_request.id)
173 174
      end

175
      def should_not_email(user_id)
176
        Notify.should_not_receive(:new_merge_request_email).with(user_id, merge_request.id)
177 178
      end
    end
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    describe :reassigned_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.reassigned_merge_request(merge_request, merge_request.author)
      end

      def should_email(user_id)
        Notify.should_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id)
      end

      def should_not_email(user_id)
        Notify.should_not_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id)
      end
    end

    describe :closed_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
204
        notification.close_mr(merge_request, @u_disabled)
205 206 207
      end

      def should_email(user_id)
208
        Notify.should_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
209 210 211
      end

      def should_not_email(user_id)
212
        Notify.should_not_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      end
    end

    describe :merged_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.merge_mr(merge_request)
      end

      def should_email(user_id)
        Notify.should_receive(:merged_merge_request_email).with(user_id, merge_request.id)
      end

      def should_not_email(user_id)
        Notify.should_not_receive(:merged_merge_request_email).with(user_id, merge_request.id)
      end
    end
  end

  def build_team(project)
    @u_watcher = create(:user, notification_level: Notification::N_WATCH)
    @u_participating = create(:user, notification_level: Notification::N_PARTICIPATING)
    @u_disabled = create(:user, notification_level: Notification::N_DISABLED)
239
    @u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_WATCH)
240 241 242 243

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
244
    project.team << [@u_mentioned, :master]
245
  end
246
end