kt-paperclip 6.2.0 → 6.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  4. data/.github/ISSUE_TEMPLATE/custom.md +10 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.hound.yml +364 -357
  7. data/.rubocop.yml +2 -0
  8. data/.travis.yml +7 -9
  9. data/Gemfile +3 -2
  10. data/README.md +38 -17
  11. data/lib/kt-paperclip.rb +1 -0
  12. data/lib/paperclip.rb +2 -1
  13. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +2 -2
  14. data/lib/paperclip/io_adapters/uri_adapter.rb +13 -3
  15. data/lib/paperclip/schema.rb +2 -2
  16. data/lib/paperclip/storage/s3.rb +2 -2
  17. data/lib/paperclip/url_generator.rb +8 -1
  18. data/lib/paperclip/validators.rb +4 -4
  19. data/lib/paperclip/validators/attachment_content_type_validator.rb +8 -1
  20. data/lib/paperclip/validators/attachment_file_name_validator.rb +8 -1
  21. data/lib/paperclip/validators/attachment_size_validator.rb +17 -1
  22. data/lib/paperclip/version.rb +1 -1
  23. data/paperclip.gemspec +3 -3
  24. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +20 -15
  25. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +13 -3
  26. data/spec/paperclip/storage/s3_spec.rb +54 -3
  27. data/spec/paperclip/url_generator_spec.rb +10 -0
  28. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +88 -0
  29. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +90 -0
  30. data/spec/paperclip/validators/attachment_size_validator_spec.rb +90 -0
  31. data/spec/support/fixtures/aws_s3.yml +13 -0
  32. data/spec/support/model_reconstruction.rb +1 -1
  33. metadata +14 -9
  34. data/.github/issue_template.md +0 -3
@@ -16,54 +16,59 @@ describe Paperclip::HttpUrlProxyAdapter do
16
16
  context "a new instance" do
17
17
  before do
18
18
  @url = "http://thoughtbot.com/images/thoughtbot-logo.png"
19
- @subject = Paperclip.io_adapters.for(@url, hash_digest: Digest::MD5)
20
19
  end
21
20
 
21
+ subject { Paperclip.io_adapters.for(@url, hash_digest: Digest::MD5) }
22
+
22
23
  after do
23
- @subject.close
24
+ subject.close
24
25
  end
25
26
 
26
27
  it "returns a file name" do
27
- assert_equal "thoughtbot-logo.png", @subject.original_filename
28
+ expect(subject.original_filename).to(eq("thoughtbot-logo.png"))
28
29
  end
29
30
 
30
31
  it "closes open handle after reading" do
31
- assert_equal true, @open_return.closed?
32
+ expect { subject }.to(change { @open_return.closed? }.from(false).to(true))
32
33
  end
33
34
 
34
35
  it "returns a content type" do
35
- assert_equal "image/png", @subject.content_type
36
+ expect(subject.content_type).to(eq("image/png"))
36
37
  end
37
38
 
38
39
  it "returns the size of the data" do
39
- assert_equal @open_return.size, @subject.size
40
+ expect(subject.size).to(eq(@open_return.size))
40
41
  end
41
42
 
42
43
  it "generates an MD5 hash of the contents" do
43
- assert_equal Digest::MD5.hexdigest("xxx"), @subject.fingerprint
44
+ expect(subject.fingerprint).to(eq(Digest::MD5.hexdigest("xxx")))
44
45
  end
45
46
 
46
47
  it "generates correct fingerprint after read" do
47
- fingerprint = Digest::MD5.hexdigest(@subject.read)
48
- assert_equal fingerprint, @subject.fingerprint
48
+ fingerprint = Digest::MD5.hexdigest(subject.read)
49
+ expect(subject.fingerprint).to(eq(fingerprint))
49
50
  end
50
51
 
51
52
  it "generates same fingerprint" do
52
- assert_equal @subject.fingerprint, @subject.fingerprint
53
+ expect(subject.fingerprint).to(eq(subject.fingerprint))
53
54
  end
54
55
 
55
56
  it "returns the data contained in the StringIO" do
