4me-sdk 1.2.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/4me-sdk.gemspec +12 -13
- data/Gemfile.lock +36 -14
- data/LICENSE +1 -1
- data/README.md +31 -23
- data/lib/sdk4me.rb +6 -5
- data/lib/sdk4me/ca-bundle.crt +1327 -1802
- data/lib/sdk4me/client.rb +97 -86
- data/lib/sdk4me/client/attachments.rb +107 -116
- data/lib/sdk4me/client/multipart.rb +16 -18
- data/lib/sdk4me/client/response.rb +17 -19
- data/lib/sdk4me/client/version.rb +1 -1
- data/spec/lib/sdk4me/attachments_spec.rb +282 -203
- data/spec/lib/sdk4me/certificate_spec.rb +3 -4
- data/spec/lib/sdk4me/client_spec.rb +165 -157
- data/spec/lib/sdk4me/response_spec.rb +25 -29
- data/spec/lib/sdk4me_spec.rb +7 -7
- data/spec/spec_helper.rb +5 -8
- data/spec/support/matchers/never_raise.rb +16 -21
- data/spec/support/util.rb +2 -2
- metadata +32 -18
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'ca-bundle.crt' do
|
4
|
-
|
5
|
-
it 'should be able to connect to the 4me API' do
|
4
|
+
it 'should be able to connect to the 4me REST API' do
|
6
5
|
WebMock.allow_net_connect!
|
7
6
|
client = Sdk4me::Client.new(api_token: 'invalid', max_retry_time: -1)
|
8
7
|
result = {}
|
@@ -16,7 +15,7 @@ describe 'ca-bundle.crt' do
|
|
16
15
|
expect(response.message).to eq('401: Bad credentials')
|
17
16
|
end
|
18
17
|
|
19
|
-
it 'should be able to connect to the 4me API (access token)' do
|
18
|
+
it 'should be able to connect to the 4me REST API (access token)' do
|
20
19
|
WebMock.allow_net_connect!
|
21
20
|
client = Sdk4me::Client.new(access_token: 'invalid', max_retry_time: -1)
|
22
21
|
result = {}
|
@@ -37,6 +36,6 @@ describe 'ca-bundle.crt' do
|
|
37
36
|
http.use_ssl = true
|
38
37
|
|
39
38
|
# no SSL error please
|
40
|
-
expect{ http.start{ |
|
39
|
+
expect { http.start { |transport| transport.request(Net::HTTP::Get.new('/exports/20141107/')) } }.to never_raise(OpenSSL::SSL::SSLError)
|
41
40
|
end
|
42
41
|
end
|
@@ -1,30 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Sdk4me::Client do
|
4
|
-
|
5
4
|
def client(authentication, options = {})
|
6
|
-
(@client ||= {})["#{authentication}-#{options
|
7
|
-
options = {max_retry_time: -1}.merge(options)
|
8
|
-
if authentication == :api_token
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
(@client ||= {})["#{authentication}-#{options}"] ||= begin
|
6
|
+
options = { max_retry_time: -1 }.merge(options)
|
7
|
+
options = if authentication == :api_token
|
8
|
+
{ api_token: 'secret' }.merge(options)
|
9
|
+
else
|
10
|
+
{ access_token: 'secret' }.merge(options)
|
11
|
+
end
|
13
12
|
Sdk4me::Client.new(options)
|
14
13
|
end
|
15
14
|
end
|
16
15
|
|
17
16
|
def credentials(authentication)
|
18
17
|
if authentication == :api_token
|
19
|
-
{ basic_auth: [
|
18
|
+
{ basic_auth: %w[secret x] }
|
20
19
|
else
|
21
|
-
{ headers: {'Authorization' => 'Bearer secret'} }
|
20
|
+
{ headers: { 'Authorization' => 'Bearer secret' } }
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
|
-
[
|
26
|
-
|
27
|
-
|
24
|
+
%i[api_token access_token].each do |authentication|
|
28
25
|
context 'Sdk4me.config' do
|
29
26
|
before(:each) do
|
30
27
|
Sdk4me.configure do |config|
|
@@ -45,23 +42,23 @@ describe Sdk4me::Client do
|
|
45
42
|
client = Sdk4me::Client.new
|
46
43
|
expect(client.option(:host)).to eq('https://api.4me.com') # default value
|
47
44
|
if authentication == :api_token
|
48
|
-
expect(client.option(:api_token)).to eq('secret')
|
45
|
+
expect(client.option(:api_token)).to eq('secret') # value set using Sdk4me.config
|
49
46
|
else
|
50
|
-
expect(client.option(:access_token)).to eq('secret')
|
47
|
+
expect(client.option(:access_token)).to eq('secret') # value set using Sdk4me.config
|
51
48
|
end
|
52
|
-
expect(client.option(:max_retry_time)).to eq(120)
|
49
|
+
expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
|
53
50
|
end
|
54
51
|
|
55
52
|
it 'should override the Sdk4me configuration' do
|
56
53
|
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)
|
54
|
+
expect(client.option(:read_timeout)).to eq(25) # default value
|
58
55
|
expect(client.option(:host)).to eq('https://demo.4me.com') # default value overridden in Client.new
|
59
56
|
expect(client.option(:api_token)).to eq('unknown') # value set using Sdk4me.config and overridden in Client.new
|
60
57
|
expect(client.option(:max_retry_time)).to eq(120) # value overridden in Sdk4me.config
|
61
58
|
expect(client.option(:block_at_rate_limit)).to eq(true) # value overridden in Client.new
|
62
59
|
end
|
63
60
|
|
64
|
-
[
|
61
|
+
%i[host api_version].each do |required_option|
|
65
62
|
it "should require option #{required_option}" do
|
66
63
|
expect { Sdk4me::Client.new(required_option => '') }.to raise_error("Missing required configuration option #{required_option}")
|
67
64
|
end
|
@@ -71,11 +68,10 @@ describe Sdk4me::Client do
|
|
71
68
|
expect { Sdk4me::Client.new(access_token: '', api_token: '') }.to raise_error('Missing required configuration option access_token')
|
72
69
|
end
|
73
70
|
|
74
|
-
[
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
].each do |host, ssl, domain, port|
|
71
|
+
[['https://api.4me.com', true, 'api.4me.com', 443],
|
72
|
+
['https://api.example.com:777', true, 'api.example.com', 777],
|
73
|
+
['http://sdk4me.example.com', false, 'sdk4me.example.com', 80],
|
74
|
+
['http://sdk4me.example.com:777', false, 'sdk4me.example.com', 777]].each do |host, ssl, domain, port|
|
79
75
|
it 'should parse ssl, host and port' do
|
80
76
|
client = Sdk4me::Client.new(host: host)
|
81
77
|
expect(client.instance_variable_get(:@ssl)).to eq(ssl)
|
@@ -91,45 +87,65 @@ describe Sdk4me::Client do
|
|
91
87
|
|
92
88
|
on_disk = `ls #{http.ca_file}`
|
93
89
|
expect(on_disk).not_to match(/cannot access/)
|
94
|
-
expect(on_disk).to match(
|
90
|
+
expect(on_disk).to match(%r{/ca-bundle.crt$})
|
95
91
|
end
|
96
92
|
|
97
93
|
describe 'headers' do
|
98
94
|
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)
|
95
|
+
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
96
|
client(authentication).get('me')
|
101
97
|
expect(stub).to have_been_requested
|
102
98
|
end
|
103
99
|
|
104
100
|
it 'should add the X-4me-Account header' do
|
105
101
|
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)
|
102
|
+
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
103
|
client.get('me')
|
108
104
|
expect(stub).to have_been_requested
|
109
105
|
end
|
110
106
|
|
111
107
|
it 'should add the X-4me-Source header' do
|
112
108
|
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)
|
109
|
+
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)
|
110
|
+
client.get('me')
|
111
|
+
expect(stub).to have_been_requested
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should add a default User-Agent header' do
|
115
|
+
client = client(authentication)
|
116
|
+
stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: { 'User-Agent' => "4me-sdk-ruby/#{Sdk4me::Client::VERSION}" }).to_return(body: { name: 'my name' }.to_json)
|
114
117
|
client.get('me')
|
115
118
|
expect(stub).to have_been_requested
|
116
119
|
end
|
117
120
|
|
118
|
-
it 'should
|
119
|
-
|
120
|
-
|
121
|
+
it 'should override the default User-Agent header' do
|
122
|
+
client = client(authentication, user_agent: 'Foo/1.0')
|
123
|
+
stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: { 'User-Agent' => 'Foo/1.0' }).to_return(body: { name: 'my name' }.to_json)
|
124
|
+
client.get('me')
|
125
|
+
expect(stub).to have_been_requested
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'should be able to override default headers' do
|
129
|
+
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)
|
130
|
+
client(authentication).get('me', {}, { 'Content-Type' => 'application/x-www-form-urlencoded' })
|
131
|
+
expect(stub).to have_been_requested
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should be able to override option headers' do
|
135
|
+
stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).with(headers: { 'X-4me-Source' => 'foo' }).to_return(body: { name: 'my name' }.to_json)
|
136
|
+
client(authentication, source: 'myapp').get('me', {}, { 'X-4me-Source' => 'foo' })
|
121
137
|
expect(stub).to have_been_requested
|
122
138
|
end
|
123
139
|
|
124
140
|
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'})
|
141
|
+
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)
|
142
|
+
client(authentication).get('me', {}, { 'X-4me-Other' => 'value' })
|
127
143
|
expect(stub).to have_been_requested
|
128
144
|
end
|
129
145
|
|
130
146
|
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|
|
147
|
+
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)
|
148
|
+
client(authentication).each('requests', { fields: 'subject' }, { 'X-4me-Secret' => 'special' }) do |request|
|
133
149
|
expect(request[:subject]).to eq("Subject #{request[:id]}")
|
134
150
|
end
|
135
151
|
expect(stub).to have_been_requested
|
@@ -138,88 +154,87 @@ describe Sdk4me::Client do
|
|
138
154
|
|
139
155
|
context 'each' do
|
140
156
|
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|
|
157
|
+
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)
|
158
|
+
nr_of_requests = client(authentication).each('requests', { fields: 'subject' }) do |request|
|
143
159
|
expect(request[:subject]).to eq("Subject #{request[:id]}")
|
144
160
|
end
|
145
161
|
expect(nr_of_requests).to eq(3)
|
146
162
|
end
|
147
163
|
|
148
164
|
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|
|
165
|
+
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"' })
|
166
|
+
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"' })
|
167
|
+
nr_of_requests = client(authentication).each('requests', { per_page: 2 }) do |request|
|
152
168
|
expect(request[:subject]).to eq("Subject #{request[:id]}")
|
153
169
|
end
|
154
170
|
expect(nr_of_requests).to eq(3)
|
171
|
+
expect(stub_page1).to have_been_requested
|
155
172
|
expect(stub_page2).to have_been_requested
|
156
173
|
end
|
157
174
|
end
|
158
175
|
|
159
176
|
context 'get' do
|
160
177
|
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)
|
178
|
+
stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: { name: 'my name' }.to_json)
|
162
179
|
response = client(authentication).get('me')
|
163
180
|
expect(response[:name]).to eq('my name')
|
164
181
|
end
|
165
182
|
|
166
183
|
describe 'parameters' do
|
167
|
-
|
168
184
|
[[nil, ''],
|
169
|
-
[
|
170
|
-
[
|
171
|
-
[
|
172
|
-
[
|
173
|
-
[
|
174
|
-
[
|
175
|
-
[
|
176
|
-
[
|
177
|
-
].each do |param_value, url_value|
|
185
|
+
%w[normal normal],
|
186
|
+
['hello;<', 'hello%3B%3C'],
|
187
|
+
[true, 'true'],
|
188
|
+
[false, 'false'],
|
189
|
+
[DateTime.now, DateTime.now.new_offset(0).iso8601.gsub('+', '%2B')],
|
190
|
+
[Date.new, Date.new.strftime('%Y-%m-%d')],
|
191
|
+
[Time.now, Time.now.strftime('%H:%M')],
|
192
|
+
[['first', 'second;<', true], 'first,second%3B%3C,true']].each do |param_value, url_value|
|
178
193
|
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})
|
194
|
+
stub = stub_request(:get, "https://api.4me.com/v1/me?value=#{url_value}").with(credentials(authentication)).to_return(body: { name: 'my name' }.to_json)
|
195
|
+
client(authentication).get('me', { value: param_value })
|
181
196
|
expect(stub).to have_been_requested
|
182
197
|
end
|
183
198
|
end
|
184
199
|
|
185
200
|
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'})
|
201
|
+
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)
|
202
|
+
client(authentication).post('people', { user_ids: [1, 2, 3] }, { 'X-4me-Custom' => 'custom' })
|
188
203
|
expect(stub).to have_been_requested
|
189
204
|
end
|
190
205
|
|
191
206
|
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'})
|
207
|
+
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)
|
208
|
+
client(authentication).put('people/55', { contacts_attributes: { 0 => { protocol: :email, label: :work, uri: 'work@example.com' } } }, { 'X-4me-Custom' => 'custom' })
|
194
209
|
expect(stub).to have_been_requested
|
195
210
|
end
|
196
211
|
|
197
212
|
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})
|
213
|
+
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)
|
214
|
+
client(authentication).put('people/55', { status: :waiting_for })
|
200
215
|
expect(stub).to have_been_requested
|
201
216
|
end
|
202
217
|
|
203
218
|
it 'should handle fancy filter operations' do
|
204
219
|
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})
|
220
|
+
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)
|
221
|
+
client(authentication).get('people', { 'created_at=>' => now, 'id!=' => 15 })
|
207
222
|
expect(stub).to have_been_requested
|
208
223
|
end
|
209
224
|
|
210
225
|
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'})
|
226
|
+
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)
|
227
|
+
client(authentication).get('people?id!=15', { primary_email: 'me@example.com' })
|
213
228
|
expect(stub).to have_been_requested
|
214
229
|
end
|
215
230
|
end
|
216
231
|
end
|
217
232
|
|
218
233
|
context 'patch' do
|
219
|
-
[
|
220
|
-
it
|
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'})
|
234
|
+
%i[put patch].each do |method|
|
235
|
+
it "should send patch requests with parameters and headers for #{method} calls" do
|
236
|
+
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)
|
237
|
+
client(authentication).send(method, 'people/1', { name: 'New Name' }, { 'X-4me-Custom' => 'custom' })
|
223
238
|
expect(stub).to have_been_requested
|
224
239
|
end
|
225
240
|
end
|
@@ -227,16 +242,16 @@ describe Sdk4me::Client do
|
|
227
242
|
|
228
243
|
context 'post' do
|
229
244
|
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'})
|
245
|
+
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)
|
246
|
+
client(authentication).post('people', { name: 'New Name' }, { 'X-4me-Custom' => 'custom' })
|
232
247
|
expect(stub).to have_been_requested
|
233
248
|
end
|
234
249
|
end
|
235
250
|
|
236
251
|
context 'delete' do
|
237
252
|
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'})
|
253
|
+
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)
|
254
|
+
response = client(authentication).delete('people', { id: 'value' }, { 'X-4me-Custom' => 'custom' })
|
240
255
|
expect(stub).to have_been_requested
|
241
256
|
expect(response.valid?).to be_truthy
|
242
257
|
expect(response.json).to eq({})
|
@@ -255,7 +270,7 @@ describe Sdk4me::Client do
|
|
255
270
|
end
|
256
271
|
|
257
272
|
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'})
|
273
|
+
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
274
|
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
260
275
|
expect_log('Redirect: http://redirect.example.com/to/here', :debug)
|
261
276
|
response = client(authentication).get('me')
|
@@ -263,51 +278,48 @@ describe Sdk4me::Client do
|
|
263
278
|
expect(response.raw.body).to eq('')
|
264
279
|
end
|
265
280
|
|
266
|
-
it
|
281
|
+
it 'should not parse attachments for get requests' do
|
267
282
|
expect(Sdk4me::Attachments).not_to receive(:new)
|
268
|
-
stub_request(:get, 'https://api.4me.com/v1/requests/777?
|
283
|
+
stub_request(:get, 'https://api.4me.com/v1/requests/777?note_attachments=/tmp/first.png,/tmp/second.zip¬e=note%20%5Battachment:/tmp/third.gif%5D').with(credentials(authentication)).to_return(body: { id: 777, upload_called: false }.to_json)
|
269
284
|
|
270
|
-
response = client(authentication).get('/requests/777', {note: 'note',
|
285
|
+
response = client(authentication).get('/requests/777', { note: 'note [attachment:/tmp/third.gif]', note_attachments: ['/tmp/first.png', '/tmp/second.zip'] })
|
271
286
|
expect(response.valid?).to be_truthy
|
272
287
|
expect(response[:upload_called]).to be_falsey
|
273
288
|
end
|
274
289
|
|
275
|
-
[
|
290
|
+
%i[post patch].each do |method|
|
276
291
|
it "should parse attachments for #{method} requests" do
|
277
292
|
attachments = double('Sdk4me::Attachments')
|
278
|
-
expect(attachments).to receive(:upload_attachments!) do |
|
279
|
-
expect(
|
280
|
-
expect(data[:attachments]).to eq ['/tmp/first.png', '/tmp/second.zip']
|
281
|
-
data.delete(:attachments)
|
293
|
+
expect(attachments).to receive(:upload_attachments!) do |data|
|
294
|
+
expect(data[:note_attachments]).to eq(['/tmp/first.png', '/tmp/second.zip'])
|
282
295
|
data[:note_attachments] = 'processed'
|
283
296
|
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)
|
297
|
+
expect(Sdk4me::Attachments).to receive(:new).with(client(authentication), '/requests/777') { attachments }
|
298
|
+
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
299
|
|
287
|
-
response = client(authentication).send(method, '/requests/777', {note: 'note',
|
300
|
+
response = client(authentication).send(method, '/requests/777', { note: 'note', note_attachments: ['/tmp/first.png', '/tmp/second.zip'] })
|
288
301
|
expect(response.valid?).to be_truthy
|
289
302
|
expect(response[:upload_called]).to be_truthy
|
290
303
|
end
|
291
304
|
end
|
292
|
-
|
293
305
|
end
|
294
306
|
|
295
307
|
context 'import' do
|
296
308
|
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
|
309
|
+
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
310
|
@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'=>
|
311
|
+
@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 4me/#{Sdk4me::Client::VERSION}" }
|
300
312
|
|
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}
|
313
|
+
@import_queued_response = { body: { state: 'queued' }.to_json }
|
314
|
+
@import_processing_response = { body: { state: 'processing' }.to_json }
|
315
|
+
@import_done_response = { body: { state: 'done', results: { errors: 0, updated: 1, created: 1, failures: 0, unchanged: 0, deleted: 0 } }.to_json }
|
316
|
+
@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
317
|
allow(client(authentication)).to receive(:sleep)
|
306
318
|
WebMock.disable_net_connect!
|
307
319
|
end
|
308
320
|
|
309
321
|
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)
|
322
|
+
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
323
|
expect_log("Import file '#{@fixture_dir}/people.csv' successfully uploaded with token '68ef5ef0f64c0'.")
|
312
324
|
|
313
325
|
response = client(authentication).import(File.new("#{@fixture_dir}/people.csv"), 'people')
|
@@ -315,17 +327,17 @@ describe Sdk4me::Client do
|
|
315
327
|
end
|
316
328
|
|
317
329
|
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)
|
330
|
+
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
331
|
response = client(authentication).import("#{@fixture_dir}/people.csv", 'people')
|
320
332
|
expect(response[:token]).to eq('68ef5ef0f64c0')
|
321
333
|
end
|
322
334
|
|
323
335
|
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)
|
336
|
+
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
337
|
progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(credentials(authentication))
|
326
|
-
|
327
|
-
|
328
|
-
|
338
|
+
.to_return(@import_queued_response, @import_processing_response)
|
339
|
+
.then.to_raise(StandardError.new('network error'))
|
340
|
+
.then.to_return(@import_done_response)
|
329
341
|
|
330
342
|
# verify the correct log statement are made
|
331
343
|
expect_log('Sending POST request to api.4me.com:443/v1/import', :debug)
|
@@ -349,73 +361,70 @@ describe Sdk4me::Client do
|
|
349
361
|
end
|
350
362
|
|
351
363
|
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)
|
364
|
+
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
365
|
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)
|
354
366
|
|
355
|
-
expect{ client(authentication).import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::Exception,
|
367
|
+
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
368
|
expect(progress_stub).to have_been_requested.times(4)
|
357
369
|
end
|
358
370
|
|
359
371
|
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)
|
372
|
+
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
373
|
progress_stub = stub_request(:get, 'https://api.4me.com/v1/import/68ef5ef0f64c0').with(credentials(authentication))
|
362
|
-
|
363
|
-
|
374
|
+
.to_return(@import_queued_response, @import_processing_response)
|
375
|
+
.then.to_raise(StandardError.new('network error')) # twice
|
364
376
|
|
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'")
|
377
|
+
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
378
|
expect(progress_stub).to have_been_requested.times(4)
|
367
379
|
end
|
368
380
|
|
369
381
|
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)
|
382
|
+
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
383
|
response = client(authentication).import("#{@fixture_dir}/people.csv", 'people', false)
|
372
384
|
expect(response.valid?).to be_falsey
|
373
385
|
expect(response.message).to eq('oops!')
|
374
386
|
end
|
375
387
|
|
376
388
|
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!')
|
389
|
+
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)
|
390
|
+
expect { client(authentication).import("#{@fixture_dir}/people.csv", 'people', true) }.to raise_error(Sdk4me::UploadFailed, 'Failed to queue people import. oops!')
|
379
391
|
end
|
380
|
-
|
381
392
|
end
|
382
393
|
|
383
|
-
|
384
394
|
context 'export' do
|
385
395
|
before(:each) do
|
386
|
-
|
387
|
-
@
|
388
|
-
@
|
389
|
-
@export_done_response = {body: {state: 'done', url: 'https://download.example.com/export.zip?AWSAccessKeyId=12345'}.to_json}
|
396
|
+
@export_queued_response = { body: { state: 'queued' }.to_json }
|
397
|
+
@export_processing_response = { body: { state: 'processing' }.to_json }
|
398
|
+
@export_done_response = { body: { state: 'done', url: 'https://download.example.com/export.zip?AWSAccessKeyId=12345' }.to_json }
|
390
399
|
allow(client(authentication)).to receive(:sleep)
|
391
400
|
end
|
392
401
|
|
393
402
|
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)
|
403
|
+
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
404
|
expect_log("Export for 'people,people_contact_details' successfully queued with token '68ef5ef0f64c0'.")
|
396
405
|
|
397
|
-
response = client(authentication).export([
|
406
|
+
response = client(authentication).export(%w[people people_contact_details])
|
398
407
|
expect(response[:token]).to eq('68ef5ef0f64c0')
|
399
408
|
end
|
400
409
|
|
401
410
|
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)
|
411
|
+
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
412
|
expect_log("No changed records for 'people' since 2012-03-30T23:00:00+00:00.")
|
404
413
|
|
405
|
-
response = client(authentication).export('people', DateTime.new(2012,
|
414
|
+
response = client(authentication).export('people', DateTime.new(2012, 0o3, 30, 23, 0o0, 0o0))
|
406
415
|
expect(response[:token]).to be_nil
|
407
416
|
end
|
408
417
|
|
409
418
|
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)
|
419
|
+
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
420
|
expect_log("Export for 'people' successfully queued with token '68ef5ef0f64c0'.")
|
412
421
|
|
413
|
-
response = client(authentication).export('people', DateTime.new(2012,
|
422
|
+
response = client(authentication).export('people', DateTime.new(2012, 0o3, 30, 23, 0o0, 0o0))
|
414
423
|
expect(response[:token]).to eq('68ef5ef0f64c0')
|
415
424
|
end
|
416
425
|
|
417
426
|
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)
|
427
|
+
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
428
|
expect_log("Export for 'translations' successfully queued with token '68ef5ef0f64c0'.")
|
420
429
|
|
421
430
|
response = client(authentication).export('translations', nil, nil, 'nl')
|
@@ -423,11 +432,11 @@ describe Sdk4me::Client do
|
|
423
432
|
end
|
424
433
|
|
425
434
|
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)
|
435
|
+
stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: { type: 'people' }).to_return(body: { token: '68ef5ef0f64c0' }.to_json)
|
427
436
|
progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(credentials(authentication))
|
428
|
-
|
429
|
-
|
430
|
-
|
437
|
+
.to_return(@export_queued_response, @export_processing_response)
|
438
|
+
.then.to_raise(StandardError.new('network error'))
|
439
|
+
.then.to_return(@export_done_response)
|
431
440
|
|
432
441
|
# verify the correct log statement are made
|
433
442
|
expect_log('Sending POST request to api.4me.com:443/v1/export', :debug)
|
@@ -451,33 +460,32 @@ describe Sdk4me::Client do
|
|
451
460
|
end
|
452
461
|
|
453
462
|
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)
|
463
|
+
stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: { type: 'people' }).to_return(body: { token: '68ef5ef0f64c0' }.to_json)
|
455
464
|
progress_stub = stub_request(:get, 'https://api.4me.com/v1/export/68ef5ef0f64c0').with(credentials(authentication))
|
456
|
-
|
457
|
-
|
465
|
+
.to_return(@export_queued_response, @export_processing_response)
|
466
|
+
.then.to_raise(StandardError.new('network error')) # twice
|
458
467
|
|
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'")
|
468
|
+
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
469
|
expect(progress_stub).to have_been_requested.times(4)
|
461
470
|
end
|
462
471
|
|
463
472
|
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)
|
473
|
+
stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: { type: 'people' }).to_return(body: { message: 'oops!' }.to_json)
|
465
474
|
response = client(authentication).export('people')
|
466
475
|
expect(response.valid?).to be_falsey
|
467
476
|
expect(response.message).to eq('oops!')
|
468
477
|
end
|
469
478
|
|
470
479
|
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!")
|
480
|
+
stub_request(:post, 'https://api.4me.com/v1/export').with(credentials(authentication)).with(body: { type: 'people' }).to_return(body: { message: 'oops!' }.to_json)
|
481
|
+
expect { client(authentication).export('people', nil, true) }.to raise_error(Sdk4me::UploadFailed, "Failed to queue 'people' export. oops!")
|
473
482
|
end
|
474
|
-
|
475
483
|
end
|
476
484
|
|
477
485
|
context 'retry' do
|
478
486
|
it 'should not retry when max_retry_time = -1' do
|
479
487
|
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
|
488
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
481
489
|
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)
|
482
490
|
|
483
491
|
response = client(authentication).get('me')
|
@@ -488,11 +496,11 @@ describe Sdk4me::Client do
|
|
488
496
|
|
489
497
|
it 'should not retry 4 times when max_retry_time = 16' do
|
490
498
|
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)
|
499
|
+
[2, 4, 8].each_with_index do |secs, i|
|
500
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
501
|
+
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
502
|
end
|
495
|
-
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug
|
503
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
496
504
|
|
497
505
|
retry_client = client(authentication, max_retry_time: 16)
|
498
506
|
allow(retry_client).to receive(:sleep)
|
@@ -503,11 +511,11 @@ describe Sdk4me::Client do
|
|
503
511
|
end
|
504
512
|
|
505
513
|
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
|
514
|
+
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)
|
515
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
508
516
|
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
|
517
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
518
|
+
expect_log(%(Response:\n{\n "name": "my name"\n}), :debug)
|
511
519
|
|
512
520
|
retry_client = client(authentication, max_retry_time: 16)
|
513
521
|
allow(retry_client).to receive(:sleep)
|
@@ -520,9 +528,9 @@ describe Sdk4me::Client do
|
|
520
528
|
|
521
529
|
context 'rate limiting' do
|
522
530
|
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(
|
531
|
+
stub = stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(status: 429, body: { message: 'Too Many Requests' }.to_json)
|
532
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
533
|
+
expect_log('GET request to api.4me.com:443/v1/me failed: 429: Too Many Requests', :error)
|
526
534
|
|
527
535
|
response = client(authentication, block_at_rate_limit: false).get('me')
|
528
536
|
expect(stub).to have_been_requested.times(1)
|
@@ -531,11 +539,11 @@ describe Sdk4me::Client do
|
|
531
539
|
end
|
532
540
|
|
533
541
|
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
|
542
|
+
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)
|
543
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
536
544
|
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
|
545
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
546
|
+
expect_log(%(Response:\n{\n "name": "my name"\n}), :debug)
|
539
547
|
|
540
548
|
block_client = client(authentication, block_at_rate_limit: true, max_retry_time: 500)
|
541
549
|
allow(block_client).to receive(:sleep)
|
@@ -546,11 +554,11 @@ describe Sdk4me::Client do
|
|
546
554
|
end
|
547
555
|
|
548
556
|
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
|
557
|
+
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)
|
558
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
551
559
|
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
|
560
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
561
|
+
expect_log(%(Response:\n{\n "name": "my name"\n}), :debug)
|
554
562
|
|
555
563
|
block_client = client(authentication, block_at_rate_limit: true)
|
556
564
|
allow(block_client).to receive(:sleep)
|
@@ -561,11 +569,11 @@ describe Sdk4me::Client do
|
|
561
569
|
end
|
562
570
|
|
563
571
|
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
|
572
|
+
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)
|
573
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
566
574
|
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
|
575
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug)
|
576
|
+
expect_log(%(Response:\n{\n "name": "my name"\n}), :debug)
|
569
577
|
|
570
578
|
block_client = client(authentication, block_at_rate_limit: true)
|
571
579
|
allow(block_client).to receive(:sleep)
|
@@ -578,11 +586,11 @@ describe Sdk4me::Client do
|
|
578
586
|
|
579
587
|
context 'logger' do
|
580
588
|
it 'should be possible to override the default logger' do
|
581
|
-
logger = Logger.new(
|
589
|
+
logger = Logger.new($stdout)
|
582
590
|
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
|
591
|
+
stub_request(:get, 'https://api.4me.com/v1/me').with(credentials(authentication)).to_return(body: { name: 'my name' }.to_json)
|
592
|
+
expect_log('Sending GET request to api.4me.com:443/v1/me', :debug, logger)
|
593
|
+
expect_log(%(Response:\n{\n "name": "my name"\n}), :debug, logger)
|
586
594
|
logger_client.get('me')
|
587
595
|
end
|
588
596
|
end
|