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.
@@ -13,7 +13,21 @@ describe 'ca-bundle.crt' do
13
13
  expect(response.valid?).to be_falsey
14
14
 
15
15
  # expecting 401 error
16
- expect(response.message).to eq('401: Access credentials required')
16
+ expect(response.message).to eq('401: Bad credentials')
17
+ end
18
+
19
+ it 'should be able to connect to the 4me API (access token)' do
20
+ WebMock.allow_net_connect!
21
+ client = Sdk4me::Client.new(access_token: 'invalid', max_retry_time: -1)
22
+ result = {}
23
+
24
+ # no exception concerning the certificate
25
+ expect { result[:response] = client.get('me') }.not_to raise_error
26
+ response = result[:response]
27
+ expect(response.valid?).to be_falsey
28
+
29
+ # expecting 401 error
30
+ expect(response.message).to eq('401: Bad credentials')
17
31
  end
18
32
 
19
33
  it 'should be able to connect to S3' do
@@ -2,582 +2,589 @@ require 'spec_helper'
2
2
 
3
3
  describe Sdk4me::Client do
4
4
 
5
- context 'Sdk4me.config' do
6
- before(:each) do
7
- Sdk4me.configure do |config|
8
- config.max_retry_time = 120 # override default value (5400)
9
- config.api_token = 'secret' # set value
5
+ def client(authentication, options = {})
6
+ (@client ||= {})["#{authentication}-#{options.to_s}"] ||= begin
7
+ options = {max_retry_time: -1}.merge(options)
8
+ if authentication == :api_token
9
+ options = {api_token: 'secret'}.merge(options)
10
+ else
11
+ options = {access_token: 'secret'}.merge(options)
10
12
  end
13
+ Sdk4me::Client.new(options)
11
14
  end
15
+ end
12
16
 
13
- it 'should define the MAX_PAGE_SIZE' do
14
- expect(Sdk4me::Client::MAX_PAGE_SIZE).to eq(100)
17
+ def credentials(authentication)
18
+ if authentication == :api_token
19
+ { basic_auth: ['secret', 'x'] }
20
+ else
21
+ { headers: {'Authorization' => 'Bearer secret'} }
15
22
  end
23
+ end
16
24
 
17
- it 'should use the Sdk4me configuration' do
18
- client = Sdk4me::Client.new
19
- expect(client.option(:host)).to eq('https://api.4me.com') # default value
20
- expect(client.option(:api_token)).to eq('secret') # value set using Sdk4me.config
21
- expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
22
- end
25
+ [:api_token, :access_token].each do |authentication|
23
26
 
24
- it 'should override the Sdk4me configuration' do
25
- client = Sdk4me::Client.new(host: 'https://demo.4me.com', api_token: 'unknown', block_at_rate_limit: true)
26
- expect(client.option(:read_timeout)).to eq(25) # default value
27
- expect(client.option(:host)).to eq('https://demo.4me.com') # default value overridden in Client.new
28
- expect(client.option(:api_token)).to eq('unknown') # value set using Sdk4me.config and overridden in Client.new
29
- expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
30
- expect(client.option(:block_at_rate_limit)).to eq(true) # value overridden in Client.new
31
- end
32
27
 
33
- [:host, :api_version, :api_token].each do |required_option|
34
- it "should require option #{required_option}" do
35
- expect { Sdk4me::Client.new(required_option => '') }.to raise_error("Missing required configuration option #{required_option}")
28
+ context 'Sdk4me.config' do
29
+ before(:each) do
30
+ Sdk4me.configure do |config|
31
+ config.max_retry_time = 120 # override default value (5400)
32
+ if authentication == :api_token
33
+ config.api_token = 'secret' # set value
34
+ else
35
+ config.access_token = 'secret'
36
+ end
37
+ end
36
38
  end
37
- end
38
39
 
39
- [ ['https://api.4me.com', true, 'api.4me.com', 443],
40
- ['https://api.example.com:777', true, 'api.example.com', 777],
41
- ['http://sdk4me.example.com', false, 'sdk4me.example.com', 80],
42
- ['http://sdk4me.example.com:777', false, 'sdk4me.example.com', 777]
43
- ].each do |host, ssl, domain, port|
44
- it 'should parse ssl, host and port' do
45
- client = Sdk4me::Client.new(host: host)
46
- expect(client.instance_variable_get(:@ssl)).to eq(ssl)
47
- expect(client.instance_variable_get(:@domain)).to eq(domain)
48
- expect(client.instance_variable_get(:@port)).to eq(port)
40
+ it 'should define the MAX_PAGE_SIZE' do
41
+ expect(Sdk4me::Client::MAX_PAGE_SIZE).to eq(100)
49
42
  end
50
- end
51
- end
52
-
53
- it 'should set the ca-bundle.crt file' do
54
- http = Net::HTTP.new('https://api.4me.com')
55
- http.use_ssl = true
56
-
57
- on_disk = `ls #{http.ca_file}`
58
- expect(on_disk).not_to match(/cannot access/)
59
- expect(on_disk).to match(/\/ca-bundle.crt$/)
60
- end
61
-
62
- describe 'headers' do
63
- before(:each) do
64
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
65
- end
66
-
67
- it 'should set the content type header' do
68
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).with(headers: {'Content-Type' => 'application/json'}).to_return(body: {name: 'my name'}.to_json)
69
- @client.get('me')
70
- expect(stub).to have_been_requested
71
- end
72
-
73
- it 'should add the X-4me-Account header' do
74
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1, account: 'test')
75
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).with(headers: {'X-4me-Account' => 'test'}).to_return(body: {name: 'my name'}.to_json)
76
- client.get('me')
77
- expect(stub).to have_been_requested
78
- end
79
-
80
- it 'should add the X-4me-Source header' do
81
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1, source: 'myapp')
82
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).with(headers: {'X-4me-Source' => 'myapp'}).to_return(body: {name: 'my name'}.to_json)
83
- client.get('me')
84
- expect(stub).to have_been_requested
85
- end
86
43
 
87
- it 'should be able to override headers' do
88
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).with(headers: {'Content-Type' => 'application/x-www-form-urlencoded'}).to_return(body: {name: 'my name'}.to_json)
89
- @client.get('me', {}, {'Content-Type' => 'application/x-www-form-urlencoded'})
90
- expect(stub).to have_been_requested
91
- end
92
-
93
- it 'should set the other headers' do
94
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).with(headers: {'X-4me-Other' => 'value'}).to_return(body: {name: 'my name'}.to_json)
95
- @client.get('me', {}, {'X-4me-Other' => 'value'})
96
- expect(stub).to have_been_requested
97
- end
44
+ it 'should use the Sdk4me configuration' do
45
+ client = Sdk4me::Client.new
46
+ expect(client.option(:host)).to eq('https://api.4me.com') # default value
47
+ if authentication == :api_token
48
+ expect(client.option(:api_token)).to eq('secret') # value set using Sdk4me.config
49
+ else
50
+ expect(client.option(:access_token)).to eq('secret') # value set using Sdk4me.config
51
+ end
52
+ expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
53
+ end
98
54
 
99
- it 'should accept headers in the each call' do
100
- stub = stub_request(:get, 'https://api.4me.com/v1/requests?fields=subject&page=1&per_page=100').with(basic_auth: ['secret', 'x']).with(headers: {'X-4me-Secret' => 'special'}).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}, {id: 3, subject: 'Subject 3'}].to_json)
101
- @client.each('requests', {fields: 'subject'}, {'X-4me-Secret' => 'special'}) do |request|
102
- expect(request[:subject]).to eq("Subject #{request[:id]}")
55
+ it 'should override the Sdk4me configuration' do
56
+ client = Sdk4me::Client.new(host: 'https://demo.4me.com', api_token: 'unknown', block_at_rate_limit: true)
57
+ expect(client.option(:read_timeout)).to eq(25) # default value
58
+ expect(client.option(:host)).to eq('https://demo.4me.com') # default value overridden in Client.new
59
+ expect(client.option(:api_token)).to eq('unknown') # value set using Sdk4me.config and overridden in Client.new
60
+ expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
61
+ expect(client.option(:block_at_rate_limit)).to eq(true) # value overridden in Client.new
103
62
  end
