notify_spec.rb 10.0 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

R
Robb Kidd 已提交
73 74
  context 'for a project' do
    describe 'items that are assignable, the email' do
75 76
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
77

R
Robb Kidd 已提交
78 79 80 81 82
      shared_examples 'an assignee email' do
        it 'is sent to the assignee' do
          should deliver_to assignee.email
        end
      end
83

R
Robb Kidd 已提交
84
      context 'for issues' do
85
        let(:issue) { create(:issue, assignee: assignee, project: project ) }
86

R
Robb Kidd 已提交
87
        describe 'that are new' do
88
          subject { Notify.new_issue_email(issue.id) }
89

R
Robb Kidd 已提交
90
          it_behaves_like 'an assignee email'
91

R
Robb Kidd 已提交
92
          it 'has the correct subject' do
93
            should have_subject /#{project.name} \| new issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
94
          end
95

R
Robb Kidd 已提交
96
          it 'contains a link to the new issue' do
97
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
98 99
          end
        end
100

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

104
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id) }
R
Robb Kidd 已提交
105 106 107 108

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
109
            should have_subject /changed issue ##{issue.id} \| #{issue.title}/
R
Robb Kidd 已提交
110 111 112
          end

          it 'contains the name of the previous assignee' do
113
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
114 115 116 117 118 119 120
          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
121
            should have_body_text /#{project_issue_path project, issue}/
R
Robb Kidd 已提交
122 123
          end
        end
A
Alex Denisov 已提交
124 125

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

A
Alex Denisov 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
          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 已提交
147 148 149
      end

      context 'for merge requests' do
150
        let(:merge_request) { create(:merge_request, assignee: assignee, project: project) }
R
Robb Kidd 已提交
151 152

        describe 'that are new' do
153
          subject { Notify.new_merge_request_email(merge_request.id) }
R
Robb Kidd 已提交
154 155 156 157

          it_behaves_like 'an assignee email'

          it 'has the correct subject' do
158
            should have_subject /new merge request !#{merge_request.id}/
R
Robb Kidd 已提交
159 160 161
          end

          it 'contains a link to the new merge request' do
162
            should have_body_text /#{project_merge_request_path(project, merge_request)}/
R
Robb Kidd 已提交
163 164 165 166 167 168 169 170 171 172 173 174
          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
175
          before(:each) { merge_request.stub(:assignee_id_was).and_return(previous_assignee.id) }
R
Robb Kidd 已提交
176

177
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id) }
R
Robb Kidd 已提交
178 179 180 181

          it_behaves_like 'a multiple recipients email'

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

          it 'contains the name of the previous assignee' do
186
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
187 188 189 190 191 192 193
          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
194
            should have_body_text /#{project_merge_request_path project, merge_request}/
R
Robb Kidd 已提交
195 196 197 198
          end

        end
      end
199 200
    end

201
    describe 'project access changed' do
202
      let(:project) { create(:project) }
203 204 205 206
      let(:user) { create(:user) }
      let(:users_project) { create(:users_project,
                                   project: project,
                                   user: user) }
207 208 209 210 211 212 213 214 215 216 217 218
      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 已提交
219
    context 'items that are noteable, the email for a note' do
220 221
      let(:note_author) { create(:user, name: 'author_name') }
      let(:note) { create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
222

223
      before :each do
D
Dmitriy Zaporozhets 已提交
224
        Note.stub(:find).with(note.id).and_return(note)
225 226
      end

R
Robb Kidd 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      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
242
        let(:note_on_the_wall_path) { wall_project_path(project, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
243

244
        subject { Notify.note_wall_email(recipient.id, note.id) }
R
Robb Kidd 已提交
245 246 247 248 249 250 251 252

        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
253
          should have_body_text /#{note_on_the_wall_path}/
R
Robb Kidd 已提交
254 255 256 257 258 259
        end
      end

      describe 'on a commit' do
        let(:commit) do
          mock(:commit).tap do |commit|
260 261
            commit.stub(:id).and_return('fauxsha1')
            commit.stub(:project).and_return(project)
262 263
            commit.stub(:short_id).and_return('fauxsha1')
            commit.stub(:safe_message).and_return('some message')
R
Robb Kidd 已提交
264 265
          end
        end
D
Dmitriy Zaporozhets 已提交
266

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

D
Dmitriy Zaporozhets 已提交
269
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
270 271 272 273

        it_behaves_like 'a note email'

        it 'has the correct subject' do
274
          should have_subject /note for commit #{commit.short_id}/
R
Robb Kidd 已提交
275 276 277
        end

        it 'contains a link to the commit' do
278
          should have_body_text /fauxsha1/
R
Robb Kidd 已提交
279 280 281 282
        end
      end

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

287
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
288 289 290 291

        it_behaves_like 'a note email'

        it 'has the correct subject' do
292
          should have_subject /note for merge request !#{merge_request.id}/
R
Robb Kidd 已提交
293 294 295
        end

        it 'contains a link to the merge request note' do
296
          should have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
297 298 299 300
        end
      end

      describe 'on an issue' do
301
        let(:issue) { create(:issue, project: project) }
302
        let(:note_on_issue_path) { project_issue_path(project, issue, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
303
        before(:each) { note.stub(:noteable).and_return(issue) }
304 305

        subject { Notify.note_issue_email(recipient.id, note.id) }
R
Robb Kidd 已提交
306 307 308 309

        it_behaves_like 'a note email'

        it 'has the correct subject' do
310
          should have_subject /note for issue ##{issue.id}/
R
Robb Kidd 已提交
311 312 313
        end

        it 'contains a link to the issue note' do
314
          should have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
315 316
        end
      end
317 318 319
    end
  end
end