4me-sdk 1.1.6 → 2.0.0.pre.rc.2

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,270 +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, 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: %w[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')
136
- end
137
-
138
- it 'should allow multiple keys' do
139
- 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')
140
51
  end
141
52
 
142
- it 'should allow nils when using multiple keys' do
143
- 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')
144
56
  end
145
- end
146
57
 
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'])
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)
150
61
  end
151
62
 
152
- it 'should allow multiple keys' do
153
- 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"))
154
65
  end
155
66
 
156
- it 'should allow nils when using multiple keys' do
157
- 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
158
127
  end
159
- end
160
- end
161
-
162
- context 'size' do
163
- it 'should return 1 for single records' do
164
- expect(@response_hash.size).to eq(1)
165
- end
166
128
 
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
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')
176
132
 
177
- context 'count' do
178
- it 'should return 1 for single records' do
179
- expect(@response_hash.count).to eq(1)
180
- 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
181
137
 
182
- it 'should return the array size for list records' do
183
- expect(@response_array.count).to eq(3)
184
- 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
185
143
 
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
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
191
149
 
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
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
203
179
 
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
180
+ context 'size' do
181
+ it 'should return 1 for single records' do
182
+ expect(@response_hash.size).to eq(1)
183
+ end
207
184
 
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
185
+ it 'should return the array size for list records' do
186
+ expect(@response_array.size).to eq(3)
187
+ end
211
188
 
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
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
215
194
 
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
195
+ context 'count' do
196
+ it 'should return 1 for single records' do
197
+ expect(@response_hash.count).to eq(1)
198
+ end
219
199
 
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|
200
+ it 'should return the array size for list records' do
201
+ expect(@response_array.count).to eq(3)
202
+ end
223
203
 
224
- it "should define pagination link for :#{relation}" do
225
- 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
226
208
  end
227
- end
228
209
 
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)
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
235
253
  end
236
- end
237
- end
238
-
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
250
254
 
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
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
257
273
 
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
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
262
278
 
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')
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
267
285
  end
268
286
  end
269
-
270
287
  end