4me-sdk 1.1.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,271 +1,291 @@
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 = "Invalid JSON - 765: unexpected token at '==$$!invalid' for:\n#{response.body}"
97
- expect(response.json[:message]).to eq(message)
98
- expect(response.json['message']).to eq(message)
99
- expect(response.message).to eq(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: ['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')
22
+ [: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')
137
51
  end
138
52
 
139
- it 'should allow multiple keys' do
140
- expect(@response_hash[:organization, 'name']).to eq('SDK4ME Institute')
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')
141
56
  end
142
57
 
143
- it 'should allow nils when using multiple keys' do
144
- expect(@response_hash[:organization, :missing, 'name']).to be_nil
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)
145
61
  end
146
- end
147
62
 
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'])
63
+ it 'should provide easy access to the body' do
64
+ expect(@response_hash.body).to include(%("primary_email":"john@example.com"))
151
65
  end
152
66
 
153
- it 'should allow multiple keys' do
154
- expect(@response_array[:organization, 'name']).to eq(['SDK4ME Institute', 'SDK4ME Institute', 'SDK4ME Institute'])
155
- end
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
156
127
 
157
- it 'should allow nils when using multiple keys' do
158
- expect(@response_array[:organization, :office, 'name']).to eq([nil, 'The Office', nil])
159
128
  end
160
- end
161
- end
162
129
 
163
- context 'size' do
164
- it 'should return 1 for single records' do
165
- expect(@response_hash.size).to eq(1)
166
- end
130
+ it 'should define empty' do
131
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
132
+ response = client(authentication).get('organizations')
167
133
 
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
134
+ expect(response.empty?).to be_truthy
135
+ expect(@person_hash.empty?).to be_falsey
136
+ expect(@people_array.empty?).to be_falsey
137
+ end
177
138
 
178
- context 'count' do
179
- it 'should return 1 for single records' do
180
- expect(@response_hash.count).to eq(1)
181
- end
139
+ context 'valid' do
140
+ it 'should be valid when the message is nil' do
141
+ expect(@response_hash).to receive(:message){ nil }
142
+ expect(@response_hash.valid?).to be_truthy
143
+ end
182
144
 
183
- it 'should return the array size for list records' do
184
- expect(@response_array.count).to eq(3)
185
- end
145
+ it 'should not be valid when the message is not nil' do
146
+ expect(@response_array).to receive(:message){ 'invalid' }
147
+ expect(@response_array.valid?).to be_falsey
148
+ end
149
+ end
186
150
 
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
151
+ context '[] access' do
152
+ context 'single records' do
153
+ it 'should delegate [] to the json' do
154
+ expect(@response_hash[:name]).to eq('John')
155
+ end
156
+
157
+ it 'should allow multiple keys' do
158
+ expect(@response_hash[:organization, 'name']).to eq('SDK4ME Institute')
159
+ end
160
+
161
+ it 'should allow nils when using multiple keys' do
162
+ expect(@response_hash[:organization, :missing, 'name']).to be_nil
163
+ end
164
+ end
165
+
166
+ context 'list of records' do
167
+ it 'should delegate [] to the json of each record' do
168
+ expect(@response_array['name']).to eq(['John', 'Lucas', 'Sheryl'])
169
+ end
170
+
171
+ it 'should allow multiple keys' do
172
+ expect(@response_array[:organization, 'name']).to eq(['SDK4ME Institute', 'SDK4ME Institute', 'SDK4ME Institute'])
173
+ end
174
+
175
+ it 'should allow nils when using multiple keys' do
176
+ expect(@response_array[:organization, :office, 'name']).to eq([nil, 'The Office', nil])
177
+ end
178
+ end
179
+ end
192
180
 
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
181
+ context 'size' do
182
+ it 'should return 1 for single records' do
183
+ expect(@response_hash.size).to eq(1)
184
+ end
204
185
 
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
186
+ it 'should return the array size for list records' do
187
+ expect(@response_array.size).to eq(3)
188
+ end
208
189
 
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
190
+ it 'should return nil if an error message is present' do
191
+ expect(@response_hash).to receive(:message){ 'error message' }
192
+ expect(@response_hash.size).to eq(0)
193
+ end
194
+ end
212
195
 
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
196
+ context 'count' do
197
+ it 'should return 1 for single records' do
198
+ expect(@response_hash.count).to eq(1)
199
+ end
216
200
 
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
201
+ it 'should return the array size for list records' do
202
+ expect(@response_array.count).to eq(3)
203
+ end
220
204
 
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|
224
-
225
- it "should define pagination link for :#{relation}" do
226
- expect(@response_array.pagination_link(relation)).to eq(link)
205
+ it 'should return nil if an error message is present' do
206
+ expect(@response_hash).to receive(:message){ 'error message' }
207
+ expect(@response_hash.count).to eq(0)
208
+ end
227
209
  end
228
- end
229
210
 
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)
211
+ context 'pagination' do
212
+ before(:each) do
213
+ @pagination_header = {
214
+ 'X-Pagination-Per-Page' => 3,
215
+ 'X-Pagination-Current-Page' => 1,
216
+ 'X-Pagination-Total-Pages' => 2,
217
+ 'X-Pagination-Total-Entries' => 5,
218
+ '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"',
219
+ }
220
+ allow(@response_array.response).to receive('header'){ @pagination_header }
221
+ end
222
+
223
+ it "should retrieve per_page from the 'X-Pagination-Per-Page' header" do
224
+ expect(@response_array.per_page).to eq(3)
225
+ end
226
+
227
+ it "should retrieve current_page from the 'X-Pagination-Current-Page' header" do
228
+ expect(@response_array.current_page).to eq(1)
229
+ end
230
+
231
+ it "should retrieve total_pages from the 'X-Pagination-Total-Pages' header" do
232
+ expect(@response_array.total_pages).to eq(2)
233
+ end
234
+
235
+ it "should retrieve total_entries from the 'X-Pagination-Total-Entries' header" do
236
+ expect(@response_array.total_entries).to eq(5)
237
+ end
238
+
239
+ {first: 'https://api.4me.com/v1/people?page=1&per_page=3',
240
+ next: 'https://api.4me.com/v1/people?page=2&per_page=3',
241
+ last: 'https://api.4me.com/v1/people?page=2&per_page=3'}.each do |relation, link|
242
+
243
+ it "should define pagination link for :#{relation}" do
244
+ expect(@response_array.pagination_link(relation)).to eq(link)
245
+ end
246
+ end
247
+
248
+ {first: '/v1/people?page=1&per_page=3',
249
+ next: '/v1/people?page=2&per_page=3',
250
+ last: '/v1/people?page=2&per_page=3'}.each do |relation, link|
251
+
252
+ it "should define pagination relative link for :#{relation}" do
253
+ expect(@response_array.pagination_relative_link(relation)).to eq(link)
254
+ end
255
+ end
236
256
  end
