2012-05-17 17:23:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe IssueObserver do
|
2012-08-10 18:07:50 -04:00
|
|
|
let(:some_user) { double(:user, id: 1) }
|
|
|
|
let(:assignee) { double(:user, id: 2) }
|
2012-08-29 03:48:17 -04:00
|
|
|
let(:author) { double(:user, id: 3) }
|
|
|
|
let(:issue) { double(:issue, id: 42, assignee: assignee, author: author) }
|
2012-05-17 17:23:34 -04:00
|
|
|
|
|
|
|
before(:each) { subject.stub(:current_user).and_return(some_user) }
|
|
|
|
|
|
|
|
subject { IssueObserver.instance }
|
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
describe '#after_create' do
|
|
|
|
|
|
|
|
it 'is called when an issue is created' do
|
|
|
|
subject.should_receive(:after_create)
|
2012-06-12 14:27:03 -04:00
|
|
|
|
|
|
|
Issue.observers.enable :issue_observer do
|
2012-08-10 18:07:50 -04:00
|
|
|
Factory.create(:issue, project: Factory.create(:project))
|
2012-06-12 14:27:03 -04:00
|
|
|
end
|
2012-05-21 13:30:53 -04:00
|
|
|
end
|
2012-05-17 17:23:34 -04:00
|
|
|
|
|
|
|
it 'sends an email to the assignee' do
|
2012-06-15 16:23:28 -04:00
|
|
|
Notify.should_receive(:new_issue_email).with(issue.id).
|
2012-08-10 18:07:50 -04:00
|
|
|
and_return(double(deliver: true))
|
2012-05-17 17:23:34 -04:00
|
|
|
|
|
|
|
subject.after_create(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send an email to the assignee if assignee created the issue' do
|
|
|
|
subject.stub(:current_user).and_return(assignee)
|
|
|
|
Notify.should_not_receive(:new_issue_email)
|
|
|
|
|
|
|
|
subject.after_create(issue)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
context '#after_update' do
|
2012-05-20 15:06:13 -04:00
|
|
|
before(:each) do
|
|
|
|
issue.stub(:is_being_reassigned?).and_return(false)
|
|
|
|
issue.stub(:is_being_closed?).and_return(false)
|
2012-05-21 13:30:53 -04:00
|
|
|
issue.stub(:is_being_reopened?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called when an issue is changed' do
|
2012-08-10 18:07:50 -04:00
|
|
|
changed = Factory.create(:issue, project: Factory.create(:project))
|
2012-05-21 13:30:53 -04:00
|
|
|
subject.should_receive(:after_update)
|
2012-06-12 14:27:03 -04:00
|
|
|
|
|
|
|
Issue.observers.enable :issue_observer do
|
|
|
|
changed.description = 'I changed'
|
|
|
|
changed.save
|
|
|
|
end
|
2012-05-20 15:06:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'a reassigned email' do
|
|
|
|
it 'is sent if the issue is being reassigned' do
|
|
|
|
issue.should_receive(:is_being_reassigned?).and_return(true)
|
|
|
|
subject.should_receive(:send_reassigned_email).with(issue)
|
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
subject.after_update(issue)
|
2012-05-20 15:06:13 -04:00
|
|
|
end
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-05-20 15:06:13 -04:00
|
|
|
it 'is not sent if the issue is not being reassigned' do
|
|
|
|
issue.should_receive(:is_being_reassigned?).and_return(false)
|
|
|
|
subject.should_not_receive(:send_reassigned_email)
|
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
subject.after_update(issue)
|
2012-05-20 15:06:13 -04:00
|
|
|
end
|
2012-05-17 17:23:34 -04:00
|
|
|
end
|
|
|
|
|
2012-08-29 03:48:17 -04:00
|
|
|
context 'a status "closed"' do
|
|
|
|
it 'note is created if the issue is being closed' do
|
2012-05-20 15:06:13 -04:00
|
|
|
issue.should_receive(:is_being_closed?).and_return(true)
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
|
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
subject.after_update(issue)
|
2012-05-20 15:06:13 -04:00
|
|
|
end
|
|
|
|
|
2012-08-29 03:48:17 -04:00
|
|
|
it 'note is not created if the issue is not being closed' do
|
2012-05-20 15:06:13 -04:00
|
|
|
issue.should_receive(:is_being_closed?).and_return(false)
|
|
|
|
Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-05-21 13:30:53 -04:00
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
2012-08-29 03:48:17 -04:00
|
|
|
|
|
|
|
it 'notification is delivered if the issue being closed' do
|
|
|
|
issue.stub(:is_being_closed?).and_return(true)
|
|
|
|
Notify.should_receive(:issue_status_changed_email).twice
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is not delivered if the issue not being closed' do
|
|
|
|
issue.stub(:is_being_closed?).and_return(false)
|
|
|
|
Notify.should_not_receive(:issue_status_changed_email)
|
|
|
|
Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is delivered only to author if the issue being closed' do
|
|
|
|
issue_without_assignee = double(:issue, id: 42, author: author, assignee: nil)
|
|
|
|
issue_without_assignee.stub(:is_being_reassigned?).and_return(false)
|
|
|
|
issue_without_assignee.stub(:is_being_closed?).and_return(true)
|
|
|
|
issue_without_assignee.stub(:is_being_reopened?).and_return(false)
|
|
|
|
Notify.should_receive(:issue_status_changed_email).once
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue_without_assignee, some_user, 'closed')
|
|
|
|
|
|
|
|
subject.after_update(issue_without_assignee)
|
|
|
|
end
|
2012-05-21 13:30:53 -04:00
|
|
|
end
|
|
|
|
|
2012-08-29 03:48:17 -04:00
|
|
|
context 'a status "reopened"' do
|
|
|
|
it 'note is created if the issue is being reopened' do
|
2012-05-21 13:30:53 -04:00
|
|
|
issue.should_receive(:is_being_reopened?).and_return(true)
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
|
|
|
|
2012-08-29 03:48:17 -04:00
|
|
|
it 'note is not created if the issue is not being reopened' do
|
2012-05-21 13:30:53 -04:00
|
|
|
issue.should_receive(:is_being_reopened?).and_return(false)
|
|
|
|
Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
2012-05-20 15:06:13 -04:00
|
|
|
end
|
2012-08-29 03:48:17 -04:00
|
|
|
|
|
|
|
it 'notification is delivered if the issue being reopened' do
|
|
|
|
issue.stub(:is_being_reopened?).and_return(true)
|
|
|
|
Notify.should_receive(:issue_status_changed_email).twice
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is not delivered if the issue not being reopened' do
|
|
|
|
issue.stub(:is_being_reopened?).and_return(false)
|
|
|
|
Notify.should_not_receive(:issue_status_changed_email)
|
|
|
|
Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
|
|
|
|
|
|
|
|
subject.after_update(issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is delivered only to author if the issue being reopened' do
|
|
|
|
issue_without_assignee = double(:issue, id: 42, author: author, assignee: nil)
|
|
|
|
issue_without_assignee.stub(:is_being_reassigned?).and_return(false)
|
|
|
|
issue_without_assignee.stub(:is_being_closed?).and_return(false)
|
|
|
|
issue_without_assignee.stub(:is_being_reopened?).and_return(true)
|
|
|
|
Notify.should_receive(:issue_status_changed_email).once
|
|
|
|
Note.should_receive(:create_status_change_note).with(issue_without_assignee, some_user, 'reopened')
|
|
|
|
|
|
|
|
subject.after_update(issue_without_assignee)
|
|
|
|
end
|
2012-05-20 14:25:34 -04:00
|
|
|
end
|
|
|
|
end
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-05-20 14:25:34 -04:00
|
|
|
describe '#send_reassigned_email' do
|
2012-08-10 18:07:50 -04:00
|
|
|
let(:previous_assignee) { double(:user, id: 3) }
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-05-20 14:25:34 -04:00
|
|
|
before(:each) do
|
|
|
|
issue.stub(:assignee_id).and_return(assignee.id)
|
|
|
|
issue.stub(:assignee_id_was).and_return(previous_assignee.id)
|
|
|
|
end
|
|
|
|
|
2012-06-15 16:23:28 -04:00
|
|
|
def it_sends_a_reassigned_email_to(recipient)
|
|
|
|
Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
|
2012-08-10 18:07:50 -04:00
|
|
|
and_return(double(deliver: true))
|
2012-06-15 16:23:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def it_does_not_send_a_reassigned_email_to(recipient)
|
|
|
|
Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
|
|
|
|
end
|
|
|
|
|
2012-05-20 14:25:34 -04:00
|
|
|
it 'sends a reassigned email to the previous and current assignees' do
|
2012-06-15 16:23:28 -04:00
|
|
|
it_sends_a_reassigned_email_to assignee.id
|
|
|
|
it_sends_a_reassigned_email_to previous_assignee.id
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-06-20 21:23:05 -04:00
|
|
|
subject.send(:send_reassigned_email, issue)
|
2012-05-20 14:25:34 -04:00
|
|
|
end
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-05-20 14:25:34 -04:00
|
|
|
context 'does not send an email to the user who made the reassignment' do
|
|
|
|
it 'if the user is the assignee' do
|
|
|
|
subject.stub(:current_user).and_return(assignee)
|
2012-06-15 16:23:28 -04:00
|
|
|
it_sends_a_reassigned_email_to previous_assignee.id
|
|
|
|
it_does_not_send_a_reassigned_email_to assignee.id
|
2012-05-20 14:25:34 -04:00
|
|
|
|
2012-06-20 21:23:05 -04:00
|
|
|
subject.send(:send_reassigned_email, issue)
|
2012-05-20 14:25:34 -04:00
|
|
|
end
|
|
|
|
it 'if the user is the previous assignee' do
|
|
|
|
subject.stub(:current_user).and_return(previous_assignee)
|
2012-06-15 16:23:28 -04:00
|
|
|
it_sends_a_reassigned_email_to assignee.id
|
|
|
|
it_does_not_send_a_reassigned_email_to previous_assignee.id
|
2012-05-17 17:23:34 -04:00
|
|
|
|
2012-06-20 21:23:05 -04:00
|
|
|
subject.send(:send_reassigned_email, issue)
|
2012-05-17 17:23:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|