4me-sdk 1.1.8 → 1.2.0

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