4me-sdk 1.1.5 → 2.0.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,271 +1,287 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Sdk4me::Response do
4
- before(:each) do
5
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
6
- @person_hash = {
7
- addresses:[],
8
- contacts:[ {id: 1365, label: 'work', telephone: '7139872946'} ],
9
- id: 562,
10
- information: 'Info about John.',
11
- job_title: 'rolling stone',
12
- locale: 'en-US',
13
- location: 'Top of John Hill',
14
- name: 'John',
15
- organization: {id: 20, name: 'SDK4ME Institute'},
16
- picture_uri: nil,
17
- primary_email: 'john@example.com',
18
- site: {id:14, name: 'IT Training Facility'},
19
- time_format_24h: false,
20
- time_zone: 'Central Time (US & Canada)'
21
- }
22
- stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(body: @person_hash.to_json)
23
- @response_hash = @client.get('me')
24
-
25
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
26
- @people_array = [
27
- {id: 562, name: 'John', organization: { id: 20, name: 'SDK4ME Institute'}, site: {id: 14, name: 'IT Training Facility'} },
28
- {id: 560, name: 'Lucas', organization: { id: 20, name: 'SDK4ME Institute', office: { name: 'The Office'}}, site: {id: 14, name: 'IT Training Facility'} },
29
- {id: 561, name: 'Sheryl', organization: { id: 20, name: 'SDK4ME Institute'}, site: {id: 14, name: 'IT Training Facility'} }
30
- ]
31
- stub_request(:get, 'https://api.4me.com/v1/people').to_return(body: @people_array.to_json).with(basic_auth: ['secret', 'x'])
32
- @response_array = @client.get('people')
33
- end
34
-
35
- it 'should contain the request' do
36
- expect(@response_hash.request.class.name).to eq('Net::HTTP::Get')
37
- expect(@response_hash.request.path).to eq('/v1/me')
38
- end
39
-
40
- it 'should contain the full request' do
41
- expect(@response_hash.response.class.name).to eq('Net::HTTPOK')
42
- expect(@response_hash.response).to respond_to(:body)
43
- end
44
-
45
- it 'should provide easy access to the body' do
46
- expect(@response_hash.body).to include(%("primary_email":"john@example.com"))
47
- end
48
-
49
- context 'json/message' do
50
- it 'should provide the JSON value for single records' do
51
- be_json_eql(@response_hash.json, @person_hash)
52
- end
53
-
54
- it 'should provide the JSON value for lists' do
55
- be_json_eql(@response_array.json, @people_array)
56
- end
57
-
58
- it 'should provide indifferent access for single records' do
59
- expect(@response_hash.json['organization']['name']).to eq('SDK4ME Institute')
60
- expect(@response_hash.json[:organization][:name]).to eq('SDK4ME Institute')
61
- expect(@response_hash.json[:organization]['name']).to eq('SDK4ME Institute')
62
- expect(@response_hash.json['organization'][:name]).to eq('SDK4ME Institute')
63
- end
64
-
65
- it 'should provide indifferent access for lists' do
66
- expect(@response_array.json.first['site']['name']).to eq('IT Training Facility')
67
- expect(@response_array.json.first[:site][:name]).to eq('IT Training Facility')
68
- expect(@response_array.json.last[:site]['name']).to eq('IT Training Facility')
69
- expect(@response_array.json.last['site'][:name]).to eq('IT Training Facility')
70
- end
71
-
72
- it 'should add a message if the body is empty' do
73
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: nil)
74
- response = @client.get('organizations')
75
-
76
- message = '429: empty body'
77
- expect(response.json[:message]).to eq(message)
78
- expect(response.json['message']).to eq(message)
79
- expect(response.message).to eq(message)
80
- end
81
-
82
- it 'should add a message if the HTTP response is not OK' do
83
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json)
84
- response = @client.get('organizations')
85
-
86
- message = '429: Too Many Requests'
87
- expect(response.json[:message]).to eq(message)
88
- expect(response.json['message']).to eq(message)
89
- expect(response.message).to eq(message)
90
- end
91
-
92
- it 'should add a message if the JSON body cannot be parsed' do
93
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(body: '==$$!invalid')
94
- response = @client.get('organizations')
95
-
96
- message = "unexpected token at '==$$!invalid' for:\n#{response.body}"
97
- expect(response.json[:message]).to include(message)
98
- expect(response.json['message']).to include(message)
99
- expect(response.message).to include(message)
100
- end
101
-
102
- it 'should have a blank message when single record is succesfully retrieved' do
103
- expect(@response_hash.message).to be_nil
104
- end
105
-
106
- it 'should have a blank message when single record is succesfully retrieved' do
107
- expect(@response_array.message).to be_nil
4
+ def client(authentication)
5
+ (@client ||= {})[authentication] ||= begin
6
+ if authentication == :api_token
7
+ Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1, block_at_rate_limit: false)
8
+ else
9
+ Sdk4me::Client.new(access_token: 'secret', max_retry_time: -1, block_at_rate_limit: false)
10
+ end
108
11
  end
