Commit 852b9c28dd80af4a49336b0ff921a1a863dbc929

Authored by Robert Speicher
1 parent f13bccc1

Move observer specs out of spec/models into spec/observers

spec/models/activity_observer_spec.rb
... ... @@ -1,48 +0,0 @@
1   -require 'spec_helper'
2   -
3   -describe ActivityObserver do
4   - let(:project) { Factory :project }
5   -
6   - def self.it_should_be_valid_event
7   - it { @event.should_not be_nil }
8   - it { @event.project.should == project }
9   - end
10   -
11   - describe "Merge Request created" do
12   - before do
13   - MergeRequest.observers.enable :activity_observer do
14   - @merge_request = Factory :merge_request, project: project
15   - @event = Event.last
16   - end
17   - end
18   -
19   - it_should_be_valid_event
20   - it { @event.action.should == Event::Created }
21   - it { @event.target.should == @merge_request }
22   - end
23   -
24   - describe "Issue created" do
25   - before do
26   - Issue.observers.enable :activity_observer do
27   - @issue = Factory :issue, project: project
28   - @event = Event.last
29   - end
30   - end
31   -
32   - it_should_be_valid_event
33   - it { @event.action.should == Event::Created }
34   - it { @event.target.should == @issue }
35   - end
36   -
37   - #describe "Issue commented" do
38   - #before do
39   - #@issue = Factory :issue, project: project
40   - #@note = Factory :note, noteable: @issue, project: project
41   - #@event = Event.last
42   - #end
43   -
44   - #it_should_be_valid_event
45   - #it { @event.action.should == Event::Commented }
46   - #it { @event.target.should == @note }
47   - #end
48   -end
spec/models/issue_observer_spec.rb
... ... @@ -1,144 +0,0 @@
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(:issue) { double(:issue, id: 42, assignee: assignee) }
7   -
8   - before(:each) { subject.stub(:current_user).and_return(some_user) }
9   -
10   - subject { IssueObserver.instance }
11   -
12   - describe '#after_create' do
13   -
14   - it 'is called when an issue is created' do
15   - subject.should_receive(:after_create)
16   -
17   - Issue.observers.enable :issue_observer do
18   - Factory.create(:issue, project: Factory.create(:project))
19   - end
20   - end
21   -
22   - it 'sends an email to the assignee' do
23   - Notify.should_receive(:new_issue_email).with(issue.id).
24   - and_return(double(deliver: true))
25   -
26   - subject.after_create(issue)
27   - end
28   -
29   - it 'does not send an email to the assignee if assignee created the issue' do
30   - subject.stub(:current_user).and_return(assignee)
31   - Notify.should_not_receive(:new_issue_email)
32   -
33   - subject.after_create(issue)
34   - end
35   - end
36   -
37   - context '#after_update' do
38   - before(:each) do
39   - issue.stub(:is_being_reassigned?).and_return(false)
40   - issue.stub(:is_being_closed?).and_return(false)
41   - issue.stub(:is_being_reopened?).and_return(false)
42   - end
43   -
44   - it 'is called when an issue is changed' do
45   - changed = Factory.create(:issue, project: Factory.create(:project))
46   - subject.should_receive(:after_update)
47   -
48   - Issue.observers.enable :issue_observer do
49   - changed.description = 'I changed'
50   - changed.save
51   - end
52   - end
53   -
54   - context 'a reassigned email' do
55   - it 'is sent if the issue is being reassigned' do
56   - issue.should_receive(:is_being_reassigned?).and_return(true)
57   - subject.should_receive(:send_reassigned_email).with(issue)
58   -
59   - subject.after_update(issue)
60   - end
61   -
62   - it 'is not sent if the issue is not being reassigned' do
63   - issue.should_receive(:is_being_reassigned?).and_return(false)
64   - subject.should_not_receive(:send_reassigned_email)
65   -
66   - subject.after_update(issue)
67   - end
68   - end
69   -
70   - context 'a status "closed" note' do
71   - it 'is created if the issue is being closed' do
72   - issue.should_receive(:is_being_closed?).and_return(true)
73   - Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
74   -
75   - subject.after_update(issue)
76   - end
77   -
78   - it 'is not created if the issue is not being closed' do
79   - issue.should_receive(:is_being_closed?).and_return(false)
80   - Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
81   -
82   - subject.after_update(issue)
83   - end
84   - end
85   -
86   - context 'a status "reopened" note' do
87   - it 'is created if the issue is being reopened' do
88   - issue.should_receive(:is_being_reopened?).and_return(true)
89   - Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
90   -
91   - subject.after_update(issue)
92   - end
93   -
94   - it 'is not created if the issue is not being reopened' do
95   - issue.should_receive(:is_being_reopened?).and_return(false)
96   - Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
97   -
98   - subject.after_update(issue)
99   - end
100   - end
101   - end
102   -
103   - describe '#send_reassigned_email' do
104   - let(:previous_assignee) { double(:user, id: 3) }
105   -
106   - before(:each) do
107   - issue.stub(:assignee_id).and_return(assignee.id)
108   - issue.stub(:assignee_id_was).and_return(previous_assignee.id)
109   - end
110   -
111   - def it_sends_a_reassigned_email_to(recipient)
112   - Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
113   - and_return(double(deliver: true))
114   - end
115   -
116   - def it_does_not_send_a_reassigned_email_to(recipient)
117   - Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
118   - end
119   -
120   - it 'sends a reassigned email to the previous and current assignees' do
121   - it_sends_a_reassigned_email_to assignee.id
122   - it_sends_a_reassigned_email_to previous_assignee.id
123   -
124   - subject.send(:send_reassigned_email, issue)
125   - end
126   -
127   - context 'does not send an email to the user who made the reassignment' do
128   - it 'if the user is the assignee' do
129   - subject.stub(:current_user).and_return(assignee)
130   - it_sends_a_reassigned_email_to previous_assignee.id
131   - it_does_not_send_a_reassigned_email_to assignee.id
132   -
133   - subject.send(:send_reassigned_email, issue)
134   - end
135   - it 'if the user is the previous assignee' do
136   - subject.stub(:current_user).and_return(previous_assignee)
137   - it_sends_a_reassigned_email_to assignee.id
138   - it_does_not_send_a_reassigned_email_to previous_assignee.id
139   -
140   - subject.send(:send_reassigned_email, issue)
141   - end
142   - end
143   - end
144   -end
spec/models/user_observer_spec.rb
... ... @@ -1,26 +0,0 @@
1   -require 'spec_helper'
2   -
3   -describe UserObserver do
4   - subject { UserObserver.instance }
5   -
6   - it 'calls #after_create when new users are created' do
7   - new_user = Factory.new(:user)
8   - subject.should_receive(:after_create).with(new_user)
9   -
10   - User.observers.enable :user_observer do
11   - new_user.save
12   - end
13   - end
14   -
15   - context 'when a new user is created' do
16   - let(:user) { double(:user, id: 42, password: 'P@ssword!') }
17   - let(:notification) { double :notification }
18   -
19   - it 'sends an email' do
20   - notification.should_receive(:deliver)
21   - Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
22   -
23   - subject.after_create(user)
24   - end
25   - end
26   -end
spec/observers/activity_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ActivityObserver do
  4 + let(:project) { Factory :project }
  5 +
  6 + def self.it_should_be_valid_event
  7 + it { @event.should_not be_nil }
  8 + it { @event.project.should == project }
  9 + end
  10 +
  11 + describe "Merge Request created" do
  12 + before do
  13 + MergeRequest.observers.enable :activity_observer do
  14 + @merge_request = Factory :merge_request, project: project
  15 + @event = Event.last
  16 + end
  17 + end
  18 +
  19 + it_should_be_valid_event
  20 + it { @event.action.should == Event::Created }
  21 + it { @event.target.should == @merge_request }
  22 + end
  23 +
  24 + describe "Issue created" do
  25 + before do
  26 + Issue.observers.enable :activity_observer do
  27 + @issue = Factory :issue, project: project
  28 + @event = Event.last
  29 + end
  30 + end
  31 +
  32 + it_should_be_valid_event
  33 + it { @event.action.should == Event::Created }
  34 + it { @event.target.should == @issue }
  35 + end
  36 +
  37 + #describe "Issue commented" do
  38 + #before do
  39 + #@issue = Factory :issue, project: project
  40 + #@note = Factory :note, noteable: @issue, project: project
  41 + #@event = Event.last
  42 + #end
  43 +
  44 + #it_should_be_valid_event
  45 + #it { @event.action.should == Event::Commented }
  46 + #it { @event.target.should == @note }
  47 + #end
  48 +end