104
- expect(stub).to have_been_requested
105
- end
106
- end
107
63
 
108
- context 'each' do
109
- before(:each) do
110
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
111
- end
64
+ [:host, :api_version].each do |required_option|
65
+ it "should require option #{required_option}" do
66
+ expect { Sdk4me::Client.new(required_option => '') }.to raise_error("Missing required configuration option #{required_option}")
67
+ end
68
+ end
112
69
 
113
- it 'should yield each result' do
114
- stub_request(:get, 'https://api.4me.com/v1/requests?fields=subject&page=1&per_page=100').with(basic_auth: ['secret', 'x']).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}, {id: 3, subject: 'Subject 3'}].to_json)
115
- nr_of_requests = @client.each('requests', {fields: 'subject'}) do |request|
116
- expect(request[:subject]).to eq("Subject #{request[:id]}")
70
+ it 'should require access_token' do
71
+ expect { Sdk4me::Client.new(access_token: '', api_token: '') }.to raise_error('Missing required configuration option access_token')
117
72
  end
118
- expect(nr_of_requests).to eq(3)
119
- end
120
73
 
121
- it 'should retrieve multiple pages' do
122
- stub_page1 = stub_request(:get, 'https://api.4me.com/v1/requests?page=1&per_page=2').with(basic_auth: ['secret', 'x']).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}].to_json, headers: {'Link' => '<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="first",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="next",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="last"'})
123
- stub_page2 = stub_request(:get, 'https://api.4me.com/v1/requests?page=2&per_page=2').with(basic_auth: ['secret', 'x']).to_return(body: [{id: 3, subject: 'Subject 3'}].to_json, headers: {'Link' => '<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="first",<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="prev",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="last"'})
124
- nr_of_requests = @client.each('requests', {per_page: 2}) do |request|
125
- expect(request[:subject]).to eq("Subject #{request[:id]}")
74
+ [ ['https://api.4me.com', true, 'api.4me.com', 443],
75
+ ['https://api.example.com:777', true, 'api.example.com', 777],
76
+ ['http://sdk4me.example.com', false, 'sdk4me.example.com', 80],
77
+ ['http://sdk4me.example.com:777', false, 'sdk4me.example.com', 777]
78
+ ].each do |host, ssl, domain, port|
79
+ it 'should parse ssl, host and port' do
80
+ client = Sdk4me::Client.new(host: host)
81
+ expect(client.instance_variable_get(:@ssl)).to eq(ssl)
82
+ expect(client.instance_variable_get(:@domain)).to eq(domain)
83
+ expect(client.instance_variable_get(:@port)).to eq(port)
84
+ end
126
85
  end
127
- expect(nr_of_requests).to eq(3)
128
- expect(stub_page2).to have_been_requested
129
86
  end
130
- end
131
87
 
132
- context 'get' do
133
- before(:each) do
134
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
135
- end
88
+ it 'should set the ca-bundle.crt file' do
89
+ http = Net::HTTP.new('https://api.4me.com')
90
+ http.use_ssl = true
136
91
 
137
- it 'should return a response' do
138
- stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(body: {name: 'my name'}.to_json)
139
- response = @client.get('me')
140
- expect(response[:name]).to eq('my name')
92
+ on_disk = `ls #{http.ca_file}`
93
+ expect(on_disk).not_to match(/cannot access/)
94
+ expect(on_disk).to match(/\/ca-bundle.crt$/)
141
95
  end
142
96
 
143
- describe 'parameters' do
144
-
145
- [[nil, ''],
146
- [ 'normal', 'normal'],
147
- [ 'hello;<', 'hello%3B%3C'],
148
- [ true, 'true'],
149
- [ false, 'false'],
150
- [ DateTime.now, DateTime.now.new_offset(0).iso8601.gsub('+', '%2B')],
151
- [ Date.new, Date.new.strftime('%Y-%m-%d')],
152
- [ Time.now, Time.now.strftime('%H:%M')],
153
- [ ['first', 'second;<', true], 'first,second%3B%3C,true']
154
- ].each do |param_value, url_value|
155
- it "should cast #{param_value.class.name}: '#{param_value}' to '#{url_value}'" do
156
- stub = stub_request(:get, "https://api.4me.com/v1/me?value=#{url_value}").with(basic_auth: ['secret', 'x']).to_return(body: {name: 'my name'}.to_json)
157
- @client.get('me', {value: param_value})
158
- expect(stub).to have_been_requested
159
- end
97
+ describe 'headers' do
98
+ it 'should set the content type header' do
99
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: {'Content-Type' => 'application/json'}).to_return(body: {name: 'my name'}.to_json)
100
+ client(authentication).get('me')
101
+ expect(stub).to have_been_requested
160
102
  end
161
103
 
162
- it 'should not cast arrays in post and put calls' do
163
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
164
- stub = stub_request(:post, 'https://api.4me.com/v1/people').with(basic_auth: ['secret', 'x']).with(body: {user_ids: [1, 2, 3]}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
165
- client.post('people', {user_ids: [1, 2, 3]}, {'X-4me-Custom' => 'custom'})
104
+ it 'should add the X-4me-Account header' do
105
+ client = client(authentication, account: 'test')
106
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: {'X-4me-Account' => 'test'}).to_return(body: {name: 'my name'}.to_json)
107
+ client.get('me')
166
108
  expect(stub).to have_been_requested
167
109
  end
168
110
 
169
- it 'should not cast hashes in post and put calls' do
170
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
171
- stub = stub_request(:patch, 'https://api.4me.com/v1/people/55').with(basic_auth: ['secret', 'x']).with(body: '{"contacts_attributes":{"0":{"protocol":"email","label":"work","uri":"work@example.com"}}}', headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
172
- client.put('people/55', {contacts_attributes: {0 => {protocol: :email, label: :work, uri: 'work@example.com'}}}, {'X-4me-Custom' => 'custom'})
111
+ it 'should add the X-4me-Source header' do
112
+ client = client(authentication, source: 'myapp')
113
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: {'X-4me-Source' => 'myapp'}).to_return(body: {name: 'my name'}.to_json)
114
+ client.get('me')
173
115
  expect(stub).to have_been_requested
174
116
  end
175
117
 
176
- it 'should not double escape symbols' do
177
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
178
- stub = stub_request(:patch, 'https://api.4me.com/v1/people/55').with(basic_auth: ['secret', 'x']).with(body: '{"status":"waiting_for"}').to_return(body: {id: 101}.to_json)
179
- client.put('people/55', {status: :waiting_for})
118
+ it 'should be able to override headers' do
119
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: {'Content-Type' => 'application/x-www-form-urlencoded'}).to_return(body: {name: 'my name'}.to_json)
120
+ client(authentication).get('me', {}, {'Content-Type' => 'application/x-www-form-urlencoded'})
180
121
  expect(stub).to have_been_requested
181
122
  end
182
123
 