56
- assert_equal "xxx", @subject.read
57
+ expect(subject.read).to(eq("xxx"))
57
58
  end
58
59
 
59
60
  it "accepts a content_type" do
60
- @subject.content_type = "image/png"
61
- assert_equal "image/png", @subject.content_type
61
+ subject.content_type = "image/png"
62
+ expect(subject.content_type).to(eq("image/png"))
62
63
  end
63
64
 
64
65
  it "accepts an original_filename" do
65
- @subject.original_filename = "image.png"
66
- assert_equal "image.png", @subject.original_filename
66
+ subject.original_filename = "image.png"
67
+ expect(subject.original_filename).to(eq("image.png"))
68
+ end
69
+
70
+ it "doesn't emit deprecation warnings" do
71
+ expect { subject }.to_not(output(/URI\.(un)?escape is obsolete/).to_stderr)
67
72
  end
68
73
  end
69
74
 
@@ -193,9 +193,18 @@ describe Paperclip::UriAdapter do
193
193
 
194
194
  describe "#download_content" do
195
195
  before do
196
- allow_any_instance_of(Paperclip::UriAdapter).to receive(:open).and_return(@open_return)
196
+ allowed_mock =
197
+ if RUBY_VERSION < '2.5'
198
+ allow_any_instance_of(Paperclip::UriAdapter)
199
+ else
200
+ allow(URI)
201
+ end
202
+
203
+ allowed_mock.to receive(:open).and_return(@open_return)
204
+
197
205
  @uri = URI.parse("https://github.com/thoughtbot/paper:clip.jpg")
198
206
  @subject = Paperclip.io_adapters.for(@uri)
207
+ @uri_opener = RUBY_VERSION < '2.5' ? @subject : URI
199
208
  end
200
209
 
201
210
  after do
@@ -204,7 +213,7 @@ describe Paperclip::UriAdapter do
204
213
 
205
214
  context "with default read_timeout" do
206
215
  it "calls open without options" do
207
- expect(@subject).to receive(:open).with(@uri, {}).at_least(1).times
216
+ expect(@uri_opener).to receive(:open).with(@uri, {}).at_least(1).times
208
217
  end
209
218
  end
210
219
 
@@ -214,7 +223,8 @@ describe Paperclip::UriAdapter do
214
223
  end
215
224
 
216
225
  it "calls open with read_timeout option" do
217
- expect(@subject).to receive(:open).with(@uri, read_timeout: 120).at_least(1).times
226
+ expect(@uri_opener)
227
+ .to receive(:open).with(@uri, read_timeout: 120).at_least(1).times
218
228
  end
219
229
  end
220
230
  end
@@ -805,9 +805,7 @@ describe Paperclip::Storage::S3 do
805
805
  s3_region: "ap-northeast-1",
806
806
  s3_host_name: "s3-ap-northeast-1.amazonaws.com"
807
807
  },
808
- test: {
809
- s3_region: ""
810
- }
808
+ test: {}
811
809
  }
812
810
  @dummy = Dummy.new
813
811
  end
@@ -1439,6 +1437,32 @@ describe Paperclip::Storage::S3 do
1439
1437
  end
1440
1438
  end
1441
1439
 
1440
+ context "with S3 credentials supplied as Pathname and aliases being set" do
1441
+ before do
1442
+ ENV["S3_KEY"] = "pathname_key"
1443
+ ENV["S3_BUCKET"] = "pathname_bucket"
1444
+ ENV["S3_SECRET"] = "pathname_secret"
1445
+
1446
+ rails_env("test") do
1447
+ rebuild_model aws2_add_region.merge storage: :s3,
1448
+ s3_credentials: Pathname.new(fixture_file("aws_s3.yml"))
1449
+
1450
+ Dummy.delete_all
1451
+ @dummy = Dummy.new
1452
+ end
1453
+ end
1454
+
1455
+ it "parses the credentials" do
1456
+ assert_equal "pathname_bucket", @dummy.avatar.bucket_name
1457
+
1458
+ assert_equal "pathname_key",
1459
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1460
+
1461
+ assert_equal "pathname_secret",
1462
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1463
+ end
1464
+ end
1465
+
1442
1466
  context "with S3 credentials in a YAML file" do
