notification_service_spec.rb 7.6 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
  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
    context 'commit note' do
51
      let(:note) { create(:note_on_commit) }
52 53 54

      before do
        build_team(note.project)
55 56
      end

57 58
      describe :new_note do
        it do
59 60 61 62 63
          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)
64 65 66 67
          notification.new_note(note)
        end

        it do
68
          new_note = create(:note_on_commit,
69 70
                 author: @u_participating,
                 project_id: note.project_id,
71 72
                 commit_id: note.commit_id,
                 note: '@mention referenced')
73

74 75 76 77 78 79
          should_email(@u_watcher.id, new_note)
          should_email(@u_mentioned.id, new_note)
          should_not_email(new_note.author_id, new_note)
          should_not_email(@u_participating.id, new_note)
          should_not_email(@u_disabled.id, new_note)
          notification.new_note(new_note)
80 81
        end

82 83
        def should_email(user_id, n)
          Notify.should_receive(:note_commit_email).with(user_id, n.id)
84 85
        end

86 87
        def should_not_email(user_id, n)
          Notify.should_not_receive(:note_commit_email).with(user_id, n.id)
88
        end
89 90 91 92
      end
    end
  end

93 94 95
  describe 'Issues' do
    let(:issue) { create :issue, assignee: create(:user) }

96 97 98 99
    before do
      build_team(issue.project)
    end

100
    describe :new_issue do
101 102 103 104 105 106 107 108 109 110 111 112 113 114
      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)
115 116 117 118
      end
    end

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

    describe :close_issue do
      it 'should sent email to issue assignee and issue author' do
139 140 141 142 143 144 145 146 147 148
        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)
149
        Notify.should_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
150 151 152
      end

      def should_not_email(user_id)
153
        Notify.should_not_receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
154 155 156
      end
    end
  end
157 158 159 160

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

161
    before do
I
Izaak Alpert 已提交
162
      build_team(merge_request.target_project)
163 164
    end

165
    describe :new_merge_request do
166 167 168 169 170 171 172 173 174
      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)
175
        Notify.should_receive(:new_merge_request_email).with(user_id, merge_request.id)
176 177
      end

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

    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)
207
        notification.close_mr(merge_request, @u_disabled)
208 209 210
      end

      def should_email(user_id)
211
        Notify.should_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
212 213 214
      end

      def should_not_email(user_id)
215
        Notify.should_not_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
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)
D
Dmitriy Zaporozhets 已提交
239 240 241 242
    @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)
243 244 245 246

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
247
    project.team << [@u_mentioned, :master]
248
  end
249
end