notify_spec.rb 7.7 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 27 28 29 30 31 32 33 34 35 36 37 38

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

    it 'has the correct subject' do
      should have_subject /Account was created for you/
    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}/
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 79 80 81 82

          it_behaves_like 'a multiple recipients email'

          it 'has the correct subject' do
            should have_subject /changed issue/
          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 94 95 96
          end
        end
      end

      context 'for merge requests' do
97
        let(:merge_request) { Factory.create(:merge_request, assignee: assignee, project: project) }
R
Robb Kidd 已提交
98 99

        describe 'that are new' do
100
          subject { Notify.new_merge_request_email(merge_request.id) }
R
Robb Kidd 已提交
101 102 103 104

          it_behaves_like 'an assignee email'

          it 'has the correct subject' do
105
            should have_subject /new merge request !#{merge_request.id}/
R
Robb Kidd 已提交
106 107 108
          end

          it 'contains a link to the new merge request' do
109
            should have_body_text /#{project_merge_request_path(project, merge_request)}/
R
Robb Kidd 已提交
110 111 112 113 114 115 116 117 118 119 120 121
          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
122
          before(:each) { merge_request.stub(:assignee_id_was).and_return(previous_assignee.id) }
R
Robb Kidd 已提交
123

124
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id) }
R
Robb Kidd 已提交
125 126 127 128

          it_behaves_like 'a multiple recipients email'

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

          it 'contains the name of the previous assignee' do
133
            should have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
134 135 136 137 138 139 140
          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
141
            should have_body_text /#{project_merge_request_path project, merge_request}/
R
Robb Kidd 已提交
142 143 144 145
          end

        end
      end
146 147
    end

R
Robb Kidd 已提交
148
    context 'items that are noteable, the email for a note' do
149 150
      let(:note_author) { Factory.create(:user, name: 'author_name') }
      let(:note) { Factory.create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
151

152 153 154 155
      before :each do
          Note.stub(:find).with(note.id).and_return(note)
      end

R
Robb Kidd 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      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
171
        let(:note_on_the_wall_path) { wall_project_path(project, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
172

173
        subject { Notify.note_wall_email(recipient.id, note.id) }
R
Robb Kidd 已提交
174 175 176 177 178 179 180 181

        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
182
          should have_body_text /#{note_on_the_wall_path}/
R
Robb Kidd 已提交
183 184 185 186 187 188
        end
      end

      describe 'on a commit' do
        let(:commit) do
          mock(:commit).tap do |commit|
189 190
            commit.stub(:id).and_return('fauxsha1')
            commit.stub(:project).and_return(project)
191 192
            commit.stub(:short_id).and_return('fauxsha1')
            commit.stub(:safe_message).and_return('some message')
R
Robb Kidd 已提交
193 194 195 196
          end
        end
        before(:each) { note.stub(:target).and_return(commit) }

197
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
198 199 200 201

        it_behaves_like 'a note email'

        it 'has the correct subject' do
202
          should have_subject /note for commit #{commit.short_id}/
R
Robb Kidd 已提交
203 204 205
        end

        it 'contains a link to the commit' do
206
          should have_body_text /fauxsha1/
R
Robb Kidd 已提交
207 208 209 210
        end
      end

      describe 'on a merge request' do
211 212
        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 已提交
213 214
        before(:each) { note.stub(:noteable).and_return(merge_request) }

215
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
216 217 218 219

        it_behaves_like 'a note email'

        it 'has the correct subject' do
220
          should have_subject /note for merge request !#{merge_request.id}/
R
Robb Kidd 已提交
221 222 223
        end

        it 'contains a link to the merge request note' do
224
          should have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
225 226 227 228
        end
      end

      describe 'on an issue' do
229 230
        let(:issue) { Factory.create(:issue, project: project) }
        let(:note_on_issue_path) { project_issue_path(project, issue, anchor: "note_#{note.id}") }
R
Robb Kidd 已提交
231
        before(:each) { note.stub(:noteable).and_return(issue) }
232 233

        subject { Notify.note_issue_email(recipient.id, note.id) }
R
Robb Kidd 已提交
234 235 236 237

        it_behaves_like 'a note email'

        it 'has the correct subject' do
238
          should have_subject /note for issue ##{issue.id}/
R
Robb Kidd 已提交
239 240 241
        end

        it 'contains a link to the issue note' do
242
          should have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
243 244
        end
      end
245 246 247
    end
  end
end