1443
1467
  before do
1444
1468
  ENV["S3_KEY"] = "env_key"
@@ -1466,6 +1490,33 @@ describe Paperclip::Storage::S3 do
1466
1490
  end
1467
1491
  end
1468
1492
 
1493
+ context "with S3 credentials in a YAML file and aliases being set" do
1494
+ before do
1495
+ ENV["S3_KEY"] = "env_key"
1496
+ ENV["S3_BUCKET"] = "env_bucket"
1497
+ ENV["S3_SECRET"] = "env_secret"
1498
+
1499
+ rails_env("test") do
1500
+ rebuild_model aws2_add_region.merge storage: :s3,
1501
+ s3_credentials: File.new(fixture_file("aws_s3.yml"))
1502
+
1503
+ Dummy.delete_all
1504
+
1505
+ @dummy = Dummy.new
1506
+ end
1507
+ end
1508
+
1509
+ it "runs the file through ERB" do
1510
+ assert_equal "env_bucket", @dummy.avatar.bucket_name
1511
+
1512
+ assert_equal "env_key",
1513
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1514
+
1515
+ assert_equal "env_secret",
1516
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1517
+ end
1518
+ end
1519
+
1469
1520
  context "S3 Permissions" do
1470
1521
  context "defaults to :public_read" do
1471
1522
  before do
@@ -194,6 +194,16 @@ describe Paperclip::UrlGenerator do
194
194
  "expected the interpolator to be passed #{expected.inspect} but it wasn't"
195
195
  end
196
196
 
197
+ it "doesn't emit deprecation warnings" do
198
+ expected = "the expected result"
199
+ mock_interpolator = MockInterpolator.new(result: expected)
200
+ options = { interpolator: mock_interpolator }
201
+ mock_attachment = MockAttachment.new(options)
202
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
203
+
204
+ expect { url_generator.for(:style_name, escape: true) }.to_not(output(/URI\.(un)?escape is obsolete/).to_stderr)
205
+ end
206
+
197
207
  describe "should be able to escape (, ), [, and ]." do
198
208
  def generate(expected, updated_at = nil)
199
209
  mock_interpolator = MockInterpolator.new(result: expected)
@@ -67,6 +67,94 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
67
67
  end
68
68
  end
69
69
 
70
+ context "with add_validation_errors_to not set (implicitly :both)" do
71
+ it "adds error to both attribute and base" do
72
+ build_validator content_type: "image/png", allow_nil: false
73
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
74
+ @validator.validate(@dummy)
75
+
76
+ assert @dummy.errors[:avatar_content_type].present?,
77
+ "Error not added to attribute"
78
+
79
+ assert @dummy.errors[:avatar].present?,
80
+ "Error not added to base attribute"
81
+ end
82
+ end
83
+
84
+ context "with add_validation_errors_to set to :attribute globally" do
85
+ before do
86
+ Paperclip.options[:add_validation_errors_to] = :attribute
87
+ end
88
+
89
+ after do
90
+ Paperclip.options[:add_validation_errors_to] = :both
91
+ end
92
+
93
+ it "only adds error to attribute not base" do
94
+ build_validator content_type: "image/png", allow_nil: false
95
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
96
+ @validator.validate(@dummy)
97
+
98
+ assert @dummy.errors[:avatar_content_type].present?,
99
+ "Error not added to attribute"
100
+
101
+ assert @dummy.errors[:avatar].blank?,
102
+ "Error added to base attribute"
103
+ end
104
+ end
105
+
106
+ context "with add_validation_errors_to set to :base globally" do
107
+ before do
108
+ Paperclip.options[:add_validation_errors_to] = :base
109
+ end
110
+
111
+ after do
112
+ Paperclip.options[:add_validation_errors_to] = :both
113
+ end
114
+
115
+ it "only adds error to base not attribute" do
116
+ build_validator content_type: "image/png", allow_nil: false
117
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
118
+ @validator.validate(@dummy)
119
+
120
+ assert @dummy.errors[:avatar].present?,
121
+ "Error not added to base attribute"
122
+
123
+ assert @dummy.errors[:avatar_content_type].blank?,
124
+ "Error added to attribute"
125
+ end
126
+ end
127
+
128
+ context "with add_validation_errors_to set to :attribute" do
129
+ it "only adds error to attribute not base" do
130
+ build_validator content_type: "image/png", allow_nil: false,
131
+ add_validation_errors_to: :attribute
132
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
133
+ @validator.validate(@dummy)
134
+
135
+ assert @dummy.errors[:avatar_content_type].present?,
136
+ "Error not added to attribute"
137
+
138
+ assert @dummy.errors[:avatar].blank?,
139
+ "Error added to base attribute"
140
+ end
141
+ end
142
+
143
+ context "with add_validation_errors_to set to :base" do
144
+ it "only adds error to base not attribute" do
145
+ build_validator content_type: "image/png", allow_nil: false,
146
+ add_validation_errors_to: :base
147
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
148
+ @validator.validate(@dummy)
149
+
150
+ assert @dummy.errors[:avatar].present?,
151
+ "Error not added to base attribute"
152
+
153
+ assert @dummy.errors[:avatar_content_type].blank?,
154
+ "Error added to attribute"
155
+ end
156
+ end
157
+
70
158
  context "with a successful validation" do