183
- it 'should handle fancy filter operations' do
184
- now = DateTime.now
185
- stub = stub_request(:get, "https://api.4me.com/v1/people?created_at=>#{now.new_offset(0).iso8601.gsub('+', '%2B')}&id!=15").with(basic_auth: ['secret', 'x']).to_return(body: {name: 'my name'}.to_json)
186
- @client.get('people', {'created_at=>' => now, 'id!=' => 15})
124
+ it 'should set the other headers' do
125
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: {'X-4me-Other' => 'value'}).to_return(body: {name: 'my name'}.to_json)
126
+ client(authentication).get('me', {}, {'X-4me-Other' => 'value'})
187
127
  expect(stub).to have_been_requested
188
128
  end
189
129
 
190
- it 'should append parameters' do
191
- stub = stub_request(:get, 'https://api.4me.com/v1/people?id!=15&primary_email=me@example.com').with(basic_auth: ['secret', 'x']).to_return(body: {name: 'my name'}.to_json)
192
- @client.get('people?id!=15', {primary_email: 'me@example.com'})
130
+ it 'should accept headers in the each call' do
131
+ stub = stub_request(:get, 'https://api.4me.com/v1/requests?fields=subject&page=1&per_page=100').with(credentials(authentication)).with(headers: {'X-4me-Secret' => 'special'}).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}, {id: 3, subject: 'Subject 3'}].to_json)
132
+ client(authentication).each('requests', {fields: 'subject'}, {'X-4me-Secret' => 'special'}) do |request|
133
+ expect(request[:subject]).to eq("Subject #{request[:id]}")
134
+ end
193
135
  expect(stub).to have_been_requested
194
136
  end
195
137
  end
196
- end
197
138
 
198
- context 'patch' do
199
- [:put, :patch].each do |method|
200
- it 'should send patch requests with parameters and headers for #{method} calls' do
201
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
202
- stub = stub_request(:patch, 'https://api.4me.com/v1/people/1').with(basic_auth: ['secret', 'x']).with(body: {name: 'New Name'}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 1}.to_json)
203
- client.send(method, 'people/1', {name: 'New Name'}, {'X-4me-Custom' => 'custom'})
204
- expect(stub).to have_been_requested
139
+ context 'each' do
140
+ it 'should yield each result' do
141
+ stub_request(:get, 'https://api.4me.com/v1/requests?fields=subject&page=1&per_page=100').with(credentials(authentication)).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}, {id: 3, subject: 'Subject 3'}].to_json)
142
+ nr_of_requests = client(authentication).each('requests', {fields: 'subject'}) do |request|
143
+ expect(request[:subject]).to eq("Subject #{request[:id]}")
144
+ end
145
+ expect(nr_of_requests).to eq(3)
205
146
  end
206
- end
207
- end
208
147
 
209
- context 'post' do
210
- it 'should send post requests with parameters and headers' do
211
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
212
- stub = stub_request(:post, 'https://api.4me.com/v1/people').with(basic_auth: ['secret', 'x']).with(body: {name: 'New Name'}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
213
- client.post('people', {name: 'New Name'}, {'X-4me-Custom' => 'custom'})
214
- expect(stub).to have_been_requested
148
+ it 'should retrieve multiple pages' do
149
+ stub_page1 = stub_request(:get, 'https://api.4me.com/v1/requests?page=1&per_page=2').with(credentials(authentication)).to_return(body: [{id: 1, subject: 'Subject 1'}, {id: 2, subject: 'Subject 2'}].to_json, headers: {'Link' => '<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="first",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="next",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="last"'})
150
+ stub_page2 = stub_request(:get, 'https://api.4me.com/v1/requests?page=2&per_page=2').with(credentials(authentication)).to_return(body: [{id: 3, subject: 'Subject 3'}].to_json, headers: {'Link' => '<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="first",<https://api.4me.com/v1/requests?page=1&per_page=2>; rel="prev",<https://api.4me.com/v1/requests?page=2&per_page=2>; rel="last"'})
151
+ nr_of_requests = client(authentication).each('requests', {per_page: 2}) do |request|
152
+ expect(request[:subject]).to eq("Subject #{request[:id]}")
153
+ end
154
+ expect(nr_of_requests).to eq(3)
155
+ expect(stub_page2).to have_been_requested
156
+ end
215
157
  end
216
- end
217
158
 
218
- context 'delete' do
219
- it 'should send delete requests with parameters and headers' do
220
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
221
- stub = stub_request(:delete, 'https://api.4me.com/v1/people?id=value').with(basic_auth: ['secret', 'x']).with(headers: {'X-4me-Custom' => 'custom'}).to_return(body: '', status: 204)
222
- response = client.delete('people', {id: 'value'}, {'X-4me-Custom' => 'custom'})
223
- expect(stub).to have_been_requested
224
- expect(response.valid?).to be_truthy
225
- expect(response.json).to eq({})
159
+ context 'get' do
160
+ it 'should return a response' do
161
+ stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: {name: 'my name'}.to_json)
162
+ response = client(authentication).get('me')
163
+ expect(response[:name]).to eq('my name')
164
+ end
165
+
166
+ describe 'parameters' do
167
+
168
+ [[nil, ''],
169
+ [ 'normal', 'normal'],
170
+ [ 'hello;<', 'hello%3B%3C'],
171
+ [ true, 'true'],
172
+ [ false, 'false'],
173
+ [ DateTime.now, DateTime.now.new_offset(0).iso8601.gsub('+', '%2B')],
174
+ [ Date.new, Date.new.strftime('%Y-%m-%d')],
175
+ [ Time.now, Time.now.strftime('%H:%M')],
176
+ [ ['first', 'second;<', true], 'first,second%3B%3C,true']
177
+ ].each do |param_value, url_value|
178
+ it "should cast #{param_value.class.name}: '#{param_value}' to '#{url_value}'" do
179
+ stub = stub_request(:get, "https://api.4me.com/v1/me?value=#{url_value}").with(credentials(authentication)).to_return(body: {name: 'my name'}.to_json)
180
+ client(authentication).get('me', {value: param_value})
181
+ expect(stub).to have_been_requested
182
+ end
183
+ end
184
+
185
+ it 'should not cast arrays in post and put calls' do
186
+ stub = stub_request(:post, 'https://api.4me.com/v1/people').with(credentials(authentication)).with(body: {user_ids: [1, 2, 3]}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
187
+ client(authentication).post('people', {user_ids: [1, 2, 3]}, {'X-4me-Custom' => 'custom'})
188
+ expect(stub).to have_been_requested
189
+ end
190
+
191
+ it 'should not cast hashes in post and put calls' do
192
+ stub = stub_request(:patch, 'https://api.4me.com/v1/people/55').with(credentials(authentication)).with(body: '{"contacts_attributes":{"0":{"protocol":"email","label":"work","uri":"work@example.com"}}}', headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
193
+ client(authentication).put('people/55', {contacts_attributes: {0 => {protocol: :email, label: :work, uri: 'work@example.com'}}}, {'X-4me-Custom' => 'custom'})
194
+ expect(stub).to have_been_requested
195
+ end
196
+
197
+ it 'should not double escape symbols' do
198
+ stub = stub_request(:patch, 'https://api.4me.com/v1/people/55').with(credentials(authentication)).with(body: '{"status":"waiting_for"}').to_return(body: {id: 101}.to_json)
199
+ client(authentication).put('people/55', {status: :waiting_for})
200
+ expect(stub).to have_been_requested
201
+ end
202
+
203
+ it 'should handle fancy filter operations' do
204
+ now = DateTime.now
205
+ stub = stub_request(:get, "https://api.4me.com/v1/people?created_at=>#{now.new_offset(0).iso8601.gsub('+', '%2B')}&id!=15").with(credentials(authentication)).to_return(body: {name: 'my name'}.to_json)
206
+ client(authentication).get('people', {'created_at=>' => now, 'id!=' => 15})
207
+ expect(stub).to have_been_requested
208
+ end
209
+
210
+ it 'should append parameters' do
211
+ stub = stub_request(:get, 'https://api.4me.com/v1/people?id!=15&primary_email=me@example.com').with(credentials(authentication)).to_return(body: {name: 'my name'}.to_json)
212
+ client(authentication).get('people?id!=15', {primary_email: 'me@example.com'})
213
+ expect(stub).to have_been_requested
214
+ end
215
+ end
226
216
  end
227
- end
228
217
 
229
- context 'attachments' do
230
- before(:each) do
231
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
218
+ context 'patch' do
219
+ [:put, :patch].each do |method|
220
+ it 'should send patch requests with parameters and headers for #{method} calls' do
221
+ stub = stub_request(:patch, 'https://api.4me.com/v1/people/1').with(credentials(authentication)).with(body: {name: 'New Name'}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 1}.to_json)
222
+ client(authentication).send(method, 'people/1', {name: 'New Name'}, {'X-4me-Custom' => 'custom'})
223
+ expect(stub).to have_been_requested
224
+ end
225
+ end
232
226
  end
233
227
 
234
- it 'should not log an error for XML responses' do
235
- xml = %(<?xml version="1.0" encoding="UTF-8"?>\n<details>some info</details>)
236
- stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(body: xml)
237
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
238
- expect_log("XML response:\n#{xml}", :debug)
239
- response = @client.get('me')
240
- expect(response.valid?).to be_falsey
241
- expect(response.raw.body).to eq(xml)
228
+ context 'post' do
229
+ it 'should send post requests with parameters and headers' do
230
+ stub = stub_request(:post, 'https://api.4me.com/v1/people').with(credentials(authentication)).with(body: {name: 'New Name'}, headers: {'X-4me-Custom' => 'custom'}).to_return(body: {id: 101}.to_json)
231
+ client(authentication).post('people', {name: 'New Name'}, {'X-4me-Custom' => 'custom'})
232
+ expect(stub).to have_been_requested
233
+ end
242
234
  end
243
235
 
244
- it 'should not log an error for redirects' do
245
- stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(body: '', status: 303, headers: {'Location' => 'http://redirect.example.com/to/here'})
246
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
247
- expect_log('Redirect: http://redirect.example.com/to/here', :debug)
248
- response = @client.get('me')
249
- expect(response.valid?).to be_falsey
250
- expect(response.raw.body).to be_nil
236
+ context 'delete' do
237
+ it 'should send delete requests with parameters and headers' do
238
+ stub = stub_request(:delete, 'https://api.4me.com/v1/people?id=value').with(credentials(authentication)).with(headers: {'X-4me-Custom' => 'custom'}).to_return(body: '', status: 204)
239
+ response = client(authentication).delete('people', {id: 'value'}, {'X-4me-Custom' => 'custom'})
240
+ expect(stub).to have_been_requested
241
+ expect(response.valid?).to be_truthy
242
+ expect(response.json).to eq({})
243
+ end
251
244
  end
252
245
 
253
- it "should not parse attachments for get requests" do
254
- expect(Sdk4me::Attachments).not_to receive(:new)
255
- stub_request(:get, 'https://api.4me.com/v1/requests/777?attachments=/tmp/first.png,/tmp/second.zip&note=note').with(basic_auth: ['secret', 'x']).to_return(body: {id: 777, upload_called: false}.to_json)
246
+ context 'attachments' do
247
+ it 'should not log an error for XML responses' do
248
+ xml = %(<?xml version="1.0" encoding="UTF-8"?>\n<details>some info</details>)
249
+ stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: xml)
250
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
251
+ expect_log("XML response:\n#{xml}", :debug)
252
+ response = client(authentication).get('me')
253
+ expect(response.valid?).to be_falsey
254
+ expect(response.raw.body).to eq(xml)
255
+ end
256
256
 