237
- end
238
- end
239
257
 
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
258
+ context 'throttled?' do
259
+ it 'should not be trhottled by default' do
260
+ expect(@response_hash.throttled?).to be_falsey
261
+ expect(@response_array.throttled?).to be_falsey
262
+ end
263
+
264
+ it 'should check the return code' do
265
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
266
+ response = client(authentication).get('organizations')
267
+ expect(response.throttled?).to be_truthy
268
+ end
269
+
270
+ it 'should check the return message' do
271
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 500, body: {message: 'Too Many Requests'}.to_json )
272
+ response = client(authentication).get('organizations')
273
+ expect(response.throttled?).to be_truthy
274
+ end
275
+ end
251
276
 
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
277
+ context 'to_s' do
278
+ it 'should return the JSON as a string' do
279
+ expect(@response_hash.to_s).to eq(JSON.parse(@person_hash.to_json).to_s)
280
+ end
258
281
 
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
282
+ it 'should return the message in case the response is not valid' do
283
+ stub_request(:get, 'https://api.4me.com/v1/organizations').with(credentials(authentication)).to_return(status: 429, body: nil)
284
+ response = client(authentication).get('organizations')
285
+ expect(response.to_s).to eq('429: empty body')
286
+ end
287
+ end
263
288
 
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')
268
289
  end
269
290
  end
270
-
271
291
  end