notify_spec.rb 10.6 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe Notify do
  include EmailSpec::Helpers
  include EmailSpec::Matchers

7 8
  let(:recipient) { create(:user, email: 'recipient@example.com') }
  let(:project) { create(:project) }
9

R
Robb Kidd 已提交
10 11 12 13 14 15 16
  shared_examples 'a multiple recipients email' do
    it 'is sent to the given recipient' do
      should deliver_to recipient.email
    end
  end

  describe 'for new users, the email' do
17
    let(:example_site_path) { root_path }
18
    let(:new_user) { create(:user, email: 'newguy@example.com') }
19

20
    subject { Notify.new_user_email(new_user.id, new_user.password) }
21 22 23 24 25 26

    it 'is sent to the new user' do
      should deliver_to new_user.email
    end

    it 'has the correct subject' do
A
Alex Denisov 已提交
27
      should have_subject /^gitlab \| Account was created for you$/i
28 29 30 31 32 33 34
    end

    it 'contains the new user\'s login name' do
      should have_body_text /#{new_user.email}/
    end

    it 'contains the new user\'s password' do
M
Marin Jankovski 已提交
35
      Gitlab.config.gitlab.stub(:signup_enabled).and_return(false)
36 37 38 39
      should have_body_text /#{new_user.password}/
    end

    it 'includes a link to the site' do
40
      should have_body_text /#{example_site_path}/
41 42 43
    end
  end

M
Marin Jankovski 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  describe 'for users that signed up, the email' do
    let(:example_site_path) { root_path }
    let(:new_user) { create(:user, email: 'newguy@example.com', password: "securePassword") }

    subject { Notify.new_user_email(new_user.id, new_user.password) }

    it 'is sent to the new user' do
      should deliver_to new_user.email
    end

    it 'has the correct subject' do
      should have_subject /^gitlab \| Account was created for you$/i
    end

    it 'contains the new user\'s login name' do
      should have_body_text /#{new_user.email}/
    end

    it 'should not contain the new user\'s password' do
      Gitlab.config.gitlab.stub(:signup_enabled).and_return(true)
      should_not have_body_text /#{new_user.password}/
    end

    it 'includes a link to the site' do
      should have_body_text /#{example_site_path}/
    end
  end

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

    it 'is sent to the new user' do
      should deliver_to key.user.email
    end

    it 'has the correct subject' do
      should have_subject /^gitlab \| SSH key was added to your account$/i
    end

    it 'contains the new ssh key title' do
      should have_body_text /#{key.title}/
    end

    it 'includes a link to ssh keys page' do
      should have_body_text /#{keys_path}/
    end
  end

R
Robb Kidd 已提交
95 96
  context 'for a project' do
    describe 'items that are assignable, the email' do
97 98
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
99

R
Robb Kidd 已提交
100 101 102 103 104
      shared_examples 'an assignee email' do
        it 'is sent to the assignee' do
          should deliver_to assignee.email
        end
      end
105

R
Robb Kidd 已提交
106
      context 'for issues' do
107
        let(:issue) { create(:issue, assignee: assignee, project: project ) }
108

R
Robb Kidd 已提交
109
        describe 'that are new' do
110
          subject { Notify.new_issue_email(issue.assignee_id, issue.id) }
111

R
Robb Kidd 已提交
112
          it_behaves_like 'an assignee email'
113

R
Robb Kidd 已提交
114
          it 'has the correct subject' do
115
            should have_subject /#{project.name} \| new issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
116
          end
117

R
Robb Kidd 已提交
118
          it 'contains a link to the new issue' do
119
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
120 121
          end
        end
122

R
Robb Kidd 已提交
123
        describe 'that have been reassigned' do
124
          before(:each) { issue.stub(:assignee_id_was).and_return(previous_assignee.id) }
125

126
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id) }
R
Robb Kidd 已提交
127 128 129 130

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
131
            should have_subject /changed issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
132 133 134
          end

          it 'contains the name of the previous assignee' do
135
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
136 137 138 139 140 141 142
          end

          it 'contains the name of the new assignee' do
            should have_body_text /#{assignee.name}/
          end

          it 'contains a link to the issue' do
143
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
144 145
          end
        end
A
Alex Denisov 已提交
146 147

        describe 'status changed' do
148
          let(:current_user) { create(:user, email: "current@email.com") }
A
Alex Denisov 已提交
149 150
          let(:status) { 'closed' }
          subject { Notify.issue_status_changed_email(recipient.id, issue.id, status, current_user) }
151

A
Alex Denisov 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
          it 'has the correct subject' do
            should have_subject /changed issue ##{issue.id} \| #{issue.title}/i
          end

          it 'contains the new status' do
            should have_body_text /#{status}/i
          end

          it 'contains the user name' do
            should have_body_text /#{current_user.name}/i
          end

          it 'contains a link to the issue' do
            should have_body_text /#{project_issue_path project, issue}/
          end
        end

R
Robb Kidd 已提交
169 170 171
      end

      context 'for merge requests' do
172
        let(:merge_request) { create(:merge_request, assignee: assignee, project: project) }
R
Robb Kidd 已提交
173 174

        describe 'that are new' do
175
          subject { Notify.new_merge_request_email(merge_request.assignee_id, merge_request.id) }
R
Robb Kidd 已提交
176 177 178 179

          it_behaves_like 'an assignee email'

          it 'has the correct subject' do
