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

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

7
  let(:recipient) { Factory.create(:user, email: 'recipient@example.com') }
R
Robb Kidd 已提交
8
  let(:project) { Factory.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) { Factory.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) { Factory.create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { Factory.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) { Factory.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 /new issue ##{issue.id} \| #{issue.title} \| #{project.name}/
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

        describe 'status changed' do
          let(:current_user) { Factory.create :user, email: "current@email.com" }
          let(:status) { 'closed' }
          subject { Notify.issue_status_changed_email(recipient.id, issue.id, status, current_user) }
        
          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) { Factory.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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    describe 'project access changed' do
      let(:project) { Factory.create(:project, 
                                      path: "Fuu", 
                                      code: "Fuu") }
      let(:user) { Factory.create :user }
      let(:users_project) { Factory.create(:users_project, 
                                           project: project, 
                                           user: user) }
      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 已提交
191
    context 'items that are noteable, the email for a note' do
192 193
      let(:note_author) { Factory.create(:user, name: 'author_name') }
      let(:note) { Factory.create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
194

195 196 197 198
      before :each do
          Note.stub(:find).with(note.id).and_return(note)
      end

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

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

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

      describe 'on a commit' do
        let(:commit) do
          mock(:commit).tap do |commit|
232 233
            commit.stub(:id).and_return('fauxsha1')
            commit.stub(:project).and_return(project)
234 235
            commit.stub(:short_id).and_return('fauxsha1')
            commit.stub(:safe_message).and_return('some message')
R
Robb Kidd 已提交
236 237 238 239
          end
        end
        before(:each) { note.stub(:target).and_return(commit) }

240
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
241 242 243 244

        it_behaves_like 'a note email'

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

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

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

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

        it_behaves_like 'a note email'

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

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

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

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

        it_behaves_like 'a note email'

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

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