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

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

6 7
  describe 'Keys' do
    describe :new_key do
D
Dmitriy Zaporozhets 已提交
8
      let!(:key) { create(:personal_key) }
9 10 11 12 13 14 15 16 17 18

      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
  describe 'Email' do
    describe :new_email do
D
Dmitriy Zaporozhets 已提交
21
      let!(:email) { create(:email) }
22 23 24 25 26 27 28 29 30 31

      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
        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
M
Marin Jankovski 已提交
60 61 62 63
      end

      describe 'new note on issue in project that belongs to a group' do
        let(:group) { create(:group) }
64

M
Marin Jankovski 已提交
65 66 67 68 69 70 71 72 73 74
        before do
          note.project.namespace_id = group.id
          note.project.group.add_user(@u_watcher, UsersGroup::MASTER)
          note.project.save
          user_project = note.project.users_projects.find_by_user_id(@u_watcher.id)
          user_project.notification_level = Notification::N_PARTICIPATING
          user_project.save
          user_group = note.project.group.users_groups.find_by_user_id(@u_watcher.id)
          user_group.notification_level = Notification::N_GLOBAL
          user_group.save
75 76
        end

M
Marin Jankovski 已提交
77 78 79 80 81 82 83 84 85
        it do
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)
          should_email(@u_mentioned.id)
          should_not_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)
86
        end
87
      end
M
Marin Jankovski 已提交
88 89 90 91 92 93 94 95

      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
96
    end
97

98
    context 'commit note' do
99
      let(:note) { create(:note_on_commit) }
100 101 102

      before do
        build_team(note.project)
103
        note.stub(:commit_author => @u_committer)
104 105
      end

106 107
      describe :new_note do
        it do
108
          should_email(@u_committer.id, note)
109 110 111 112 113
          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)
114 115 116 117
          notification.new_note(note)
        end

        it do
118 119 120 121 122 123 124 125
          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)
126 127
        end

128 129
        def should_email(user_id, n)
          Notify.should_receive(:note_commit_email).with(user_id, n.id)
130 131
        end

132 133
        def should_not_email(user_id, n)
          Notify.should_not_receive(:note_commit_email).with(user_id, n.id)
134
        end
135 136 137 138
      end
    end
  end

139 140 141
  describe 'Issues' do
    let(:issue) { create :issue, assignee: create(:user) }

142 143 144 145
    before do
      build_team(issue.project)
    end

146
    describe :new_issue do
147 148 149 150 151 152 153 154 155 156 157 158 159 160
      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)
161 162 163 164
      end
    end

    describe :reassigned_issue do
165 166 167 168 169 170 171 172 173 174
      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)
175
        Notify.should_receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id, @u_disabled.id)
176 177 178
      end

      def should_not_email(user_id)
179
        Notify.should_not_receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id, @u_disabled.id)
180 181 182 183 184
      end
    end

    describe :close_issue do
      it 'should sent email to issue assignee and issue author' do
185 186 187 188 189 190 191 192 193 194
        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)
195
        Notify.should_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
196 197 198
      end

      def should_not_email(user_id)
199
        Notify.should_not_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
200 201 202
      end
    end
  end
203 204 205 206

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

207
    before do
I
Izaak Alpert 已提交
208
      build_team(merge_request.target_project)
209 210
    end

211
    describe :new_merge_request do
212 213 214 215 216 217 218 219 220
      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)
221
        Notify.should_receive(:new_merge_request_email).with(user_id, merge_request.id)
222 223
      end

224
      def should_not_email(user_id)
225
        Notify.should_not_receive(:new_merge_request_email).with(user_id, merge_request.id)
226 227
      end
    end
228 229 230 231 232 233 234 235 236 237 238

    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)
239
        Notify.should_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id)
240 241 242
      end

      def should_not_email(user_id)
243
        Notify.should_not_receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id)
244 245 246 247 248 249 250 251 252
      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)
253
        notification.close_mr(merge_request, @u_disabled)
254 255 256
      end

      def should_email(user_id)
257
        Notify.should_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
258 259 260
      end

      def should_not_email(user_id)
261
        Notify.should_not_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
262 263 264 265 266 267 268 269 270
      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)
271
        notification.merge_mr(merge_request, @u_disabled)
272 273 274
      end

      def should_email(user_id)
275
        Notify.should_receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
276 277 278
      end

      def should_not_email(user_id)
279
        Notify.should_not_receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
280 281 282 283
      end
    end
  end

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  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

309
  def build_team(project)
D
Dmitriy Zaporozhets 已提交
310 311 312 313
    @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)
314
    @u_committer = create(:user, username: 'committer')
315 316 317 318

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
319
    project.team << [@u_mentioned, :master]
320
    project.team << [@u_committer, :master]
321
  end
322
end