notice_observer_spec.rb
7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'spec_helper'
describe "Callback on Notice" do
describe "email notifications (configured individually for each app)" do
custom_thresholds = [2, 4, 8, 16, 32, 64]
before do
Errbit::Config.per_app_email_at_notices = true
@app = Fabricate(:app_with_watcher, :email_at_notices => custom_thresholds)
@err = Fabricate(:err, :problem => Fabricate(:problem, :app => @app))
end
after do
Errbit::Config.per_app_email_at_notices = false
end
custom_thresholds.each do |threshold|
it "sends an email notification after #{threshold} notice(s)" do
@err.problem.stub(:notices_count).and_return(threshold)
Mailer.should_receive(:err_notification).
and_return(double('email', :deliver => true))
Fabricate(:notice, :err => @err)
end
end
end
describe "email notifications for a resolved issue" do
before do
Errbit::Config.per_app_email_at_notices = true
@app = Fabricate(:app_with_watcher, :email_at_notices => [1])
@err = Fabricate(:err, :problem => Fabricate(:problem, :app => @app, :notices_count => 100))
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it "should send email notification after 1 notice since an error has been resolved" do
@err.problem.resolve!
Mailer.should_receive(:err_notification).
and_return(double('email', :deliver => true))
Fabricate(:notice, :err => @err)
end
it 'self notify if mailer failed' do
@err.problem.resolve!
Mailer.should_receive(:err_notification).
and_raise(ArgumentError)
HoptoadNotifier.should_receive(:notify)
Fabricate(:notice, :err => @err)
end
end
describe "should send a notification if a notification service is configured with defaults" do
let(:app) { Fabricate(:app, :email_at_notices => [1], :notification_service => Fabricate(:campfire_notification_service))}
let(:err) { Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 100)) }
let(:backtrace) { Fabricate(:backtrace) }
before do
Errbit::Config.per_app_email_at_notices = true
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it "should create a campfire notification" do
app.notification_service.should_receive(:create_notification)
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
end
describe "send a notification if a notification service is configured with defaults but failed" do
let(:app) { Fabricate(:app_with_watcher,
:notify_on_errs => true,
:email_at_notices => [1, 100], :notification_service => Fabricate(:campfire_notification_service))}
let(:err) { Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 99)) }
let(:backtrace) { Fabricate(:backtrace) }
before do
Errbit::Config.per_app_email_at_notices = true
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it "send email" do
app.notification_service.should_receive(:create_notification).and_raise(ArgumentError)
Mailer.should_receive(:err_notification).and_return(double(:deliver => true))
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
end
describe "should not send a notification if a notification service is not configured" do
let(:app) { Fabricate(:app, :email_at_notices => [1], :notification_service => Fabricate(:notification_service))}
let(:err) { Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 100)) }
let(:backtrace) { Fabricate(:backtrace) }
before do
Errbit::Config.per_app_email_at_notices = true
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it "should not create a campfire notification" do
app.notification_service.should_not_receive(:create_notification)
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
end
describe 'hipcat notifications' do
let(:app) { Fabricate(:app, :email_at_notices => [1], :notification_service => Fabricate(:hipchat_notification_service))}
let(:err) { Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 100)) }
before do
Errbit::Config.per_app_email_at_notices = true
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it 'creates a hipchat notification' do
app.notification_service.should_receive(:create_notification)
Fabricate(:notice, :err => err)
end
end
describe "should send a notification at desired intervals" do
let(:app) { Fabricate(:app, :email_at_notices => [1], :notification_service => Fabricate(:campfire_notification_service, :notify_at_notices => [1,2]))}
let(:backtrace) { Fabricate(:backtrace) }
before do
Errbit::Config.per_app_email_at_notices = true
end
after do
Errbit::Config.per_app_email_at_notices = false
end
it "should create a campfire notification on first notice" do
err = Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 1))
app.notification_service.should_receive(:create_notification)
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
it "should create a campfire notification on second notice" do
err = Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 1))
app.notification_service.should_receive(:create_notification)
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
it "should not create a campfire notification on third notice" do
err = Fabricate(:err, :problem => Fabricate(:problem, :app => app, :notices_count => 1))
app.notification_service.should_receive(:create_notification)
Notice.create!(:err => err, :message => 'FooError: Too Much Bar', :server_environment => {'environment-name' => 'production'},
:backtrace => backtrace, :notifier => { 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' })
end
end
end