app_spec.rb
7.68 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
235
236
237
require 'spec_helper'
describe App do
context 'validations' do
it 'requires a name' do
app = Fabricate.build(:app, :name => nil)
app.should_not be_valid
app.errors[:name].should include("can't be blank")
end
it 'requires unique names' do
Fabricate(:app, :name => 'Errbit')
app = Fabricate.build(:app, :name => 'Errbit')
app.should_not be_valid
app.errors[:name].should include('is already taken')
end
it 'requires unique api_keys' do
Fabricate(:app, :api_key => 'APIKEY')
app = Fabricate.build(:app, :api_key => 'APIKEY')
app.should_not be_valid
app.errors[:api_key].should include('is already taken')
end
end
context 'being created' do
it 'generates a new api-key' do
app = Fabricate.build(:app)
app.api_key.should be_nil
app.save
app.api_key.should_not be_nil
end
it 'generates a correct api-key' do
app = Fabricate(:app)
app.api_key.should match(/^[a-f0-9]{32}$/)
end
it 'is fine with blank github urls' do
app = Fabricate.build(:app, :github_url => "")
app.save
app.github_url.should == ""
end
it 'does not touch https github urls' do
app = Fabricate.build(:app, :github_url => "https://github.com/errbit/errbit")
app.save
app.github_url.should == "https://github.com/errbit/errbit"
end
it 'normalizes http github urls' do
app = Fabricate.build(:app, :github_url => "http://github.com/errbit/errbit")
app.save
app.github_url.should == "https://github.com/errbit/errbit"
end
it 'normalizes public git repo as a github url' do
app = Fabricate.build(:app, :github_url => "https://github.com/errbit/errbit.git")
app.save
app.github_url.should == "https://github.com/errbit/errbit"
end
it 'normalizes private git repo as a github url' do
app = Fabricate.build(:app, :github_url => "git@github.com:errbit/errbit.git")
app.save
app.github_url.should == "https://github.com/errbit/errbit"
end
end
context '#github_url_to_file' do
it 'resolves to full path to file' do
app = Fabricate(:app, :github_url => "https://github.com/errbit/errbit")
app.github_url_to_file('/path/to/file').should == "https://github.com/errbit/errbit/blob/master/path/to/file"
end
end
context '#github_url?' do
it 'is true when there is a github_url' do
app = Fabricate(:app, :github_url => "https://github.com/errbit/errbit")
app.github_url?.should be_true
end
it 'is false when no github_url' do
app = Fabricate(:app)
app.github_url?.should be_false
end
end
context "notification recipients" do
it "should send notices to either all users, or the configured watchers" do
@app = Fabricate(:app)
3.times { Fabricate(:user) }
5.times { Fabricate(:watcher, :app => @app) }
@app.notify_all_users = true
@app.notification_recipients.size.should == 3
@app.notify_all_users = false
@app.notification_recipients.size.should == 5
end
end
context "copying attributes from existing app" do
it "should only copy the necessary fields" do
@app, @copy_app = Fabricate(:app, :name => "app", :github_url => "url"),
Fabricate(:app, :name => "copy_app", :github_url => "copy url")
@copy_watcher = Fabricate(:watcher, :email => "copywatcher@example.com", :app => @copy_app)
@app.copy_attributes_from(@copy_app.id)
@app.name.should == "app"
@app.github_url.should == "copy url"
@app.watchers.first.email.should == "copywatcher@example.com"
end
end
context '#find_or_create_err!' do
before do
@app = Fabricate(:app)
@conditions = {
:klass => 'Whoops',
:component => 'Foo',
:action => 'bar',
:environment => 'production'
}
end
it 'returns the correct err if one already exists' do
existing = Fabricate(:err, @conditions.merge(:problem => Fabricate(:problem, :app => @app)))
Err.where(@conditions).first.should == existing
@app.find_or_create_err!(@conditions).should == existing
end
it 'assigns the returned err to the given app' do
@app.find_or_create_err!(@conditions).app.should == @app
end
it 'creates a new problem if a matching one does not already exist' do
Err.where(@conditions).first.should be_nil
lambda {
@app.find_or_create_err!(@conditions)
}.should change(Problem,:count).by(1)
end
end
context '#report_error!' do
before do
@xml = Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read
@app = Fabricate(:app, :api_key => 'APIKEY')
ErrorReport.any_instance.stub(:fingerprint).and_return('fingerprintdigest')
end
it 'finds the correct app' do
@notice = App.report_error!(@xml)
@notice.err.app.should == @app
end
it 'finds the correct err for the notice' do
App.should_receive(:find_by_api_key!).and_return(@app)
@app.should_receive(:find_or_create_err!).with({
:klass => 'HoptoadTestingException',
:component => 'application',
:action => 'verify',
:environment => 'development',
:fingerprint => 'fingerprintdigest'
}).and_return(err = Fabricate(:err))
err.notices.stub(:create!)
@notice = App.report_error!(@xml)
end
it 'marks the err as unresolved if it was previously resolved' do
App.should_receive(:find_by_api_key!).and_return(@app)
@app.should_receive(:find_or_create_err!).with({
:klass => 'HoptoadTestingException',
:component => 'application',
:action => 'verify',
:environment => 'development',
:fingerprint => 'fingerprintdigest'
}).and_return(err = Fabricate(:err, :problem => Fabricate(:problem, :resolved => true)))
err.should be_resolved
@notice = App.report_error!(@xml)
@notice.err.should == err
@notice.err.should_not be_resolved
end
it 'should create a new notice' do
@notice = App.report_error!(@xml)
@notice.should be_persisted
end
it 'assigns an err to the notice' do
@notice = App.report_error!(@xml)
@notice.err.should be_a(Err)
end
it 'captures the err message' do
@notice = App.report_error!(@xml)
@notice.message.should == 'HoptoadTestingException: Testing hoptoad via "rake hoptoad:test". If you can see this, it works.'
end
it 'captures the backtrace' do
@notice = App.report_error!(@xml)
@notice.backtrace.size.should == 73
@notice.backtrace.last['file'].should == '[GEM_ROOT]/bin/rake'
end
it 'captures the server_environment' do
@notice = App.report_error!(@xml)
@notice.server_environment['environment-name'].should == 'development'
end
it 'captures the request' do
@notice = App.report_error!(@xml)
@notice.request['url'].should == 'http://example.org/verify'
@notice.request['params']['controller'].should == 'application'
end
it 'captures the notifier' do
@notice = App.report_error!(@xml)
@notice.notifier['name'].should == 'Hoptoad Notifier'
end
it "should handle params without 'request' section" do
xml = Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read
lambda { App.report_error!(xml) }.should_not raise_error
end
it "should handle params with only a single line of backtrace" do
xml = Rails.root.join('spec','fixtures','hoptoad_test_notice_with_one_line_of_backtrace.xml').read
lambda { @notice = App.report_error!(xml) }.should_not raise_error
@notice.backtrace.length.should == 1
end
end
end