109
-
110
- end
111
-
112
- it 'should define empty' do
113
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: nil)
114
- response = @client.get('organizations')
115
-
116
- expect(response.empty?).to be_truthy
117
- expect(@person_hash.empty?).to be_falsey
118
- expect(@people_array.empty?).to be_falsey
119
12
  end
120
13
 
121
- context 'valid' do
122
- it 'should be valid when the message is nil' do
123
- expect(@response_hash).to receive(:message){ nil }
124
- expect(@response_hash.valid?).to be_truthy
125
- end
126
-
127
- it 'should not be valid when the message is not nil' do
128
- expect(@response_array).to receive(:message){ 'invalid' }
129
- expect(@response_array.valid?).to be_falsey
14
+ def credentials(authentication)
15
+ if authentication == :api_token
16
+ { basic_auth: %w[secret x] }
17
+ else
18
+ { headers: { 'Authorization' => 'Bearer secret' } }
130
19
  end
131
20
  end
132
21
 
133
- context '[] access' do
134
- context 'single records' do
135
- it 'should delegate [] to the json' do
136
- expect(@response_hash[:name]).to eq('John')
137
- end
138
-
139
- it 'should allow multiple keys' do
140
- expect(@response_hash[:organization, 'name']).to eq('SDK4ME Institute')
22
+ %i[api_token access_token].each do |authentication|
23
+ context "#{authentication} - " do
24
+ before(:each) do
25
+ @person_hash = {
26
+ addresses: [],
27
+ contacts: [{ id: 1365, label: 'work', telephone: '7139872946' }],
28
+ id: 562,
29
+ information: 'Info about John.',
30
+ job_title: 'rolling stone',
31
+ locale: 'en-US',
32
+ location: 'Top of John Hill',
33
+ name: 'John',
34
+ organization: { id: 20, name: 'SDK4ME Institute' },
35
+ picture_uri: nil,
36
+ primary_email: 'john@example.com',
37
+ site: { id: 14, name: 'IT Training Facility' },
38
+ time_format_24h: false,
39
+ time_zone: 'Central Time (US & Canada)'
40
+ }
41
+ stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: @person_hash.to_json)
42
+ @response_hash = client(authentication).get('me')
43
+
44
+ @people_array = [
45
+ { id: 562, name: 'John', organization: { id: 20, name: 'SDK4ME Institute' }, site: { id: 14, name: 'IT Training Facility' } },
46
+ { id: 560, name: 'Lucas', organization: { id: 20, name: 'SDK4ME Institute', office: { name: 'The Office' } }, site: { id: 14, name: 'IT Training Facility' } },
47
+ { id: 561, name: 'Sheryl', organization: { id: 20, name: 'SDK4ME Institute' }, site: { id: 14, name: 'IT Training Facility' } }
48
+ ]
49
+ stub_request(:get, 'https://api.4me.com/v1/people').to_return(body: @people_array.to_json).with(credentials(authentication))
50
+ @response_array = client(authentication).get('people')
141
51
  end
142
52
 
143
- it 'should allow nils when using multiple keys' do
144
- expect(@response_hash[:organization, :missing, 'name']).to be_nil
53
+ it 'should contain the request' do
54
+ expect(@response_hash.request.class.name).to eq('Net::HTTP::Get')
55
+ expect(@response_hash.request.path).to eq('/v1/me')
145
56
  end
146
- end
147
57
 
148
- context 'list of records' do
149
- it 'should delegate [] to the json of each record' do
150
- expect(@response_array['name']).to eq(['John', 'Lucas', 'Sheryl'])
58
+ it 'should contain the full request' do
59
+ expect(@response_hash.response.class.name).to eq('Net::HTTPOK')
60
+ expect(@response_hash.response).to respond_to(:body)
151
61
  end
152
62
 