257
- response = @client.get('/requests/777', {note: 'note', attachments: ['/tmp/first.png', '/tmp/second.zip'] })
258
- expect(response.valid?).to be_truthy
259
- expect(response[:upload_called]).to be_falsey
260
- end
257
+ it 'should not log an error for redirects' do
258
+ stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: '', status: 303, headers: {'Location' => 'http://redirect.example.com/to/here'})
259
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
260
+ expect_log('Redirect: http://redirect.example.com/to/here', :debug)
261
+ response = client(authentication).get('me')
262
+ expect(response.valid?).to be_falsey
263
+ expect(response.raw.body).to eq('')
264
+ end
261
265
 
262
- [:post, :patch].each do |method|
263
- it "should parse attachments for #{method} requests" do
264
- attachments = double('Sdk4me::Attachments')
265
- expect(attachments).to receive(:upload_attachments!) do |path, data|
266
- expect(path).to eq '/requests/777'
267
- expect(data[:attachments]).to eq ['/tmp/first.png', '/tmp/second.zip']
268
- data.delete(:attachments)
269
- data[:note_attachments] = 'processed'
270
- end
271
- expect(Sdk4me::Attachments).to receive(:new).with(@client){ attachments }
272
- stub_request(method, 'https://api.4me.com/v1/requests/777').with(basic_auth: ['secret', 'x']).with(body: {note: 'note', note_attachments: 'processed' }).to_return(body: {id: 777, upload_called: true}.to_json)
266
+ it "should not parse attachments for get requests" do
267
+ expect(Sdk4me::Attachments).not_to receive(:new)
268
+ stub_request(:get, 'https://api.4me.com/v1/requests/777?attachments=/tmp/first.png,/tmp/second.zip&note=note').with(credentials(authentication)).to_return(body: {id: 777, upload_called: false}.to_json)
273
269
 
274
- response = @client.send(method, '/requests/777', {note: 'note', attachments: ['/tmp/first.png', '/tmp/second.zip'] })
270
+ response = client(authentication).get('/requests/777', {note: 'note', attachments: ['/tmp/first.png', '/tmp/second.zip'] })
275
271
  expect(response.valid?).to be_truthy
276
- expect(response[:upload_called]).to be_truthy
272
+ expect(response[:upload_called]).to be_falsey
277
273
  end
278
- end
279
274
 
280
- end
275
+ [:post, :patch].each do |method|
276
+ it "should parse attachments for #{method} requests" do
277
+ attachments = double('Sdk4me::Attachments')
278
+ expect(attachments).to receive(:upload_attachments!) do |path, data|
279
+ expect(path).to eq '/requests/777'
280
+ expect(data[:attachments]).to eq ['/tmp/first.png', '/tmp/second.zip']
281
+ data.delete(:attachments)
282
+ data[:note_attachments] = 'processed'
283
+ end
284
+ expect(Sdk4me::Attachments).to receive(:new).with(client(authentication)){ attachments }
285
+ stub_request(method, 'https://api.4me.com/v1/requests/777').with(credentials(authentication)).with(body: {note: 'note', note_attachments: 'processed' }).to_return(body: {id: 777, upload_called: true}.to_json)
286
+
287
+ response = client(authentication).send(method, '/requests/777', {note: 'note', attachments: ['/tmp/first.png', '/tmp/second.zip'] })
288
+ expect(response.valid?).to be_truthy
289
+ expect(response[:upload_called]).to be_truthy
290
+ end
291
+ end
281
292
 
