OSDN Git Service

Fixed tests
[wvm/gitlab.git] / spec / observers / issue_observer_spec.rb
1 require 'spec_helper'
2
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) }
8
9   before(:each) { subject.stub(:current_user).and_return(some_user) }
10
11   subject { IssueObserver.instance }
12
13   describe '#after_create' do
14
15     it 'is called when an issue is created' do
16       subject.should_receive(:after_create)
17
18       Issue.observers.enable :issue_observer do
19         create(:issue, project: create(:project))
20       end
21     end
22
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))
26
27       subject.after_create(issue)
28     end
29
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)
33
34       subject.after_create(issue)
35     end
36   end
37
38   context '#after_update' do
39     before(:each) 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)
43     end
44
45     it 'is called when an issue is changed' do
46       changed = create(:issue, project: create(:project))
47       subject.should_receive(:after_update)
48
49       Issue.observers.enable :issue_observer do
50         changed.description = 'I changed'
51         changed.save
52       end
53     end
54
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)
59
60         subject.after_update(issue)
61       end
62
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)
66
67         subject.after_update(issue)
68       end
69     end
70
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')
75
76         subject.after_update(issue)
77       end
78
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')
82
83         subject.after_update(issue)
84       end
85
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')
90
91         subject.after_update(issue)
92       end
93
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')
98
99         subject.after_update(issue)
100       end
101
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')
109
110         subject.after_update(issue_without_assignee)
111       end
112     end
113
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')
118
119         subject.after_update(issue)
120       end
121
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')
125
126         subject.after_update(issue)
127       end
128
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')
133
134         subject.after_update(issue)
135       end
136
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')
141
142         subject.after_update(issue)
143       end
144
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')
152
153         subject.after_update(issue_without_assignee)
154       end
155     end
156   end
157
158   describe '#send_reassigned_email' do
159     let(:previous_assignee) { double(:user, id: 3) }
160
161     before(:each) do
162       issue.stub(:assignee_id).and_return(assignee.id)
163       issue.stub(:assignee_id_was).and_return(previous_assignee.id)
164     end
165
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))
169     end
170
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)
173     end
174
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
178
179       subject.send(:send_reassigned_email, issue)
180     end
181
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
187
188         subject.send(:send_reassigned_email, issue)
189       end
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
194
195         subject.send(:send_reassigned_email, issue)
196       end
197     end
198   end
199 end