... ...
spec/observers/issue_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,144 @@
  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(:issue) { double(:issue, id: 42, assignee: assignee) }
  7 +
  8 + before(:each) { subject.stub(:current_user).and_return(some_user) }
  9 +
  10 + subject { IssueObserver.instance }
  11 +
  12 + describe '#after_create' do
  13 +
  14 + it 'is called when an issue is created' do
  15 + subject.should_receive(:after_create)
  16 +
  17 + Issue.observers.enable :issue_observer do
  18 + Factory.create(:issue, project: Factory.create(:project))
  19 + end
  20 + end
  21 +
  22 + it 'sends an email to the assignee' do
  23 + Notify.should_receive(:new_issue_email).with(issue.id).
  24 + and_return(double(deliver: true))
  25 +
  26 + subject.after_create(issue)
  27 + end
  28 +
  29 + it 'does not send an email to the assignee if assignee created the issue' do
  30 + subject.stub(:current_user).and_return(assignee)
  31 + Notify.should_not_receive(:new_issue_email)
  32 +
  33 + subject.after_create(issue)
  34 + end
  35 + end
  36 +
  37 + context '#after_update' do
  38 + before(:each) do
  39 + issue.stub(:is_being_reassigned?).and_return(false)
  40 + issue.stub(:is_being_closed?).and_return(false)
  41 + issue.stub(:is_being_reopened?).and_return(false)
  42 + end
  43 +
  44 + it 'is called when an issue is changed' do
  45 + changed = Factory.create(:issue, project: Factory.create(:project))
  46 + subject.should_receive(:after_update)
  47 +
  48 + Issue.observers.enable :issue_observer do
  49 + changed.description = 'I changed'
  50 + changed.save
  51 + end
  52 + end
  53 +
  54 + context 'a reassigned email' do
  55 + it 'is sent if the issue is being reassigned' do
  56 + issue.should_receive(:is_being_reassigned?).and_return(true)
  57 + subject.should_receive(:send_reassigned_email).with(issue)
  58 +
  59 + subject.after_update(issue)
  60 + end
  61 +
  62 + it 'is not sent if the issue is not being reassigned' do
  63 + issue.should_receive(:is_being_reassigned?).and_return(false)
  64 + subject.should_not_receive(:send_reassigned_email)
  65 +
  66 + subject.after_update(issue)
  67 + end
  68 + end
  69 +
  70 + context 'a status "closed" note' do
  71 + it 'is created if the issue is being closed' do
  72 + issue.should_receive(:is_being_closed?).and_return(true)
  73 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
  74 +
  75 + subject.after_update(issue)
  76 + end
  77 +
  78 + it 'is not created if the issue is not being closed' do
  79 + issue.should_receive(:is_being_closed?).and_return(false)
  80 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
  81 +
  82 + subject.after_update(issue)
  83 + end
  84 + end
  85 +
  86 + context 'a status "reopened" note' do
  87 + it 'is created if the issue is being reopened' do
  88 + issue.should_receive(:is_being_reopened?).and_return(true)
  89 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  90 +
  91 + subject.after_update(issue)
  92 + end
  93 +
  94 + it 'is not created if the issue is not being reopened' do
  95 + issue.should_receive(:is_being_reopened?).and_return(false)
  96 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  97 +
  98 + subject.after_update(issue)
  99 + end
  100 + end
  101 + end
  102 +
  103 + describe '#send_reassigned_email' do
  104 + let(:previous_assignee) { double(:user, id: 3) }
  105 +
  106 + before(:each) do
  107 + issue.stub(:assignee_id).and_return(assignee.id)
  108 + issue.stub(:assignee_id_was).and_return(previous_assignee.id)
  109 + end
  110 +
  111 + def it_sends_a_reassigned_email_to(recipient)
  112 + Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
  113 + and_return(double(deliver: true))
  114 + end
  115 +
  116 + def it_does_not_send_a_reassigned_email_to(recipient)
  117 + Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
  118 + end
  119 +
  120 + it 'sends a reassigned email to the previous and current assignees' do
  121 + it_sends_a_reassigned_email_to assignee.id
  122 + it_sends_a_reassigned_email_to previous_assignee.id
  123 +
  124 + subject.send(:send_reassigned_email, issue)
  125 + end
  126 +
  127 + context 'does not send an email to the user who made the reassignment' do
  128 + it 'if the user is the assignee' do
  129 + subject.stub(:current_user).and_return(assignee)
  130 + it_sends_a_reassigned_email_to previous_assignee.id
  131 + it_does_not_send_a_reassigned_email_to assignee.id
  132 +
  133 + subject.send(:send_reassigned_email, issue)
  134 + end
  135 + it 'if the user is the previous assignee' do
  136 + subject.stub(:current_user).and_return(previous_assignee)
  137 + it_sends_a_reassigned_email_to assignee.id
  138 + it_does_not_send_a_reassigned_email_to previous_assignee.id
  139 +
  140 + subject.send(:send_reassigned_email, issue)
  141 + end
  142 + end
  143 + end
  144 +end
... ...
spec/observers/user_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +require 'spec_helper'
  2 +
  3 +describe UserObserver do
  4 + subject { UserObserver.instance }
  5 +
  6 + it 'calls #after_create when new users are created' do
  7 + new_user = Factory.new(:user)
  8 + subject.should_receive(:after_create).with(new_user)
  9 +
  10 + User.observers.enable :user_observer do
  11 + new_user.save
  12 + end
  13 + end
  14 +
  15 + context 'when a new user is created' do
  16 + let(:user) { double(:user, id: 42, password: 'P@ssword!') }
  17 + let(:notification) { double :notification }
  18 +
  19 + it 'sends an email' do
  20 + notification.should_receive(:deliver)
  21 + Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
  22 +
  23 + subject.after_create(user)
  24 + end
  25 + end
  26 +end
... ...