282
- context 'import' do
283
- before(:each) do
284
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
285
- csv_mime_type = ['text/csv', 'text/comma-separated-values'].detect{|t| MIME::Types[t].any?} # which mime type is used depends on version of mime-types gem
286
- @multi_part_body = "--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\npeople\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{@fixture_dir}/people.csv\"\r\nContent-Type: #{csv_mime_type}\r\n\r\nPrimary Email,Name\nchess.cole@example.com,Chess Cole\ned.turner@example.com,Ed Turner\r\n--0123456789ABLEWASIEREISAWELBA9876543210--"
287
- @multi_part_headers = {'Accept'=>'*/*', 'Content-Type'=>'multipart/form-data; boundary=0123456789ABLEWASIEREISAWELBA9876543210', 'User-Agent'=>'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6'}
288
-
289
- @import_queued_response = {body: {state: 'queued'}.to_json}
290
- @import_processing_response = {body: {state: 'processing'}.to_json}
291
- @import_done_response = {body: {state: 'done', results: {errors: 0, updated: 1, created: 1, failures: 0, unchanged: 0, deleted: 0}}.to_json}
292
- @import_failed_response = {body: {state: 'error', message: 'Invalid byte sequence in UTF-8 on line 2', results: {errors: 1, updated: 1, created: 0, failures: 1, unchanged: 0, deleted: 0}}.to_json}
293
- allow(@client).to receive(:sleep)
294
- WebMock.disable_net_connect!
295
293
  end
296
294
 
297
- it 'should import a CSV file' do
298
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
299
- expect_log("Import file '#{@fixture_dir}/people.csv' successfully uploaded with token '68ef5ef0f64c0'.")
295
+ context 'import' do
296
+ before(:each) do
297
+ csv_mime_type = ['text/csv', 'text/comma-separated-values'].detect{|t| MIME::Types[t].any?} # which mime type is used depends on version of mime-types gem
298
+ @multi_part_body = "--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\npeople\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{@fixture_dir}/people.csv\"\r\nContent-Type: #{csv_mime_type}\r\n\r\nPrimary Email,Name\nchess.cole@example.com,Chess Cole\ned.turner@example.com,Ed Turner\r\n--0123456789ABLEWASIEREISAWELBA9876543210--"
299
+ @multi_part_headers = {'Accept'=>'*/*', 'Content-Type'=>'multipart/form-data; boundary=0123456789ABLEWASIEREISAWELBA9876543210', 'User-Agent'=>'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6'}
300
300
 
301
- response = @client.import(File.new("#{@fixture_dir}/people.csv"), 'people')
302
- expect(response[:token]).to eq('68ef5ef0f64c0')
303
- end
301
+ @import_queued_response = {body: {state: 'queued'}.to_json}
302
+ @import_processing_response = {body: {state: 'processing'}.to_json}
303
+ @import_done_response = {body: {state: 'done', results: {errors: 0, updated: 1, created: 1, failures: 0, unchanged: 0, deleted: 0}}.to_json}
304
+ @import_failed_response = {body: {state: 'error', message: 'Invalid byte sequence in UTF-8 on line 2', results: {errors: 1, updated: 1, created: 0, failures: 1, unchanged: 0, deleted: 0}}.to_json}
305
+ allow(client(authentication)).to receive(:sleep)
306
+ WebMock.disable_net_connect!
307
+ end
304
308
 
305
- it 'should import a CSV file by filename' do
306
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
307
- response = @client.import("#{@fixture_dir}/people.csv", 'people')
308
- expect(response[:token]).to eq('68ef5ef0f64c0')
309
- end
309
+ it 'should import a CSV file' do
310
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
311
+ expect_log("Import file '#{@fixture_dir}/people.csv' successfully uploaded with token '68ef5ef0f64c0'.")
310
312
 
311
- it 'should wait for the import to complete' do
312
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
313
- progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(basic_auth: ['secret', 'x'])
314
- .to_return(@import_queued_response, @import_processing_response)
315
- .then.to_raise(StandardError.new('network error'))
316
- .then.to_return(@import_done_response)
317
-
318
- # verify the correct log statement are made
319
- expect_log('Sending POST request to api.4me.com:443/v1/import', :debug)
320
- expect_log("Response:\n{\n \"token\": \"68ef5ef0f64c0\"\n}", :debug)
321
- expect_log("Import file '#{@fixture_dir}/people.csv' successfully uploaded with token '68ef5ef0f64c0'.")
322
- expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
323
- expect_log("Response:\n{\n \"state\": \"queued\"\n}", :debug)
324
- expect_log("Import of '#{@fixture_dir}/people.csv' is queued. Checking again in 30 seconds.", :debug)
325
- expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
326
- expect_log("Response:\n{\n \"state\": \"processing\"\n}", :debug)
327
- expect_log("Import of '#{@fixture_dir}/people.csv' is processing. Checking again in 30 seconds.", :debug)
328
- expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
329
- expect_log("GET request to api.4me.com:443/v1/import/68ef5ef0f64c0 failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/import/68ef5ef0f64c0'", :error)
330
- expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
331
- expect_log("Response:\n{\n \"state\": \"done\",\n \"results\": {\n \"errors\": 0,\n \"updated\": 1,\n \"created\": 1,\n \"failures\": 0,\n \"unchanged\": 0,\n \"deleted\": 0\n }\n}", :debug)
332
-
333
- response = @client.import("#{@fixture_dir}/people.csv", 'people', true)
334
- expect(response[:state]).to eq('done')
335
- expect(response[:results][:updated]).to eq(1)
336
- expect(progress_stub).to have_been_requested.times(4)
337
- end
313
+ response = client(authentication).import(File.new("#{@fixture_dir}/people.csv"), 'people')
314
+ expect(response[:token]).to eq('68ef5ef0f64c0')
315
+ end
338
316
 
339
- it 'should wait for the import to fail' do
340
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
341
- progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(basic_auth: ['secret', 'x']).to_return(@import_queued_response, @import_processing_response, @import_failed_response)
317
+ it 'should import a CSV file by filename' do
318
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
319
+ response = client(authentication).import("#{@fixture_dir}/people.csv", 'people')
320
+ expect(response[:token]).to eq('68ef5ef0f64c0')
321
+ end
342
322
 