180
            should have_subject /new merge request !#{merge_request.id}/
R
Robb Kidd 已提交
181 182 183
          end

          it 'contains a link to the new merge request' do
184
            should have_body_text /#{project_merge_request_path(project, merge_request)}/
R
Robb Kidd 已提交
185 186 187 188 189 190 191 192 193 194 195 196
          end

          it 'contains the source branch for the merge request' do
            should have_body_text /#{merge_request.source_branch}/
          end

          it 'contains the target branch for the merge request' do
            should have_body_text /#{merge_request.target_branch}/
          end
        end

        describe 'that are reassigned' do
197
          before(:each) { merge_request.stub(:assignee_id_was).and_return(previous_assignee.id) }
R
Robb Kidd 已提交
198

199
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id) }
R
Robb Kidd 已提交
200 201 202 203

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
204
            should have_subject /changed merge request !#{merge_request.id}/
R
Robb Kidd 已提交
205 206 207
          end

          it 'contains the name of the previous assignee' do
208
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
209 210 211 212 213 214 215
          end

          it 'contains the name of the new assignee' do
            should have_body_text /#{assignee.name}/
          end

          it 'contains a link to the merge request' do
216
            should have_body_text /#{project_merge_request_path project, merge_request}/
R
Robb Kidd 已提交
217 218 219 220
          end

        end
      end
221 222
    end

223
    describe 'project access changed' do
224
      let(:project) { create(:project) }
225 226 227 228
      let(:user) { create(:user) }
      let(:users_project) { create(:users_project,
                                   project: project,
                                   user: user) }
229 230 231 232 233 234 235 236 237 238 239 240
      subject { Notify.project_access_granted_email(users_project.id) }
      it 'has the correct subject' do
        should have_subject /access to project was granted/
      end
      it 'contains name of project' do
        should have_body_text /#{project.name}/
      end
      it 'contains new user role' do
        should have_body_text /#{users_project.project_access_human}/
      end
    end

R
Robb Kidd 已提交
241
    context 'items that are noteable, the email for a note' do
242 243
      let(:note_author) { create(:user, name: 'author_name') }
      let(:note) { create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
244

245
      before :each do
D
Dmitriy Zaporozhets 已提交
246
        Note.stub(:find).with(note.id).and_return(note)
247 248
      end

R
Robb Kidd 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      shared_examples 'a note email' do
        it 'is sent to the given recipient' do
          should deliver_to recipient.email
        end

        it 'contains the name of the note\'s author' do
          should have_body_text /#{note_author.name}/
        end

        it 'contains the message from the note' do
          should have_body_text /#{note.note}/
        end
      end

      describe 'on a project wall' do
264
        let(:note_on_the_wall_path) { project_wall_path(project, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
265

266
        subject { Notify.note_wall_email(recipient.id, note.id) }
R
Robb Kidd 已提交
267 268 269 270 271 272 273 274

        it_behaves_like 'a note email'

        it 'has the correct subject' do
          should have_subject /#{project.name}/
        end

        it 'contains a link to the wall note' do
275
          should have_body_text /#{note_on_the_wall_path}/
R
Robb Kidd 已提交
276 277 278 279 280 281
        end
      end

      describe 'on a commit' do
        let(:commit) do
          mock(:commit).tap do |commit|
282 283
            commit.stub(:id).and_return('fauxsha1')
            commit.stub(:project).and_return(project)
284 285
            commit.stub(:short_id).and_return('fauxsha1')
            commit.stub(:safe_message).and_return('some message')
R
Robb Kidd 已提交
286 287
          end
        end
D
Dmitriy Zaporozhets 已提交
288

R
Riyad Preukschas 已提交
289
        before(:each) { note.stub(:noteable).and_return(commit) }
R
Robb Kidd 已提交
290

D
Dmitriy Zaporozhets 已提交
291
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
292 293 294 295

        it_behaves_like 'a note email'

        it 'has the correct subject' do
296
          should have_subject /note for commit #{commit.short_id}/
R
Robb Kidd 已提交
297 298 299
        end

        it 'contains a link to the commit' do
300
          should have_body_text /fauxsha1/
R
Robb Kidd 已提交
301 302 303 304
        end
      end

      describe 'on a merge request' do
305
        let(:merge_request) { create(:merge_request, project: project) }
306
        let(:note_on_merge_request_path) { project_merge_request_path(project, merge_request, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
307 308
        before(:each) { note.stub(:noteable).and_return(merge_request) }

309
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
310 311 312 313

        it_behaves_like 'a note email'

        it 'has the correct subject' do
314
          should have_subject /note for merge request !#{merge_request.id}/
R
Robb Kidd 已提交
315 316 317
        end

        it 'contains a link to the merge request note' do
318
          should have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
319 320 321 322
        end
      end

      describe 'on an issue' do
323
        let(:issue) { create(:issue, project: project) }
324
        let(:note_on_issue_path) { project_issue_path(project, issue, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
325
        before(:each) { note.stub(:noteable).and_return(issue) }
326 327

        subject { Notify.note_issue_email(recipient.id, note.id) }
R
Robb Kidd 已提交
328 329 330 331

        it_behaves_like 'a note email'

        it 'has the correct subject' do
332
          should have_subject /note for issue ##{issue.id}/
R
Robb Kidd 已提交
333 334 335
        end

        it 'contains a link to the issue note' do
336
          should have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
337 338
        end
      end
339 340 341
    end
  end
end