zendesk_api 1.13.1 → 1.13.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zendesk_api/client.rb +1 -1
- data/lib/zendesk_api/middleware/response/parse_json.rb +0 -1
- data/lib/zendesk_api/version.rb +1 -1
- metadata +10 -205
- data/.gitignore +0 -15
- data/.rspec +0 -2
- data/.travis.yml +0 -10
- data/.yardopts +0 -10
- data/Gemfile +0 -14
- data/LICENSE +0 -176
- data/README.md +0 -425
- data/Rakefile +0 -52
- data/spec/core/association_spec.rb +0 -256
- data/spec/core/bulk_actions_spec.rb +0 -69
- data/spec/core/client_spec.rb +0 -280
- data/spec/core/collection_spec.rb +0 -901
- data/spec/core/configuration_spec.rb +0 -28
- data/spec/core/create_resource_spec.rb +0 -39
- data/spec/core/data_namespace_spec.rb +0 -15
- data/spec/core/data_resource_spec.rb +0 -239
- data/spec/core/inflection_spec.rb +0 -7
- data/spec/core/lru_cache_spec.rb +0 -26
- data/spec/core/middleware/request/encode_json_spec.rb +0 -73
- data/spec/core/middleware/request/etag_cache_spec.rb +0 -21
- data/spec/core/middleware/request/retry_spec.rb +0 -49
- data/spec/core/middleware/request/test.jpg +0 -0
- data/spec/core/middleware/request/upload_spec.rb +0 -164
- data/spec/core/middleware/response/callback_spec.rb +0 -17
- data/spec/core/middleware/response/deflate_spec.rb +0 -21
- data/spec/core/middleware/response/gzip_spec.rb +0 -25
- data/spec/core/middleware/response/parse_iso_dates_spec.rb +0 -44
- data/spec/core/middleware/response/parse_json_spec.rb +0 -53
- data/spec/core/middleware/response/raise_error_spec.rb +0 -128
- data/spec/core/middleware/response/sanitize_response_spec.rb +0 -19
- data/spec/core/read_resource_spec.rb +0 -97
- data/spec/core/resource_spec.rb +0 -541
- data/spec/core/resources/automation_spec.rb +0 -81
- data/spec/core/resources/macro_spec.rb +0 -27
- data/spec/core/resources/trigger_spec.rb +0 -67
- data/spec/core/resources/view_spec.rb +0 -97
- data/spec/core/search_spec.rb +0 -23
- data/spec/core/spec_helper.rb +0 -162
- data/spec/core/trackie_spec.rb +0 -129
- data/spec/fixtures/Argentina.gif +0 -0
- data/spec/fixtures/Argentina2.gif +0 -0
- data/spec/fixtures/credentials.yml.example +0 -3
- data/spec/fixtures/sample_app.zip +0 -0
- data/spec/fixtures/test_resources.rb +0 -22
- data/spec/fixtures/zendesk.rb +0 -109
- data/spec/live/Readme.md +0 -4
- data/spec/live/activity_spec.rb +0 -12
- data/spec/live/app_installation_spec.rb +0 -58
- data/spec/live/app_spec.rb +0 -58
- data/spec/live/audit_spec.rb +0 -15
- data/spec/live/automation_spec.rb +0 -23
- data/spec/live/bookmark_spec.rb +0 -11
- data/spec/live/brand_spec.rb +0 -14
- data/spec/live/category_spec.rb +0 -12
- data/spec/live/collection_spec.rb +0 -64
- data/spec/live/custom_role_spec.rb +0 -5
- data/spec/live/dynamic_content/item_spec.rb +0 -16
- data/spec/live/dynamic_content/variant_spec.rb +0 -19
- data/spec/live/forum_spec.rb +0 -14
- data/spec/live/forum_subscription_spec.rb +0 -12
- data/spec/live/group_membership_spec.rb +0 -18
- data/spec/live/group_spec.rb +0 -32
- data/spec/live/identity_spec.rb +0 -14
- data/spec/live/locale_spec.rb +0 -9
- data/spec/live/macro_spec.rb +0 -44
- data/spec/live/organization_field_spec.rb +0 -12
- data/spec/live/organization_membership_spec.rb +0 -11
- data/spec/live/organization_spec.rb +0 -12
- data/spec/live/push_notification_device_spec.rb +0 -21
- data/spec/live/request_spec.rb +0 -38
- data/spec/live/satisfaction_rating_spec.rb +0 -6
- data/spec/live/setting_spec.rb +0 -25
- data/spec/live/suspended_ticket_spec.rb +0 -8
- data/spec/live/tag_spec.rb +0 -58
- data/spec/live/target_spec.rb +0 -17
- data/spec/live/ticket_field_spec.rb +0 -12
- data/spec/live/ticket_form_spec.rb +0 -14
- data/spec/live/ticket_metrics_spec.rb +0 -6
- data/spec/live/ticket_spec.rb +0 -136
- data/spec/live/topic_comment_spec.rb +0 -52
- data/spec/live/topic_spec.rb +0 -46
- data/spec/live/topic_subscription_spec.rb +0 -12
- data/spec/live/topic_vote_spec.rb +0 -13
- data/spec/live/trigger_spec.rb +0 -23
- data/spec/live/upload_spec.rb +0 -10
- data/spec/live/user_field_spec.rb +0 -12
- data/spec/live/user_spec.rb +0 -71
- data/spec/live/user_view_spec.rb +0 -16
- data/spec/live/view_spec.rb +0 -22
- data/spec/live/voice/phone_number_spec.rb +0 -13
- data/spec/macros/resource_macros.rb +0 -169
- data/zendesk_api.gemspec +0 -38
@@ -1,901 +0,0 @@
|
|
1
|
-
require 'core/spec_helper'
|
2
|
-
|
3
|
-
describe ZendeskAPI::Collection do
|
4
|
-
subject do
|
5
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource)
|
6
|
-
end
|
7
|
-
|
8
|
-
context "initialization" do
|
9
|
-
it "should set the resource class" do
|
10
|
-
expect(subject.instance_variable_get(:@resource_class)).to eq(ZendeskAPI::TestResource)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should initially be empty" do
|
14
|
-
expect(subject.instance_variable_defined?(:@resources)).to be(false)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "with array option passed in" do
|
19
|
-
subject { ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource, :ids => [1, 2, 3, 4]) }
|
20
|
-
|
21
|
-
it "should join array with commas" do
|
22
|
-
expect(subject.instance_variable_get(:@options)[:ids]).to eq("1,2,3,4")
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context "deferral" do
|
27
|
-
it "should defer #create_many! to the resource class" do
|
28
|
-
collection = ZendeskAPI::Collection.new(client, ZendeskAPI::BulkTestResource)
|
29
|
-
stub_json_request(:post, %r{bulk_test_resources/create_many$}, json(:job_status => {}))
|
30
|
-
collection.create_many!([{:name => 'Mick'}, {:name => 'Steven'}])
|
31
|
-
assert_requested(:post, %r{bulk_test_resources/create_many$},
|
32
|
-
:body => {
|
33
|
-
:bulk_test_resources => [{:name => 'Mick'}, {:name => 'Steven'}]
|
34
|
-
}
|
35
|
-
)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should defer #destroy_many! to the resource class" do
|
39
|
-
collection = ZendeskAPI::Collection.new(client, ZendeskAPI::BulkTestResource)
|
40
|
-
stub_json_request(:delete, %r{bulk_test_resources/destroy_many\?}, json(:job_status => {}))
|
41
|
-
collection.destroy_many!([1,2,3])
|
42
|
-
assert_requested(:delete, %r{bulk_test_resources/destroy_many\?ids=1,2,3$})
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should defer #update_many! to the resource class" do
|
46
|
-
collection = ZendeskAPI::Collection.new(client, ZendeskAPI::BulkTestResource)
|
47
|
-
stub_json_request(:put, %r{bulk_test_resources/update_many\?}, json(:job_status => {}))
|
48
|
-
collection.update_many!([1,2,3], {:name => 'Mick'})
|
49
|
-
assert_requested(:put, %r{bulk_test_resources/update_many\?ids=1,2,3$})
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should defer #create to the resource class" do
|
53
|
-
stub_json_request(:post, %r{test_resources$}, json(:test_resource => {}))
|
54
|
-
subject.create
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should defer #find to the resource class" do
|
58
|
-
stub_json_request(:get, %r{test_resources/1$}, json(:test_resource => {}))
|
59
|
-
subject.find(:id => 1)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should defer #destroy to the resource class" do
|
63
|
-
stub_json_request(:delete, %r{test_resources/1$}, json(:test_resource => {}))
|
64
|
-
subject.destroy(:id => 1)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should defer #update to the resource class" do
|
68
|
-
stub_json_request(:put, %r{test_resources/1$}, json(:test_resource => {}))
|
69
|
-
subject.update(:id => 1)
|
70
|
-
end
|
71
|
-
|
72
|
-
context "when class doesn't have method" do
|
73
|
-
subject do
|
74
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::NilDataResource)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should raise NoMethodError" do
|
78
|
-
expect { subject.create }.to raise_error(NoMethodError)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should raise NoMethodError" do
|
82
|
-
expect { subject.create! }.to raise_error(NoMethodError)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "with a class with a parent" do
|
87
|
-
let(:association) do
|
88
|
-
ZendeskAPI::Association.new(:class => ZendeskAPI::TestResource::TestChild,
|
89
|
-
:parent => ZendeskAPI::TestResource.new(client, :id => 1), :name => :children)
|
90
|
-
end
|
91
|
-
|
92
|
-
subject do
|
93
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource::TestChild,
|
94
|
-
:association => association)
|
95
|
-
end
|
96
|
-
|
97
|
-
before(:each) do
|
98
|
-
stub_json_request(:any, %r{/test_resources/\d+/child}, json("test_child" => {}))
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should defer #create to the resource class with the parent id" do
|
102
|
-
subject.create
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should defer #destroy the resource class with the parent id" do
|
106
|
-
subject.destroy(:id => 1)
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should defer #find to the resource class with the parent id" do
|
110
|
-
subject.find(:id => 1)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should defer #update to the resource class with the parent id" do
|
114
|
-
subject.update(:id => 1)
|
115
|
-
end
|
116
|
-
|
117
|
-
context "on object push" do
|
118
|
-
before(:each) do
|
119
|
-
stub_json_request(:get, %r{test_resources/\d+/children}, json(:test_children => []))
|
120
|
-
subject << { :id => 1 }
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should pass association" do
|
124
|
-
expect(subject.last.association).to eq(association)
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should #build a resource and add it" do
|
128
|
-
resource = subject.build
|
129
|
-
expect(subject).to include(resource)
|
130
|
-
expect(resource.association).to eq(subject.association)
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should #build! a resource and add it" do
|
134
|
-
resource = subject.build!
|
135
|
-
expect(subject).to include(resource)
|
136
|
-
expect(resource.association).to eq(subject.association)
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
context "pagination with data" do
|
143
|
-
before(:each) do
|
144
|
-
stub_json_request(:get, %r{test_resources}, json(
|
145
|
-
:test_resources => [{ :id => 1 }]
|
146
|
-
))
|
147
|
-
subject.fetch(true)
|
148
|
-
end
|
149
|
-
|
150
|
-
context "on #page" do
|
151
|
-
context "with nil" do
|
152
|
-
before(:each) { subject.page(nil) }
|
153
|
-
|
154
|
-
it "should not empty the cache" do
|
155
|
-
expect(subject.instance_variable_get(:@resources)).to_not be_empty
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
context "with a number" do
|
160
|
-
before(:each) { subject.page(3) }
|
161
|
-
|
162
|
-
it "should empty the cache" do
|
163
|
-
expect(subject.instance_variable_get(:@resources)).to be_nil
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context "on #per_page" do
|
169
|
-
context "with nil" do
|
170
|
-
before(:each) { subject.per_page(nil) }
|
171
|
-
|
172
|
-
it "should not empty the cache" do
|
173
|
-
expect(subject.instance_variable_get(:@resources)).to_not be_empty
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
context "with a number" do
|
178
|
-
before(:each) { subject.per_page(20) }
|
179
|
-
|
180
|
-
it "should empty the cache" do
|
181
|
-
expect(subject.instance_variable_get(:@resources)).to be_nil
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
context "pagination with no options and no data" do
|
188
|
-
it "should return an empty array on #next" do
|
189
|
-
expect(subject.next).to be_empty
|
190
|
-
end
|
191
|
-
|
192
|
-
it "should return an empty array on #prev" do
|
193
|
-
expect(subject.prev).to be_empty
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
context "pagination with options and no data" do
|
198
|
-
before(:each) { subject.per_page(5).page(2) }
|
199
|
-
|
200
|
-
it "should set per_page option" do
|
201
|
-
expect(subject.per_page(10)).to eq(subject)
|
202
|
-
expect(subject.instance_variable_get(:@options)["per_page"]).to eq(10)
|
203
|
-
end
|
204
|
-
|
205
|
-
it "should set page option" do
|
206
|
-
expect(subject.page(10)).to eq(subject)
|
207
|
-
expect(subject.instance_variable_get(:@options)["page"]).to eq(10)
|
208
|
-
end
|
209
|
-
|
210
|
-
it "should increate page option" do
|
211
|
-
expect(subject.next).to eq(3)
|
212
|
-
end
|
213
|
-
|
214
|
-
it "should decreate page option" do
|
215
|
-
expect(subject.prev).to eq(1)
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
context "all" do
|
220
|
-
context "Faraday errors" do
|
221
|
-
before(:each) do
|
222
|
-
stub_json_request(:get, %r{test_resources$}, json(
|
223
|
-
:test_resources => [{:id => 1}], :next_page => "/test_resources?page=2"
|
224
|
-
))
|
225
|
-
|
226
|
-
stub_request(:get, %r{test_resources\?page=2}).to_return(:status => 500).then.to_return(
|
227
|
-
:headers => { :content_type => "application/json" }, :status => 200,
|
228
|
-
:body => json(:test_resources => [{:id => 2}], :next_page => "/test_resources?page=3"))
|
229
|
-
|
230
|
-
stub_request(:get, %r{test_resources\?page=3}).to_return(:status => 404)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "should retry from the same page" do
|
234
|
-
class SearchError < Exception; end
|
235
|
-
|
236
|
-
expect do |b|
|
237
|
-
client.insert_callback do |env|
|
238
|
-
if env[:status] == 500 && env[:url].request_uri =~ /test_resources/
|
239
|
-
raise SearchError
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
begin
|
244
|
-
silence_logger { subject.all(&b) }
|
245
|
-
rescue SearchError
|
246
|
-
retry
|
247
|
-
end
|
248
|
-
end.to yield_successive_args(
|
249
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
250
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 2]
|
251
|
-
)
|
252
|
-
end
|
253
|
-
|
254
|
-
it "should retry from the same page!" do
|
255
|
-
expect do |b|
|
256
|
-
begin
|
257
|
-
subject.all!(&b)
|
258
|
-
rescue ZendeskAPI::Error::NetworkError
|
259
|
-
retry
|
260
|
-
rescue ZendeskAPI::Error::ClientError
|
261
|
-
end
|
262
|
-
end.to yield_successive_args(
|
263
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
264
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 2]
|
265
|
-
)
|
266
|
-
end
|
267
|
-
|
268
|
-
it "raises an ArgumentError without a block (all)" do
|
269
|
-
expect do |b|
|
270
|
-
subject.all
|
271
|
-
end.to raise_error(ArgumentError)
|
272
|
-
end
|
273
|
-
|
274
|
-
it "raises an ArgumentError without a block (all!)" do
|
275
|
-
expect do |b|
|
276
|
-
subject.all!
|
277
|
-
end.to raise_error(ArgumentError)
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
context "requests with no next_page" do
|
282
|
-
before(:each) do
|
283
|
-
stub_json_request(:get, %r{test_resources$}, json(
|
284
|
-
:test_resources => [{:id => 1}],
|
285
|
-
:next_page => "/test_resources?page=2"
|
286
|
-
))
|
287
|
-
|
288
|
-
stub_json_request(:get, %r{test_resources\?page=2}, json(
|
289
|
-
:test_resources => [{:id => 2}]
|
290
|
-
))
|
291
|
-
end
|
292
|
-
|
293
|
-
it "should yield resource and page" do
|
294
|
-
expect do |b|
|
295
|
-
silence_logger { subject.all(&b) }
|
296
|
-
end.to yield_successive_args(
|
297
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
298
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 2]
|
299
|
-
)
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
context "incremental requests" do
|
304
|
-
subject do
|
305
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource, :path => 'exports/test_resources?start_time=0')
|
306
|
-
end
|
307
|
-
|
308
|
-
before(:each) do
|
309
|
-
stub_json_request(:get, %r{exports/test_resources\?start_time=0$}, json(
|
310
|
-
:test_resources => [{:id => 1}],
|
311
|
-
:next_page => "/exports/test_resources?start_time=200"
|
312
|
-
))
|
313
|
-
|
314
|
-
stub_json_request(:get, %r{exports/test_resources\?start_time=200$}, json(
|
315
|
-
:test_resources => [{:id => 2}],
|
316
|
-
:next_page => "/exports/test_resources?start_time=200"
|
317
|
-
))
|
318
|
-
end
|
319
|
-
|
320
|
-
it "should yield resource and page (and not infinitely loop)" do
|
321
|
-
expect do |b|
|
322
|
-
Timeout.timeout(5) do
|
323
|
-
silence_logger { subject.all(&b) }
|
324
|
-
end
|
325
|
-
end.to yield_successive_args(
|
326
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
327
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 1] # page defaults to 1
|
328
|
-
)
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
context "infinite loops" do
|
333
|
-
before(:each) do
|
334
|
-
stub_json_request(:get, %r{test_resources$}, json(
|
335
|
-
:test_resources => [{:id => 1}],
|
336
|
-
:next_page => "/test_resources?page=2"
|
337
|
-
))
|
338
|
-
|
339
|
-
stub_json_request(:get, %r{/test_resources\?page=2$}, json(
|
340
|
-
:test_resources => [{:id => 2}],
|
341
|
-
:next_page => "/test_resources?page=2"
|
342
|
-
))
|
343
|
-
end
|
344
|
-
|
345
|
-
xit "should yield resource and page (and not infinitely loop)" do
|
346
|
-
expect do |b|
|
347
|
-
Timeout.timeout(5) do
|
348
|
-
silence_logger { subject.all(&b) }
|
349
|
-
end
|
350
|
-
end.to yield_successive_args(
|
351
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
352
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 2]
|
353
|
-
)
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
context "successful requests" do
|
358
|
-
before(:each) do
|
359
|
-
stub_json_request(:get, %r{test_resources$}, json(
|
360
|
-
:test_resources => [{:id => 1}],
|
361
|
-
:next_page => "/test_resources?page=2"
|
362
|
-
))
|
363
|
-
|
364
|
-
stub_json_request(:get, %r{test_resources\?page=2}, json(
|
365
|
-
:test_resources => [{:id => 2}],
|
366
|
-
:next_page => "/test_resources?page=3"
|
367
|
-
))
|
368
|
-
|
369
|
-
stub_request(:get, %r{test_resources\?page=3}).to_return(:status => 404)
|
370
|
-
end
|
371
|
-
|
372
|
-
it "should yield resource if arity == 1" do
|
373
|
-
expect do |block|
|
374
|
-
# Needed to make sure the arity == 1
|
375
|
-
block.instance_eval do
|
376
|
-
def to_proc
|
377
|
-
@used = true
|
378
|
-
|
379
|
-
probe = self
|
380
|
-
Proc.new do |arg|
|
381
|
-
probe.num_yields += 1
|
382
|
-
probe.yielded_args << [arg]
|
383
|
-
end
|
384
|
-
end
|
385
|
-
end
|
386
|
-
|
387
|
-
silence_logger { subject.all(&block) }
|
388
|
-
end.to yield_successive_args(
|
389
|
-
ZendeskAPI::TestResource.new(client, :id => 1),
|
390
|
-
ZendeskAPI::TestResource.new(client, :id => 2)
|
391
|
-
)
|
392
|
-
end
|
393
|
-
|
394
|
-
it "should yield resource and page" do
|
395
|
-
expect do |b|
|
396
|
-
silence_logger { subject.all(&b) }
|
397
|
-
end.to yield_successive_args(
|
398
|
-
[ZendeskAPI::TestResource.new(client, :id => 1), 1],
|
399
|
-
[ZendeskAPI::TestResource.new(client, :id => 2), 2]
|
400
|
-
)
|
401
|
-
end
|
402
|
-
|
403
|
-
context "afterwards" do
|
404
|
-
before(:each) do
|
405
|
-
silence_logger { subject.all {|_|} }
|
406
|
-
end
|
407
|
-
|
408
|
-
it "should reset the collection" do
|
409
|
-
expect(subject.first_page?).to be(true)
|
410
|
-
expect(subject.fetch).to eq([ZendeskAPI::TestResource.new(client, :id => 1)])
|
411
|
-
end
|
412
|
-
end
|
413
|
-
end
|
414
|
-
end
|
415
|
-
|
416
|
-
context "fetch" do
|
417
|
-
context "grabbing the current page" do
|
418
|
-
context "from next_page" do
|
419
|
-
before(:each) do
|
420
|
-
stub_json_request(:get, %r{test_resources}, json(
|
421
|
-
:test_resources => [{:id => 2}],
|
422
|
-
:next_page => "/test_resources?page=2"
|
423
|
-
))
|
424
|
-
|
425
|
-
subject.fetch(true)
|
426
|
-
@page = subject.instance_variable_get(:@options)["page"]
|
427
|
-
end
|
428
|
-
|
429
|
-
it "should set the page to 1" do
|
430
|
-
expect(@page).to eq(1)
|
431
|
-
end
|
432
|
-
end
|
433
|
-
|
434
|
-
context "from prev_page" do
|
435
|
-
before(:each) do
|
436
|
-
stub_json_request(:get, %r{test_resources}, json(
|
437
|
-
:test_resources => [{:id => 2}],
|
438
|
-
:previous_page => "/test_resources?page=1"
|
439
|
-
))
|
440
|
-
|
441
|
-
subject.fetch(true)
|
442
|
-
@page = subject.instance_variable_get(:@options)["page"]
|
443
|
-
end
|
444
|
-
|
445
|
-
it "should set the page to 2" do
|
446
|
-
expect(@page).to eq(2)
|
447
|
-
end
|
448
|
-
end
|
449
|
-
|
450
|
-
context "with nothing" do
|
451
|
-
before(:each) do
|
452
|
-
stub_json_request(:get, %r{test_resources}, json(:test_resources => [{:id => 2}]))
|
453
|
-
subject.fetch(true)
|
454
|
-
@page = subject.instance_variable_get(:@options)["page"]
|
455
|
-
end
|
456
|
-
|
457
|
-
it "should not set the page" do
|
458
|
-
expect(@page).to be_nil
|
459
|
-
end
|
460
|
-
end
|
461
|
-
end
|
462
|
-
|
463
|
-
it "does not fetch if associated is a new record" do
|
464
|
-
expect(ZendeskAPI::Category.new(client).forums.fetch).to eq([])
|
465
|
-
expect(ZendeskAPI::Category.new(client).forums.to_a).to eq([])
|
466
|
-
end
|
467
|
-
|
468
|
-
context "with client error" do
|
469
|
-
before(:each) do
|
470
|
-
stub_request(:get, %r{test_resources}).to_return(:status => 500)
|
471
|
-
end
|
472
|
-
|
473
|
-
it "should properly be handled" do
|
474
|
-
silence_logger { expect(subject.fetch(true)).to be_empty }
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
context "with an invalid model key expectation" do
|
479
|
-
before(:each) do
|
480
|
-
stub_json_request(:get, %r{test_resources}, json(
|
481
|
-
:test_resource_stuff => [{:id => 2}],
|
482
|
-
:next_page => "/test_resources?page=2"
|
483
|
-
))
|
484
|
-
end
|
485
|
-
|
486
|
-
it "should properly be handled" do
|
487
|
-
expect(subject.fetch(true)).to be_empty
|
488
|
-
end
|
489
|
-
end
|
490
|
-
|
491
|
-
context "with nil body" do
|
492
|
-
before(:each) do
|
493
|
-
stub_request(:get, %r{test_resources}).to_return(:status => 200)
|
494
|
-
end
|
495
|
-
|
496
|
-
it "should properly be handled" do
|
497
|
-
silence_logger { expect(subject.fetch(true)).to be_empty }
|
498
|
-
end
|
499
|
-
end
|
500
|
-
|
501
|
-
context "with unfetchable resource" do
|
502
|
-
subject { ZendeskAPI::Collection.new(client, ZendeskAPI::NilResource) }
|
503
|
-
|
504
|
-
it "should not call connection" do
|
505
|
-
expect(client.connection).to_not receive(:get)
|
506
|
-
expect(subject.fetch(true)).to be_empty
|
507
|
-
end
|
508
|
-
end
|
509
|
-
end
|
510
|
-
|
511
|
-
context "save" do
|
512
|
-
let(:options) { { :abc => 1 } }
|
513
|
-
before(:each) do
|
514
|
-
stub_json_request(:get, %r{test_resources}, json(:test_resources => []))
|
515
|
-
subject.clear_cache
|
516
|
-
end
|
517
|
-
|
518
|
-
context "with a hash" do
|
519
|
-
let(:object) { double('ZendeskAPI::TestResource', :changes => [:xxx], :changed? => true, :destroyed? => false) }
|
520
|
-
|
521
|
-
it "should call create with those options" do
|
522
|
-
expect(ZendeskAPI::TestResource).to receive(:new).
|
523
|
-
with(client, options.merge(:association => subject.association)).
|
524
|
-
and_return(object)
|
525
|
-
|
526
|
-
subject << options
|
527
|
-
|
528
|
-
expect(object).to receive(:save)
|
529
|
-
subject.save
|
530
|
-
|
531
|
-
expect(subject).to include(object)
|
532
|
-
end
|
533
|
-
end
|
534
|
-
|
535
|
-
context "with a new object" do
|
536
|
-
let(:object) { ZendeskAPI::TestResource.new(client, options) }
|
537
|
-
before(:each) do
|
538
|
-
subject << object
|
539
|
-
end
|
540
|
-
|
541
|
-
it "should save object" do
|
542
|
-
expect(object).to receive(:save)
|
543
|
-
subject.save
|
544
|
-
end
|
545
|
-
|
546
|
-
it "should have object in collection" do
|
547
|
-
expect(subject).to include(object)
|
548
|
-
end
|
549
|
-
end
|
550
|
-
|
551
|
-
context "with everything else" do
|
552
|
-
it "should pass to new, since this is how attachment handles it" do
|
553
|
-
attachment = double(:changes => [:xxx], :changed? => true, :destroyed? => false)
|
554
|
-
expect(ZendeskAPI::TestResource).to receive(:new).
|
555
|
-
with(client, :id => "img.jpg", :association => instance_of(ZendeskAPI::Association)).
|
556
|
-
and_return attachment
|
557
|
-
|
558
|
-
subject << "img.jpg"
|
559
|
-
|
560
|
-
expect(attachment).to receive(:save)
|
561
|
-
subject.save
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
context "with a destroyed object" do
|
566
|
-
let(:object) { ZendeskAPI::TestResource.new(client, options) }
|
567
|
-
|
568
|
-
before(:each) do
|
569
|
-
subject << object
|
570
|
-
end
|
571
|
-
|
572
|
-
it "should not save object" do
|
573
|
-
expect(object).to receive(:destroyed?).and_return(true)
|
574
|
-
expect(object).to_not receive(:save)
|
575
|
-
|
576
|
-
subject.save
|
577
|
-
end
|
578
|
-
|
579
|
-
it "should have object in collection" do
|
580
|
-
expect(subject).to include(object)
|
581
|
-
end
|
582
|
-
end
|
583
|
-
end
|
584
|
-
|
585
|
-
context "without real data" do
|
586
|
-
subject do
|
587
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::User)
|
588
|
-
end
|
589
|
-
|
590
|
-
before(:each) do
|
591
|
-
stub_json_request(:get, %r{users\?page=2}, json(
|
592
|
-
:users => [{:id => 2}],
|
593
|
-
:next_page => "/users?page=3&per_page=1",
|
594
|
-
:previous_page => "/users?page=1&per_page=1"
|
595
|
-
))
|
596
|
-
|
597
|
-
subject.per_page(1).page(2)
|
598
|
-
subject.fetch(true)
|
599
|
-
end
|
600
|
-
|
601
|
-
context "pagination with no options" do
|
602
|
-
before(:each) do
|
603
|
-
stub_json_request(:get, %r{users\?page=(1|3)}, json(:users => [{:id => 3}]))
|
604
|
-
|
605
|
-
subject.per_page(nil).page(nil)
|
606
|
-
end
|
607
|
-
|
608
|
-
it "should find the next page by calling fetch" do
|
609
|
-
current = subject.to_a.dup
|
610
|
-
nxt = subject.next
|
611
|
-
|
612
|
-
expect(nxt.size).to eq(1)
|
613
|
-
expect(nxt).to_not eq(current)
|
614
|
-
end
|
615
|
-
|
616
|
-
it "should find the prev page by calling fetch" do
|
617
|
-
current = subject.to_a.dup
|
618
|
-
prev = subject.prev
|
619
|
-
|
620
|
-
expect(prev.size).to eq(1)
|
621
|
-
expect(prev).to_not eq(current)
|
622
|
-
end
|
623
|
-
end
|
624
|
-
|
625
|
-
context "pagination with options", :vcr do
|
626
|
-
before(:each) { subject.per_page(1).page(2) }
|
627
|
-
|
628
|
-
it "should increase page option and not call fetch" do
|
629
|
-
expect(subject.next).to eq(3)
|
630
|
-
end
|
631
|
-
|
632
|
-
it "should decrease page option and not call fetch" do
|
633
|
-
expect(subject.prev).to eq(1)
|
634
|
-
end
|
635
|
-
|
636
|
-
context "with page == 1" do
|
637
|
-
before do
|
638
|
-
subject.page(1)
|
639
|
-
subject.clear_cache
|
640
|
-
expect(subject).to_not receive(:fetch)
|
641
|
-
end
|
642
|
-
|
643
|
-
it "should do nothing on #prev" do
|
644
|
-
expect(subject.prev).to eq([])
|
645
|
-
end
|
646
|
-
end
|
647
|
-
end
|
648
|
-
end
|
649
|
-
|
650
|
-
context "side loading" do
|
651
|
-
before(:each) do
|
652
|
-
subject.include(:nil_resources)
|
653
|
-
end
|
654
|
-
|
655
|
-
context "singular id on resource" do
|
656
|
-
before(:each) do
|
657
|
-
ZendeskAPI::TestResource.has ZendeskAPI::NilResource
|
658
|
-
|
659
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
660
|
-
:test_resources => [{ :id => 1, :nil_resource_id => 4 }],
|
661
|
-
:nil_resources => [{ :id => 1, :name => :bye }, { :id => 4, :name => :hi }]
|
662
|
-
))
|
663
|
-
|
664
|
-
subject.fetch(true)
|
665
|
-
|
666
|
-
@resource = subject.detect {|res| res.id == 1}
|
667
|
-
end
|
668
|
-
|
669
|
-
it "should side load nil_resources" do
|
670
|
-
expect(@resource.nil_resource).to_not be_nil
|
671
|
-
end
|
672
|
-
|
673
|
-
it "should side load the correct nil_resource" do
|
674
|
-
expect(@resource.nil_resource.name).to eq("hi")
|
675
|
-
end
|
676
|
-
end
|
677
|
-
|
678
|
-
|
679
|
-
context "multiple resources" do
|
680
|
-
before(:each) do
|
681
|
-
ZendeskAPI::TestResource.has ZendeskAPI::NilResource
|
682
|
-
|
683
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
684
|
-
:test_resources => [{ :id => 1, :nil_resource_id => 4 }, { :id => 2, :nil_resource_id => 1 }],
|
685
|
-
:nil_resources => [{ :id => 1, :name => :bye }, { :id => 4, :name => :hi }]
|
686
|
-
))
|
687
|
-
|
688
|
-
subject.fetch(true)
|
689
|
-
|
690
|
-
end
|
691
|
-
|
692
|
-
context "first resource" do
|
693
|
-
before(:each) { @resource = subject.detect {|res| res.id == 1} }
|
694
|
-
|
695
|
-
it "should side load nil_resources" do
|
696
|
-
expect(@resource.nil_resource).to_not be_nil
|
697
|
-
end
|
698
|
-
|
699
|
-
it "should side load the correct nil_resource" do
|
700
|
-
expect(@resource.nil_resource.name).to eq("hi")
|
701
|
-
end
|
702
|
-
end
|
703
|
-
|
704
|
-
context "second resource" do
|
705
|
-
before(:each) { @resource = subject.detect {|res| res.id == 2} }
|
706
|
-
|
707
|
-
it "should side load nil_resources" do
|
708
|
-
expect(@resource.nil_resource).to_not be_nil
|
709
|
-
end
|
710
|
-
|
711
|
-
it "should side load the correct nil_resource" do
|
712
|
-
expect(@resource.nil_resource.name).to eq("bye")
|
713
|
-
end
|
714
|
-
end
|
715
|
-
end
|
716
|
-
|
717
|
-
context "plural ids on resource" do
|
718
|
-
before(:each) do
|
719
|
-
ZendeskAPI::TestResource.has_many ZendeskAPI::NilResource
|
720
|
-
|
721
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
722
|
-
:test_resources => [{ :id => 1, :nil_resource_ids => [1, 4] }],
|
723
|
-
:nil_resources => [{ :id => 1, :name => :hi }, { :id => 4, :name => :hello }, { :id => 5, :name => :goodbye }]
|
724
|
-
))
|
725
|
-
|
726
|
-
subject.fetch(true)
|
727
|
-
|
728
|
-
@resource = subject.detect {|res| res.id == 1}
|
729
|
-
end
|
730
|
-
|
731
|
-
it "should side load nil_resources" do
|
732
|
-
expect(@resource.nil_resources).to_not be_empty
|
733
|
-
end
|
734
|
-
|
735
|
-
it "should side load the correct nil_resources" do
|
736
|
-
expect(@resource.nil_resources.map(&:name)).to eq(%w{hi hello})
|
737
|
-
end
|
738
|
-
end
|
739
|
-
|
740
|
-
context "ids in side load" do
|
741
|
-
before(:each) do
|
742
|
-
ZendeskAPI::TestResource.has_many ZendeskAPI::NilResource
|
743
|
-
|
744
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
745
|
-
:test_resources => [{ :id => 1 }],
|
746
|
-
:nil_resources => [{ :id => 1, :test_resource_id => 2 }, { :id => 2, :test_resource_id => 1 }, { :id => 4, :test_resource_id => 1 }]
|
747
|
-
))
|
748
|
-
|
749
|
-
subject.fetch(true)
|
750
|
-
@resource = subject.detect {|res| res.id == 1}
|
751
|
-
end
|
752
|
-
|
753
|
-
it "should side load nil_resources" do
|
754
|
-
expect(@resource.nil_resources).to_not be_empty
|
755
|
-
end
|
756
|
-
|
757
|
-
it "should side load the correct nil_resources" do
|
758
|
-
expect(@resource.nil_resources.map(&:id)).to eq([2, 4])
|
759
|
-
end
|
760
|
-
end
|
761
|
-
|
762
|
-
context "id in side load" do
|
763
|
-
before(:each) do
|
764
|
-
ZendeskAPI::TestResource.has ZendeskAPI::NilResource
|
765
|
-
|
766
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
767
|
-
:test_resources => [{ :id => 1 }],
|
768
|
-
:nil_resources => [{ :id => 1, :test_resource_id => 2 }, { :id => 2, :test_resource_id => 1 }]
|
769
|
-
))
|
770
|
-
|
771
|
-
subject.fetch(true)
|
772
|
-
@resource = subject.detect {|res| res.id == 1}
|
773
|
-
end
|
774
|
-
|
775
|
-
it "should side load nil_resources" do
|
776
|
-
expect(@resource.nil_resource).to_not be_nil
|
777
|
-
end
|
778
|
-
|
779
|
-
it "should side load the correct nil_resources" do
|
780
|
-
expect(@resource.nil_resource.id).to eq(2)
|
781
|
-
end
|
782
|
-
end
|
783
|
-
|
784
|
-
context "with name as key" do
|
785
|
-
before(:each) do
|
786
|
-
ZendeskAPI::TestResource.has ZendeskAPI::NilResource, :include_key => :name
|
787
|
-
|
788
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
789
|
-
:test_resources => [{ :id => 1, :nil_resource_id => 4 }],
|
790
|
-
:nil_resources => [{ :name => 1 }, { :name => 4 }]
|
791
|
-
))
|
792
|
-
|
793
|
-
subject.fetch(true)
|
794
|
-
|
795
|
-
@resource = subject.detect {|res| res.id == 1}
|
796
|
-
end
|
797
|
-
|
798
|
-
it "should side load nil_resources" do
|
799
|
-
expect(@resource.nil_resource).to_not be_nil
|
800
|
-
end
|
801
|
-
|
802
|
-
it "should side load the correct nil_resource" do
|
803
|
-
expect(@resource.nil_resource.name).to eq(4)
|
804
|
-
end
|
805
|
-
end
|
806
|
-
|
807
|
-
context "sub-loading" do
|
808
|
-
before(:each) do
|
809
|
-
ZendeskAPI::TestResource.has ZendeskAPI::TestResource::TestChild
|
810
|
-
ZendeskAPI::TestResource::TestChild.has ZendeskAPI::NilResource
|
811
|
-
|
812
|
-
stub_json_request(:get, %r{test_resources\?include=nil_resources}, json(
|
813
|
-
:test_resources => [{ :id => 1, :test_child => { :nil_resource_id => 4 } }],
|
814
|
-
:nil_resources => [{ :id => 1 }, { :id => 4 }]
|
815
|
-
))
|
816
|
-
|
817
|
-
subject.fetch(true)
|
818
|
-
|
819
|
-
@resource = subject.detect {|res| res.id == 1}.test_child
|
820
|
-
end
|
821
|
-
|
822
|
-
it "should side load nil_resources" do
|
823
|
-
expect(@resource.nil_resource).to_not be_nil
|
824
|
-
end
|
825
|
-
|
826
|
-
it "should side load the correct nil_resource" do
|
827
|
-
expect(@resource.nil_resource.id).to eq(4)
|
828
|
-
end
|
829
|
-
end
|
830
|
-
end
|
831
|
-
|
832
|
-
context "method missing" do
|
833
|
-
before(:each) { allow(subject).to receive(:fetch).and_return([1, 2, nil, 3]) }
|
834
|
-
|
835
|
-
context "with an class method on the resource class" do
|
836
|
-
it "should pass methods to class if defined" do
|
837
|
-
expect(subject.test).to eq("hi")
|
838
|
-
end
|
839
|
-
end
|
840
|
-
|
841
|
-
it "should pass all methods not defined to resources" do
|
842
|
-
expect(subject.compact).to eq([1, 2, 3])
|
843
|
-
end
|
844
|
-
|
845
|
-
it "should take a block" do
|
846
|
-
expect(subject.map {|i| i.to_i + 1}).to eq([2, 3, 1, 4])
|
847
|
-
end
|
848
|
-
|
849
|
-
it "should create a new collection if it isn't an array method" do
|
850
|
-
expect(subject.recent).to be_instance_of(ZendeskAPI::Collection)
|
851
|
-
end
|
852
|
-
|
853
|
-
it "should pass the correct query_path to the new collection" do
|
854
|
-
expect(subject.recent.instance_variable_get(:@collection_path).last).to eq(:recent)
|
855
|
-
end
|
856
|
-
end
|
857
|
-
|
858
|
-
context "with a module (Search)" do
|
859
|
-
subject { ZendeskAPI::Collection.new(client, ZendeskAPI::Search, :query => "hello") }
|
860
|
-
|
861
|
-
before(:each) do
|
862
|
-
stub_json_request(:get, %r{search\?query=hello}, json(:results => []))
|
863
|
-
end
|
864
|
-
|
865
|
-
it "should not blow up" do
|
866
|
-
expect(subject.to_a).to eq([])
|
867
|
-
end
|
868
|
-
end
|
869
|
-
|
870
|
-
context "with different path" do
|
871
|
-
subject do
|
872
|
-
ZendeskAPI::Collection.new(client, ZendeskAPI::TestResource, :collection_path => ["test_resources", "active"])
|
873
|
-
end
|
874
|
-
|
875
|
-
before(:each) do
|
876
|
-
stub_json_request(:post, %r{test_resources/active})
|
877
|
-
end
|
878
|
-
|
879
|
-
context "deferral" do
|
880
|
-
it "should defer #create to the resource class with proper path" do
|
881
|
-
subject.create
|
882
|
-
end
|
883
|
-
end
|
884
|
-
|
885
|
-
context "resources" do
|
886
|
-
before(:each) do
|
887
|
-
stub_json_request(:get, %r{test_resources/active},
|
888
|
-
json(:test_resources => [{ :id => 1 }]))
|
889
|
-
|
890
|
-
subject.fetch
|
891
|
-
|
892
|
-
stub_json_request(:put, %r{test_resources/1})
|
893
|
-
end
|
894
|
-
|
895
|
-
it "should not save using the collection path" do
|
896
|
-
resource = subject.first
|
897
|
-
resource.save
|
898
|
-
end
|
899
|
-
end
|
900
|
-
end
|
901
|
-
end
|