3 describe IssueObserver do
4 let(:some_user) { double(:user, id: 1) }
5 let(:assignee) { double(:user, id: 2) }
6 let(:author) { double(:user, id: 3) }
7 let(:issue) { double(:issue, id: 42, assignee: assignee, author: author) }
9 before(:each) { subject.stub(:current_user).and_return(some_user) }
11 subject { IssueObserver.instance }
13 describe '#after_create' do
15 it 'is called when an issue is created' do
16 subject.should_receive(:after_create)
18 Issue.observers.enable :issue_observer do
19 create(:issue, project: create(:project))
23 it 'sends an email to the assignee' do
24 Notify.should_receive(:new_issue_email).with(issue.id).
25 and_return(double(deliver: true))
27 subject.after_create(issue)
30 it 'does not send an email to the assignee if assignee created the issue' do
31 subject.stub(:current_user).and_return(assignee)
32 Notify.should_not_receive(:new_issue_email)
34 subject.after_create(issue)
38 context '#after_update' do
40 issue.stub(:is_being_reassigned?).and_return(false)
41 issue.stub(:is_being_closed?).and_return(false)
42 issue.stub(:is_being_reopened?).and_return(false)
45 it 'is called when an issue is changed' do
46 changed = create(:issue, project: create(:project))
47 subject.should_receive(:after_update)
49 Issue.observers.enable :issue_observer do
50 changed.description = 'I changed'
55 context 'a reassigned email' do
56 it 'is sent if the issue is being reassigned' do
57 issue.should_receive(:is_being_reassigned?).and_return(true)
58 subject.should_receive(:send_reassigned_email).with(issue)
60 subject.after_update(issue)
63 it 'is not sent if the issue is not being reassigned' do
64 issue.should_receive(:is_being_reassigned?).and_return(false)
65 subject.should_not_receive(:send_reassigned_email)
67 subject.after_update(issue)
71 context 'a status "closed"' do
72 it 'note is created if the issue is being closed' do
73 issue.should_receive(:is_being_closed?).and_return(true)
74 Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
76 subject.after_update(issue)
79 it 'note is not created if the issue is not being closed' do
80 issue.should_receive(:is_being_closed?).and_return(false)
81 Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
83 subject.after_update(issue)
86 it 'notification is delivered if the issue being closed' do
87 issue.stub(:is_being_closed?).and_return(true)
88 Notify.should_receive(:issue_status_changed_email).twice.and_return(stub(deliver: true))
89 Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
91 subject.after_update(issue)
94 it 'notification is not delivered if the issue not being closed' do
95 issue.stub(:is_being_closed?).and_return(false)
96 Notify.should_not_receive(:issue_status_changed_email)
97 Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
99 subject.after_update(issue)
102 it 'notification is delivered only to author if the issue being closed' do
103 issue_without_assignee = double(:issue, id: 42, author: author, assignee: nil)
104 issue_without_assignee.stub(:is_being_reassigned?).and_return(false)
105 issue_without_assignee.stub(:is_being_closed?).and_return(true)
106 issue_without_assignee.stub(:is_being_reopened?).and_return(false)
107 Notify.should_receive(:issue_status_changed_email).once.and_return(stub(deliver: true))
108 Note.should_receive(:create_status_change_note).with(issue_without_assignee, some_user, 'closed')
110 subject.after_update(issue_without_assignee)
114 context 'a status "reopened"' do
115 it 'note is created if the issue is being reopened' do
116 issue.should_receive(:is_being_reopened?).and_return(true)
117 Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
119 subject.after_update(issue)
122 it 'note is not created if the issue is not being reopened' do
123 issue.should_receive(:is_being_reopened?).and_return(false)
124 Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
126 subject.after_update(issue)
129 it 'notification is delivered if the issue being reopened' do
130 issue.stub(:is_being_reopened?).and_return(true)
131 Notify.should_receive(:issue_status_changed_email).twice.and_return(stub(deliver: true))
132 Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
134 subject.after_update(issue)
137 it 'notification is not delivered if the issue not being reopened' do
138 issue.stub(:is_being_reopened?).and_return(false)
139 Notify.should_not_receive(:issue_status_changed_email)
140 Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
142 subject.after_update(issue)
145 it 'notification is delivered only to author if the issue being reopened' do
146 issue_without_assignee = double(:issue, id: 42, author: author, assignee: nil)
147 issue_without_assignee.stub(:is_being_reassigned?).and_return(false)
148 issue_without_assignee.stub(:is_being_closed?).and_return(false)
149 issue_without_assignee.stub(:is_being_reopened?).and_return(true)
150 Notify.should_receive(:issue_status_changed_email).once.and_return(stub(deliver: true))
151 Note.should_receive(:create_status_change_note).with(issue_without_assignee, some_user, 'reopened')
153 subject.after_update(issue_without_assignee)
158 describe '#send_reassigned_email' do
159 let(:previous_assignee) { double(:user, id: 3) }
162 issue.stub(:assignee_id).and_return(assignee.id)
163 issue.stub(:assignee_id_was).and_return(previous_assignee.id)
166 def it_sends_a_reassigned_email_to(recipient)
167 Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
168 and_return(double(deliver: true))
171 def it_does_not_send_a_reassigned_email_to(recipient)
172 Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
175 it 'sends a reassigned email to the previous and current assignees' do
176 it_sends_a_reassigned_email_to assignee.id
177 it_sends_a_reassigned_email_to previous_assignee.id
179 subject.send(:send_reassigned_email, issue)
182 context 'does not send an email to the user who made the reassignment' do
183 it 'if the user is the assignee' do
184 subject.stub(:current_user).and_return(assignee)
185 it_sends_a_reassigned_email_to previous_assignee.id
186 it_does_not_send_a_reassigned_email_to assignee.id
188 subject.send(:send_reassigned_email, issue)
190 it 'if the user is the previous assignee' do
191 subject.stub(:current_user).and_return(previous_assignee)
192 it_sends_a_reassigned_email_to assignee.id
193 it_does_not_send_a_reassigned_email_to previous_assignee.id
195 subject.send(:send_reassigned_email, issue)