-
Notifications
You must be signed in to change notification settings - Fork 23
/
client_spec.rb
261 lines (218 loc) · 7.81 KB
/
client_spec.rb
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require 'spec_helper'
describe AirbrakeAPI::Client do
describe 'initialization' do
before do
@keys = AirbrakeAPI::Configuration::VALID_OPTIONS_KEYS
end
context "with module configuration" do
before do
AirbrakeAPI.configure do |config|
@keys.each do |key|
config.send("#{key}=", key)
end
end
end
after do
AirbrakeAPI.reset
end
it "should inherit module configuration" do
api = AirbrakeAPI::Client.new
@keys.each do |key|
expect(api.send(key)).to eq(key)
end
end
context "with class configuration" do
before do
@configuration = {
:account => 'test',
:auth_token => 'token',
:secure => true,
:connection_options => {},
:adapter => :em_http,
:user_agent => 'Airbrake API Tests',
:middleware => AirbrakeAPI::Configuration::DEFAULT_MIDDLEWARE
}
end
context "during initialization" do
it "should override module configuration" do
api = AirbrakeAPI::Client.new(@configuration)
@keys.each do |key|
expect(api.send(key)).to eq(@configuration[key])
end
end
end
context "after initilization" do
it "should override module configuration after initialization" do
api = AirbrakeAPI::Client.new
@configuration.each do |key, value|
api.send("#{key}=", value)
end
@keys.each do |key|
expect(api.send(key)).to eq(@configuration[key])
end
end
end
end
end
context 'with customized middleware' do
let(:logdev) { StringIO.new }
# Client#connection is a private method.
# Adding logger middleware component with an argument it should receive
# when a connection is initialized
let(:logger_middleware) { [Faraday::Response::Logger, Logger.new(logdev)] }
let(:options) do
{
:account => 'myapp', :auth_token => 'abcdefg123456', :secure => false,
:middleware => AirbrakeAPI::Configuration::DEFAULT_MIDDLEWARE + [logger_middleware]
}
end
let(:api) { AirbrakeAPI::Client.new(options) }
# request something to initialize @connection with middleware
before { api.projects }
it 'splats array to initialize middleware with arguments' do
# check that the logger added above did receive the argument
expect(logdev.string).to include(api.projects_path)
end
end
end
describe 'api requests'do
before(:all) do
options = { :account => 'myapp', :auth_token => 'abcdefg123456', :secure => false }
AirbrakeAPI.configure(options)
@client = AirbrakeAPI::Client.new
end
it "should fail with errors" do
expect {
@client.notices(1696172)
}.to raise_error(AirbrakeAPI::AirbrakeError, /You are not authorized to see that page/)
end
describe '#deploys' do
it 'returns an array of deploys' do
expect(@client.deploys('12345')).to be_kind_of(Array)
end
it 'returns deploy data' do
deploys = @client.deploys('12345')
first_deploy = deploys.first
expect(first_deploy.rails_env).to eq('production')
end
it 'returns empty when no data' do
expect(@client.deploys('67890')).to be_kind_of(Array)
end
end
describe '#projects' do
it 'returns an array of projects' do
expect(@client.projects).to be_kind_of(Array)
end
it 'returns project data' do
projects = @client.projects
expect(projects.size).to eq(4)
expect(projects.first.id).to eq('1')
expect(projects.first.name).to eq('Venkman')
end
end
describe '#update' do
it 'should update the status of an error' do
error = @client.update(1696170, :group => { :resolved => true})
expect(error.resolved).to be_truthy
end
end
describe '#errors' do
it "should find a page of the 30 most recent errors" do
errors = @client.errors
ordered = errors.sort_by(&:most_recent_notice_at).reverse
expect(ordered).to eq(errors)
expect(errors.size).to eq(30)
end
it "should paginate errors" do
errors = @client.errors(:page => 2)
ordered = errors.sort_by(&:most_recent_notice_at).reverse
expect(ordered).to eq(errors)
expect(errors.size).to eq(2)
end
it "should use project_id for error path" do
expect(@client).to receive(:request).with(:get, "/projects/123/groups.xml", {}).and_return(double(:group => 111))
@client.errors(:project_id => 123)
end
end
describe '#error' do
it "should find an individual error" do
error = @client.error(1696170)
expect(error.action).to eq('index')
expect(error.id).to eq(1696170)
end
end
describe '#notice' do
it "finds individual notices" do
expect(@client.notice(1234, 1696170)).not_to be_nil
end
it "finds broken notices" do
expect(@client.notice(666, 1696170)).not_to be_nil
end
end
describe '#notices' do
it "finds all error notices" do
notices = @client.notices(1696170)
expect(notices.size).to eq(42)
end
it "finds error notices for a specific page" do
notices = @client.notices(1696170, :page => 1)
expect(notices.size).to eq(30)
expect(notices.first.backtrace).not_to eq(nil)
expect(notices.first.id).to eq(1234)
end
it "finds all error notices with a page limit" do
notices = @client.notices(1696171, :pages => 2)
expect(notices.size).to eq(60)
end
it "yields batches" do
batches = []
notices = @client.notices(1696171, :pages => 2) do |batch|
batches << batch
end
expect(notices.size).to eq(60)
expect(batches.map(&:size)).to eq([30,30])
end
it "can return raw results" do
notices = @client.notices(1696170, :raw => true)
expect(notices.first.backtrace).to eq(nil)
expect(notices.first.id).to eq(1234)
end
end
describe '#connection' do
it 'returns a Faraday connection' do
expect(@client.send(:connection)).to be_kind_of(Faraday::Connection)
end
end
end
describe '#url_for' do
before(:all) do
options = { :account => 'myapp', :auth_token => 'abcdefg123456', :secure => false }
AirbrakeAPI.configure(options)
@client = AirbrakeAPI::Client.new
end
it 'generates web urls for projects' do
expect(@client.url_for(:projects)).to eq('http://myapp.airbrake.io/projects')
end
it 'generates web urls for deploys' do
expect(@client.url_for(:deploys, '2000')).to eq('http://myapp.airbrake.io/projects/2000/deploys')
end
it 'generates web urls for errors' do
expect(@client.url_for(:errors)).to eq('http://myapp.airbrake.io/groups')
end
it 'generates web urls for errors with project_id' do
expect(@client.url_for(:errors, :project_id => 123)).to eq('http://myapp.airbrake.io/projects/123/groups')
end
it 'generates web urls for individual errors' do
expect(@client.url_for(:error, 1696171)).to eq('http://myapp.airbrake.io/errors/1696171')
end
it 'generates web urls for notices' do
expect(@client.url_for(:notices, 1696171)).to eq('http://myapp.airbrake.io/groups/1696171/notices')
end
it 'generates web urls for individual notices' do
expect(@client.url_for(:notice, 123, 1696171)).to eq('http://myapp.airbrake.io/groups/1696171/notices/123')
end
it 'raises an exception when passed an unknown endpoint' do
expect { @client.url_for(:foo) }.to raise_error(ArgumentError)
end
end
end