343
- expect{ @client.import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for people import. Invalid byte sequence in UTF-8 on line 2")
344
- expect(progress_stub).to have_been_requested.times(4)
345
- end
323
+ it 'should wait for the import to complete' do
324
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
325
+ progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(credentials(authentication))
326
+ .to_return(@import_queued_response, @import_processing_response)
327
+ .then.to_raise(StandardError.new('network error'))
328
+ .then.to_return(@import_done_response)
329
+
330
+ # verify the correct log statement are made
331
+ expect_log('Sending POST request to api.4me.com:443/v1/import', :debug)
332
+ expect_log("Response:\n{\n \"token\": \"68ef5ef0f64c0\"\n}", :debug)
333
+ expect_log("Import file '#{@fixture_dir}/people.csv' successfully uploaded with token '68ef5ef0f64c0'.")
334
+ expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
335
+ expect_log("Response:\n{\n \"state\": \"queued\"\n}", :debug)
336
+ expect_log("Import of '#{@fixture_dir}/people.csv' is queued. Checking again in 30 seconds.", :debug)
337
+ expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
338
+ expect_log("Response:\n{\n \"state\": \"processing\"\n}", :debug)
339
+ expect_log("Import of '#{@fixture_dir}/people.csv' is processing. Checking again in 30 seconds.", :debug)
340
+ expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
341
+ expect_log("GET request to api.4me.com:443/v1/import/68ef5ef0f64c0 failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/import/68ef5ef0f64c0'", :error)
342
+ expect_log('Sending GET request to api.4me.com:443/v1/import/68ef5ef0f64c0', :debug)
343
+ expect_log("Response:\n{\n \"state\": \"done\",\n \"results\": {\n \"errors\": 0,\n \"updated\": 1,\n \"created\": 1,\n \"failures\": 0,\n \"unchanged\": 0,\n \"deleted\": 0\n }\n}", :debug)
344
+
345
+ response = client(authentication).import("#{@fixture_dir}/people.csv", 'people', true)
346
+ expect(response[:state]).to eq('done')
347
+ expect(response[:results][:updated]).to eq(1)
348
+ expect(progress_stub).to have_been_requested.times(4)
349
+ end
346
350
 
347
- it 'should not continue when there is an error connecting to 4me' do
348
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
349
- progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(basic_auth: ['secret', 'x'])
350
- .to_return(@import_queued_response, @import_processing_response)
351
- .then.to_raise(StandardError.new('network error')) # twice
351
+ it 'should wait for the import to fail' do
352
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
353
+ progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(credentials(authentication)).to_return(@import_queued_response, @import_processing_response, @import_failed_response)
352
354
 
353
- expect{ @client.import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for people import. 500: No Response from Server - network error for 'api.4me.com:443/v1/import/68ef5ef0f64c0'")
354
- expect(progress_stub).to have_been_requested.times(4)
355
- end
355
+ expect{ client(authentication).import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for people import. Invalid byte sequence in UTF-8 on line 2")
356
+ expect(progress_stub).to have_been_requested.times(4)
357
+ end
356
358
 
357
- it 'should return an invalid response in case waiting for progress is false' do
358
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {message: 'oops!'}.to_json)
359
- response = @client.import("#{@fixture_dir}/people.csv", 'people', false)
360
- expect(response.valid?).to be_falsey
361
- expect(response.message).to eq('oops!')
362
- end
359
+ it 'should not continue when there is an error connecting to 4me' do
360
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
361
+ progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(credentials(authentication))
362
+ .to_return(@import_queued_response, @import_processing_response)
363
+ .then.to_raise(StandardError.new('network error')) # twice
364
+
365
+ expect{ client(authentication).import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for people import. 500: No Response from Server - network error for 'api.4me.com:443/v1/import/68ef5ef0f64c0'")
366
+ expect(progress_stub).to have_been_requested.times(4)
367
+ end
368
+
369
+ it 'should return an invalid response in case waiting for progress is false' do
370
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {message: 'oops!'}.to_json)
371
+ response = client(authentication).import("#{@fixture_dir}/people.csv", 'people', false)
372
+ expect(response.valid?).to be_falsey
373
+ expect(response.message).to eq('oops!')
374
+ end
375
+
376
+ it 'should raise an UploadFailed exception in case waiting for progress is true' do
377
+ stub_request(:post, 'https://api.4me.com/v1/import').with(credentials(authentication)).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {message: 'oops!'}.to_json)
378
+ expect{ client(authentication).import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::UploadFailed, 'Failed to queue people import. oops!')
379
+ end
363
380
 
364
- it 'should raise an UploadFailed exception in case waiting for progress is true' do
365
- stub_request(:post, 'https://api.4me.com/v1/import').with(basic_auth: ['secret', 'x']).with(body: @multi_part_body, headers: @multi_part_headers).to_return(body: {message: 'oops!'}.to_json)
366
- expect{ @client.import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::UploadFailed, 'Failed to queue people import. oops!')
367
381
  end
368
382
 
369
- end
370
383
 
384
+ context 'export' do
385
+ before(:each) do
371
386
 
372
- context 'export' do
373
- before(:each) do
374
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
387
+ @export_queued_response = {body: {state: 'queued'}.to_json}
388
+ @export_processing_response = {body: {state: 'processing'}.to_json}
389
+ @export_done_response = {body: {state: 'done', url: 'https://download.example.com/export.zip?AWSAccessKeyId=12345'}.to_json}
390
+ allow(client(authentication)).to receive(:sleep)
391
+ end
375
392
 
376
- @export_queued_response = {body: {state: 'queued'}.to_json}
377
- @export_processing_response = {body: {state: 'processing'}.to_json}
378
- @export_done_response = {body: {state: 'done', url: 'https://download.example.com/export.zip?AWSAccessKeyId=12345'}.to_json}
379
- allow(@client).to receive(:sleep)
380
- end
393
+ it 'should export multiple types' do
394
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people,people_contact_details'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
395
+ expect_log("Export for 'people,people_contact_details' successfully queued with token '68ef5ef0f64c0'.")
381
396
 
382
- it 'should export multiple types' do
383
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people,people_contact_details'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
384
- expect_log("Export for 'people,people_contact_details' successfully queued with token '68ef5ef0f64c0'.")
397
+ response = client(authentication).export(['people', 'people_contact_details'])
398
+ expect(response[:token]).to eq('68ef5ef0f64c0')
399
+ end
385
400
 
386
- response = @client.export(['people', 'people_contact_details'])
387
- expect(response[:token]).to eq('68ef5ef0f64c0')
388
- end
401
+ it 'should indicate when nothing is exported' do
402
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people', from: '2012-03-30T23:00:00+00:00'}).to_return(status: 204)
403
+ expect_log("No changed records for 'people' since 2012-03-30T23:00:00+00:00.")
389
404
 
390
- it 'should indicate when nothing is exported' do
391
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people', from: '2012-03-30T23:00:00+00:00'}).to_return(status: 204)
392
- expect_log("No changed records for 'people' since 2012-03-30T23:00:00+00:00.")
405
+ response = client(authentication).export('people', DateTime.new(2012,03,30,23,00,00))
406
+ expect(response[:token]).to be_nil
407
+ end
393
408
 
394
- response = @client.export('people', DateTime.new(2012,03,30,23,00,00))
395
- expect(response[:token]).to be_nil
396
- end
409
+ it 'should export since a certain time' do
410
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people', from: '2012-03-30T23:00:00+00:00'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
411
+ expect_log("Export for 'people' successfully queued with token '68ef5ef0f64c0'.")
397
412
 
398
- it 'should export since a certain time' do
399
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people', from: '2012-03-30T23:00:00+00:00'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
400
- expect_log("Export for 'people' successfully queued with token '68ef5ef0f64c0'.")
413
+ response = client(authentication).export('people', DateTime.new(2012,03,30,23,00,00))
414
+ expect(response[:token]).to eq('68ef5ef0f64c0')
415
+ end
401
416
 
402
- response = @client.export('people', DateTime.new(2012,03,30,23,00,00))
403
- expect(response[:token]).to eq('68ef5ef0f64c0')
404
- end
417
+ it 'should export with locale' do
418
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'translations', locale: 'nl'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
419
+ expect_log("Export for 'translations' successfully queued with token '68ef5ef0f64c0'.")
405
420
 
406
- it 'should export with locale' do
407
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'translations', locale: 'nl'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
408
- expect_log("Export for 'translations' successfully queued with token '68ef5ef0f64c0'.")
421
+ response = client(authentication).export('translations', nil, nil, 'nl')
422
+ expect(response[:token]).to eq('68ef5ef0f64c0')
423
+ end
409
424
 
410
- response = @client.export('translations', nil, nil, 'nl')
411
- expect(response[:token]).to eq('68ef5ef0f64c0')
412
- end
425
+ it 'should wait for the export to complete' do
426
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
427
+ progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(credentials(authentication))
428
+ .to_return(@export_queued_response, @export_processing_response)
429
+ .then.to_raise(StandardError.new('network error'))
430
+ .then.to_return(@export_done_response)
431
+
432
+ # verify the correct log statement are made
433
+ expect_log('Sending POST request to api.4me.com:443/v1/export', :debug)
434
+ expect_log(%(Response:\n{\n "token": "68ef5ef0f64c0"\n}), :debug)
435
+ expect_log("Export for 'people' successfully queued with token '68ef5ef0f64c0'.")
436
+ expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
437
+ expect_log(%(Response:\n{\n "state": "queued"\n}), :debug)
438
+ expect_log("Export of 'people' is queued. Checking again in 30 seconds.", :debug)
439
+ expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
440
+ expect_log(%(Response:\n{\n "state": "processing"\n}), :debug)
441
+ expect_log("Export of 'people' is processing. Checking again in 30 seconds.", :debug)
442
+ expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
443
+ expect_log("GET request to api.4me.com:443/v1/export/68ef5ef0f64c0 failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/export/68ef5ef0f64c0'", :error)
444
+ expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
445
+ expect_log(%(Response:\n{\n "state": "done",\n "url": "https://download.example.com/export.zip?AWSAccessKeyId=12345"\n}), :debug)
446
+
447
+ response = client(authentication).export('people', nil, true)
448
+ expect(response[:state]).to eq('done')
449
+ expect(response[:url]).to eq('https://download.example.com/export.zip?AWSAccessKeyId=12345')
450
+ expect(progress_stub).to have_been_requested.times(4)
451
+ end
413
452
 
414
- it 'should wait for the export to complete' do
415
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
416
- progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(basic_auth: ['secret', 'x'])
417
- .to_return(@export_queued_response, @export_processing_response)
418
- .then.to_raise(StandardError.new('network error'))
419
- .then.to_return(@export_done_response)
420
-
421
- # verify the correct log statement are made
422
- expect_log('Sending POST request to api.4me.com:443/v1/export', :debug)
423
- expect_log(%(Response:\n{\n "token": "68ef5ef0f64c0"\n}), :debug)
424
- expect_log("Export for 'people' successfully queued with token '68ef5ef0f64c0'.")
425
- expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
426
- expect_log(%(Response:\n{\n "state": "queued"\n}), :debug)
427
- expect_log("Export of 'people' is queued. Checking again in 30 seconds.", :debug)
428
- expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
429
- expect_log(%(Response:\n{\n "state": "processing"\n}), :debug)
430
- expect_log("Export of 'people' is processing. Checking again in 30 seconds.", :debug)
431
- expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
432
- expect_log("GET request to api.4me.com:443/v1/export/68ef5ef0f64c0 failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/export/68ef5ef0f64c0'", :error)
433
- expect_log('Sending GET request to api.4me.com:443/v1/export/68ef5ef0f64c0', :debug)
434
- expect_log(%(Response:\n{\n "state": "done",\n "url": "https://download.example.com/export.zip?AWSAccessKeyId=12345"\n}), :debug)
435
-
436
- response = @client.export('people', nil, true)
437
- expect(response[:state]).to eq('done')
438
- expect(response[:url]).to eq('https://download.example.com/export.zip?AWSAccessKeyId=12345')
439
- expect(progress_stub).to have_been_requested.times(4)
440
- end
453
+ it 'should not continue when there is an error connecting to 4me' do
454
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
455
+ progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(credentials(authentication))
456
+ .to_return(@export_queued_response, @export_processing_response)
457
+ .then.to_raise(StandardError.new('network error')) # twice
441
458
 
442
- it 'should not continue when there is an error connecting to 4me' do
443
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people'}).to_return(body: {token: '68ef5ef0f64c0'}.to_json)
444
- progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(basic_auth: ['secret', 'x'])
445
- .to_return(@export_queued_response, @export_processing_response)
446
- .then.to_raise(StandardError.new('network error')) # twice
459
+ expect{ client(authentication).export('people', nil, true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for 'people' export. 500: No Response from Server - network error for 'api.4me.com:443/v1/export/68ef5ef0f64c0'")
460
+ expect(progress_stub).to have_been_requested.times(4)
461
+ end
447
462
 
448
- expect{ @client.export('people', nil, true) }.to raise_error(Sdk4me::Exception, "Unable to monitor progress for 'people' export. 500: No Response from Server - network error for 'api.4me.com:443/v1/export/68ef5ef0f64c0'")
449
- expect(progress_stub).to have_been_requested.times(4)
450
- end
463
+ it 'should return an invalid response in case waiting for progress is false' do
464
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people'}).to_return(body: {message: 'oops!'}.to_json)
465
+ response = client(authentication).export('people')
466
+ expect(response.valid?).to be_falsey
467
+ expect(response.message).to eq('oops!')
468
+ end
451
469
 
452
- it 'should return an invalid response in case waiting for progress is false' do
453
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people'}).to_return(body: {message: 'oops!'}.to_json)
454
- response = @client.export('people')
455
- expect(response.valid?).to be_falsey
456
- expect(response.message).to eq('oops!')
457
- end
470
+ it 'should raise an UploadFailed exception in case waiting for progress is true' do
471
+ stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: {type: 'people'}).to_return(body: {message: 'oops!'}.to_json)
472
+ expect{ client(authentication).export('people', nil, true) }.to raise_error(Sdk4me::UploadFailed, "Failed to queue 'people' export. oops!")
473
+ end
458
474
 
459
- it 'should raise an UploadFailed exception in case waiting for progress is true' do
460
- stub_request(:post, 'https://api.4me.com/v1/export').with(basic_auth: ['secret', 'x']).with(body: {type: 'people'}).to_return(body: {message: 'oops!'}.to_json)
461
- expect{ @client.export('people', nil, true) }.to raise_error(Sdk4me::UploadFailed, "Failed to queue 'people' export. oops!")
462
475
  end
463
476
 
464
- end
477
+ context 'retry' do
478
+ it 'should not retry when max_retry_time = -1' do
479
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_raise(StandardError.new('network error'))
480
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
481
+ expect_log("GET request to api.4me.com:443/v1/me failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :error)
465
482
 
466
- context 'retry' do
467
- it 'should not retry when max_retry_time = -1' do
468
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_raise(StandardError.new('network error'))
469
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
470
- expect_log("GET request to api.4me.com:443/v1/me failed: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :error)
471
-
472
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
473
- response = client.get('me')
474
- expect(stub).to have_been_requested.times(1)
475
- expect(response.valid?).to be_falsey
476
- expect(response.message).to eq("500: No Response from Server - network error for 'api.4me.com:443/v1/me'")
477
- end
483
+ response = client(authentication).get('me')
484
+ expect(stub).to have_been_requested.times(1)
485
+ expect(response.valid?).to be_falsey
486
+ expect(response.message).to eq("500: No Response from Server - network error for 'api.4me.com:443/v1/me'")
487
+ end
478
488
 
479
- it 'should not retry 4 times when max_retry_time = 16' do
480
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_raise(StandardError.new('network error'))
481
- [2,4,8].each_with_index do |secs, i|
489
+ it 'should not retry 4 times when max_retry_time = 16' do
490
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_raise(StandardError.new('network error'))
491
+ [2,4,8].each_with_index do |secs, i|
492
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
493
+ expect_log("Request failed, retry ##{i+1} in #{secs} seconds: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :warn)
494
+ end
482
495
  expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
483
- expect_log("Request failed, retry ##{i+1} in #{secs} seconds: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :warn)
496
+
497
+ retry_client = client(authentication, max_retry_time: 16)
498
+ allow(retry_client).to receive(:sleep)
499
+ response = retry_client.get('me')
500
+ expect(stub).to have_been_requested.times(4)
501
+ expect(response.valid?).to be_falsey
502
+ expect(response.message).to eq("500: No Response from Server - network error for 'api.4me.com:443/v1/me'")
484
503
  end
485
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
486
504
 
487
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: 16)
488
- allow(client).to receive(:sleep)
489
- response = client.get('me')
490
- expect(stub).to have_been_requested.times(4)
491
- expect(response.valid?).to be_falsey
492
- expect(response.message).to eq("500: No Response from Server - network error for 'api.4me.com:443/v1/me'")
493
- end
505
+ it 'should return the response after retry succeeds' do
506
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_raise(StandardError.new('network error')).then.to_return(body: {name: 'my name'}.to_json)
507
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
508
+ expect_log("Request failed, retry #1 in 2 seconds: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :warn)
509
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
510
+ expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
494
511
 
495
- it 'should return the response after retry succeeds' do
496
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_raise(StandardError.new('network error')).then.to_return(body: {name: 'my name'}.to_json)
497
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
498
- expect_log("Request failed, retry #1 in 2 seconds: 500: No Response from Server - network error for 'api.4me.com:443/v1/me'", :warn)
499
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
500
- expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
501
-
502
- client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: 16)
503
- allow(client).to receive(:sleep)
504
- response = client.get('me')
505
- expect(stub).to have_been_requested.times(2)
506
- expect(response.valid?).to be_truthy
507
- expect(response[:name]).to eq('my name')
512
+ retry_client = client(authentication, max_retry_time: 16)
513
+ allow(retry_client).to receive(:sleep)
514
+ response = retry_client.get('me')
515
+ expect(stub).to have_been_requested.times(2)
516
+ expect(response.valid?).to be_truthy
517
+ expect(response[:name]).to eq('my name')
518
+ end
508
519
  end
509
- end
510
520
 
511
- context 'rate limiting' do
512
- it 'should not block on rate limit when block_at_rate_limit is false' do
513
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json)
514
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
515
- expect_log("GET request to api.4me.com:443/v1/me failed: 429: Too Many Requests", :error)
516
-
517
- client = Sdk4me::Client.new(api_token: 'secret', block_at_rate_limit: false)
518
- response = client.get('me')
519
- expect(stub).to have_been_requested.times(1)
520
- expect(response.valid?).to be_falsey
521
- expect(response.message).to eq('429: Too Many Requests')
522
- end
521
+ context 'rate limiting' do
522
+ it 'should not block on rate limit when block_at_rate_limit is false' do
523
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json)
524
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
525
+ expect_log("GET request to api.4me.com:443/v1/me failed: 429: Too Many Requests", :error)
523
526
 
524
- it 'should block on rate limit when block_at_rate_limit is true' do
525
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json).then.to_return(body: {name: 'my name'}.to_json)
526
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
527
- expect_log('Request throttled, trying again in 300 seconds: 429: Too Many Requests', :warn)
528
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
529
- expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
530
-
531
- client = Sdk4me::Client.new(api_token: 'secret', block_at_rate_limit: true, max_retry_time: 500)
532
- allow(client).to receive(:sleep)
533
- response = client.get('me')
534
- expect(stub).to have_been_requested.times(2)
535
- expect(response.valid?).to be_truthy
536
- expect(response[:name]).to eq('my name')
537
- end
527
+ response = client(authentication, block_at_rate_limit: false).get('me')
528
+ expect(stub).to have_been_requested.times(1)
529
+ expect(response.valid?).to be_falsey
530
+ expect(response.message).to eq('429: Too Many Requests')
531
+ end
538
532
 
539
- it 'should block on rate limit using Retry-After when block_at_rate_limit is true' do
540
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json, headers: {'Retry-After' => '20'}).then.to_return(body: {name: 'my name'}.to_json)
541
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
542
- expect_log('Request throttled, trying again in 20 seconds: 429: Too Many Requests', :warn)
543
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
544
- expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
545
-
546
- client = Sdk4me::Client.new(api_token: 'secret', block_at_rate_limit: true)
547
- allow(client).to receive(:sleep)
548
- response = client.get('me')
549
- expect(stub).to have_been_requested.times(2)
550
- expect(response.valid?).to be_truthy
551
- expect(response[:name]).to eq('my name')
552
- end
533
+ it 'should block on rate limit when block_at_rate_limit is true' do
534
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json).then.to_return(body: {name: 'my name'}.to_json)
535
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
536
+ expect_log('Request throttled, trying again in 300 seconds: 429: Too Many Requests', :warn)
537
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
538
+ expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
553
539
 
