capybara-screenshot 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/CHANGELOG.md +4 -0
- data/README.md +8 -0
- data/lib/capybara-screenshot/s3_saver.rb +5 -3
- data/lib/capybara-screenshot/version.rb +1 -1
- data/spec/unit/s3_saver_spec.rb +117 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9afdd9d33462cf19d2cd005844e6790171b23073
|
4
|
+
data.tar.gz: ac0e7b147e924204f72657cbdfd92b96e2b912cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8966719fe9c36701a98e20c72f92d00be96df39374e871ceb18de0aaf91284c40a9c1bedf714186e0c6967da75df8e477f247bfd0201229c1fd32ce4f57489c4
|
7
|
+
data.tar.gz: 936a36d7efd3fb8e50a203e3130e0575eb0ce5b99c24ae85e56eede5f19faf5b32cb01a42aa997151ebf00971fe85bed1b78734fed5d27116748e1ca9ab7dc10
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -223,6 +223,14 @@ Capybara::Screenshot.s3_object_configuration = {
|
|
223
223
|
}
|
224
224
|
```
|
225
225
|
|
226
|
+
You may optionally specify a `:key_prefix` when generating the S3 keys, which can be used to create virtual [folders](http://docs.aws.amazon.com/AmazonS3/latest/UG/FolderOperations.html) in S3, e.g.:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
Capybara::Screenshot.s3_configuration = {
|
230
|
+
... # other config here
|
231
|
+
key_prefix: "some/folder/"
|
232
|
+
}
|
233
|
+
```
|
226
234
|
|
227
235
|
Pruning old screenshots automatically
|
228
236
|
--------------------------
|
@@ -5,10 +5,11 @@ module Capybara
|
|
5
5
|
class S3Saver
|
6
6
|
DEFAULT_REGION = 'us-east-1'
|
7
7
|
|
8
|
-
def initialize(saver, s3_client, bucket_name, object_configuration)
|
8
|
+
def initialize(saver, s3_client, bucket_name, object_configuration, options={})
|
9
9
|
@saver = saver
|
10
10
|
@s3_client = s3_client
|
11
11
|
@bucket_name = bucket_name
|
12
|
+
@key_prefix = options[:key_prefix]
|
12
13
|
@object_configuration = object_configuration
|
13
14
|
end
|
14
15
|
|
@@ -24,7 +25,7 @@ module Capybara
|
|
24
25
|
s3_client = Aws::S3::Client.new(s3_client_credentials)
|
25
26
|
bucket_name = configuration.fetch(:bucket_name)
|
26
27
|
|
27
|
-
new(saver, s3_client, bucket_name, object_configuration)
|
28
|
+
new(saver, s3_client, bucket_name, object_configuration, configuration)
|
28
29
|
rescue KeyError
|
29
30
|
raise "Invalid S3 Configuration #{configuration}. Please refer to the documentation for the necessary configurations."
|
30
31
|
end
|
@@ -34,7 +35,7 @@ module Capybara
|
|
34
35
|
File.open(local_file_path) do |file|
|
35
36
|
object_payload = {
|
36
37
|
bucket: bucket_name,
|
37
|
-
key: File.basename(local_file_path),
|
38
|
+
key: "#{@key_prefix}#{File.basename(local_file_path)}",
|
38
39
|
body: file
|
39
40
|
}
|
40
41
|
|
@@ -60,6 +61,7 @@ module Capybara
|
|
60
61
|
:s3_client,
|
61
62
|
:bucket_name,
|
62
63
|
:object_configuration
|
64
|
+
:key_prefix
|
63
65
|
|
64
66
|
def save_and
|
65
67
|
saver.save
|
data/spec/unit/s3_saver_spec.rb
CHANGED
@@ -6,8 +6,10 @@ describe Capybara::Screenshot::S3Saver do
|
|
6
6
|
let(:bucket_name) { double('bucket_name') }
|
7
7
|
let(:s3_object_configuration) { {} }
|
8
8
|
let(:s3_client) { double('s3_client') }
|
9
|
+
let(:key_prefix){ "some/path/" }
|
9
10
|
|
10
|
-
let(:s3_saver) {
|
11
|
+
let(:s3_saver) { described_class.new(saver, s3_client, bucket_name, s3_object_configuration) }
|
12
|
+
let(:s3_saver_with_key_prefix) { described_class.new(saver, s3_client, bucket_name, s3_object_configuration, key_prefix: key_prefix) }
|
11
13
|
|
12
14
|
describe '.new_with_configuration' do
|
13
15
|
let(:access_key_id) { double('access_key_id') }
|
@@ -24,26 +26,36 @@ describe Capybara::Screenshot::S3Saver do
|
|
24
26
|
s3_client_credentials_using_defaults.merge(region: region)
|
25
27
|
}
|
26
28
|
|
27
|
-
|
29
|
+
before do
|
28
30
|
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
|
29
|
-
allow(
|
31
|
+
allow(described_class).to receive(:new)
|
32
|
+
end
|
30
33
|
|
31
|
-
|
34
|
+
it 'destructures the configuration into its components' do
|
35
|
+
described_class.new_with_configuration(saver, {
|
32
36
|
s3_client_credentials: s3_client_credentials,
|
33
37
|
bucket_name: bucket_name
|
34
38
|
}, s3_object_configuration)
|
35
39
|
|
36
40
|
expect(Aws::S3::Client).to have_received(:new).with(s3_client_credentials)
|
37
|
-
expect(
|
41
|
+
expect(described_class).to have_received(:new).with(saver, s3_client, bucket_name, s3_object_configuration, hash_including({}))
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'passes key_prefix option if specified' do
|
45
|
+
described_class.new_with_configuration(saver, {
|
46
|
+
s3_client_credentials: s3_client_credentials,
|
47
|
+
bucket_name: bucket_name,
|
48
|
+
key_prefix: key_prefix,
|
49
|
+
}, s3_object_configuration)
|
50
|
+
|
51
|
+
expect(Aws::S3::Client).to have_received(:new).with(s3_client_credentials)
|
52
|
+
expect(described_class).to have_received(:new).with(saver, s3_client, bucket_name, s3_object_configuration, hash_including(key_prefix: key_prefix))
|
38
53
|
end
|
39
54
|
|
40
55
|
it 'defaults the region to us-east-1' do
|
41
56
|
default_region = 'us-east-1'
|
42
57
|
|
43
|
-
|
44
|
-
allow(Capybara::Screenshot::S3Saver).to receive(:new)
|
45
|
-
|
46
|
-
Capybara::Screenshot::S3Saver.new_with_configuration(saver, {
|
58
|
+
described_class.new_with_configuration(saver, {
|
47
59
|
s3_client_credentials: s3_client_credentials_using_defaults,
|
48
60
|
bucket_name: bucket_name
|
49
61
|
}, s3_object_configuration)
|
@@ -52,23 +64,31 @@ describe Capybara::Screenshot::S3Saver do
|
|
52
64
|
s3_client_credentials.merge(region: default_region)
|
53
65
|
)
|
54
66
|
|
55
|
-
expect(
|
67
|
+
expect(described_class).to have_received(:new).with(saver, s3_client, bucket_name, s3_object_configuration, hash_including({}))
|
56
68
|
end
|
57
69
|
|
58
70
|
it 'stores the object configuration when passed' do
|
59
71
|
s3_object_configuration = { acl: 'public-read' }
|
60
72
|
Capybara::Screenshot.s3_object_configuration = { acl: 'public-read' }
|
61
73
|
|
62
|
-
|
63
|
-
allow(Capybara::Screenshot::S3Saver).to receive(:new)
|
64
|
-
|
65
|
-
Capybara::Screenshot::S3Saver.new_with_configuration(saver, {
|
74
|
+
described_class.new_with_configuration(saver, {
|
66
75
|
s3_client_credentials: s3_client_credentials,
|
67
76
|
bucket_name: bucket_name
|
68
77
|
}, s3_object_configuration)
|
69
78
|
|
70
79
|
expect(Aws::S3::Client).to have_received(:new).with(s3_client_credentials)
|
71
|
-
expect(
|
80
|
+
expect(described_class).to have_received(:new).with(saver, s3_client, bucket_name, s3_object_configuration, hash_including({}))
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'passes key_prefix option if specified' do
|
84
|
+
described_class.new_with_configuration(saver, {
|
85
|
+
s3_client_credentials: s3_client_credentials,
|
86
|
+
bucket_name: bucket_name,
|
87
|
+
key_prefix: key_prefix,
|
88
|
+
}, s3_object_configuration)
|
89
|
+
|
90
|
+
expect(Aws::S3::Client).to have_received(:new).with(s3_client_credentials)
|
91
|
+
expect(described_class).to have_received(:new).with(saver, s3_client, bucket_name, s3_object_configuration, hash_including(key_prefix: key_prefix))
|
72
92
|
end
|
73
93
|
end
|
74
94
|
|
@@ -121,9 +141,9 @@ describe Capybara::Screenshot::S3Saver do
|
|
121
141
|
s3_saver.save
|
122
142
|
end
|
123
143
|
|
124
|
-
|
144
|
+
context 'with object configuration' do
|
125
145
|
let(:s3_object_configuration) { { acl: 'public-read' } }
|
126
|
-
let(:s3_saver) {
|
146
|
+
let(:s3_saver) { described_class.new(saver, s3_client, bucket_name, s3_object_configuration) }
|
127
147
|
|
128
148
|
it 'uploads the html' do
|
129
149
|
html_path = '/foo/bar.html'
|
@@ -163,6 +183,86 @@ describe Capybara::Screenshot::S3Saver do
|
|
163
183
|
s3_saver.save
|
164
184
|
end
|
165
185
|
end
|
186
|
+
|
187
|
+
context 'with key_prefix specified' do
|
188
|
+
it 'uploads the html with key prefix' do
|
189
|
+
html_path = '/foo/bar.html'
|
190
|
+
expect(saver).to receive(:html_path).and_return(html_path)
|
191
|
+
expect(saver).to receive(:html_saved?).and_return(true)
|
192
|
+
|
193
|
+
html_file = double('html_file')
|
194
|
+
|
195
|
+
expect(File).to receive(:open).with(html_path).and_yield(html_file)
|
196
|
+
|
197
|
+
expect(s3_client).to receive(:put_object).with(
|
198
|
+
bucket: bucket_name,
|
199
|
+
key: 'some/path/bar.html',
|
200
|
+
body: html_file
|
201
|
+
)
|
202
|
+
|
203
|
+
s3_saver_with_key_prefix.save
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'uploads the screenshot with key prefix' do
|
207
|
+
screenshot_path = '/baz/bim.jpg'
|
208
|
+
expect(saver).to receive(:screenshot_path).and_return(screenshot_path)
|
209
|
+
expect(saver).to receive(:screenshot_saved?).and_return(true)
|
210
|
+
|
211
|
+
screenshot_file = double('screenshot_file')
|
212
|
+
|
213
|
+
expect(File).to receive(:open).with(screenshot_path).and_yield(screenshot_file)
|
214
|
+
|
215
|
+
expect(s3_client).to receive(:put_object).with(
|
216
|
+
bucket: bucket_name,
|
217
|
+
key: 'some/path/bim.jpg',
|
218
|
+
body: screenshot_file
|
219
|
+
)
|
220
|
+
|
221
|
+
s3_saver_with_key_prefix.save
|
222
|
+
end
|
223
|
+
|
224
|
+
context 'with object configuration' do
|
225
|
+
let(:s3_object_configuration) { { acl: 'public-read' } }
|
226
|
+
|
227
|
+
it 'uploads the html' do
|
228
|
+
html_path = '/foo/bar.html'
|
229
|
+
expect(saver).to receive(:html_path).and_return(html_path)
|
230
|
+
expect(saver).to receive(:html_saved?).and_return(true)
|
231
|
+
|
232
|
+
html_file = double('html_file')
|
233
|
+
|
234
|
+
expect(File).to receive(:open).with(html_path).and_yield(html_file)
|
235
|
+
|
236
|
+
expect(s3_client).to receive(:put_object).with(
|
237
|
+
bucket: bucket_name,
|
238
|
+
key: 'some/path/bar.html',
|
239
|
+
body: html_file,
|
240
|
+
acl: 'public-read'
|
241
|
+
)
|
242
|
+
|
243
|
+
s3_saver_with_key_prefix.save
|
244
|
+
end
|
245
|
+
|
246
|
+
it 'uploads the screenshot' do
|
247
|
+
screenshot_path = '/baz/bim.jpg'
|
248
|
+
expect(saver).to receive(:screenshot_path).and_return(screenshot_path)
|
249
|
+
expect(saver).to receive(:screenshot_saved?).and_return(true)
|
250
|
+
|
251
|
+
screenshot_file = double('screenshot_file')
|
252
|
+
|
253
|
+
expect(File).to receive(:open).with(screenshot_path).and_yield(screenshot_file)
|
254
|
+
|
255
|
+
expect(s3_client).to receive(:put_object).with(
|
256
|
+
bucket: bucket_name,
|
257
|
+
key: 'some/path/bim.jpg',
|
258
|
+
body: screenshot_file,
|
259
|
+
acl: 'public-read'
|
260
|
+
)
|
261
|
+
|
262
|
+
s3_saver_with_key_prefix.save
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
166
266
|
end
|
167
267
|
|
168
268
|
# Needed because we cannot depend on Verifying Doubles
|