notification_service_spec.rb 8.2 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
        note.stub(:commit_author => @u_committer)
56 57
      end

58 59
      describe :new_note do
        it do
60
          should_email(@u_committer.id, note)
61 62 63 64 65
          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)
66 67 68 69
          notification.new_note(note)
        end

        it do
70 71 72 73 74 75 76 77
          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)
78 79
        end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      def should_not_email(user_id)
213
        Notify.should_not_receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      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

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  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

261
  def build_team(project)
D
Dmitriy Zaporozhets 已提交
262 263 264 265
    @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)
266
    @u_committer = create(:user, username: 'committer')
267 268 269 270

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
271
    project.team << [@u_mentioned, :master]
272
    project.team << [@u_committer, :master]
273
  end
274
end