153
- it 'should allow multiple keys' do
154
- expect(@response_array[:organization, 'name']).to eq(['SDK4ME Institute', 'SDK4ME Institute', 'SDK4ME Institute'])
63
+ it 'should provide easy access to the body' do
64
+ expect(@response_hash.body).to include(%("primary_email":"john@example.com"))
155
65
  end
156
66
 
157
- it 'should allow nils when using multiple keys' do
158
- expect(@response_array[:organization, :office, 'name']).to eq([nil, 'The Office', nil])
67
+ context 'json/message' do
68
+ it 'should provide the JSON value for single records' do
69
+ be_json_eql(@response_hash.json, @person_hash)
70
+ end
71
+
72
+ it 'should provide the JSON value for lists' do
73
+ be_json_eql(@response_array.json, @people_array)
74
+ end
75
+
76
+ it 'should provide indifferent access for single records' do
77
+ expect(@response_hash.json['organization']['name']).to eq('SDK4ME Institute')
78
+ expect(@response_hash.json[:organization][:name]).to eq('SDK4ME Institute')
79
+ expect(@response_hash.json[:organization]['name']).to eq('SDK4ME Institute')
80
+ expect(@response_hash.json['organization'][:name]).to eq('SDK4ME Institute')
81
+ end
82
+
83
+ it 'should provide indifferent access for lists' do
84
+ expect(@response_array.json.first['site']['name']).to eq('IT Training Facility')
85
+ expect(@response_array.json.first[:site][:name]).to eq('IT Training Facility')
86
+ expect(@response_array.json.last[:site]['name']).to eq('IT Training Facility')
87
+ expect(@response_array.json.last['site'][:name]).to eq('IT Training Facility')
88
+ end
89
+
90
+ it 'should add a message if the body is empty' do
91
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
92
+ response = client(authentication).get('organizations')
93
+
94
+ message = '429: empty body'
95
+ expect(response.json[:message]).to eq(message)
96
+ expect(response.json['message']).to eq(message)
97
+ expect(response.message).to eq(message)
98
+ end
99
+
100
+ it 'should add a message if the HTTP response is not OK' do
101
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: { message: 'Too Many Requests' }.to_json)
102
+ response = client(authentication).get('organizations')
103
+
104
+ message = '429: Too Many Requests'
105
+ expect(response.json[:message]).to eq(message)
106
+ expect(response.json['message']).to eq(message)
107
+ expect(response.message).to eq(message)
108
+ end
109
+
110
+ it 'should add a message if the JSON body cannot be parsed' do
111
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(body: '==$$!invalid')
112
+ response = client(authentication).get('organizations')
113
+
114
+ message = "unexpected token at '==$$!invalid' for:\n#{response.body}"
115
+ expect(response.json[:message]).to include(message)
116
+ expect(response.json['message']).to include(message)
117
+ expect(response.message).to include(message)
118
+ end
119
+
120
+ it 'should have a blank message when single record is succesfully retrieved' do
121
+ expect(@response_hash.message).to be_nil
122
+ end
123
+
124
+ it 'should have a blank message when single record is succesfully retrieved' do
125
+ expect(@response_array.message).to be_nil
126
+ end
159
127
  end
160
- end
161
- end
162
-
163
- context 'size' do
164
- it 'should return 1 for single records' do
165
- expect(@response_hash.size).to eq(1)
166
- end
167
128
 
168
- it 'should return the array size for list records' do
169
- expect(@response_array.size).to eq(3)
170
- end
171
-
172
- it 'should return nil if an error message is present' do
173
- expect(@response_hash).to receive(:message){ 'error message' }
174
- expect(@response_hash.size).to eq(0)
175
- end
176
- end
129
+ it 'should define empty' do
130
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
131
+ response = client(authentication).get('organizations')
177
132
 
178
- context 'count' do
179
- it 'should return 1 for single records' do
180
- expect(@response_hash.count).to eq(1)
181
- end
133
+ expect(response.empty?).to be_truthy
134
+ expect(@person_hash.empty?).to be_falsey
135
+ expect(@people_array.empty?).to be_falsey
136
+ end
182
137
 
183
- it 'should return the array size for list records' do
184
- expect(@response_array.count).to eq(3)
185
- end
138
+ context 'valid' do
139
+ it 'should be valid when the message is nil' do
140
+ expect(@response_hash).to receive(:message) { nil }
141
+ expect(@response_hash.valid?).to be_truthy
142
+ end
186
143
 