554
- it 'should block on rate limit using Retry-After with minimum of 2 seconds when block_at_rate_limit is true' do
555
- stub = stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json, headers: {'Retry-After' => '1'}).then.to_return(body: {name: 'my name'}.to_json)
556
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
557
- expect_log('Request throttled, trying again in 2 seconds: 429: Too Many Requests', :warn)
558
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
559
- expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
560
-
561
- client = Sdk4me::Client.new(api_token: 'secret', block_at_rate_limit: true)
562
- allow(client).to receive(:sleep)
563
- response = client.get('me')
564
- expect(stub).to have_been_requested.times(2)
565
- expect(response.valid?).to be_truthy
566
- expect(response[:name]).to eq('my name')
567
- end
568
- end
540
+ block_client = client(authentication, block_at_rate_limit: true, max_retry_time: 500)
541
+ allow(block_client).to receive(:sleep)
542
+ response = block_client.get('me')
543
+ expect(stub).to have_been_requested.times(2)
544
+ expect(response.valid?).to be_truthy
545
+ expect(response[:name]).to eq('my name')
546
+ end
547
+
548
+ it 'should block on rate limit using Retry-After when block_at_rate_limit is true' do
549
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json, headers: {'Retry-After' => '20'}).then.to_return(body: {name: 'my name'}.to_json)
550
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
551
+ expect_log('Request throttled, trying again in 20 seconds: 429: Too Many Requests', :warn)
552
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
553
+ expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
569
554
 
