error_report_spec.rb
6.45 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require 'spec_helper'
require 'airbrake/version'
require 'airbrake/backtrace'
require 'airbrake/notice'
require 'airbrake/utils/params_cleaner'
# MonkeyPatch to instanciate a Airbrake::Notice without configure
# Airbrake
#
module Airbrake
API_VERSION = '2.4'
class Notice
def framework
'rails'
end
end
end
describe ErrorReport do
context "with notice without line of backtrace" do
let(:xml){
Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read
}
let(:error_report) {
ErrorReport.new(xml)
}
let!(:app) {
Fabricate(
:app,
:api_key => 'APIKEY'
)
}
describe "#app" do
it 'find the good app' do
expect(error_report.app).to eq app
end
end
describe "#backtrace" do
it 'should have valid backtrace' do
error_report.backtrace.should be_valid
end
end
describe "#generate_notice!" do
it "save a notice" do
expect {
error_report.generate_notice!
}.to change {
app.reload.problems.count
}.by(1)
end
context "with notice generate by Airbrake gem" do
let(:xml) { Airbrake::Notice.new(
:exception => Exception.new,
:api_key => 'APIKEY',
:project_root => Rails.root
).to_xml }
it 'save a notice' do
expect {
error_report.generate_notice!
}.to change {
app.reload.problems.count
}.by(1)
end
end
describe "notice create" do
before { error_report.generate_notice! }
subject { error_report.notice }
its(:message) { 'HoptoadTestingException: Testing hoptoad via "rake hoptoad:test". If you can see this, it works.' }
its(:framework) { should == 'Rails: 3.2.11' }
it 'has complete backtrace' do
subject.backtrace_lines.size.should == 73
subject.backtrace_lines.last['file'].should == '[GEM_ROOT]/bin/rake'
end
it 'has server_environement' do
subject.server_environment['environment-name'].should == 'development'
end
it 'has request' do
subject.request['url'].should == 'http://example.org/verify/cupcake=fistfight&lovebird=doomsayer'
subject.request['params']['controller'].should == 'application'
end
it 'has notifier' do
subject.notifier['name'].should == 'Hoptoad Notifier'
end
it 'get user_attributes' do
subject.user_attributes['id'].should == '123'
subject.user_attributes['name'].should == 'Mr. Bean'
subject.user_attributes['email'].should == 'mr.bean@example.com'
subject.user_attributes['username'].should == 'mrbean'
end
it 'valid env_vars' do
# XML: <var key="SCRIPT_NAME"/>
subject.env_vars.should have_key('SCRIPT_NAME')
subject.env_vars['SCRIPT_NAME'].should be_nil # blank ends up nil
# XML representation:
# <var key="rack.session.options">
# <var key="secure">false</var>
# <var key="httponly">true</var>
# <var key="path">/</var>
# <var key="expire_after"/>
# <var key="domain"/>
# <var key="id"/>
# </var>
expected = {
'secure' => 'false',
'httponly' => 'true',
'path' => '/',
'expire_after' => nil,
'domain' => nil,
'id' => nil
}
subject.env_vars.should have_key('rack_session_options')
subject.env_vars['rack_session_options'].should eql(expected)
end
end
it 'save a notice assignes to err' do
error_report.generate_notice!
error_report.notice.err.should be_a(Err)
end
it 'memoize the notice' do
expect {
error_report.generate_notice!
error_report.generate_notice!
}.to change {
Notice.count
}.by(1)
end
it 'find the correct err for the notice' do
err = Fabricate(:err, :problem => Fabricate(:problem, :resolved => true))
ErrorReport.any_instance.stub(:fingerprint).and_return(err.fingerprint)
expect {
error_report.generate_notice!
}.to change {
error_report.error.resolved?
}.from(true).to(false)
end
context "with notification service configured" do
before do
app.notify_on_errs = true
app.watchers.build(:email => 'foo@example.com')
app.save
end
it 'send email' do
notice = error_report.generate_notice!
email = ActionMailer::Base.deliveries.last
email.to.should include(app.watchers.first.email)
email.subject.should include(notice.message.truncate(50))
email.subject.should include("[#{app.name}]")
email.subject.should include("[#{notice.environment_name}]")
end
end
context "with xml without request section" do
let(:xml){
Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read
}
it "save a notice" do
expect {
error_report.generate_notice!
}.to change {
app.reload.problems.count
}.by(1)
end
end
context "with xml with only a single line of backtrace" do
let(:xml){
Rails.root.join('spec','fixtures','hoptoad_test_notice_with_one_line_of_backtrace.xml').read
}
it "save a notice" do
expect {
error_report.generate_notice!
}.to change {
app.reload.problems.count
}.by(1)
end
end
end
describe "#valid?" do
context "with valid error report" do
it "return true" do
expect(error_report.valid?).to be true
end
end
context "with not valid api_key" do
before do
App.where(:api_key => app.api_key).delete_all
end
it "return false" do
expect(error_report.valid?).to be false
end
end
end
describe "#notice" do
context "before generate_notice!" do
it 'return nil' do
expect(error_report.notice).to be nil
end
end
context "after generate_notice!" do
before do
error_report.generate_notice!
end
it 'return the notice' do
expect(error_report.notice).to be_a Notice
end
end
end
end
end