kt-paperclip 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.hound.yml +1066 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +23 -0
- data/Appraisals +11 -0
- data/CONTRIBUTING.md +75 -0
- data/Gemfile +21 -0
- data/LICENSE +24 -0
- data/NEWS +420 -0
- data/README.md +977 -0
- data/RELEASING.md +17 -0
- data/Rakefile +44 -0
- data/UPGRADING +14 -0
- data/cucumber/paperclip_steps.rb +6 -0
- data/features/basic_integration.feature +80 -0
- data/features/migration.feature +94 -0
- data/features/rake_tasks.feature +62 -0
- data/features/step_definitions/attachment_steps.rb +110 -0
- data/features/step_definitions/html_steps.rb +15 -0
- data/features/step_definitions/rails_steps.rb +236 -0
- data/features/step_definitions/s3_steps.rb +14 -0
- data/features/step_definitions/web_steps.rb +107 -0
- data/features/support/env.rb +11 -0
- data/features/support/fakeweb.rb +13 -0
- data/features/support/file_helpers.rb +34 -0
- data/features/support/fixtures/boot_config.txt +15 -0
- data/features/support/fixtures/gemfile.txt +5 -0
- data/features/support/fixtures/preinitializer.txt +20 -0
- data/features/support/paths.rb +28 -0
- data/features/support/rails.rb +63 -0
- data/features/support/selectors.rb +19 -0
- data/gemfiles/3.2.gemfile +19 -0
- data/gemfiles/4.1.gemfile +19 -0
- data/gemfiles/4.2.gemfile +19 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +30 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
- data/lib/kt-paperclip.rb +1 -0
- data/lib/paperclip/attachment.rb +608 -0
- data/lib/paperclip/attachment_registry.rb +59 -0
- data/lib/paperclip/callbacks.rb +40 -0
- data/lib/paperclip/content_type_detector.rb +79 -0
- data/lib/paperclip/deprecations.rb +42 -0
- data/lib/paperclip/errors.rb +32 -0
- data/lib/paperclip/file_command_content_type_detector.rb +30 -0
- data/lib/paperclip/filename_cleaner.rb +16 -0
- data/lib/paperclip/geometry.rb +158 -0
- data/lib/paperclip/geometry_detector_factory.rb +48 -0
- data/lib/paperclip/geometry_parser_factory.rb +31 -0
- data/lib/paperclip/glue.rb +17 -0
- data/lib/paperclip/has_attached_file.rb +109 -0
- data/lib/paperclip/helpers.rb +56 -0
- data/lib/paperclip/interpolations/plural_cache.rb +18 -0
- data/lib/paperclip/interpolations.rb +197 -0
- data/lib/paperclip/io_adapters/abstract_adapter.rb +47 -0
- data/lib/paperclip/io_adapters/attachment_adapter.rb +36 -0
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +18 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
- data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
- data/lib/paperclip/io_adapters/registry.rb +32 -0
- data/lib/paperclip/io_adapters/stringio_adapter.rb +33 -0
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +42 -0
- data/lib/paperclip/io_adapters/uri_adapter.rb +63 -0
- data/lib/paperclip/locales/de.yml +18 -0
- data/lib/paperclip/locales/en.yml +18 -0
- data/lib/paperclip/locales/es.yml +18 -0
- data/lib/paperclip/locales/ja.yml +18 -0
- data/lib/paperclip/locales/pt-BR.yml +18 -0
- data/lib/paperclip/locales/zh-CN.yml +18 -0
- data/lib/paperclip/locales/zh-HK.yml +18 -0
- data/lib/paperclip/locales/zh-TW.yml +18 -0
- data/lib/paperclip/logger.rb +21 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +100 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +96 -0
- data/lib/paperclip/matchers.rb +64 -0
- data/lib/paperclip/media_type_spoof_detector.rb +89 -0
- data/lib/paperclip/missing_attachment_styles.rb +79 -0
- data/lib/paperclip/processor.rb +48 -0
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/railtie.rb +31 -0
- data/lib/paperclip/schema.rb +83 -0
- data/lib/paperclip/storage/filesystem.rb +90 -0
- data/lib/paperclip/storage/fog.rb +242 -0
- data/lib/paperclip/storage/s3.rb +440 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/style.rb +109 -0
- data/lib/paperclip/tempfile.rb +43 -0
- data/lib/paperclip/tempfile_factory.rb +23 -0
- data/lib/paperclip/thumbnail.rb +121 -0
- data/lib/paperclip/url_generator.rb +79 -0
- data/lib/paperclip/validators/attachment_content_type_validator.rb +88 -0
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +30 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +115 -0
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +74 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/paperclip.rb +213 -0
- data/lib/tasks/paperclip.rake +127 -0
- data/paperclip.gemspec +52 -0
- data/shoulda_macros/paperclip.rb +134 -0
- data/spec/database.yml +4 -0
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/spec/paperclip/attachment_processing_spec.rb +82 -0
- data/spec/paperclip/attachment_registry_spec.rb +130 -0
- data/spec/paperclip/attachment_spec.rb +1494 -0
- data/spec/paperclip/content_type_detector_spec.rb +48 -0
- data/spec/paperclip/deprecations_spec.rb +65 -0
- data/spec/paperclip/file_command_content_type_detector_spec.rb +26 -0
- data/spec/paperclip/filename_cleaner_spec.rb +14 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/spec/paperclip/geometry_parser_spec.rb +73 -0
- data/spec/paperclip/geometry_spec.rb +255 -0
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +142 -0
- data/spec/paperclip/integration_spec.rb +667 -0
- data/spec/paperclip/interpolations_spec.rb +262 -0
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +139 -0
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +83 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +104 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
- data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +127 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +99 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +222 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/spec/paperclip/processor_spec.rb +26 -0
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/spec/paperclip/rake_spec.rb +103 -0
- data/spec/paperclip/schema_spec.rb +248 -0
- data/spec/paperclip/storage/filesystem_spec.rb +79 -0
- data/spec/paperclip/storage/fog_spec.rb +540 -0
- data/spec/paperclip/storage/s3_live_spec.rb +182 -0
- data/spec/paperclip/storage/s3_spec.rb +1526 -0
- data/spec/paperclip/style_spec.rb +255 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/thumbnail_spec.rb +500 -0
- data/spec/paperclip/url_generator_spec.rb +221 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +229 -0
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/assertions.rb +71 -0
- data/spec/support/deprecations.rb +9 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/12k.png +0 -0
- data/spec/support/fixtures/50x50.png +0 -0
- data/spec/support/fixtures/5k.png +0 -0
- data/spec/support/fixtures/animated +0 -0
- data/spec/support/fixtures/animated.gif +0 -0
- data/spec/support/fixtures/animated.unknown +0 -0
- data/spec/support/fixtures/bad.png +1 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/fog.yml +8 -0
- data/spec/support/fixtures/rotated.jpg +0 -0
- data/spec/support/fixtures/s3.yml +8 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/fixtures/spaced file.png +0 -0
- data/spec/support/fixtures/text.txt +1 -0
- data/spec/support/fixtures/twopage.pdf +0 -0
- data/spec/support/fixtures/uppercase.PNG +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/spec/support/mock_attachment.rb +22 -0
- data/spec/support/mock_interpolator.rb +24 -0
- data/spec/support/mock_url_generator_builder.rb +27 -0
- data/spec/support/model_reconstruction.rb +60 -0
- data/spec/support/rails_helpers.rb +7 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +648 -0
@@ -0,0 +1,540 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fog/aws'
|
3
|
+
require 'fog/local'
|
4
|
+
require 'timecop'
|
5
|
+
|
6
|
+
describe Paperclip::Storage::Fog do
|
7
|
+
context "" do
|
8
|
+
before { Fog.mock! }
|
9
|
+
|
10
|
+
context "with credentials provided in a path string" do
|
11
|
+
before do
|
12
|
+
rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
|
13
|
+
storage: :fog,
|
14
|
+
url: '/:attachment/:filename',
|
15
|
+
fog_directory: "paperclip",
|
16
|
+
fog_credentials: fixture_file('fog.yml')
|
17
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
18
|
+
@dummy = Dummy.new
|
19
|
+
@dummy.avatar = @file
|
20
|
+
end
|
21
|
+
|
22
|
+
after { @file.close }
|
23
|
+
|
24
|
+
it "has the proper information loading credentials from a file" do
|
25
|
+
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with credentials provided in a File object" do
|
30
|
+
before do
|
31
|
+
rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
|
32
|
+
storage: :fog,
|
33
|
+
url: '/:attachment/:filename',
|
34
|
+
fog_directory: "paperclip",
|
35
|
+
fog_credentials: File.open(fixture_file('fog.yml'))
|
36
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
37
|
+
@dummy = Dummy.new
|
38
|
+
@dummy.avatar = @file
|
39
|
+
end
|
40
|
+
|
41
|
+
after { @file.close }
|
42
|
+
|
43
|
+
it "has the proper information loading credentials from a file" do
|
44
|
+
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with default values for path and url" do
|
49
|
+
before do
|
50
|
+
rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
|
51
|
+
storage: :fog,
|
52
|
+
url: '/:attachment/:filename',
|
53
|
+
fog_directory: "paperclip",
|
54
|
+
fog_credentials: {
|
55
|
+
provider: 'AWS',
|
56
|
+
aws_access_key_id: 'AWS_ID',
|
57
|
+
aws_secret_access_key: 'AWS_SECRET'
|
58
|
+
}
|
59
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
60
|
+
@dummy = Dummy.new
|
61
|
+
@dummy.avatar = @file
|
62
|
+
end
|
63
|
+
|
64
|
+
after { @file.close }
|
65
|
+
|
66
|
+
it "is able to interpolate the path without blowing up" do
|
67
|
+
assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../../../tmp/public/avatars/5k.png")),
|
68
|
+
@dummy.avatar.path
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "with no path or url given and using defaults" do
|
73
|
+
before do
|
74
|
+
rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
|
75
|
+
storage: :fog,
|
76
|
+
fog_directory: "paperclip",
|
77
|
+
fog_credentials: {
|
78
|
+
provider: 'AWS',
|
79
|
+
aws_access_key_id: 'AWS_ID',
|
80
|
+
aws_secret_access_key: 'AWS_SECRET'
|
81
|
+
}
|
82
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
83
|
+
@dummy = Dummy.new
|
84
|
+
@dummy.id = 1
|
85
|
+
@dummy.avatar = @file
|
86
|
+
end
|
87
|
+
|
88
|
+
after { @file.close }
|
89
|
+
|
90
|
+
it "has correct path and url from interpolated defaults" do
|
91
|
+
assert_equal "dummies/avatars/000/000/001/original/5k.png", @dummy.avatar.path
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "with file params provided as lambda" do
|
96
|
+
before do
|
97
|
+
fog_file = lambda{ |a| { custom_header: a.instance.custom_method }}
|
98
|
+
klass = rebuild_model storage: :fog,
|
99
|
+
fog_file: fog_file
|
100
|
+
|
101
|
+
klass.class_eval do
|
102
|
+
def custom_method
|
103
|
+
'foobar'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
@dummy = Dummy.new
|
109
|
+
end
|
110
|
+
|
111
|
+
it "is able to evaluate correct values for file headers" do
|
112
|
+
assert_equal @dummy.avatar.send(:fog_file), { custom_header: 'foobar' }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
before do
|
117
|
+
@fog_directory = 'papercliptests'
|
118
|
+
|
119
|
+
@credentials = {
|
120
|
+
provider: 'AWS',
|
121
|
+
aws_access_key_id: 'ID',
|
122
|
+
aws_secret_access_key: 'SECRET'
|
123
|
+
}
|
124
|
+
|
125
|
+
@connection = Fog::Storage.new(@credentials)
|
126
|
+
@connection.directories.create(
|
127
|
+
key: @fog_directory
|
128
|
+
)
|
129
|
+
|
130
|
+
@options = {
|
131
|
+
fog_directory: @fog_directory,
|
132
|
+
fog_credentials: @credentials,
|
133
|
+
fog_host: nil,
|
134
|
+
fog_file: {cache_control: 1234},
|
135
|
+
path: ":attachment/:basename:dotextension",
|
136
|
+
storage: :fog
|
137
|
+
}
|
138
|
+
|
139
|
+
rebuild_model(@options)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "is extended by the Fog module" do
|
143
|
+
assert Dummy.new.avatar.is_a?(Paperclip::Storage::Fog)
|
144
|
+
end
|
145
|
+
|
146
|
+
context "when assigned" do
|
147
|
+
before do
|
148
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
149
|
+
@dummy = Dummy.new
|
150
|
+
@dummy.avatar = @file
|
151
|
+
end
|
152
|
+
|
153
|
+
after do
|
154
|
+
@file.close
|
155
|
+
directory = @connection.directories.new(key: @fog_directory)
|
156
|
+
directory.files.each {|file| file.destroy}
|
157
|
+
directory.destroy
|
158
|
+
end
|
159
|
+
|
160
|
+
it "is rewound after flush_writes" do
|
161
|
+
@dummy.avatar.instance_eval "def after_flush_writes; end"
|
162
|
+
|
163
|
+
files = @dummy.avatar.queued_for_write.values
|
164
|
+
@dummy.save
|
165
|
+
assert files.none?(&:eof?), "Expect all the files to be rewinded."
|
166
|
+
end
|
167
|
+
|
168
|
+
it "is removed after after_flush_writes" do
|
169
|
+
paths = @dummy.avatar.queued_for_write.values.map(&:path)
|
170
|
+
@dummy.save
|
171
|
+
assert paths.none?{ |path| File.exist?(path) },
|
172
|
+
"Expect all the files to be deleted."
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'is able to be copied to a local file' do
|
176
|
+
@dummy.save
|
177
|
+
tempfile = Tempfile.new("known_location")
|
178
|
+
tempfile.binmode
|
179
|
+
@dummy.avatar.copy_to_local_file(:original, tempfile.path)
|
180
|
+
tempfile.rewind
|
181
|
+
assert_equal @connection.directories.get(@fog_directory).files.get(@dummy.avatar.path).body,
|
182
|
+
tempfile.read
|
183
|
+
tempfile.close
|
184
|
+
end
|
185
|
+
|
186
|
+
it "passes the content type to the Fog::Storage::AWS::Files instance" do
|
187
|
+
Fog::Storage::AWS::Files.any_instance.expects(:create).with do |hash|
|
188
|
+
hash[:content_type]
|
189
|
+
end
|
190
|
+
@dummy.save
|
191
|
+
end
|
192
|
+
|
193
|
+
context "without a bucket" do
|
194
|
+
before do
|
195
|
+
@connection.directories.get(@fog_directory).destroy
|
196
|
+
end
|
197
|
+
|
198
|
+
it "creates the bucket" do
|
199
|
+
assert @dummy.save
|
200
|
+
assert @connection.directories.get(@fog_directory)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "sucessfully rewinds the file during bucket creation" do
|
204
|
+
assert @dummy.save
|
205
|
+
expect(Paperclip.io_adapters.for(@dummy.avatar).read.length).to be > 0
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
context "with a bucket" do
|
210
|
+
it "succeeds" do
|
211
|
+
assert @dummy.save
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context "without a fog_host" do
|
216
|
+
before do
|
217
|
+
rebuild_model(@options.merge(fog_host: nil))
|
218
|
+
@dummy = Dummy.new
|
219
|
+
@dummy.avatar = StringIO.new('.')
|
220
|
+
@dummy.save
|
221
|
+
end
|
222
|
+
|
223
|
+
it "provides a public url" do
|
224
|
+
assert !@dummy.avatar.url.nil?
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context "with a fog_host" do
|
229
|
+
before do
|
230
|
+
rebuild_model(@options.merge(fog_host: 'http://example.com'))
|
231
|
+
@dummy = Dummy.new
|
232
|
+
@dummy.avatar = StringIO.new(".\n")
|
233
|
+
@dummy.save
|
234
|
+
end
|
235
|
+
|
236
|
+
it "provides a public url" do
|
237
|
+
expect(@dummy.avatar.url).to match(/^http:\/\/example\.com\/avatars\/data\?\d*$/)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context "with a fog_host that includes a wildcard placeholder" do
|
242
|
+
before do
|
243
|
+
rebuild_model(
|
244
|
+
fog_directory: @fog_directory,
|
245
|
+
fog_credentials: @credentials,
|
246
|
+
fog_host: 'http://img%d.example.com',
|
247
|
+
path: ":attachment/:basename:dotextension",
|
248
|
+
storage: :fog
|
249
|
+
)
|
250
|
+
@dummy = Dummy.new
|
251
|
+
@dummy.avatar = StringIO.new(".\n")
|
252
|
+
@dummy.save
|
253
|
+
end
|
254
|
+
|
255
|
+
it "provides a public url" do
|
256
|
+
expect(@dummy.avatar.url).to match(/^http:\/\/img[0123]\.example\.com\/avatars\/data\?\d*$/)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context "with fog_public set to false" do
|
261
|
+
before do
|
262
|
+
rebuild_model(@options.merge(fog_public: false))
|
263
|
+
@dummy = Dummy.new
|
264
|
+
@dummy.avatar = StringIO.new('.')
|
265
|
+
@dummy.save
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'sets the @fog_public instance variable to false' do
|
269
|
+
assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
|
270
|
+
assert_equal false, @dummy.avatar.fog_public
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context "with styles set and fog_public set to false" do
|
275
|
+
before do
|
276
|
+
rebuild_model(@options.merge(fog_public: false, styles: { medium: "300x300>", thumb: "100x100>" }))
|
277
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
278
|
+
@dummy = Dummy.new
|
279
|
+
@dummy.avatar = @file
|
280
|
+
@dummy.save
|
281
|
+
end
|
282
|
+
|
283
|
+
it 'sets the @fog_public for a particular style to false' do
|
284
|
+
assert_equal false, @dummy.avatar.instance_variable_get('@options')[:fog_public]
|
285
|
+
assert_equal false, @dummy.avatar.fog_public(:thumb)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
context "with styles set and fog_public set per-style" do
|
290
|
+
before do
|
291
|
+
rebuild_model(@options.merge(fog_public: { medium: false, thumb: true}, styles: { medium: "300x300>", thumb: "100x100>" }))
|
292
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
293
|
+
@dummy = Dummy.new
|
294
|
+
@dummy.avatar = @file
|
295
|
+
@dummy.save
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'sets the fog_public for a particular style to correct value' do
|
299
|
+
assert_equal false, @dummy.avatar.fog_public(:medium)
|
300
|
+
assert_equal true, @dummy.avatar.fog_public(:thumb)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
context "with fog_public not set" do
|
305
|
+
before do
|
306
|
+
rebuild_model(@options)
|
307
|
+
@dummy = Dummy.new
|
308
|
+
@dummy.avatar = StringIO.new('.')
|
309
|
+
@dummy.save
|
310
|
+
end
|
311
|
+
|
312
|
+
it "defaults fog_public to true" do
|
313
|
+
assert_equal true, @dummy.avatar.fog_public
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
context "with scheme set" do
|
318
|
+
before do
|
319
|
+
rebuild_model(@options.merge(:fog_credentials => @credentials.merge(:scheme => 'http')))
|
320
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
321
|
+
@dummy = Dummy.new
|
322
|
+
@dummy.avatar = @file
|
323
|
+
@dummy.save
|
324
|
+
end
|
325
|
+
|
326
|
+
it "honors the scheme in public url" do
|
327
|
+
assert_match(/^http:\/\//, @dummy.avatar.url)
|
328
|
+
end
|
329
|
+
it "honors the scheme in expiring url" do
|
330
|
+
assert_match(/^http:\/\//, @dummy.avatar.expiring_url)
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
context "with scheme not set" do
|
335
|
+
before do
|
336
|
+
rebuild_model(@options)
|
337
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
338
|
+
@dummy = Dummy.new
|
339
|
+
@dummy.avatar = @file
|
340
|
+
@dummy.save
|
341
|
+
end
|
342
|
+
|
343
|
+
it "provides HTTPS public url" do
|
344
|
+
assert_match(/^https:\/\//, @dummy.avatar.url)
|
345
|
+
end
|
346
|
+
it "provides HTTPS expiring url" do
|
347
|
+
assert_match(/^https:\/\//, @dummy.avatar.expiring_url)
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
context "with a valid bucket name for a subdomain" do
|
352
|
+
before { @dummy.stubs(:new_record?).returns(false) }
|
353
|
+
|
354
|
+
it "provides an url in subdomain style" do
|
355
|
+
assert_match(/^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png/, @dummy.avatar.url)
|
356
|
+
end
|
357
|
+
|
358
|
+
it "provides an url that expires in subdomain style" do
|
359
|
+
assert_match(/^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png.+Expires=.+$/, @dummy.avatar.expiring_url)
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
context "generating an expiring url" do
|
364
|
+
it "generates the same url when using Times and Integer offsets" do
|
365
|
+
Timecop.freeze do
|
366
|
+
offset = 1234
|
367
|
+
rebuild_model(@options)
|
368
|
+
dummy = Dummy.new
|
369
|
+
dummy.avatar = StringIO.new('.')
|
370
|
+
|
371
|
+
assert_equal dummy.avatar.expiring_url(offset),
|
372
|
+
dummy.avatar.expiring_url(Time.now + offset )
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
it 'matches the default url if there is no assignment' do
|
377
|
+
dummy = Dummy.new
|
378
|
+
assert_equal dummy.avatar.url, dummy.avatar.expiring_url
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'matches the default url when given a style if there is no assignment' do
|
382
|
+
dummy = Dummy.new
|
383
|
+
assert_equal dummy.avatar.url(:thumb), dummy.avatar.expiring_url(3600, :thumb)
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
context "with an invalid bucket name for a subdomain" do
|
388
|
+
before do
|
389
|
+
rebuild_model(@options.merge(fog_directory: "this_is_invalid"))
|
390
|
+
@dummy = Dummy.new
|
391
|
+
@dummy.avatar = @file
|
392
|
+
@dummy.save
|
393
|
+
end
|
394
|
+
|
395
|
+
it "does not match the bucket-subdomain restrictions" do
|
396
|
+
invalid_subdomains = %w(this_is_invalid in iamareallylongbucketnameiamareallylongbucketnameiamareallylongbu invalid- inval..id inval-.id inval.-id -invalid 192.168.10.2)
|
397
|
+
invalid_subdomains.each do |name|
|
398
|
+
assert_no_match Paperclip::Storage::Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX, name
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
it "provides an url in folder style" do
|
403
|
+
assert_match(/^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?\d*$/, @dummy.avatar.url)
|
404
|
+
end
|
405
|
+
|
406
|
+
it "provides a url that expires in folder style" do
|
407
|
+
assert_match(/^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png.+Expires=.+$/, @dummy.avatar.expiring_url)
|
408
|
+
end
|
409
|
+
|
410
|
+
end
|
411
|
+
|
412
|
+
context "with a proc for a bucket name evaluating a model method" do
|
413
|
+
before do
|
414
|
+
@dynamic_fog_directory = 'dynamicpaperclip'
|
415
|
+
rebuild_model(@options.merge(fog_directory: lambda { |attachment| attachment.instance.bucket_name }))
|
416
|
+
@dummy = Dummy.new
|
417
|
+
@dummy.stubs(:bucket_name).returns(@dynamic_fog_directory)
|
418
|
+
@dummy.avatar = @file
|
419
|
+
@dummy.save
|
420
|
+
end
|
421
|
+
|
422
|
+
it "has created the bucket" do
|
423
|
+
assert @connection.directories.get(@dynamic_fog_directory).inspect
|
424
|
+
end
|
425
|
+
|
426
|
+
end
|
427
|
+
|
428
|
+
context "with a proc for the fog_host evaluating a model method" do
|
429
|
+
before do
|
430
|
+
rebuild_model(@options.merge(fog_host: lambda { |attachment| attachment.instance.fog_host }))
|
431
|
+
@dummy = Dummy.new
|
432
|
+
@dummy.stubs(:fog_host).returns('http://dynamicfoghost.com')
|
433
|
+
@dummy.avatar = @file
|
434
|
+
@dummy.save
|
435
|
+
end
|
436
|
+
|
437
|
+
it "provides a public url" do
|
438
|
+
assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
|
439
|
+
end
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
context "with a custom fog_host" do
|
444
|
+
before do
|
445
|
+
rebuild_model(@options.merge(fog_host: "http://dynamicfoghost.com"))
|
446
|
+
@dummy = Dummy.new
|
447
|
+
@dummy.avatar = @file
|
448
|
+
@dummy.save
|
449
|
+
end
|
450
|
+
|
451
|
+
it "provides a public url" do
|
452
|
+
assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.url)
|
453
|
+
end
|
454
|
+
|
455
|
+
it "provides an expiring url" do
|
456
|
+
assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
|
457
|
+
end
|
458
|
+
|
459
|
+
context "with an invalid bucket name for a subdomain" do
|
460
|
+
before do
|
461
|
+
rebuild_model(@options.merge({fog_directory: "this_is_invalid", fog_host: "http://dynamicfoghost.com"}))
|
462
|
+
@dummy = Dummy.new
|
463
|
+
@dummy.avatar = @file
|
464
|
+
@dummy.save
|
465
|
+
end
|
466
|
+
|
467
|
+
it "provides an expiring url" do
|
468
|
+
assert_match(/http:\/\/dynamicfoghost\.com/, @dummy.avatar.expiring_url)
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
end
|
473
|
+
|
474
|
+
context "with a proc for the fog_credentials evaluating a model method" do
|
475
|
+
before do
|
476
|
+
@dynamic_fog_credentials = {
|
477
|
+
provider: 'AWS',
|
478
|
+
aws_access_key_id: 'DYNAMIC_ID',
|
479
|
+
aws_secret_access_key: 'DYNAMIC_SECRET'
|
480
|
+
}
|
481
|
+
rebuild_model(@options.merge(fog_credentials: lambda { |attachment| attachment.instance.fog_credentials }))
|
482
|
+
@dummy = Dummy.new
|
483
|
+
@dummy.stubs(:fog_credentials).returns(@dynamic_fog_credentials)
|
484
|
+
@dummy.avatar = @file
|
485
|
+
@dummy.save
|
486
|
+
end
|
487
|
+
|
488
|
+
it "provides a public url" do
|
489
|
+
assert_equal @dummy.avatar.fog_credentials, @dynamic_fog_credentials
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
context "with custom fog_options" do
|
494
|
+
before do
|
495
|
+
rebuild_model(
|
496
|
+
@options.merge(fog_options: { multipart_chunk_size: 104857600 }),
|
497
|
+
)
|
498
|
+
@dummy = Dummy.new
|
499
|
+
@dummy.avatar = @file
|
500
|
+
end
|
501
|
+
|
502
|
+
it "applies the options to the fog #create call" do
|
503
|
+
files = stub
|
504
|
+
@dummy.avatar.stubs(:directory).returns stub(files: files)
|
505
|
+
files.expects(:create).with(
|
506
|
+
has_entries(multipart_chunk_size: 104857600),
|
507
|
+
)
|
508
|
+
@dummy.save
|
509
|
+
end
|
510
|
+
end
|
511
|
+
end
|
512
|
+
|
513
|
+
end
|
514
|
+
|
515
|
+
context "when using local storage" do
|
516
|
+
before do
|
517
|
+
Fog.unmock!
|
518
|
+
rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
|
519
|
+
storage: :fog,
|
520
|
+
url: '/:attachment/:filename',
|
521
|
+
fog_directory: "paperclip",
|
522
|
+
fog_credentials: { provider: :local, local_root: "." },
|
523
|
+
fog_host: 'localhost'
|
524
|
+
|
525
|
+
@file = File.new(fixture_file('5k.png'), 'rb')
|
526
|
+
@dummy = Dummy.new
|
527
|
+
@dummy.avatar = @file
|
528
|
+
@dummy.stubs(:new_record?).returns(false)
|
529
|
+
end
|
530
|
+
|
531
|
+
after do
|
532
|
+
@file.close
|
533
|
+
Fog.mock!
|
534
|
+
end
|
535
|
+
|
536
|
+
it "returns the public url in place of the expiring url" do
|
537
|
+
assert_match @dummy.avatar.public_url, @dummy.avatar.expiring_url
|
538
|
+
end
|
539
|
+
end
|
540
|
+
end
|