570
- context 'logger' do
571
- before(:each) do
572
- @logger = Logger.new(STDOUT)
573
- @client = Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1, logger: @logger)
555
+ block_client = client(authentication, block_at_rate_limit: true)
556
+ allow(block_client).to receive(:sleep)
557
+ response = block_client.get('me')
558
+ expect(stub).to have_been_requested.times(2)
559
+ expect(response.valid?).to be_truthy
560
+ expect(response[:name]).to eq('my name')
561
+ end
562
+
563
+ it 'should block on rate limit using Retry-After with minimum of 2 seconds when block_at_rate_limit is true' do
564
+ stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(status: 429, body: {message: 'Too Many Requests'}.to_json, headers: {'Retry-After' => '1'}).then.to_return(body: {name: 'my name'}.to_json)
565
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
566
+ expect_log('Request throttled, trying again in 2 seconds: 429: Too Many Requests', :warn)
567
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug )
568
+ expect_log(%(Response:\n{\n "name": "my name"\n}), :debug )
569
+
570
+ block_client = client(authentication, block_at_rate_limit: true)
571
+ allow(block_client).to receive(:sleep)
572
+ response = block_client.get('me')
573
+ expect(stub).to have_been_requested.times(2)
574
+ expect(response.valid?).to be_truthy
575
+ expect(response[:name]).to eq('my name')
576
+ end
574
577
  end
575
578
 
576
- it 'should be possible to override the default logger' do
577
- stub_request(:get, 'https://api.4me.com/v1/me').with(basic_auth: ['secret', 'x']).to_return(body: {name: 'my name'}.to_json)
578
- expect_log('Sending GET request to api.4me.com:443/v1/me', :debug, @logger )
579
- expect_log(%(Response:\n{\n "name": "my name"\n}), :debug, @logger )
580
- @client.get('me')
579
+ context 'logger' do
580
+ it 'should be possible to override the default logger' do
581
+ logger = Logger.new(STDOUT)
582
+ logger_client = client(authentication, max_retry_time: -1, logger: logger)
583
+ stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: {name: 'my name'}.to_json)
584
+ expect_log('Sending GET request to api.4me.com:443/v1/me', :debug, logger )
585
+ expect_log(%(Response:\n{\n "name": "my name"\n}), :debug, logger )
586
+ logger_client.get('me')
587
+ end
581
588
  end
582
589
  end
583
590
  end