4me-sdk 2.0.4 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,342 +0,0 @@
1
- require 'spec_helper'
2
- require 'tempfile'
3
-
4
- describe Sdk4me::Attachments do
5
- def attachments(authentication, path)
6
- @client = if authentication == :api_token
7
- Sdk4me::Client.new(api_token: 'secret', max_retry_time: -1)
8
- else
9
- Sdk4me::Client.new(access_token: 'secret', max_retry_time: -1)
10
- end
11
- Sdk4me::Attachments.new(@client, path)
12
- end
13
-
14
- def credentials(authentication)
15
- if authentication == :api_token
16
- { basic_auth: %w[secret x] }
17
- else
18
- { headers: { 'Authorization' => 'Bearer secret' } }
19
- end
20
- end
21
-
22
- %i[api_token access_token].each do |authentication|
23
- context "#{authentication} -" do
24
- context 'upload_attachments! -' do
25
- context 'field attachments' do
26
- it 'should not do anything when no attachments are present' do
27
- a = attachments(authentication, '/requests')
28
- expect(@client).not_to receive(:send_file)
29
- a.upload_attachments!({ status: :in_progress })
30
- end
31
-
32
- it 'should not do anything when attachments is nil' do
33
- a = attachments(authentication, '/requests')
34
- expect(@client).not_to receive(:send_file)
35
- a.upload_attachments!({ note_attachments: nil })
36
- end
37
-
38
- it 'should not do anything when attachments is empty' do
39
- a = attachments(authentication, '/requests')
40
- expect(@client).not_to receive(:send_file)
41
- a.upload_attachments!({ note_attachments: [] })
42
- a.upload_attachments!({ note_attachments: [nil] })
43
- end
44
-
45
- it 'should raise an error if no attachment provider can be determined' do
46
- a = attachments(authentication, '/requests')
47
- expect(@client).not_to receive(:send_file)
48
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 404, body: { message: 'Not Found' }.to_json)
49
- expect_log('GET request to api.4me.com:443/v1/attachments/storage failed: 404: Not Found', :error)
50
- expect_log('Attachment upload failed: No provider found', :error)
51
- expect { a.upload_attachments!({ note_attachments: ['file1.png'] }) }.to raise_error(::Sdk4me::UploadFailed, 'Attachment upload failed: No provider found')
52
- end
53
-
54
- it 'should upload' do
55
- a = attachments(authentication, '/requests')
56
- resp = {
57
- provider: 'local',
58
- upload_uri: 'https://widget.example.com/attachments',
59
- local: {
60
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
61
- x_4me_expiration: '2020-11-01T23:59:59Z',
62
- x_4me_signature: 'foobar'
63
- }
64
- }
65
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
66
-
67
- expect(a).to(receive(:upload_attachment).with('/tmp/file1.png').ordered { { key: 'attachments/5/requests/000/000/777/abc/file1.png', filesize: 1234 } })
68
- expect(a).to(receive(:upload_attachment).with('/tmp/file2.zip').ordered { { key: 'attachments/5/requests/000/000/777/abc/file2.zip', filesize: 9876 } })
69
-
70
- data = { subject: 'Foobar', note_attachments: ['/tmp/file1.png', '/tmp/file2.zip'] }
71
- a.upload_attachments!(data)
72
- expect(data).to eq({ subject: 'Foobar', note_attachments: [
73
- { filesize: 1234, key: 'attachments/5/requests/000/000/777/abc/file1.png' },
74
- { filesize: 9876, key: 'attachments/5/requests/000/000/777/abc/file2.zip' }
75
- ] })
76
- end
77
- end
78
-
79
- context 'rich text inline attachments' do
80
- it 'should not do anything when no [note_attachments: <idx>] is present in the note' do
81
- a = attachments(authentication, '/requests')
82
- expect(@client).not_to receive(:send_file)
83
- data = { note: '[note_attachments: foo]' }
84
- a.upload_attachments!(data)
85
- expect(data).to eq({ note: '[note_attachments: foo]' })
86
- end
87
-
88
- it 'should not do anything when note attachments is empty' do
89
- a = attachments(authentication, '/requests')
90
- expect(@client).not_to receive(:send_file)
91
- data = { note: '[note_attachments: 0]' }
92
- a.upload_attachments!(data)
93
- expect(data).to eq({ note: '[note_attachments: 0]' })
94
- end
95
-
96
- it 'should raise an error if no attachment provider can be determined' do
97
- a = attachments(authentication, '/requests')
98
- expect(@client).not_to receive(:send_file)
99
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 404, body: { message: 'Not Found' }.to_json)
100
- expect_log('GET request to api.4me.com:443/v1/attachments/storage failed: 404: Not Found', :error)
101
- expect_log('Attachment upload failed: No provider found', :error)
102
- data = {
103
- note: '[note_attachments: 0]', note_attachments: ['/tmp/doesnotexist.log']
104
- }
105
- expect { a.upload_attachments!(data) }.to raise_error(::Sdk4me::UploadFailed, 'Attachment upload failed: No provider found')
106
- end
107
-
108
- it 'should upload' do
109
- a = attachments(authentication, '/requests')
110
- resp = {
111
- provider: 'local',
112
- upload_uri: 'https://widget.example.com/attachments',
113
- local: {
114
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
115
- x_4me_expiration: '2020-11-01T23:59:59Z',
116
- x_4me_signature: 'foobar'
117
- }
118
- }
119
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
120
-
121
- expect(a).to(receive(:upload_attachment).with('/tmp/file1.png').ordered { { key: 'attachments/5/requests/000/000/777/abc/file1.png', filesize: 1234 } })
122
- expect(a).to(receive(:upload_attachment).with('/tmp/file2.jpg').ordered { { key: 'attachments/5/requests/000/000/777/abc/file2.jpg', filesize: 9876 } })
123
-
124
- data = {
125
- subject: 'Foobar',
126
- note: 'Foo [note_attachments: 0] Bar [note_attachments: 1]',
127
- note_attachments: ['/tmp/file1.png', '/tmp/file2.jpg']
128
- }
129
- a.upload_attachments!(data)
130
-
131
- expect(data).to eq({
132
- note: 'Foo ![](attachments/5/requests/000/000/777/abc/file1.png) Bar ![](attachments/5/requests/000/000/777/abc/file2.jpg)',
133
- note_attachments: [
134
- { filesize: 1234, inline: true, key: 'attachments/5/requests/000/000/777/abc/file1.png' },
135
- { filesize: 9876, inline: true, key: 'attachments/5/requests/000/000/777/abc/file2.jpg' }
136
- ],
137
- subject: 'Foobar'
138
- })
139
- end
140
- end
141
-
142
- context 'field attachments and rich text inline attachments' do
143
- it 'should upload, and replace the data in place' do
144
- a = attachments(authentication, '/requests')
145
- resp = {
146
- provider: 'local',
147
- upload_uri: 'https://widget.example.com/attachments',
148
- local: {
149
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
150
- x_4me_expiration: '2020-11-01T23:59:59Z',
151
- x_4me_signature: 'foobar'
152
- }
153
- }
154
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
155
-
156
- expect(a).to(receive(:upload_attachment).with('/tmp/file3.log').ordered { { key: 'attachments/5/requests/000/000/777/abc/file3.log', filesize: 5678 } })
157
- expect(a).to(receive(:upload_attachment).with('/tmp/file1.png').ordered { { key: 'attachments/5/requests/000/000/777/abc/file1.png', filesize: 1234 } })
158
- expect(a).to(receive(:upload_attachment).with('/tmp/file2.jpg').ordered { { key: 'attachments/5/requests/000/000/777/abc/file2.jpg', filesize: 9876 } })
159
-
160
- data = {
161
- subject: 'Foobar',
162
- note: 'Foo [note_attachments: 2] Bar [note_attachments: 1]',
163
- note_attachments: ['/tmp/file3.log', '/tmp/file1.png', '/tmp/file2.jpg']
164
- }
165
- a.upload_attachments!(data)
166
-
167
- expect(data).to eq({
168
- note: 'Foo ![](attachments/5/requests/000/000/777/abc/file2.jpg) Bar ![](attachments/5/requests/000/000/777/abc/file1.png)',
169
- note_attachments: [
170
- { filesize: 5678, key: 'attachments/5/requests/000/000/777/abc/file3.log' },
171
- { filesize: 1234, inline: true, key: 'attachments/5/requests/000/000/777/abc/file1.png' },
172
- { filesize: 9876, inline: true, key: 'attachments/5/requests/000/000/777/abc/file2.jpg' }
173
- ],
174
- subject: 'Foobar'
175
- })
176
- end
177
-
178
- it 'failed uploads' do
179
- a = attachments(authentication, '/requests')
180
- resp = {
181
- provider: 'local',
182
- upload_uri: 'https://widget.example.com/attachments',
183
- local: {
184
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
185
- x_4me_expiration: '2020-11-01T23:59:59Z',
186
- x_4me_signature: 'foobar'
187
- }
188
- }
189
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
190
-
191
- expect_log('Attachment upload failed: file does not exist: /tmp/doesnotexist.png', :error)
192
-
193
- data = {
194
- subject: 'Foobar',
195
- note: 'Foo [note_attachments: 2] Bar [note_attachments: 1]',
196
- note_attachments: ['/tmp/doesnotexist.png']
197
- }
198
- expect { a.upload_attachments!(data) }.to raise_error(::Sdk4me::UploadFailed, 'Attachment upload failed: file does not exist: /tmp/doesnotexist.png')
199
- end
200
- end
201
- end
202
-
203
- context :upload_attachment do
204
- before(:each) do
205
- resp = {
206
- provider: 'local',
207
- upload_uri: 'https://widget.example.com/attachments',
208
- local: {
209
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
210
- x_4me_expiration: '2020-11-01T23:59:59Z',
211
- x_4me_signature: 'foobar'
212
- }
213
- }
214
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
215
- end
216
-
217
- it 'should raise an error when the file could not be found' do
218
- a = attachments(authentication, '/requests')
219
- expect(@client).not_to receive(:send_file)
220
- message = 'Attachment upload failed: file does not exist: /tmp/unknown_file'
221
- expect_log(message, :error)
222
- expect { a.send(:upload_attachment, '/tmp/unknown_file') }.to raise_error(::Sdk4me::UploadFailed, message)
223
- end
224
- end
225
-
226
- context :s3 do
227
- before(:each) do
228
- resp = {
229
- provider: 's3',
230
- upload_uri: 'https://example.s3-accelerate.amazonaws.com/',
231
- s3: {
232
- acl: 'private',
233
- key: 'attachments/5/reqs/000/070/451/zxxb4ot60xfd6sjg/${filename}',
234
- policy: 'eydlgIH0=',
235
- success_action_status: 201,
236
- x_amz_algorithm: 'AWS4-HMAC-SHA256',
237
- x_amz_credential: 'AKIATRO999Z9E9D2EQ7B/20201107/us-east-1/s3/aws4_request',
238
- x_amz_date: '20201107T000000Z',
239
- x_amz_server_side_encryption: 'AES256',
240
- x_amz_signature: 'nbhdec4k='
241
- }
242
- }
243
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
244
- end
245
-
246
- it 'should upload a file from disk' do
247
- Tempfile.create('4me_attachments_spec.txt') do |file|
248
- file << 'foobar'
249
- file.flush
250
-
251
- a = attachments(authentication, '/requests')
252
-
253
- stub_request(:post, 'https://example.s3-accelerate.amazonaws.com/')
254
- .with(
255
- body: "--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"Content-Type\"\r\n\r\napplication/octet-stream\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"acl\"\r\n\r\nprivate\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\nattachments/5/reqs/000/070/451/zxxb4ot60xfd6sjg/${filename}\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"policy\"\r\n\r\neydlgIH0=\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_algorithm\"\r\n\r\nAWS4-HMAC-SHA256\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_credential\"\r\n\r\nAKIATRO999Z9E9D2EQ7B/20201107/us-east-1/s3/aws4_request\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_date\"\r\n\r\n20201107T000000Z\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_server_side_encryption\"\r\n\r\nAES256\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_signature\"\r\n\r\nnbhdec4k=\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{file.path}\"\r\nContent-Type: application/octet-stream\r\n\r\nfoobar\r\n--0123456789ABLEWASIEREISAWELBA9876543210--",
256
- headers: {
257
- 'Accept' => '*/*',
258
- 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
259
- 'Content-Type' => 'multipart/form-data; boundary=0123456789ABLEWASIEREISAWELBA9876543210',
260
- '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}"
261
- }
262
- )
263
- .to_return(status: 200, headers: {}, body: %(<?xml version="1.0" encoding="UTF-8"?>\n<PostResponse><Location>foo</Location><Bucket>example</Bucket><Key>attachments/5/zxxb4ot60xfd6sjg/s3test.txt</Key><ETag>"bar"</ETag></PostResponse>))
264
-
265
- expect(a.send(:upload_attachment, file.path)).to eq({
266
- key: 'attachments/5/zxxb4ot60xfd6sjg/s3test.txt',
267
- filesize: 6
268
- })
269
- end
270
- end
271
-
272
- it 'should report an error when upload fails' do
273
- Tempfile.create('4me_attachments_spec.txt') do |file|
274
- file << 'foobar'
275
- file.flush
276
-
277
- a = attachments(authentication, '/requests')
278
-
279
- stub_request(:post, 'https://example.s3-accelerate.amazonaws.com/')
280
- .with(
281
- body: "--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"Content-Type\"\r\n\r\napplication/octet-stream\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"acl\"\r\n\r\nprivate\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\nattachments/5/reqs/000/070/451/zxxb4ot60xfd6sjg/${filename}\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"policy\"\r\n\r\neydlgIH0=\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"success_action_status\"\r\n\r\n201\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_algorithm\"\r\n\r\nAWS4-HMAC-SHA256\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_credential\"\r\n\r\nAKIATRO999Z9E9D2EQ7B/20201107/us-east-1/s3/aws4_request\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_date\"\r\n\r\n20201107T000000Z\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_server_side_encryption\"\r\n\r\nAES256\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_amz_signature\"\r\n\r\nnbhdec4k=\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{file.path}\"\r\nContent-Type: application/octet-stream\r\n\r\nfoobar\r\n--0123456789ABLEWASIEREISAWELBA9876543210--",
282
- headers: {
283
- 'Accept' => '*/*',
284
- 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
285
- 'Content-Type' => 'multipart/form-data; boundary=0123456789ABLEWASIEREISAWELBA9876543210',
286
- '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}"
287
- }
288
- )
289
- .to_return(status: 400, body: '<Error><Message>Foo Bar Failure</Message></Error>', headers: {})
290
-
291
- key = "attachments/5/reqs/000/070/451/zxxb4ot60xfd6sjg/#{File.basename(file.path)}"
292
- message = "Attachment upload failed: AWS S3 upload to https://example.s3-accelerate.amazonaws.com/ for #{key} failed: Foo Bar Failure"
293
- # expect_log(message, :error)
294
- expect { a.send(:upload_attachment, file.path) }.to raise_error(::Sdk4me::UploadFailed, message)
295
- end
296
- end
297
- end
298
-
299
- context '4me local' do
300
- before(:each) do
301
- resp = {
302
- provider: 'local',
303
- upload_uri: 'https://widget.example.com/attachments',
304
- local: {
305
- key: 'attachments/5/requests/000/000/777/abc/${filename}',
306
- x_4me_expiration: '2020-11-01T23:59:59Z',
307
- x_4me_signature: 'foobar'
308
- }
309
- }
310
- stub_request(:get, 'https://api.4me.com/v1/attachments/storage').with(credentials(authentication)).to_return(status: 200, body: resp.to_json)
311
- end
312
-
313
- it 'should upload a file from disk' do
314
- Tempfile.create('4me_我attáchments_spec.txt') do |file|
315
- file << 'foobar'
316
- file.flush
317
-
318
- a = attachments(authentication, '/requests')
319
-
320
- stub_request(:post, 'https://widget.example.com/attachments')
321
- .with(
322
- body: "--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"Content-Type\"\r\n\r\napplication/octet-stream\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\nattachments/5/requests/000/000/777/abc/${filename}\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_4me_expiration\"\r\n\r\n2020-11-01T23:59:59Z\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"x_4me_signature\"\r\n\r\nfoobar\r\n--0123456789ABLEWASIEREISAWELBA9876543210\r\nContent-Disposition: form-data; name=\"file\"; filename=\"#{file.path}\"\r\nContent-Type: application/octet-stream\r\n\r\nfoobar\r\n--0123456789ABLEWASIEREISAWELBA9876543210--",
323
- headers: {
324
- 'Accept' => '*/*',
325
- 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
326
- 'Authorization' => (authentication == :api_token ? 'Basic c2VjcmV0Ong=' : 'Bearer secret'),
327
- 'Content-Type' => 'multipart/form-data; boundary=0123456789ABLEWASIEREISAWELBA9876543210',
328
- '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}"
329
- }
330
- )
331
- .to_return(status: 200, headers: {}, body: %({"key":"attachments/5/zxxb4ot60xfd6sjg/s3test.txt"}))
332
-
333
- expect(a.send(:upload_attachment, file.path)).to eq({
334
- key: 'attachments/5/zxxb4ot60xfd6sjg/s3test.txt',
335
- filesize: 6
336
- })
337
- end
338
- end
339
- end
340
- end
341
- end
342
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'ca-bundle.crt' do
4
- it 'should be able to connect to the 4me REST API' do
5
- WebMock.allow_net_connect!
6
- client = Sdk4me::Client.new(api_token: 'invalid', max_retry_time: -1)
7
- result = {}
8
-
9
- # no exception concerning the certificate
10
- expect { result[:response] = client.get('me') }.not_to raise_error
11
- response = result[:response]
12
- expect(response.valid?).to be_falsey
13
-
14
- # expecting 401 error
15
- expect(response.message).to start_with('401:')
16
- end
17
-
18
- it 'should be able to connect to the 4me REST API (access token)' do
19
- WebMock.allow_net_connect!
20
- client = Sdk4me::Client.new(access_token: 'invalid', max_retry_time: -1)
21
- result = {}
22
-
23
- # no exception concerning the certificate
24
- expect { result[:response] = client.get('me') }.not_to raise_error
25
- response = result[:response]
26
- expect(response.valid?).to be_falsey
27
-
28
- # expecting 401 error
29
- expect(response.message).to start_with('401:')
30
- end
31
-
32
- it 'should be able to connect to S3' do
33
- WebMock.allow_net_connect!
34
- http = Net::HTTP.new('sdk4me-eu.s3-eu-west-1.amazonaws.com', 443)
35
- http.read_timeout = 1
36
- http.use_ssl = true
37
-
38
- # no SSL error please
39
- expect { http.start { |transport| transport.request(Net::HTTP::Get.new('/exports/20141107/')) } }.to never_raise(OpenSSL::SSL::SSLError)
40
- end
41
- end