187
- it 'should return nil if an error message is present' do
188
- expect(@response_hash).to receive(:message){ 'error message' }
189
- expect(@response_hash.count).to eq(0)
190
- end
191
- end
144
+ it 'should not be valid when the message is not nil' do
145
+ expect(@response_array).to receive(:message) { 'invalid' }
146
+ expect(@response_array.valid?).to be_falsey
147
+ end
148
+ end
192
149
 
193
- context 'pagination' do
194
- before(:each) do
195
- @pagination_header = {
196
- 'X-Pagination-Per-Page' => 3,
197
- 'X-Pagination-Current-Page' => 1,
198
- 'X-Pagination-Total-Pages' => 2,
199
- 'X-Pagination-Total-Entries' => 5,
200
- 'Link' => '<https://api.4me.com/v1/people?page=1&per_page=3>; rel="first",<https://api.4me.com/v1/people?page=2&per_page=3>; rel="next", <https://api.4me.com/v1/people?page=2&per_page=3>; rel="last"',
201
- }
202
- allow(@response_array.response).to receive('header'){ @pagination_header }
203
- end
150
+ context '[] access' do
151
+ context 'single records' do
152
+ it 'should delegate [] to the json' do
153
+ expect(@response_hash[:name]).to eq('John')
154
+ end
155
+
156
+ it 'should allow multiple keys' do
157
+ expect(@response_hash[:organization, 'name']).to eq('SDK4ME Institute')
158
+ end
159
+
160
+ it 'should allow nils when using multiple keys' do
161
+ expect(@response_hash[:organization, :missing, 'name']).to be_nil
162
+ end
163
+ end
164
+
165
+ context 'list of records' do
166
+ it 'should delegate [] to the json of each record' do
167
+ expect(@response_array['name']).to eq(%w[John Lucas Sheryl])
168
+ end
169
+
170
+ it 'should allow multiple keys' do
171
+ expect(@response_array[:organization, 'name']).to eq(['SDK4ME Institute', 'SDK4ME Institute', 'SDK4ME Institute'])
172
+ end
173
+
174
+ it 'should allow nils when using multiple keys' do
175
+ expect(@response_array[:organization, :office, 'name']).to eq([nil, 'The Office', nil])
176
+ end
177
+ end
178
+ end
204
179
 
205
- it "should retrieve per_page from the 'X-Pagination-Per-Page' header" do
206
- expect(@response_array.per_page).to eq(3)
207
- end
180
+ context 'size' do
181
+ it 'should return 1 for single records' do
182
+ expect(@response_hash.size).to eq(1)
183
+ end
208
184
 
209
- it "should retrieve current_page from the 'X-Pagination-Current-Page' header" do
210
- expect(@response_array.current_page).to eq(1)
211
- end
185
+ it 'should return the array size for list records' do
186
+ expect(@response_array.size).to eq(3)
187
+ end
212
188
 
213
- it "should retrieve total_pages from the 'X-Pagination-Total-Pages' header" do
214
- expect(@response_array.total_pages).to eq(2)
215
- end
189
+ it 'should return nil if an error message is present' do
190
+ expect(@response_hash).to receive(:message) { 'error message' }
191
+ expect(@response_hash.size).to eq(0)
192
+ end
193
+ end
216
194
 
217
- it "should retrieve total_entries from the 'X-Pagination-Total-Entries' header" do
218
- expect(@response_array.total_entries).to eq(5)
219
- end
195
+ context 'count' do
196
+ it 'should return 1 for single records' do
197
+ expect(@response_hash.count).to eq(1)
198
+ end
220
199
 
221
- {first: 'https://api.4me.com/v1/people?page=1&per_page=3',
222
- next: 'https://api.4me.com/v1/people?page=2&per_page=3',
223
- last: 'https://api.4me.com/v1/people?page=2&per_page=3'}.each do |relation, link|
200
+ it 'should return the array size for list records' do
201
+ expect(@response_array.count).to eq(3)
202
+ end
224
203
 
225
- it "should define pagination link for :#{relation}" do
226
- expect(@response_array.pagination_link(relation)).to eq(link)
204
+ it 'should return nil if an error message is present' do
205
+ expect(@response_hash).to receive(:message) { 'error message' }
206
+ expect(@response_hash.count).to eq(0)
207
+ end
227
208
  end
228
- end
229
209
 