71
159
  before do
72
160
  build_validator content_type: "image/png", allow_nil: false
@@ -29,6 +29,96 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
29
29
  end
30
30
  end
31
31
 
32
+ context "with add_validation_errors_to not set (implicitly :both)" do
33
+ it "adds error to both attribute and base" do
34
+ build_validator matches: /.*\.png$/, allow_nil: false
35
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
36
+ @validator.validate(@dummy)
37
+
38
+ assert @dummy.errors[:avatar_file_name].present?,
39
+ "Error not added to attribute"
40
+
41
+ assert @dummy.errors[:avatar].present?,
42
+ "Error not added to base attribute"
43
+ end
44
+ end
45
+
46
+ context "with add_validation_errors_to set to :attribute globally" do
47
+ before do
48
+ Paperclip.options[:add_validation_errors_to] = :attribute
49
+ end
50
+
51
+ after do
52
+ Paperclip.options[:add_validation_errors_to] = :both
53
+ end
54
+
55
+ it "only adds error to attribute not base" do
56
+ build_validator matches: /.*\.png$/, allow_nil: false
57
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
58
+ @validator.validate(@dummy)
59
+
60
+ assert @dummy.errors[:avatar_file_name].present?,
61
+ "Error not added to attribute"
62
+
63
+ assert @dummy.errors[:avatar].blank?,
64
+ "Error added to base attribute"
65
+ end
66
+ end
67
+
68
+ context "with add_validation_errors_to set to :base globally" do
69
+ before do
70
+ Paperclip.options[:add_validation_errors_to] = :base
71
+ end
72
+
73
+ after do
74
+ Paperclip.options[:add_validation_errors_to] = :both
75
+ end
76
+
77
+ it "only adds error to base not attribute" do
78
+ build_validator matches: /.*\.png$/, allow_nil: false
79
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
80
+ @validator.validate(@dummy)
81
+
82
+ assert @dummy.errors[:avatar].present?,
83
+ "Error not added to base attribute"
84
+
85
+ assert @dummy.errors[:avatar_file_name].blank?,
86
+ "Error added to attribute"
87
+ end
88
+ end
89
+
90
+ context "with add_validation_errors_to set to :attribute" do
91
+ it "only adds error to attribute not base" do
92
+ build_validator matches: /.*\.png$/, allow_nil: false,
93
+ add_validation_errors_to: :attribute
94
+
95
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
96
+ @validator.validate(@dummy)
97
+
98
+ assert @dummy.errors[:avatar_file_name].present?,
99
+ "Error not added to attribute"
100
+
101
+ assert @dummy.errors[:avatar].blank?,
102
+ "Error added to base attribute"
103
+ end
104
+ end
105
+
106
+ context "with add_validation_errors_to set to :base" do
107
+ it "only adds error to base not attribute" do
108
+ build_validator matches: /.*\.png$/, allow_nil: false,
109
+ add_validation_errors_to: :base
110
+
111
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
112
+ @validator.validate(@dummy)
113
+
114
+ assert @dummy.errors[:avatar].present?,
115
+ "Error not added to base attribute"
116
+
117
+ assert @dummy.errors[:avatar_file_name].blank?,
118
+ "Error added to attribute"
119
+ end
120
+ end
121
+
32
122
  it "does not add error to the base object with a successful validation" do
