issue_observer_spec.rb 5.16 KB
Newer Older
1 2 3
require 'spec_helper'

describe IssueObserver do
Andrew8xx8's avatar
Andrew8xx8 committed
4 5 6 7 8 9 10 11 12
  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) }

13 14 15 16 17

  before(:each) { subject.stub(:current_user).and_return(some_user) }

  subject { IssueObserver.instance }

18 19 20 21
  describe '#after_create' do

    it 'is called when an issue is created' do
      subject.should_receive(:after_create)
22 23

      Issue.observers.enable :issue_observer do
24
        create(:issue, project: create(:project))
25
      end
26
    end
27 28

    it 'sends an email to the assignee' do
Andrew8xx8's avatar
Andrew8xx8 committed
29
      Notify.should_receive(:new_issue_email).with(mock_issue.id)
30

Andrew8xx8's avatar
Andrew8xx8 committed
31
      subject.after_create(mock_issue)
32 33 34 35 36 37
    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)

Andrew8xx8's avatar
Andrew8xx8 committed
38
      subject.after_create(mock_issue)
39 40 41
    end
  end

Andrew8xx8's avatar
Andrew8xx8 committed
42
  context '#after_close' do
43 44
    context 'a status "closed"' do
      it 'note is created if the issue is being closed' do
Andrew8xx8's avatar
Andrew8xx8 committed
45
        Note.should_receive(:create_status_change_note).with(assigned_issue, some_user, 'closed')
46

Andrew8xx8's avatar
Andrew8xx8 committed
47
        assigned_issue.close
48
      end
49 50

      it 'notification is delivered if the issue being closed' do
51
        Notify.should_receive(:issue_status_changed_email).twice
52

Andrew8xx8's avatar
Andrew8xx8 committed
53
        assigned_issue.close
54 55 56
      end

      it 'notification is delivered only to author if the issue being closed' do
57
        Notify.should_receive(:issue_status_changed_email).once
Andrew8xx8's avatar
Andrew8xx8 committed
58
        Note.should_receive(:create_status_change_note).with(unassigned_issue, some_user, 'closed')
59

Andrew8xx8's avatar
Andrew8xx8 committed
60
        unassigned_issue.close
61
      end
62 63
    end

64 65
    context 'a status "reopened"' do
      it 'note is created if the issue is being reopened' do
Andrew8xx8's avatar
Andrew8xx8 committed
66 67 68 69 70 71
        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
72
        Notify.should_receive(:issue_status_changed_email).twice
73

Andrew8xx8's avatar
Andrew8xx8 committed
74
        closed_assigned_issue.reopen
75 76
      end

Andrew8xx8's avatar
Andrew8xx8 committed
77 78 79
      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')
80

Andrew8xx8's avatar
Andrew8xx8 committed
81
        closed_unassigned_issue.reopen
82
      end
Andrew8xx8's avatar
Andrew8xx8 committed
83 84
    end
  end
85

Andrew8xx8's avatar
Andrew8xx8 committed
86 87 88 89 90 91 92 93
  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)
94

Andrew8xx8's avatar
Andrew8xx8 committed
95 96 97
      Issue.observers.enable :issue_observer do
        changed.description = 'I changed'
        changed.save
98
      end
Andrew8xx8's avatar
Andrew8xx8 committed
99
    end
100

Andrew8xx8's avatar
Andrew8xx8 committed
101 102 103 104
    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)
105

Andrew8xx8's avatar
Andrew8xx8 committed
106
        subject.after_update(mock_issue)
107 108
      end

Andrew8xx8's avatar
Andrew8xx8 committed
109 110 111
      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)
112

Andrew8xx8's avatar
Andrew8xx8 committed
113
        subject.after_update(mock_issue)
114
      end
115 116
    end
  end
117

118
  describe '#send_reassigned_email' do
119
    let(:previous_assignee) { double(:user, id: 3) }
120

121
    before(:each) do
Andrew8xx8's avatar
Andrew8xx8 committed
122 123
      mock_issue.stub(:assignee_id).and_return(assignee.id)
      mock_issue.stub(:assignee_id_was).and_return(previous_assignee.id)
124 125
    end

Robb Kidd's avatar
Robb Kidd committed
126
    def it_sends_a_reassigned_email_to(recipient)
Andrew8xx8's avatar
Andrew8xx8 committed
127
      Notify.should_receive(:reassigned_issue_email).with(recipient, mock_issue.id, previous_assignee.id)
Robb Kidd's avatar
Robb Kidd committed
128 129 130
    end

    def it_does_not_send_a_reassigned_email_to(recipient)
Andrew8xx8's avatar
Andrew8xx8 committed
131
      Notify.should_not_receive(:reassigned_issue_email).with(recipient, mock_issue.id, previous_assignee.id)
Robb Kidd's avatar
Robb Kidd committed
132 133
    end

134
    it 'sends a reassigned email to the previous and current assignees' do
Robb Kidd's avatar
Robb Kidd committed
135 136
      it_sends_a_reassigned_email_to assignee.id
      it_sends_a_reassigned_email_to previous_assignee.id
137

Andrew8xx8's avatar
Andrew8xx8 committed
138
      subject.send(:send_reassigned_email, mock_issue)
139
    end
140

141 142 143
    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)
Robb Kidd's avatar
Robb Kidd committed
144 145
        it_sends_a_reassigned_email_to previous_assignee.id
        it_does_not_send_a_reassigned_email_to assignee.id
146

Andrew8xx8's avatar
Andrew8xx8 committed
147
        subject.send(:send_reassigned_email, mock_issue)
148 149 150
      end
      it 'if the user is the previous assignee' do
        subject.stub(:current_user).and_return(previous_assignee)
Robb Kidd's avatar
Robb Kidd committed
151 152
        it_sends_a_reassigned_email_to assignee.id
        it_does_not_send_a_reassigned_email_to previous_assignee.id
153

Andrew8xx8's avatar
Andrew8xx8 committed
154
        subject.send(:send_reassigned_email, mock_issue)
155 156 157 158
      end
    end
  end
end