230
- {first: '/v1/people?page=1&per_page=3',
231
- next: '/v1/people?page=2&per_page=3',
232
- last: '/v1/people?page=2&per_page=3'}.each do |relation, link|
233
-
234
- it "should define pagination relative link for :#{relation}" do
235
- expect(@response_array.pagination_relative_link(relation)).to eq(link)
210
+ context 'pagination' do
211
+ before(:each) do
212
+ @pagination_header = {
213
+ 'X-Pagination-Per-Page' => 3,
214
+ 'X-Pagination-Current-Page' => 1,
215
+ 'X-Pagination-Total-Pages' => 2,
216
+ 'X-Pagination-Total-Entries' => 5,
217
+ 'Link' => '<https://api.4me.com/v1/people?page=1&per_page=3>; rel="first",<https://api.4me.com/v1/people?page=2&per_page=3>; rel="next", <https://api.4me.com/v1/people?page=2&per_page=3>; rel="last"'
218
+ }
219
+ allow(@response_array.response).to receive('header') { @pagination_header }
220
+ end
221
+
222
+ it "should retrieve per_page from the 'X-Pagination-Per-Page' header" do
223
+ expect(@response_array.per_page).to eq(3)
224
+ end
225
+
226
+ it "should retrieve current_page from the 'X-Pagination-Current-Page' header" do
227
+ expect(@response_array.current_page).to eq(1)
228
+ end
229
+
230
+ it "should retrieve total_pages from the 'X-Pagination-Total-Pages' header" do
231
+ expect(@response_array.total_pages).to eq(2)
232
+ end
233
+
234
+ it "should retrieve total_entries from the 'X-Pagination-Total-Entries' header" do
235
+ expect(@response_array.total_entries).to eq(5)
236
+ end
237
+
238
+ { first: 'https://api.4me.com/v1/people?page=1&per_page=3',
239
+ next: 'https://api.4me.com/v1/people?page=2&per_page=3',
240
+ last: 'https://api.4me.com/v1/people?page=2&per_page=3' }.each do |relation, link|
241
+ it "should define pagination link for :#{relation}" do
242
+ expect(@response_array.pagination_link(relation)).to eq(link)
243
+ end
244
+ end
245
+
246
+ { first: '/v1/people?page=1&per_page=3',
247
+ next: '/v1/people?page=2&per_page=3',
248
+ last: '/v1/people?page=2&per_page=3' }.each do |relation, link|
249
+ it "should define pagination relative link for :#{relation}" do
250
+ expect(@response_array.pagination_relative_link(relation)).to eq(link)
251
+ end
252
+ end
236
253
  end
237
- end
238
- end
239
-
240
- context 'throttled?' do
241
- it 'should not be trhottled by default' do
242
- expect(@response_hash.throttled?).to be_falsey
243
- expect(@response_array.throttled?).to be_falsey
244
- end
245
-
246
- it 'should check the return code' do
247
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: nil)
248
- response = @client.get('organizations')
249
- expect(response.throttled?).to be_truthy
250
- end
251
254
 
252
- it 'should check the return message' do
253
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 500, body: {message: 'Too Many Requests'}.to_json )
254
- response = @client.get('organizations')
255
- expect(response.throttled?).to be_truthy
256
- end
257
- end
255
+ context 'throttled?' do
256
+ it 'should not be trhottled by default' do
257
+ expect(@response_hash.throttled?).to be_falsey
258
+ expect(@response_array.throttled?).to be_falsey
259
+ end
260
+
261
+ it 'should check the return code' do
262
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
263
+ response = client(authentication).get('organizations')
264
+ expect(response.throttled?).to be_truthy
265
+ end
266
+
267
+ it 'should check the return message' do
268
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 500, body: { message: 'Too Many Requests' }.to_json)
269
+ response = client(authentication).get('organizations')
270
+ expect(response.throttled?).to be_truthy
271
+ end
272
+ end
258
273
 
259
- context 'to_s' do
260
- it 'should return the JSON as a string' do
261
- expect(@response_hash.to_s).to eq(JSON.parse(@person_hash.to_json).to_s)
262
- end
274
+ context 'to_s' do
275
+ it 'should return the JSON as a string' do
276
+ expect(@response_hash.to_s).to eq(JSON.parse(@person_hash.to_json).to_s)
277
+ end
263
278
 
264
- it 'should return the message in case the response is not valid' do
265
- stub_request(:get, 'https://api.4me.com/v1/organizations').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: nil)
266
- response = @client.get('organizations')
267
- expect(response.to_s).to eq('429: empty body')
279
+ it 'should return the message in case the response is not valid' do
280
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
281
+ response = client(authentication).get('organizations')
282
+ expect(response.to_s).to eq('429: empty body')
283
+ end
284
+ end
268
285
  end
269
286
  end
270
-
271
287
  end