33
123
  build_validator matches: /.*\.png$/, allow_nil: false
34
124
  allow(@dummy).to receive_messages(avatar_file_name: "image.png")
@@ -205,6 +205,96 @@ describe Paperclip::Validators::AttachmentSizeValidator do
205
205
  end
206
206
  end
207
207
 
208
+ context "with add_validation_errors_to not set (implicitly :both)" do
209
+ it "adds error to both attribute and base" do
210
+ build_validator in: (5.kilobytes..10.kilobytes)
211
+ allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
212
+ @validator.validate(@dummy)
213
+
214
+ assert @dummy.errors[:avatar_file_size].present?,
215
+ "Error not added to attribute"
216
+
217
+ assert @dummy.errors[:avatar].present?,
218
+ "Error not added to base attribute"
219
+ end
220
+ end
221
+
222
+ context "with add_validation_errors_to set to :attribute globally" do
223
+ before do
224
+ Paperclip.options[:add_validation_errors_to] = :attribute
225
+ end
226
+
227
+ after do
228
+ Paperclip.options[:add_validation_errors_to] = :both
229
+ end
230
+
231
+ it "only adds error to attribute not base" do
232
+ build_validator in: (5.kilobytes..10.kilobytes)
233
+ allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
234
+ @validator.validate(@dummy)
235
+
236
+ assert @dummy.errors[:avatar_file_size].present?,
237
+ "Error not added to attribute"
238
+
239
+ assert @dummy.errors[:avatar].blank?,
240
+ "Error added to base attribute"
241
+ end
242
+ end
243
+
244
+ context "with add_validation_errors_to set to :base globally" do
245
+ before do
246
+ Paperclip.options[:add_validation_errors_to] = :base
247
+ end
248
+
249
+ after do
250
+ Paperclip.options[:add_validation_errors_to] = :both
251
+ end
252
+
253
+ it "only adds error to base not attribute" do
254
+ build_validator in: (5.kilobytes..10.kilobytes)
255
+ allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
256
+ @validator.validate(@dummy)
257
+
258
+ assert @dummy.errors[:avatar].present?,
259
+ "Error not added to base attribute"
260
+
261
+ assert @dummy.errors[:avatar_file_size].blank?,
262
+ "Error added to attribute"
263
+ end
264
+ end
265
+
266
+ context "with add_validation_errors_to set to :attribute" do
267
+ it "only adds error to attribute not base" do
268
+ build_validator in: (5.kilobytes..10.kilobytes),
269
+ add_validation_errors_to: :attribute
270
+
271
+ allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
272
+ @validator.validate(@dummy)
273
+
274
+ assert @dummy.errors[:avatar_file_size].present?,
275
+ "Error not added to attribute"
276
+
277
+ assert @dummy.errors[:avatar].blank?,
278
+ "Error added to base attribute"
279
+ end
280
+ end
281
+
282
+ context "with add_validation_errors_to set to :base" do
283
+ it "only adds error to base not attribute" do
284
+ build_validator in: (5.kilobytes..10.kilobytes),
285
+ add_validation_errors_to: :base
286
+
287
+ allow(@dummy).to receive(:avatar_file_size).and_return(11.kilobytes)
288
+ @validator.validate(@dummy)
289
+
290
+ assert @dummy.errors[:avatar].present?,
291
+ "Error not added to base attribute"
292
+
293
+ assert @dummy.errors[:avatar_file_size].blank?,
294
+ "Error added to attribute"
295
+ end
296
+ end
297
+
208
298
  context "using the helper" do
209
299
  before do
210
300
  Dummy.validates_attachment_size :avatar, in: (5.kilobytes..10.kilobytes)