notification_service_spec.rb 9.0 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe NotificationService do
  let(:notification) { NotificationService.new }
D
Dmitriy Zaporozhets 已提交
5

6 7 8 9 10 11 12 13 14 15 16 17 18
  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 20 21 22 23 24 25 26 27 28 29 30 31
  describe 'Email' do
    describe :new_email do
      let(:email) { create(:email) }

      it { notification.new_email(email).should be_true }

      it 'should send email to email owner' do
        Notify.should_receive(:new_email_email).with(email.id)
        notification.new_email(email)
      end
    end
  end

32
  describe 'Notes' do
33 34
    context 'issue note' do
      let(:issue) { create(:issue, assignee: create(:user)) }
35
      let(:mentioned_issue) { create(:issue, assignee: issue.assignee) }
36
      let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@mention referenced') }
37

38 39
      before do
        build_team(note.project)
40 41
      end

42 43 44 45 46
      describe :new_note do
        it do
          should_email(@u_watcher.id)
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)
47
          should_email(@u_mentioned.id)
48 49 50 51 52 53
          should_not_email(note.author_id)
          should_not_email(@u_participating.id)
          should_not_email(@u_disabled.id)
          notification.new_note(note)
        end

54 55 56 57 58 59 60
        it 'filters out "mentioned in" notes' do
          mentioned_note = Note.create_cross_reference_note(mentioned_issue, issue, issue.author, issue.project)

          Notify.should_not_receive(:note_issue_email)
          notification.new_note(mentioned_note)
        end

61 62 63 64 65 66 67
        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
68
      end
69
    end
70

71
    context 'commit note' do
72
      let(:note) { create(:note_on_commit) }
73 74 75

      before do
        build_team(note.project)
76
        note.stub(:commit_author => @u_committer)
77 78
      end

79 80
      describe :new_note do
        it do
81
          should_email(@u_committer.id, note)
82 83 84 85 86
          should_email(@u_watcher.id, note)
          should_not_email(@u_mentioned.id, note)
          should_not_email(note.author_id, note)
          should_not_email(@u_participating.id, note)
          should_not_email(@u_disabled.id, note)
87 88 89 90
          notification.new_note(note)
        end

        it do
91 92 93 94 95 96 97 98
          note.update_attribute(:note, '@mention referenced')
          should_email(@u_committer.id, note)
          should_email(@u_watcher.id, note)
          should_email(@u_mentioned.id, note)
          should_not_email(note.author_id, note)
          should_not_email(@u_participating.id, note)
          should_not_email(@u_disabled.id, note)
          notification.new_note(note)
99 100
        end

101 102
        def should_email(user_id, n)
          Notify.should_receive(:note_commit_email).with(user_id, n.id)
103 104
        end

105 106
        def should_not_email(user_id, n)
          Notify.should_not_receive(:note_commit_email).with(user_id, n.id)
107
        end
108 109 110 111
      end
    end
  end

112 113 114
  describe 'Issues' do
    let(:issue) { create :issue, assignee: create(:user) }

115 116 117 118
    before do
      build_team(issue.project)
    end

119
    describe :new_issue do
120 121 122 123 124 125 126 127 128 129 130 131 132 133
      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)
134 135 136 137
      end
    end

    describe :reassigned_issue do
138 139 140 141 142 143 144 145 146 147
      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)
148
        Notify.should_receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id, @u_disabled.id)
149 150 151
      end

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

    describe :close_issue do
      it 'should sent email to issue assignee and issue author' do
158 159 160 161 162 163 164 165 166 167
        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)
168
        Notify.should_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
169 170 171
      end

      def should_not_email(user_id)
172
        Notify.should_not_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
173 174 175
      end
    end
  end
176 177 178 179

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

180
    before do
I
Izaak Alpert 已提交
181
      build_team(merge_request.target_project)
182 183
    end

184
    describe :new_merge_request do
185 186 187 188 189 190 191 192 193
      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)
194
        Notify.should_receive(:new_merge_request_email).with(user_id, merge_request.id)
195 196
      end

197
      def should_not_email(user_id)
198
        Notify.should_not_receive(:new_merge_request_email).with(user_id, merge_request.id)
199 200
      end
    end
201 202 203 204 205 206 207 208 209 210 211

    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)
212
        Notify.should_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id)
213 214 215
      end

      def should_not_email(user_id)
216
        Notify.should_not_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id)
217 218 219 220 221 222 223 224 225
      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)
226
        notification.close_mr(merge_request, @u_disabled)
227 228 229
      end

      def should_email(user_id)
230
        Notify.should_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
231 232 233
      end

      def should_not_email(user_id)
234
        Notify.should_not_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
235 236 237 238 239 240 241 242 243
      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)
244
        notification.merge_mr(merge_request, @u_disabled)
245 246 247
      end

      def should_email(user_id)
248
        Notify.should_receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
249 250 251
      end

      def should_not_email(user_id)
252
        Notify.should_not_receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
253 254 255 256
      end
    end
  end

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  describe 'Projects' do
    let(:project) { create :project }

    before do
      build_team(project)
    end

    describe :project_was_moved do
      it do
        should_email(@u_watcher.id)
        should_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.project_was_moved(project)
      end

      def should_email(user_id)
        Notify.should_receive(:project_was_moved_email).with(project.id, user_id)
      end

      def should_not_email(user_id)
        Notify.should_not_receive(:project_was_moved_email).with(project.id, user_id)
      end
    end
  end

282
  def build_team(project)
D
Dmitriy Zaporozhets 已提交
283 284 285 286
    @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)
    @u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_PARTICIPATING)
287
    @u_committer = create(:user, username: 'committer')
288 289 290 291

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
292
    project.team << [@u_mentioned, :master]
293
    project.team << [@u_committer, :master]
294
  end
295
end