2012-05-17 23:23:34 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe IssueObserver do
|
2013-02-18 10:10:58 +01:00
|
|
|
let(:some_user) { create :user }
|
|
|
|
let(:assignee) { create :user }
|
|
|
|
let(:author) { create :user }
|
|
|
|
let(:mock_issue) { double(:issue, id: 42, assignee: assignee, author: author) }
|
|
|
|
let(:assigned_issue) { create(:issue, assignee: assignee, author: author) }
|
|
|
|
let(:unassigned_issue) { create(:issue, author: author) }
|
|
|
|
let(:closed_assigned_issue) { create(:closed_issue, assignee: assignee, author: author) }
|
|
|
|
let(:closed_unassigned_issue) { create(:closed_issue, author: author) }
|
|
|
|
|
2012-05-17 23:23:34 +02:00
|
|
|
|
|
|
|
before(:each) { subject.stub(:current_user).and_return(some_user) }
|
|
|
|
|
|
|
|
subject { IssueObserver.instance }
|
|
|
|
|
2012-05-21 19:30:53 +02:00
|
|
|
describe '#after_create' do
|
|
|
|
|
|
|
|
it 'is called when an issue is created' do
|
|
|
|
subject.should_receive(:after_create)
|
2012-06-12 20:27:03 +02:00
|
|
|
|
|
|
|
Issue.observers.enable :issue_observer do
|
2012-11-06 04:31:55 +01:00
|
|
|
create(:issue, project: create(:project))
|
2012-06-12 20:27:03 +02:00
|
|
|
end
|
2012-05-21 19:30:53 +02:00
|
|
|
end
|
2012-05-17 23:23:34 +02:00
|
|
|
|
|
|
|
it 'sends an email to the assignee' do
|
2013-02-18 10:10:58 +01:00
|
|
|
Notify.should_receive(:new_issue_email).with(mock_issue.id)
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.after_create(mock_issue)
|
2012-05-17 23:23:34 +02:00
|
|
|
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)
|
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.after_create(mock_issue)
|
2012-05-17 23:23:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
context '#after_close' do
|
2012-08-29 09:48:17 +02:00
|
|
|
context 'a status "closed"' do
|
|
|
|
it 'note is created if the issue is being closed' do
|
2013-02-18 10:10:58 +01:00
|
|
|
Note.should_receive(:create_status_change_note).with(assigned_issue, some_user, 'closed')
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
assigned_issue.close
|
2012-05-21 19:30:53 +02:00
|
|
|
end
|
2012-08-29 09:48:17 +02:00
|
|
|
|
|
|
|
it 'notification is delivered if the issue being closed' do
|
2013-01-09 07:14:05 +01:00
|
|
|
Notify.should_receive(:issue_status_changed_email).twice
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
assigned_issue.close
|
2012-08-29 09:48:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is delivered only to author if the issue being closed' do
|
2013-01-09 07:14:05 +01:00
|
|
|
Notify.should_receive(:issue_status_changed_email).once
|
2013-02-18 10:10:58 +01:00
|
|
|
Note.should_receive(:create_status_change_note).with(unassigned_issue, some_user, 'closed')
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
unassigned_issue.close
|
2012-08-29 09:48:17 +02:00
|
|
|
end
|
2012-05-21 19:30:53 +02:00
|
|
|
end
|
|
|
|
|
2012-08-29 09:48:17 +02:00
|
|
|
context 'a status "reopened"' do
|
|
|
|
it 'note is created if the issue is being reopened' do
|
2013-02-18 10:10:58 +01:00
|
|
|
Note.should_receive(:create_status_change_note).with(closed_assigned_issue, some_user, 'reopened')
|
|
|
|
|
|
|
|
closed_assigned_issue.reopen
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notification is delivered if the issue being reopened' do
|
2013-01-09 07:14:05 +01:00
|
|
|
Notify.should_receive(:issue_status_changed_email).twice
|
2012-05-21 19:30:53 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
closed_assigned_issue.reopen
|
2012-05-21 19:30:53 +02:00
|
|
|
end
|
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
it 'notification is delivered only to author if the issue being reopened' do
|
|
|
|
Notify.should_receive(:issue_status_changed_email).once
|
|
|
|
Note.should_receive(:create_status_change_note).with(closed_unassigned_issue, some_user, 'reopened')
|
2012-05-21 19:30:53 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
closed_unassigned_issue.reopen
|
2012-05-20 21:06:13 +02:00
|
|
|
end
|
2013-02-18 10:10:58 +01:00
|
|
|
end
|
|
|
|
end
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
context '#after_update' do
|
|
|
|
before(:each) do
|
|
|
|
mock_issue.stub(:is_being_reassigned?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called when an issue is changed' do
|
|
|
|
changed = create(:issue, project: create(:project))
|
|
|
|
subject.should_receive(:after_update)
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
Issue.observers.enable :issue_observer do
|
|
|
|
changed.description = 'I changed'
|
|
|
|
changed.save
|
2012-08-29 09:48:17 +02:00
|
|
|
end
|
2013-02-18 10:10:58 +01:00
|
|
|
end
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
context 'a reassigned email' do
|
|
|
|
it 'is sent if the issue is being reassigned' do
|
|
|
|
mock_issue.should_receive(:is_being_reassigned?).and_return(true)
|
|
|
|
subject.should_receive(:send_reassigned_email).with(mock_issue)
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.after_update(mock_issue)
|
2012-08-29 09:48:17 +02:00
|
|
|
end
|
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
it 'is not sent if the issue is not being reassigned' do
|
|
|
|
mock_issue.should_receive(:is_being_reassigned?).and_return(false)
|
|
|
|
subject.should_not_receive(:send_reassigned_email)
|
2012-08-29 09:48:17 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.after_update(mock_issue)
|
2012-08-29 09:48:17 +02:00
|
|
|
end
|
2012-05-20 20:25:34 +02:00
|
|
|
end
|
|
|
|
end
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2012-05-20 20:25:34 +02:00
|
|
|
describe '#send_reassigned_email' do
|
2012-08-11 00:07:50 +02:00
|
|
|
let(:previous_assignee) { double(:user, id: 3) }
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2012-05-20 20:25:34 +02:00
|
|
|
before(:each) do
|
2013-02-18 10:10:58 +01:00
|
|
|
mock_issue.stub(:assignee_id).and_return(assignee.id)
|
|
|
|
mock_issue.stub(:assignee_id_was).and_return(previous_assignee.id)
|
2012-05-20 20:25:34 +02:00
|
|
|
end
|
|
|
|
|
2012-06-15 22:23:28 +02:00
|
|
|
def it_sends_a_reassigned_email_to(recipient)
|
2013-02-18 10:10:58 +01:00
|
|
|
Notify.should_receive(:reassigned_issue_email).with(recipient, mock_issue.id, previous_assignee.id)
|
2012-06-15 22:23:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def it_does_not_send_a_reassigned_email_to(recipient)
|
2013-02-18 10:10:58 +01:00
|
|
|
Notify.should_not_receive(:reassigned_issue_email).with(recipient, mock_issue.id, previous_assignee.id)
|
2012-06-15 22:23:28 +02:00
|
|
|
end
|
|
|
|
|
2012-05-20 20:25:34 +02:00
|
|
|
it 'sends a reassigned email to the previous and current assignees' do
|
2012-06-15 22:23:28 +02:00
|
|
|
it_sends_a_reassigned_email_to assignee.id
|
|
|
|
it_sends_a_reassigned_email_to previous_assignee.id
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.send(:send_reassigned_email, mock_issue)
|
2012-05-20 20:25:34 +02:00
|
|
|
end
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2012-05-20 20:25:34 +02: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 22:23:28 +02:00
|
|
|
it_sends_a_reassigned_email_to previous_assignee.id
|
|
|
|
it_does_not_send_a_reassigned_email_to assignee.id
|
2012-05-20 20:25:34 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.send(:send_reassigned_email, mock_issue)
|
2012-05-20 20:25:34 +02:00
|
|
|
end
|
|
|
|
it 'if the user is the previous assignee' do
|
|
|
|
subject.stub(:current_user).and_return(previous_assignee)
|
2012-06-15 22:23:28 +02:00
|
|
|
it_sends_a_reassigned_email_to assignee.id
|
|
|
|
it_does_not_send_a_reassigned_email_to previous_assignee.id
|
2012-05-17 23:23:34 +02:00
|
|
|
|
2013-02-18 10:10:58 +01:00
|
|
|
subject.send(:send_reassigned_email, mock_issue)
|
2012-05-17 23:23:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|