notify_spec.rb 9.1 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 35 36 37 38
    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
      should have_body_text /#{new_user.password}/
    end

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

R
Robb Kidd 已提交
43 44
  context 'for a project' do
    describe 'items that are assignable, the email' do
45 46
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
47

R
Robb Kidd 已提交
48 49 50 51 52
      shared_examples 'an assignee email' do
        it 'is sent to the assignee' do
          should deliver_to assignee.email
        end
      end
53

R
Robb Kidd 已提交
54
      context 'for issues' do
55
        let(:issue) { create(:issue, assignee: assignee, project: project ) }
56

R
Robb Kidd 已提交
57
        describe 'that are new' do
58
          subject { Notify.new_issue_email(issue.id) }
59

R
Robb Kidd 已提交
60
          it_behaves_like 'an assignee email'
61

R
Robb Kidd 已提交
62
          it 'has the correct subject' do
63
            should have_subject /#{project.name} \| new issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
64
          end
65

R
Robb Kidd 已提交
66
          it 'contains a link to the new issue' do
67
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
68 69
          end
        end
70

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

74
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id) }
R
Robb Kidd 已提交
75 76 77 78

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
79
            should have_subject /changed issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
80 81 82
          end

          it 'contains the name of the previous assignee' do
83
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
84 85 86 87 88 89 90
          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
91
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
92 93
          end
        end
A
Alex Denisov 已提交
94 95

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

A
Alex Denisov 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
          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 已提交
117 118 119
      end

      context 'for merge requests' do
120
        let(:merge_request) { create(:merge_request, assignee: assignee, project: project) }
R
Robb Kidd 已提交
121 122

        describe 'that are new' do
123
          subject { Notify.new_merge_request_email(merge_request.id) }
R
Robb Kidd 已提交
124 125 126 127

          it_behaves_like 'an assignee email'

          it 'has the correct subject' do
128
            should have_subject /new merge request !#{merge_request.id}/
R
Robb Kidd 已提交
129 130 131
          end

          it 'contains a link to the new merge request' do
132
            should have_body_text /#{project_merge_request_path(project, merge_request)}/
R
Robb Kidd 已提交
133 134 135 136 137 138 139 140 141 142 143 144
          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
145
          before(:each) { merge_request.stub(:assignee_id_was).and_return(previous_assignee.id) }
R
Robb Kidd 已提交
146

147
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id) }
R
Robb Kidd 已提交
148 149 150 151

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
152
            should have_subject /changed merge request !#{merge_request.id}/
R
Robb Kidd 已提交
153 154 155
          end

          it 'contains the name of the previous assignee' do
156
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
157 158 159 160 161 162 163
          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
164
            should have_body_text /#{project_merge_request_path project, merge_request}/
R
Robb Kidd 已提交
165 166 167 168
          end

        end
      end
169 170
    end

171
    describe 'project access changed' do
172
      let(:project) { create(:project) }
173 174 175 176
      let(:user) { create(:user) }
      let(:users_project) { create(:users_project,
                                   project: project,
                                   user: user) }
177 178 179 180 181 182 183 184 185 186 187 188
      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 已提交
189
    context 'items that are noteable, the email for a note' do
190 191
      let(:note_author) { create(:user, name: 'author_name') }
      let(:note) { create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
192

193
      before :each do
D
Dmitriy Zaporozhets 已提交
194
        Note.stub(:find).with(note.id).and_return(note)
195 196
      end

R
Robb Kidd 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
      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
212
        let(:note_on_the_wall_path) { wall_project_path(project, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
213

214
        subject { Notify.note_wall_email(recipient.id, note.id) }
R
Robb Kidd 已提交
215 216 217 218 219 220 221 222

        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
223
          should have_body_text /#{note_on_the_wall_path}/
R
Robb Kidd 已提交
224 225 226 227 228 229
        end
      end

      describe 'on a commit' do
        let(:commit) do
          mock(:commit).tap do |commit|
230 231
            commit.stub(:id).and_return('fauxsha1')
            commit.stub(:project).and_return(project)
232 233
            commit.stub(:short_id).and_return('fauxsha1')
            commit.stub(:safe_message).and_return('some message')
R
Robb Kidd 已提交
234 235
          end
        end
D
Dmitriy Zaporozhets 已提交
236

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

D
Dmitriy Zaporozhets 已提交
239
        subject { Notify.note_commit_email(recipient.email, note.id) }
R
Robb Kidd 已提交
240 241 242 243

        it_behaves_like 'a note email'

        it 'has the correct subject' do
244
          should have_subject /note for commit #{commit.short_id}/
R
Robb Kidd 已提交
245 246 247
        end

        it 'contains a link to the commit' do
248
          should have_body_text /fauxsha1/
R
Robb Kidd 已提交
249 250 251 252
        end
      end

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

257
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
258 259 260 261

        it_behaves_like 'a note email'

        it 'has the correct subject' do
262
          should have_subject /note for merge request !#{merge_request.id}/
R
Robb Kidd 已提交
263 264 265
        end

        it 'contains a link to the merge request note' do
266
          should have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
267 268 269 270
        end
      end

      describe 'on an issue' do
271
        let(:issue) { create(:issue, project: project) }
272
        let(:note_on_issue_path) { project_issue_path(project, issue, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
273
        before(:each) { note.stub(:noteable).and_return(issue) }
274 275

        subject { Notify.note_issue_email(recipient.id, note.id) }
R
Robb Kidd 已提交
276 277 278 279

        it_behaves_like 'a note email'

        it 'has the correct subject' do
280
          should have_subject /note for issue ##{issue.id}/
R
Robb Kidd 已提交
281 282 283
        end

        it 'contains a link to the issue note' do
284
          should have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
285 286
        end
      end
287 288 289
    end
  end
end