paperclip 3.5.2 → 5.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -13
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +10 -7
- data/LICENSE +1 -3
- data/NEWS +226 -23
- data/README.md +494 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +36 -28
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +0 -2
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
- data/lib/paperclip/attachment.rb +160 -47
- data/lib/paperclip/attachment_registry.rb +4 -1
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/geometry_detector_factory.rb +10 -3
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +14 -10
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +27 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +28 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -8
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +89 -0
- data/lib/paperclip/processor.rb +5 -41
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +9 -7
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +47 -22
- data/lib/paperclip/storage/s3.rb +144 -73
- data/lib/paperclip/style.rb +8 -2
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +26 -14
- data/lib/paperclip/url_generator.rb +25 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -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 +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +12 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +31 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/{test → spec}/database.yml +0 -0
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +524 -400
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -19
- data/{test/file_command_content_type_detector_test.rb → spec/paperclip/file_command_content_type_detector_spec.rb} +7 -6
- data/spec/paperclip/filename_cleaner_spec.rb +14 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +141 -133
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +101 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +38 -34
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -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 +121 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -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/{test/paperclip_test.rb → spec/paperclip/paperclip_spec.rb} +46 -71
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +5 -5
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +561 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +255 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +150 -131
- data/spec/paperclip/url_generator_spec.rb +222 -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/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
- 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 +47 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/conditional_filter_helper.rb +5 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/{test → spec/support}/fixtures/12k.png +0 -0
- data/{test → spec/support}/fixtures/50x50.png +0 -0
- data/{test → spec/support}/fixtures/5k.png +0 -0
- data/{test → spec/support}/fixtures/animated +0 -0
- data/{test → spec/support}/fixtures/animated.gif +0 -0
- data/{test → spec/support}/fixtures/animated.unknown +0 -0
- data/{test → spec/support}/fixtures/bad.png +0 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/{test → spec/support}/fixtures/fog.yml +0 -0
- data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- data/{test → spec/support}/fixtures/s3.yml +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/{test → spec/support}/fixtures/spaced file.png +0 -0
- data/{test → spec/support}/fixtures/text.txt +0 -0
- data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- data/{test → 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/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_interpolator.rb +0 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +262 -297
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -77
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -215
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -67
- data/test/io_adapters/empty_string_adapter_test.rb +0 -17
- data/test/io_adapters/file_adapter_test.rb +0 -119
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -90
- data/test/plural_cache_test.rb +0 -36
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -453
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1348
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -13
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -323
- data/test/validators_test.rb +0 -32
@@ -1,185 +1,204 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require '
|
3
|
-
require 'paperclip/attachment'
|
2
|
+
require 'spec_helper'
|
4
3
|
|
5
|
-
|
4
|
+
describe Paperclip::Attachment do
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
context "when file not set" do
|
16
|
-
should "not be present" do
|
17
|
-
assert @dummy.avatar.blank?
|
18
|
-
refute @dummy.avatar.present?
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "when file set" do
|
23
|
-
setup { @dummy.avatar = File.new(fixture_file("50x50.png"), "rb") }
|
6
|
+
it "is not present when file not set" do
|
7
|
+
rebuild_class
|
8
|
+
dummy = Dummy.new
|
9
|
+
expect(dummy.avatar).to be_blank
|
10
|
+
expect(dummy.avatar).to_not be_present
|
11
|
+
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
13
|
+
it "is present when the file is set" do
|
14
|
+
rebuild_class
|
15
|
+
dummy = Dummy.new
|
16
|
+
dummy.avatar = File.new(fixture_file("50x50.png"), "rb")
|
17
|
+
expect(dummy.avatar).to_not be_blank
|
18
|
+
expect(dummy.avatar).to be_present
|
30
19
|
end
|
31
20
|
|
32
|
-
|
21
|
+
it "processes :original style first" do
|
33
22
|
file = File.new(fixture_file("50x50.png"), 'rb')
|
34
|
-
rebuild_class :
|
23
|
+
rebuild_class styles: { small: '100x>', original: '42x42#' }
|
35
24
|
dummy = Dummy.new
|
36
25
|
dummy.avatar = file
|
37
26
|
dummy.save
|
38
27
|
|
39
28
|
# :small avatar should be 42px wide (processed original), not 50px (preprocessed original)
|
40
|
-
|
29
|
+
expect(`identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip).to eq "42"
|
41
30
|
|
42
31
|
file.close
|
43
32
|
end
|
44
33
|
|
45
|
-
|
34
|
+
it "does not delete styles that don't get reprocessed" do
|
46
35
|
file = File.new(fixture_file("50x50.png"), 'rb')
|
47
|
-
rebuild_class :
|
48
|
-
|
49
|
-
|
36
|
+
rebuild_class styles: {
|
37
|
+
small: "100x>",
|
38
|
+
large: "500x>",
|
39
|
+
original: "42x42#"
|
40
|
+
}
|
41
|
+
|
50
42
|
dummy = Dummy.new
|
51
43
|
dummy.avatar = file
|
52
44
|
dummy.save
|
53
45
|
|
54
|
-
|
55
|
-
|
56
|
-
|
46
|
+
expect(dummy.avatar.path(:small)).to exist
|
47
|
+
expect(dummy.avatar.path(:large)).to exist
|
48
|
+
expect(dummy.avatar.path(:original)).to exist
|
57
49
|
|
58
50
|
dummy.avatar.reprocess!(:small)
|
59
51
|
|
60
|
-
|
61
|
-
|
62
|
-
|
52
|
+
expect(dummy.avatar.path(:small)).to exist
|
53
|
+
expect(dummy.avatar.path(:large)).to exist
|
54
|
+
expect(dummy.avatar.path(:original)).to exist
|
63
55
|
end
|
64
56
|
|
65
|
-
|
57
|
+
context "having a not empty hash as a default option" do
|
58
|
+
before do
|
59
|
+
@old_default_options = Paperclip::Attachment.default_options.dup
|
60
|
+
@new_default_options = { convert_options: { all: "-background white" } }
|
61
|
+
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
62
|
+
end
|
63
|
+
|
64
|
+
after do
|
65
|
+
Paperclip::Attachment.default_options.merge!(@old_default_options)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "deep merges when it is overridden" do
|
69
|
+
new_options = { convert_options: { thumb: "-thumbnailize" } }
|
70
|
+
attachment = Paperclip::Attachment.new(:name, :instance, new_options)
|
71
|
+
|
72
|
+
expect(Paperclip::Attachment.default_options.deep_merge(new_options)).to eq attachment.instance_variable_get("@options")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "handles a boolean second argument to #url" do
|
66
77
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
67
|
-
attachment = Paperclip::Attachment.new(
|
78
|
+
attachment = Paperclip::Attachment.new(
|
79
|
+
:name,
|
80
|
+
FakeModel.new,
|
81
|
+
url_generator: mock_url_generator_builder
|
82
|
+
)
|
68
83
|
|
69
84
|
attachment.url(:style_name, true)
|
70
|
-
|
85
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: true, escape: true)).to eq true
|
71
86
|
|
72
87
|
attachment.url(:style_name, false)
|
73
|
-
|
88
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: false, escape: true)).to eq true
|
74
89
|
end
|
75
90
|
|
76
|
-
|
91
|
+
it "passes the style and options through to the URL generator on #url" do
|
77
92
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
78
|
-
attachment = Paperclip::Attachment.new(
|
79
|
-
|
80
|
-
|
81
|
-
|
93
|
+
attachment = Paperclip::Attachment.new(
|
94
|
+
:name,
|
95
|
+
FakeModel.new,
|
96
|
+
url_generator: mock_url_generator_builder
|
97
|
+
)
|
98
|
+
|
99
|
+
attachment.url(:style_name, options: :values)
|
100
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(options: :values)).to eq true
|
82
101
|
end
|
83
102
|
|
84
|
-
|
103
|
+
it "passes default options through when #url is given one argument" do
|
85
104
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
86
105
|
attachment = Paperclip::Attachment.new(:name,
|
87
|
-
|
88
|
-
:
|
89
|
-
:
|
106
|
+
FakeModel.new,
|
107
|
+
url_generator: mock_url_generator_builder,
|
108
|
+
use_timestamp: true)
|
90
109
|
|
91
110
|
attachment.url(:style_name)
|
92
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
111
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
93
112
|
end
|
94
113
|
|
95
|
-
|
114
|
+
it "passes default style and options through when #url is given no arguments" do
|
96
115
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
97
116
|
attachment = Paperclip::Attachment.new(:name,
|
98
|
-
|
99
|
-
:
|
100
|
-
:
|
101
|
-
:
|
117
|
+
FakeModel.new,
|
118
|
+
default_style: 'default style',
|
119
|
+
url_generator: mock_url_generator_builder,
|
120
|
+
use_timestamp: true)
|
102
121
|
|
103
122
|
attachment.url
|
104
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
123
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
105
124
|
assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
|
106
125
|
end
|
107
126
|
|
108
|
-
|
127
|
+
it "passes the option timestamp: true if :use_timestamp is true and :timestamp is not passed" do
|
109
128
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
110
129
|
attachment = Paperclip::Attachment.new(:name,
|
111
|
-
|
112
|
-
:
|
113
|
-
:
|
130
|
+
FakeModel.new,
|
131
|
+
url_generator: mock_url_generator_builder,
|
132
|
+
use_timestamp: true)
|
114
133
|
|
115
134
|
attachment.url(:style_name)
|
116
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
135
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
117
136
|
end
|
118
137
|
|
119
|
-
|
138
|
+
it "passes the option timestamp: false if :use_timestamp is false and :timestamp is not passed" do
|
120
139
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
121
140
|
attachment = Paperclip::Attachment.new(:name,
|
122
|
-
|
123
|
-
:
|
124
|
-
:
|
141
|
+
FakeModel.new,
|
142
|
+
url_generator: mock_url_generator_builder,
|
143
|
+
use_timestamp: false)
|
125
144
|
|
126
145
|
attachment.url(:style_name)
|
127
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
146
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: false)
|
128
147
|
end
|
129
148
|
|
130
|
-
|
149
|
+
it "does not change the :timestamp if :timestamp is passed" do
|
131
150
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
132
151
|
attachment = Paperclip::Attachment.new(:name,
|
133
|
-
|
134
|
-
:
|
135
|
-
:
|
152
|
+
FakeModel.new,
|
153
|
+
url_generator: mock_url_generator_builder,
|
154
|
+
use_timestamp: false)
|
136
155
|
|
137
|
-
attachment.url(:style_name, :
|
138
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
156
|
+
attachment.url(:style_name, timestamp: true)
|
157
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
139
158
|
end
|
140
159
|
|
141
|
-
|
160
|
+
it "renders JSON as default style" do
|
142
161
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
143
162
|
attachment = Paperclip::Attachment.new(:name,
|
144
|
-
|
145
|
-
:
|
146
|
-
:
|
163
|
+
FakeModel.new,
|
164
|
+
default_style: 'default style',
|
165
|
+
url_generator: mock_url_generator_builder)
|
147
166
|
|
148
|
-
|
167
|
+
attachment.as_json
|
149
168
|
assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
|
150
169
|
end
|
151
170
|
|
152
|
-
|
171
|
+
it "passes the option escape: true if :escape_url is true and :escape is not passed" do
|
153
172
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
154
173
|
attachment = Paperclip::Attachment.new(:name,
|
155
|
-
|
156
|
-
:
|
157
|
-
:
|
174
|
+
FakeModel.new,
|
175
|
+
url_generator: mock_url_generator_builder,
|
176
|
+
escape_url: true)
|
158
177
|
|
159
178
|
attachment.url(:style_name)
|
160
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
179
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true)
|
161
180
|
end
|
162
181
|
|
163
|
-
|
182
|
+
it "passes the option escape: false if :escape_url is false and :escape is not passed" do
|
164
183
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
165
184
|
attachment = Paperclip::Attachment.new(:name,
|
166
|
-
|
167
|
-
:
|
168
|
-
:
|
185
|
+
FakeModel.new,
|
186
|
+
url_generator: mock_url_generator_builder,
|
187
|
+
escape_url: false)
|
169
188
|
|
170
189
|
attachment.url(:style_name)
|
171
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
190
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: false)
|
172
191
|
end
|
173
192
|
|
174
|
-
|
175
|
-
@attachment = attachment :
|
193
|
+
it "returns the path based on the url by default" do
|
194
|
+
@attachment = attachment url: "/:class/:id/:basename"
|
176
195
|
@model = @attachment.instance
|
177
196
|
@model.id = 1234
|
178
197
|
@model.avatar_file_name = "fake.jpg"
|
179
198
|
assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
|
180
199
|
end
|
181
200
|
|
182
|
-
|
201
|
+
it "defaults to a path that scales" do
|
183
202
|
avatar_attachment = attachment
|
184
203
|
model = avatar_attachment.instance
|
185
204
|
model.id = 1234
|
@@ -188,7 +207,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
188
207
|
assert_equal expected_path, avatar_attachment.path
|
189
208
|
end
|
190
209
|
|
191
|
-
|
210
|
+
it "renders JSON as the URL to the attachment" do
|
192
211
|
avatar_attachment = attachment
|
193
212
|
model = avatar_attachment.instance
|
194
213
|
model.id = 1234
|
@@ -196,35 +215,33 @@ class AttachmentTest < Test::Unit::TestCase
|
|
196
215
|
assert_equal attachment.url, attachment.as_json
|
197
216
|
end
|
198
217
|
|
199
|
-
|
218
|
+
it "renders JSON from the model when requested by :methods" do
|
200
219
|
rebuild_model
|
201
220
|
dummy = Dummy.new
|
202
221
|
dummy.id = 1234
|
203
222
|
dummy.avatar_file_name = "fake.jpg"
|
223
|
+
dummy.stubs(:new_record?).returns(false)
|
204
224
|
expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
|
205
|
-
if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
|
206
|
-
expected_string = %({"dummy":#{expected_string}})
|
207
|
-
end
|
208
225
|
# active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
|
209
|
-
assert_equal expected_string, dummy.to_json(:
|
226
|
+
assert_equal expected_string, dummy.to_json(only: [:dummy_key_for_old_active_model], methods: [:avatar])
|
210
227
|
end
|
211
228
|
|
212
229
|
context "Attachment default_options" do
|
213
|
-
|
230
|
+
before do
|
214
231
|
rebuild_model
|
215
232
|
@old_default_options = Paperclip::Attachment.default_options.dup
|
216
233
|
@new_default_options = @old_default_options.merge({
|
217
|
-
:
|
218
|
-
:
|
219
|
-
:
|
234
|
+
path: "argle/bargle",
|
235
|
+
url: "fooferon",
|
236
|
+
default_url: "not here.png"
|
220
237
|
})
|
221
238
|
end
|
222
239
|
|
223
|
-
|
240
|
+
after do
|
224
241
|
Paperclip::Attachment.default_options.merge! @old_default_options
|
225
242
|
end
|
226
243
|
|
227
|
-
|
244
|
+
it "is overrideable" do
|
228
245
|
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
229
246
|
@new_default_options.keys.each do |key|
|
230
247
|
assert_equal @new_default_options[key],
|
@@ -233,41 +250,46 @@ class AttachmentTest < Test::Unit::TestCase
|
|
233
250
|
end
|
234
251
|
|
235
252
|
context "without an Attachment" do
|
236
|
-
|
253
|
+
before do
|
254
|
+
rebuild_model default_url: "default.url"
|
237
255
|
@dummy = Dummy.new
|
238
256
|
end
|
239
257
|
|
240
|
-
|
258
|
+
it "returns false when asked exists?" do
|
241
259
|
assert !@dummy.avatar.exists?
|
242
260
|
end
|
261
|
+
|
262
|
+
it "#url returns the default_url" do
|
263
|
+
expect(@dummy.avatar.url).to eq "default.url"
|
264
|
+
end
|
243
265
|
end
|
244
266
|
|
245
267
|
context "on an Attachment" do
|
246
|
-
|
268
|
+
before do
|
247
269
|
@dummy = Dummy.new
|
248
270
|
@attachment = @dummy.avatar
|
249
271
|
end
|
250
272
|
|
251
273
|
Paperclip::Attachment.default_options.keys.each do |key|
|
252
|
-
|
274
|
+
it "is the default_options for #{key}" do
|
253
275
|
assert_equal @old_default_options[key],
|
254
276
|
@attachment.instance_variable_get("@options")[key],
|
255
|
-
key
|
277
|
+
key.to_s
|
256
278
|
end
|
257
279
|
end
|
258
280
|
|
259
281
|
context "when redefined" do
|
260
|
-
|
282
|
+
before do
|
261
283
|
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
262
284
|
@dummy = Dummy.new
|
263
285
|
@attachment = @dummy.avatar
|
264
286
|
end
|
265
287
|
|
266
288
|
Paperclip::Attachment.default_options.keys.each do |key|
|
267
|
-
|
289
|
+
it "is the new default_options for #{key}" do
|
268
290
|
assert_equal @new_default_options[key],
|
269
291
|
@attachment.instance_variable_get("@options")[key],
|
270
|
-
key
|
292
|
+
key.to_s
|
271
293
|
end
|
272
294
|
end
|
273
295
|
end
|
@@ -275,23 +297,23 @@ class AttachmentTest < Test::Unit::TestCase
|
|
275
297
|
end
|
276
298
|
|
277
299
|
context "An attachment with similarly named interpolations" do
|
278
|
-
|
279
|
-
rebuild_model :
|
300
|
+
before do
|
301
|
+
rebuild_model path: ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
|
280
302
|
@dummy = Dummy.new
|
281
303
|
@dummy.stubs(:id).returns(1024)
|
282
304
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
283
305
|
@dummy.avatar = @file
|
284
306
|
end
|
285
307
|
|
286
|
-
|
308
|
+
after { @file.close }
|
287
309
|
|
288
|
-
|
310
|
+
it "makes sure that they are interpolated correctly" do
|
289
311
|
assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
|
290
312
|
end
|
291
313
|
end
|
292
314
|
|
293
315
|
context "An attachment with :timestamp interpolations" do
|
294
|
-
|
316
|
+
before do
|
295
317
|
@file = StringIO.new("...")
|
296
318
|
@zone = 'UTC'
|
297
319
|
Time.stubs(:zone).returns(@zone)
|
@@ -300,78 +322,77 @@ class AttachmentTest < Test::Unit::TestCase
|
|
300
322
|
end
|
301
323
|
|
302
324
|
context "using default time zone" do
|
303
|
-
|
304
|
-
rebuild_model :
|
325
|
+
before do
|
326
|
+
rebuild_model path: ":timestamp", use_default_time_zone: true
|
305
327
|
@dummy = Dummy.new
|
306
328
|
@dummy.avatar = @file
|
307
329
|
end
|
308
330
|
|
309
|
-
|
331
|
+
it "returns a time in the default zone" do
|
310
332
|
assert_equal @dummy.avatar_updated_at.in_time_zone(@zone_default).to_s, @dummy.avatar.path
|
311
333
|
end
|
312
334
|
end
|
313
335
|
|
314
336
|
context "using per-thread time zone" do
|
315
|
-
|
316
|
-
rebuild_model :
|
337
|
+
before do
|
338
|
+
rebuild_model path: ":timestamp", use_default_time_zone: false
|
317
339
|
@dummy = Dummy.new
|
318
340
|
@dummy.avatar = @file
|
319
341
|
end
|
320
342
|
|
321
|
-
|
343
|
+
it "returns a time in the per-thread zone" do
|
322
344
|
assert_equal @dummy.avatar_updated_at.in_time_zone(@zone).to_s, @dummy.avatar.path
|
323
345
|
end
|
324
346
|
end
|
325
347
|
end
|
326
348
|
|
327
349
|
context "An attachment with :hash interpolations" do
|
328
|
-
|
329
|
-
@file =
|
350
|
+
before do
|
351
|
+
@file = File.open(fixture_file("5k.png"))
|
352
|
+
end
|
353
|
+
|
354
|
+
after do
|
355
|
+
@file.close
|
330
356
|
end
|
331
357
|
|
332
|
-
|
333
|
-
|
358
|
+
it "raises if no secret is provided" do
|
359
|
+
rebuild_model path: ":hash"
|
360
|
+
@attachment = Dummy.new.avatar
|
334
361
|
@attachment.assign @file
|
335
362
|
|
336
|
-
|
363
|
+
assert_raises ArgumentError do
|
337
364
|
@attachment.path
|
338
365
|
end
|
339
366
|
end
|
340
367
|
|
341
368
|
context "when secret is set" do
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
@attachment
|
369
|
+
before do
|
370
|
+
rebuild_model path: ":hash",
|
371
|
+
hash_secret: "w00t",
|
372
|
+
hash_data: ":class/:attachment/:style/:filename"
|
373
|
+
@attachment = Dummy.new.avatar
|
347
374
|
@attachment.assign @file
|
348
375
|
end
|
349
376
|
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
should "result in the correct interpolation" do
|
356
|
-
assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options[:hash_data])
|
357
|
-
end
|
358
|
-
|
359
|
-
should "result in a correct hash" do
|
360
|
-
assert_equal "d22b617d1bf10016aa7d046d16427ae203f39fce", @attachment.path
|
377
|
+
it "results in the correct interpolation" do
|
378
|
+
assert_equal "dummies/avatars/original/5k.png",
|
379
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data])
|
380
|
+
assert_equal "dummies/avatars/thumb/5k.png",
|
381
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data], :thumb)
|
361
382
|
end
|
362
383
|
|
363
|
-
|
364
|
-
|
365
|
-
@attachment.path(
|
384
|
+
it "results in a correct hash" do
|
385
|
+
assert_equal "0a59e9142bba11576de1d353d8747b1acad5ad34", @attachment.path
|
386
|
+
assert_equal "b39a062c1e62e85a6c785ed00cf3bebf5f850e2b", @attachment.path(:thumb)
|
366
387
|
end
|
367
388
|
end
|
368
389
|
end
|
369
390
|
|
370
391
|
context "An attachment with a :rails_env interpolation" do
|
371
|
-
|
392
|
+
before do
|
372
393
|
@rails_env = "blah"
|
373
394
|
@id = 1024
|
374
|
-
rebuild_model :
|
395
|
+
rebuild_model path: ":rails_env/:id.png"
|
375
396
|
@dummy = Dummy.new
|
376
397
|
@dummy.stubs(:id).returns(@id)
|
377
398
|
@file = StringIO.new(".")
|
@@ -379,83 +400,84 @@ class AttachmentTest < Test::Unit::TestCase
|
|
379
400
|
Rails.stubs(:env).returns(@rails_env)
|
380
401
|
end
|
381
402
|
|
382
|
-
|
403
|
+
it "returns the proper path" do
|
383
404
|
assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
|
384
405
|
end
|
385
406
|
end
|
386
407
|
|
387
408
|
context "An attachment with a default style and an extension interpolation" do
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
@
|
393
|
-
@file.
|
409
|
+
before do
|
410
|
+
rebuild_model path: ":basename.:extension",
|
411
|
+
styles: { default: ["100x100", :jpg] },
|
412
|
+
default_style: :default
|
413
|
+
@attachment = Dummy.new.avatar
|
414
|
+
@file = File.open(fixture_file("5k.png"))
|
415
|
+
@file.stubs(:original_filename).returns("file.png")
|
394
416
|
end
|
395
|
-
|
417
|
+
it "returns the right extension for the path" do
|
396
418
|
@attachment.assign(@file)
|
397
|
-
assert_equal "file.
|
419
|
+
assert_equal "file.jpg", @attachment.path
|
398
420
|
end
|
399
421
|
end
|
400
422
|
|
401
423
|
context "An attachment with :convert_options" do
|
402
|
-
|
403
|
-
rebuild_model :
|
404
|
-
:
|
405
|
-
:
|
424
|
+
before do
|
425
|
+
rebuild_model styles: {
|
426
|
+
thumb: "100x100",
|
427
|
+
large: "400x400"
|
406
428
|
},
|
407
|
-
:
|
408
|
-
:
|
409
|
-
:
|
429
|
+
convert_options: {
|
430
|
+
all: "-do_stuff",
|
431
|
+
thumb: "-thumbnailize"
|
410
432
|
}
|
411
433
|
@dummy = Dummy.new
|
412
434
|
@dummy.avatar
|
413
435
|
end
|
414
436
|
|
415
|
-
|
437
|
+
it "reports the correct options when sent #extra_options_for(:thumb)" do
|
416
438
|
assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
|
417
439
|
end
|
418
440
|
|
419
|
-
|
441
|
+
it "reports the correct options when sent #extra_options_for(:large)" do
|
420
442
|
assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
|
421
443
|
end
|
422
444
|
end
|
423
445
|
|
424
446
|
context "An attachment with :source_file_options" do
|
425
|
-
|
426
|
-
rebuild_model :
|
427
|
-
:
|
428
|
-
:
|
447
|
+
before do
|
448
|
+
rebuild_model styles: {
|
449
|
+
thumb: "100x100",
|
450
|
+
large: "400x400"
|
429
451
|
},
|
430
|
-
:
|
431
|
-
:
|
432
|
-
:
|
452
|
+
source_file_options: {
|
453
|
+
all: "-density 400",
|
454
|
+
thumb: "-depth 8"
|
433
455
|
}
|
434
456
|
@dummy = Dummy.new
|
435
457
|
@dummy.avatar
|
436
458
|
end
|
437
459
|
|
438
|
-
|
460
|
+
it "reports the correct options when sent #extra_source_file_options_for(:thumb)" do
|
439
461
|
assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
|
440
462
|
end
|
441
463
|
|
442
|
-
|
464
|
+
it "reports the correct options when sent #extra_source_file_options_for(:large)" do
|
443
465
|
assert_equal "-density 400", @dummy.avatar.send(:extra_source_file_options_for, :large)
|
444
466
|
end
|
445
467
|
end
|
446
468
|
|
447
469
|
context "An attachment with :only_process" do
|
448
|
-
|
449
|
-
rebuild_model :
|
450
|
-
:
|
451
|
-
:
|
470
|
+
before do
|
471
|
+
rebuild_model styles: {
|
472
|
+
thumb: "100x100",
|
473
|
+
large: "400x400"
|
452
474
|
},
|
453
|
-
:
|
475
|
+
only_process: [:thumb]
|
454
476
|
@file = StringIO.new("...")
|
455
477
|
@attachment = Dummy.new.avatar
|
456
478
|
end
|
457
479
|
|
458
|
-
|
480
|
+
it "only processes the provided style" do
|
459
481
|
@attachment.expects(:post_process).with(:thumb)
|
460
482
|
@attachment.expects(:post_process).with(:large).never
|
461
483
|
@attachment.assign(@file)
|
@@ -463,33 +485,34 @@ class AttachmentTest < Test::Unit::TestCase
|
|
463
485
|
end
|
464
486
|
|
465
487
|
context "An attachment with :only_process that is a proc" do
|
466
|
-
|
467
|
-
rebuild_model :
|
468
|
-
:
|
469
|
-
:
|
488
|
+
before do
|
489
|
+
rebuild_model styles: {
|
490
|
+
thumb: "100x100",
|
491
|
+
large: "400x400"
|
470
492
|
},
|
471
|
-
:
|
493
|
+
only_process: lambda { |attachment| [:thumb] }
|
472
494
|
|
473
495
|
@file = StringIO.new("...")
|
474
496
|
@attachment = Dummy.new.avatar
|
475
497
|
end
|
476
498
|
|
477
|
-
|
499
|
+
it "only processes the provided style" do
|
478
500
|
@attachment.expects(:post_process).with(:thumb)
|
479
501
|
@attachment.expects(:post_process).with(:large).never
|
480
502
|
@attachment.assign(@file)
|
503
|
+
@attachment.save
|
481
504
|
end
|
482
505
|
end
|
483
506
|
|
484
507
|
context "An attachment with :convert_options that is a proc" do
|
485
|
-
|
486
|
-
rebuild_model :
|
487
|
-
:
|
488
|
-
:
|
508
|
+
before do
|
509
|
+
rebuild_model styles: {
|
510
|
+
thumb: "100x100",
|
511
|
+
large: "400x400"
|
489
512
|
},
|
490
|
-
:
|
491
|
-
:
|
492
|
-
:
|
513
|
+
convert_options: {
|
514
|
+
all: lambda{|i| i.all },
|
515
|
+
thumb: lambda{|i| i.thumb }
|
493
516
|
}
|
494
517
|
Dummy.class_eval do
|
495
518
|
def all; "-all"; end
|
@@ -499,54 +522,54 @@ class AttachmentTest < Test::Unit::TestCase
|
|
499
522
|
@dummy.avatar
|
500
523
|
end
|
501
524
|
|
502
|
-
|
525
|
+
it "reports the correct options when sent #extra_options_for(:thumb)" do
|
503
526
|
assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
|
504
527
|
end
|
505
528
|
|
506
|
-
|
529
|
+
it "reports the correct options when sent #extra_options_for(:large)" do
|
507
530
|
assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large)
|
508
531
|
end
|
509
532
|
end
|
510
533
|
|
511
534
|
context "An attachment with :path that is a proc" do
|
512
|
-
|
513
|
-
rebuild_model :
|
535
|
+
before do
|
536
|
+
rebuild_model path: lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
|
514
537
|
|
515
538
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
516
|
-
@dummyA = Dummy.new(:
|
539
|
+
@dummyA = Dummy.new(other: 'a')
|
517
540
|
@dummyA.avatar = @file
|
518
|
-
@dummyB = Dummy.new(:
|
541
|
+
@dummyB = Dummy.new(other: 'b')
|
519
542
|
@dummyB.avatar = @file
|
520
543
|
end
|
521
544
|
|
522
|
-
|
545
|
+
after { @file.close }
|
523
546
|
|
524
|
-
|
547
|
+
it "returns correct path" do
|
525
548
|
assert_equal "path/a.png", @dummyA.avatar.path
|
526
549
|
assert_equal "path/b.png", @dummyB.avatar.path
|
527
550
|
end
|
528
551
|
end
|
529
552
|
|
530
553
|
context "An attachment with :styles that is a proc" do
|
531
|
-
|
532
|
-
rebuild_model :
|
554
|
+
before do
|
555
|
+
rebuild_model styles: lambda{ |attachment| {thumb: "50x50#", large: "400x400"} }
|
533
556
|
|
534
557
|
@attachment = Dummy.new.avatar
|
535
558
|
end
|
536
559
|
|
537
|
-
|
560
|
+
it "has the correct geometry" do
|
538
561
|
assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
|
539
562
|
end
|
540
563
|
end
|
541
564
|
|
542
565
|
context "An attachment with conditional :styles that is a proc" do
|
543
|
-
|
544
|
-
rebuild_model :
|
566
|
+
before do
|
567
|
+
rebuild_model styles: lambda{ |attachment| attachment.instance.other == 'a' ? {thumb: "50x50#"} : {large: "400x400"} }
|
545
568
|
|
546
|
-
@dummy = Dummy.new(:
|
569
|
+
@dummy = Dummy.new(other: 'a')
|
547
570
|
end
|
548
571
|
|
549
|
-
|
572
|
+
it "has the correct styles for the assigned instance values" do
|
550
573
|
assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
|
551
574
|
assert_nil @dummy.avatar.styles[:large]
|
552
575
|
|
@@ -560,22 +583,22 @@ class AttachmentTest < Test::Unit::TestCase
|
|
560
583
|
geometry_specs = [
|
561
584
|
[ lambda{|z| "50x50#" }, :png ],
|
562
585
|
lambda{|z| "50x50#" },
|
563
|
-
{ :
|
586
|
+
{ geometry: lambda{|z| "50x50#" } }
|
564
587
|
]
|
565
588
|
geometry_specs.each do |geometry_spec|
|
566
589
|
context "An attachment geometry like #{geometry_spec}" do
|
567
|
-
|
568
|
-
rebuild_model :
|
590
|
+
before do
|
591
|
+
rebuild_model styles: { normal: geometry_spec }
|
569
592
|
@attachment = Dummy.new.avatar
|
570
593
|
end
|
571
594
|
|
572
595
|
context "when assigned" do
|
573
|
-
|
596
|
+
before do
|
574
597
|
@file = StringIO.new(".")
|
575
598
|
@attachment.assign(@file)
|
576
599
|
end
|
577
600
|
|
578
|
-
|
601
|
+
it "has the correct geometry" do
|
579
602
|
assert_equal "50x50#", @attachment.styles[:normal][:geometry]
|
580
603
|
end
|
581
604
|
end
|
@@ -583,122 +606,178 @@ class AttachmentTest < Test::Unit::TestCase
|
|
583
606
|
end
|
584
607
|
|
585
608
|
context "An attachment with both 'normal' and hash-style styles" do
|
586
|
-
|
587
|
-
rebuild_model :
|
588
|
-
:
|
589
|
-
:
|
609
|
+
before do
|
610
|
+
rebuild_model styles: {
|
611
|
+
normal: ["50x50#", :png],
|
612
|
+
hash: { geometry: "50x50#", format: :png }
|
590
613
|
}
|
591
614
|
@dummy = Dummy.new
|
592
615
|
@attachment = @dummy.avatar
|
593
616
|
end
|
594
617
|
|
595
618
|
[:processors, :whiny, :convert_options, :geometry, :format].each do |field|
|
596
|
-
|
619
|
+
it "has the same #{field} field" do
|
597
620
|
assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
|
598
621
|
end
|
599
622
|
end
|
600
623
|
end
|
601
624
|
|
602
625
|
context "An attachment with :processors that is a proc" do
|
603
|
-
|
626
|
+
before do
|
604
627
|
class Paperclip::Test < Paperclip::Processor; end
|
605
628
|
@file = StringIO.new("...")
|
606
629
|
Paperclip::Test.stubs(:make).returns(@file)
|
607
630
|
|
608
|
-
rebuild_model :
|
631
|
+
rebuild_model styles: { normal: '' }, processors: lambda { |a| [ :test ] }
|
609
632
|
@attachment = Dummy.new.avatar
|
610
633
|
end
|
611
634
|
|
612
635
|
context "when assigned" do
|
613
|
-
|
636
|
+
before do
|
614
637
|
@attachment.assign(StringIO.new("."))
|
615
638
|
end
|
616
639
|
|
617
|
-
|
640
|
+
it "has the correct processors" do
|
618
641
|
assert_equal [ :test ], @attachment.styles[:normal][:processors]
|
619
642
|
end
|
620
643
|
end
|
621
644
|
end
|
622
645
|
|
623
646
|
context "An attachment with erroring processor" do
|
624
|
-
|
625
|
-
rebuild_model :
|
647
|
+
before do
|
648
|
+
rebuild_model processor: [:thumbnail], styles: { small: '' }, whiny_thumbnails: true
|
626
649
|
@dummy = Dummy.new
|
627
|
-
Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
|
628
650
|
@file = StringIO.new("...")
|
629
651
|
@file.stubs(:to_tempfile).returns(@file)
|
630
|
-
@dummy.avatar = @file
|
631
652
|
end
|
632
653
|
|
633
|
-
|
634
|
-
|
635
|
-
|
654
|
+
context "when error is meaningful for the end user" do
|
655
|
+
before do
|
656
|
+
Paperclip::Thumbnail.expects(:make).raises(
|
657
|
+
Paperclip::Errors::NotIdentifiedByImageMagickError,
|
658
|
+
"cannot be processed."
|
659
|
+
)
|
660
|
+
end
|
661
|
+
|
662
|
+
it "correctly forwards processing error message to the instance" do
|
663
|
+
@dummy.avatar = @file
|
664
|
+
@dummy.valid?
|
665
|
+
assert_contains(
|
666
|
+
@dummy.errors.full_messages,
|
667
|
+
"Avatar cannot be processed."
|
668
|
+
)
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
672
|
+
context "when error is intended for the developer" do
|
673
|
+
before do
|
674
|
+
Paperclip::Thumbnail.expects(:make).raises(
|
675
|
+
Paperclip::Errors::CommandNotFoundError
|
676
|
+
)
|
677
|
+
end
|
678
|
+
|
679
|
+
it "propagates the error" do
|
680
|
+
assert_raises(Paperclip::Errors::CommandNotFoundError) do
|
681
|
+
@dummy.avatar = @file
|
682
|
+
end
|
683
|
+
end
|
636
684
|
end
|
637
685
|
end
|
638
686
|
|
639
687
|
context "An attachment with multiple processors" do
|
640
|
-
|
688
|
+
before do
|
641
689
|
class Paperclip::Test < Paperclip::Processor; end
|
642
|
-
@style_params = { :
|
643
|
-
rebuild_model :
|
690
|
+
@style_params = { once: {one: 1, two: 2} }
|
691
|
+
rebuild_model processors: [:thumbnail, :test], styles: @style_params
|
644
692
|
@dummy = Dummy.new
|
645
693
|
@file = StringIO.new("...")
|
646
|
-
@file.stubs(:
|
694
|
+
@file.stubs(:close)
|
647
695
|
Paperclip::Test.stubs(:make).returns(@file)
|
648
696
|
Paperclip::Thumbnail.stubs(:make).returns(@file)
|
649
697
|
end
|
650
698
|
|
651
699
|
context "when assigned" do
|
652
|
-
|
700
|
+
it "calls #make on all specified processors" do
|
701
|
+
@dummy.avatar = @file
|
653
702
|
|
654
|
-
|
655
|
-
Paperclip::
|
656
|
-
Paperclip::Test.expects(:make).with(any_parameters).returns(@file)
|
703
|
+
expect(Paperclip::Thumbnail).to have_received(:make)
|
704
|
+
expect(Paperclip::Test).to have_received(:make)
|
657
705
|
end
|
658
706
|
|
659
|
-
|
707
|
+
it "calls #make with the right parameters passed as second argument" do
|
660
708
|
expected_params = @style_params[:once].merge({
|
661
|
-
:
|
662
|
-
:
|
663
|
-
:
|
664
|
-
:
|
709
|
+
style: :once,
|
710
|
+
processors: [:thumbnail, :test],
|
711
|
+
whiny: true,
|
712
|
+
convert_options: "",
|
713
|
+
source_file_options: ""
|
665
714
|
})
|
666
|
-
|
715
|
+
|
716
|
+
@dummy.avatar = @file
|
717
|
+
|
718
|
+
expect(Paperclip::Thumbnail).to have_received(:make).with(anything, expected_params, anything)
|
719
|
+
end
|
720
|
+
|
721
|
+
it "calls #make with attachment passed as third argument" do
|
722
|
+
@dummy.avatar = @file
|
723
|
+
|
724
|
+
expect(Paperclip::Test).to have_received(:make).with(anything, anything, @dummy.avatar)
|
725
|
+
end
|
726
|
+
|
727
|
+
it "calls #make and unlinks intermediary files afterward" do
|
728
|
+
@dummy.avatar.expects(:unlink_files).with([@file, @file])
|
729
|
+
|
730
|
+
@dummy.avatar = @file
|
731
|
+
end
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
context "An attachment with a processor that returns original file" do
|
736
|
+
before do
|
737
|
+
class Paperclip::Test < Paperclip::Processor
|
738
|
+
def make; @file; end
|
667
739
|
end
|
740
|
+
rebuild_model processors: [:test], styles: { once: "100x100" }
|
741
|
+
@file = StringIO.new("...")
|
742
|
+
@file.stubs(:close)
|
743
|
+
@dummy = Dummy.new
|
744
|
+
end
|
668
745
|
|
669
|
-
|
670
|
-
|
746
|
+
context "when assigned" do
|
747
|
+
it "#calls #make and doesn't unlink the original file" do
|
748
|
+
@dummy.avatar.expects(:unlink_files).with([])
|
749
|
+
|
750
|
+
@dummy.avatar = @file
|
671
751
|
end
|
672
752
|
end
|
673
753
|
end
|
674
754
|
|
675
|
-
|
676
|
-
rebuild_model :
|
755
|
+
it "includes the filesystem module when loading the filesystem storage" do
|
756
|
+
rebuild_model storage: :filesystem
|
677
757
|
@dummy = Dummy.new
|
678
758
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
679
759
|
end
|
680
760
|
|
681
|
-
|
682
|
-
rebuild_model :
|
761
|
+
it "includes the filesystem module even if capitalization is wrong" do
|
762
|
+
rebuild_model storage: :FileSystem
|
683
763
|
@dummy = Dummy.new
|
684
764
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
685
765
|
|
686
|
-
rebuild_model :
|
766
|
+
rebuild_model storage: :Filesystem
|
687
767
|
@dummy = Dummy.new
|
688
768
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
689
769
|
end
|
690
770
|
|
691
|
-
|
692
|
-
rebuild_model :
|
771
|
+
it "converts underscored storage name to camelcase" do
|
772
|
+
rebuild_model storage: :not_here
|
693
773
|
@dummy = Dummy.new
|
694
|
-
exception = assert_raises(Paperclip::Errors::StorageMethodNotFound) do
|
774
|
+
exception = assert_raises(Paperclip::Errors::StorageMethodNotFound, /NotHere/) do
|
695
775
|
@dummy.avatar
|
696
776
|
end
|
697
|
-
assert exception.message.include?("NotHere")
|
698
777
|
end
|
699
778
|
|
700
|
-
|
701
|
-
rebuild_model :
|
779
|
+
it "raises an error if you try to include a storage module that doesn't exist" do
|
780
|
+
rebuild_model storage: :not_here
|
702
781
|
@dummy = Dummy.new
|
703
782
|
assert_raises(Paperclip::Errors::StorageMethodNotFound) do
|
704
783
|
@dummy.avatar
|
@@ -706,30 +785,30 @@ class AttachmentTest < Test::Unit::TestCase
|
|
706
785
|
end
|
707
786
|
|
708
787
|
context "An attachment with styles but no processors defined" do
|
709
|
-
|
710
|
-
rebuild_model :
|
788
|
+
before do
|
789
|
+
rebuild_model processors: [], styles: {something: '1'}
|
711
790
|
@dummy = Dummy.new
|
712
791
|
@file = StringIO.new("...")
|
713
792
|
end
|
714
|
-
|
793
|
+
it "raises when assigned to" do
|
715
794
|
assert_raises(RuntimeError){ @dummy.avatar = @file }
|
716
795
|
end
|
717
796
|
end
|
718
797
|
|
719
798
|
context "An attachment without styles and with no processors defined" do
|
720
|
-
|
721
|
-
rebuild_model :
|
799
|
+
before do
|
800
|
+
rebuild_model processors: [], styles: {}
|
722
801
|
@dummy = Dummy.new
|
723
802
|
@file = StringIO.new("...")
|
724
803
|
end
|
725
|
-
|
804
|
+
it "does not raise when assigned to" do
|
726
805
|
@dummy.avatar = @file
|
727
806
|
end
|
728
807
|
end
|
729
808
|
|
730
809
|
context "Assigning an attachment with post_process hooks" do
|
731
|
-
|
732
|
-
rebuild_class :
|
810
|
+
before do
|
811
|
+
rebuild_class styles: { something: "100x100#" }
|
733
812
|
Dummy.class_eval do
|
734
813
|
before_avatar_post_process :do_before_avatar
|
735
814
|
after_avatar_post_process :do_after_avatar
|
@@ -747,7 +826,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
747
826
|
@attachment = @dummy.avatar
|
748
827
|
end
|
749
828
|
|
750
|
-
|
829
|
+
it "calls the defined callbacks when assigned" do
|
751
830
|
@dummy.expects(:do_before_avatar).with()
|
752
831
|
@dummy.expects(:do_after_avatar).with()
|
753
832
|
@dummy.expects(:do_before_all).with()
|
@@ -756,7 +835,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
756
835
|
@dummy.avatar = @file
|
757
836
|
end
|
758
837
|
|
759
|
-
|
838
|
+
it "does not cancel the processing if a before_post_process returns nil" do
|
760
839
|
@dummy.expects(:do_before_avatar).with().returns(nil)
|
761
840
|
@dummy.expects(:do_after_avatar).with()
|
762
841
|
@dummy.expects(:do_before_all).with().returns(nil)
|
@@ -765,7 +844,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
765
844
|
@dummy.avatar = @file
|
766
845
|
end
|
767
846
|
|
768
|
-
|
847
|
+
it "cancels the processing if a before_post_process returns false" do
|
769
848
|
@dummy.expects(:do_before_avatar).never
|
770
849
|
@dummy.expects(:do_after_avatar).never
|
771
850
|
@dummy.expects(:do_before_all).with().returns(false)
|
@@ -774,7 +853,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
774
853
|
@dummy.avatar = @file
|
775
854
|
end
|
776
855
|
|
777
|
-
|
856
|
+
it "cancels the processing if a before_avatar_post_process returns false" do
|
778
857
|
@dummy.expects(:do_before_avatar).with().returns(false)
|
779
858
|
@dummy.expects(:do_after_avatar)
|
780
859
|
@dummy.expects(:do_before_all).with().returns(true)
|
@@ -785,37 +864,37 @@ class AttachmentTest < Test::Unit::TestCase
|
|
785
864
|
end
|
786
865
|
|
787
866
|
context "Assigning an attachment" do
|
788
|
-
|
789
|
-
rebuild_model :
|
867
|
+
before do
|
868
|
+
rebuild_model styles: { something: "100x100#" }
|
790
869
|
@file = File.new(fixture_file("5k.png"), "rb")
|
791
870
|
@dummy = Dummy.new
|
792
871
|
@dummy.avatar = @file
|
793
872
|
end
|
794
873
|
|
795
|
-
|
874
|
+
it "strips whitespace from original_filename field" do
|
796
875
|
assert_equal "5k.png", @dummy.avatar.original_filename
|
797
876
|
end
|
798
877
|
|
799
|
-
|
878
|
+
it "strips whitespace from content_type field" do
|
800
879
|
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
|
801
880
|
end
|
802
881
|
end
|
803
882
|
|
804
883
|
context "Assigning an attachment" do
|
805
|
-
|
806
|
-
rebuild_model :
|
884
|
+
before do
|
885
|
+
rebuild_model styles: { something: "100x100#" }
|
807
886
|
@file = File.new(fixture_file("5k.png"), "rb")
|
808
887
|
@dummy = Dummy.new
|
809
888
|
@dummy.avatar = @file
|
810
889
|
end
|
811
890
|
|
812
|
-
|
891
|
+
it "makes sure the content_type is a string" do
|
813
892
|
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
|
814
893
|
end
|
815
894
|
end
|
816
895
|
|
817
896
|
context "Attachment with strange letters" do
|
818
|
-
|
897
|
+
before do
|
819
898
|
rebuild_model
|
820
899
|
@file = File.new(fixture_file("5k.png"), "rb")
|
821
900
|
@file.stubs(:original_filename).returns("sheep_say_bæ.png")
|
@@ -823,18 +902,18 @@ class AttachmentTest < Test::Unit::TestCase
|
|
823
902
|
@dummy.avatar = @file
|
824
903
|
end
|
825
904
|
|
826
|
-
|
905
|
+
it "does not remove strange letters" do
|
827
906
|
assert_equal "sheep_say_bæ.png", @dummy.avatar.original_filename
|
828
907
|
end
|
829
908
|
end
|
830
909
|
|
831
910
|
context "Attachment with reserved filename" do
|
832
|
-
|
911
|
+
before do
|
833
912
|
rebuild_model
|
834
913
|
@file = Tempfile.new(["filename","png"])
|
835
914
|
end
|
836
915
|
|
837
|
-
|
916
|
+
after do
|
838
917
|
@file.unlink
|
839
918
|
end
|
840
919
|
|
@@ -843,37 +922,37 @@ class AttachmentTest < Test::Unit::TestCase
|
|
843
922
|
context "with character #{character}" do
|
844
923
|
|
845
924
|
context "at beginning of filename" do
|
846
|
-
|
925
|
+
before do
|
847
926
|
@file.stubs(:original_filename).returns("#{character}filename.png")
|
848
927
|
@dummy = Dummy.new
|
849
928
|
@dummy.avatar = @file
|
850
929
|
end
|
851
930
|
|
852
|
-
|
931
|
+
it "converts special character into underscore" do
|
853
932
|
assert_equal "_filename.png", @dummy.avatar.original_filename
|
854
933
|
end
|
855
934
|
end
|
856
935
|
|
857
936
|
context "at end of filename" do
|
858
|
-
|
937
|
+
before do
|
859
938
|
@file.stubs(:original_filename).returns("filename.png#{character}")
|
860
939
|
@dummy = Dummy.new
|
861
940
|
@dummy.avatar = @file
|
862
941
|
end
|
863
942
|
|
864
|
-
|
943
|
+
it "converts special character into underscore" do
|
865
944
|
assert_equal "filename.png_", @dummy.avatar.original_filename
|
866
945
|
end
|
867
946
|
end
|
868
947
|
|
869
948
|
context "in the middle of filename" do
|
870
|
-
|
949
|
+
before do
|
871
950
|
@file.stubs(:original_filename).returns("file#{character}name.png")
|
872
951
|
@dummy = Dummy.new
|
873
952
|
@dummy.avatar = @file
|
874
953
|
end
|
875
954
|
|
876
|
-
|
955
|
+
it "converts special character into underscore" do
|
877
956
|
assert_equal "file_name.png", @dummy.avatar.original_filename
|
878
957
|
end
|
879
958
|
end
|
@@ -883,53 +962,53 @@ class AttachmentTest < Test::Unit::TestCase
|
|
883
962
|
end
|
884
963
|
|
885
964
|
context "with specified regexp replacement" do
|
886
|
-
|
965
|
+
before do
|
887
966
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
888
967
|
end
|
889
968
|
|
890
|
-
|
969
|
+
after do
|
891
970
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
892
971
|
end
|
893
972
|
|
894
973
|
context 'as another regexp' do
|
895
|
-
|
896
|
-
Paperclip::Attachment.default_options.merge! :
|
974
|
+
before do
|
975
|
+
Paperclip::Attachment.default_options.merge! restricted_characters: /o/
|
897
976
|
|
898
977
|
@file.stubs(:original_filename).returns("goood.png")
|
899
978
|
@dummy = Dummy.new
|
900
979
|
@dummy.avatar = @file
|
901
980
|
end
|
902
981
|
|
903
|
-
|
982
|
+
it "matches and converts that character" do
|
904
983
|
assert_equal "g___d.png", @dummy.avatar.original_filename
|
905
984
|
end
|
906
985
|
end
|
907
986
|
|
908
987
|
context 'as nil' do
|
909
|
-
|
910
|
-
Paperclip::Attachment.default_options.merge! :
|
988
|
+
before do
|
989
|
+
Paperclip::Attachment.default_options.merge! restricted_characters: nil
|
911
990
|
|
912
991
|
@file.stubs(:original_filename).returns("goood.png")
|
913
992
|
@dummy = Dummy.new
|
914
993
|
@dummy.avatar = @file
|
915
994
|
end
|
916
995
|
|
917
|
-
|
996
|
+
it "ignores and returns the original file name" do
|
918
997
|
assert_equal "goood.png", @dummy.avatar.original_filename
|
919
998
|
end
|
920
999
|
end
|
921
1000
|
end
|
922
1001
|
|
923
1002
|
context 'with specified cleaner' do
|
924
|
-
|
1003
|
+
before do
|
925
1004
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
926
1005
|
end
|
927
1006
|
|
928
|
-
|
1007
|
+
after do
|
929
1008
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
930
1009
|
end
|
931
1010
|
|
932
|
-
|
1011
|
+
it 'calls the given proc and take the result as cleaned filename' do
|
933
1012
|
Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
|
934
1013
|
"from_proc_#{str}"
|
935
1014
|
end
|
@@ -940,7 +1019,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
940
1019
|
assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
|
941
1020
|
end
|
942
1021
|
|
943
|
-
|
1022
|
+
it 'calls the given object and take the result as the cleaned filename' do
|
944
1023
|
class MyCleaner
|
945
1024
|
def call(filename)
|
946
1025
|
"foo"
|
@@ -957,102 +1036,100 @@ class AttachmentTest < Test::Unit::TestCase
|
|
957
1036
|
end
|
958
1037
|
|
959
1038
|
context "Attachment with uppercase extension and a default style" do
|
960
|
-
|
1039
|
+
before do
|
961
1040
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
962
1041
|
Paperclip::Attachment.default_options.merge!({
|
963
|
-
:
|
1042
|
+
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
964
1043
|
})
|
965
1044
|
FileUtils.rm_rf("tmp")
|
966
|
-
rebuild_model
|
1045
|
+
rebuild_model styles: { large: ["400x400", :jpg],
|
1046
|
+
medium: ["100x100", :jpg],
|
1047
|
+
small: ["32x32#", :jpg]},
|
1048
|
+
default_style: :small
|
967
1049
|
@instance = Dummy.new
|
968
1050
|
@instance.stubs(:id).returns 123
|
969
|
-
|
970
1051
|
@file = File.new(fixture_file("uppercase.PNG"), 'rb')
|
971
1052
|
|
972
|
-
|
973
|
-
|
974
|
-
:small => ["32x32#", :jpg]},
|
975
|
-
:default_style => :small}
|
976
|
-
@attachment = Paperclip::Attachment.new(:avatar,
|
977
|
-
@instance,
|
978
|
-
styles)
|
1053
|
+
@attachment = @instance.avatar
|
1054
|
+
|
979
1055
|
now = Time.now
|
980
1056
|
Time.stubs(:now).returns(now)
|
981
1057
|
@attachment.assign(@file)
|
982
1058
|
@attachment.save
|
983
1059
|
end
|
984
1060
|
|
985
|
-
|
1061
|
+
after do
|
986
1062
|
@file.close
|
987
1063
|
Paperclip::Attachment.default_options.merge!(@old_defaults)
|
988
1064
|
end
|
989
1065
|
|
990
|
-
|
991
|
-
|
992
|
-
|
1066
|
+
it "has matching to_s and url methods" do
|
1067
|
+
assert_equal @attachment.to_s, @attachment.url
|
1068
|
+
assert_equal @attachment.to_s(:small), @attachment.url(:small)
|
993
1069
|
end
|
994
1070
|
|
995
|
-
|
996
|
-
|
1071
|
+
it "has matching expiring_url and url methods when using the filesystem storage" do
|
1072
|
+
assert_equal @attachment.expiring_url, @attachment.url
|
997
1073
|
end
|
998
1074
|
end
|
999
1075
|
|
1000
1076
|
context "An attachment" do
|
1001
|
-
|
1077
|
+
before do
|
1002
1078
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
1003
1079
|
Paperclip::Attachment.default_options.merge!({
|
1004
|
-
:
|
1080
|
+
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
1005
1081
|
})
|
1006
1082
|
FileUtils.rm_rf("tmp")
|
1007
1083
|
rebuild_model
|
1008
1084
|
@instance = Dummy.new
|
1009
1085
|
@instance.stubs(:id).returns 123
|
1010
|
-
@attachment = Paperclip::Attachment.new(:avatar, @instance)
|
1086
|
+
# @attachment = Paperclip::Attachment.new(:avatar, @instance)
|
1087
|
+
@attachment = @instance.avatar
|
1011
1088
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1012
1089
|
end
|
1013
1090
|
|
1014
|
-
|
1091
|
+
after do
|
1015
1092
|
@file.close
|
1016
1093
|
Paperclip::Attachment.default_options.merge!(@old_defaults)
|
1017
1094
|
end
|
1018
1095
|
|
1019
|
-
|
1096
|
+
it "raises if there are not the correct columns when you try to assign" do
|
1020
1097
|
@other_attachment = Paperclip::Attachment.new(:not_here, @instance)
|
1021
1098
|
assert_raises(Paperclip::Error) do
|
1022
1099
|
@other_attachment.assign(@file)
|
1023
1100
|
end
|
1024
1101
|
end
|
1025
1102
|
|
1026
|
-
|
1103
|
+
it 'clears out the previous assignment when assigned nil' do
|
1027
1104
|
@attachment.assign(@file)
|
1028
|
-
|
1105
|
+
@attachment.queued_for_write[:original]
|
1029
1106
|
@attachment.assign(nil)
|
1030
1107
|
assert_nil @attachment.queued_for_write[:original]
|
1031
1108
|
end
|
1032
1109
|
|
1033
|
-
|
1110
|
+
it 'does not do anything when it is assigned an empty string' do
|
1034
1111
|
@attachment.assign(@file)
|
1035
1112
|
original_file = @attachment.queued_for_write[:original]
|
1036
1113
|
@attachment.assign("")
|
1037
1114
|
assert_equal original_file, @attachment.queued_for_write[:original]
|
1038
1115
|
end
|
1039
1116
|
|
1040
|
-
|
1117
|
+
it "returns nil as path when no file assigned" do
|
1041
1118
|
assert_equal nil, @attachment.path
|
1042
1119
|
assert_equal nil, @attachment.path(:blah)
|
1043
1120
|
end
|
1044
1121
|
|
1045
1122
|
context "with a file assigned but not saved yet" do
|
1046
|
-
|
1123
|
+
it "clears out any attached files" do
|
1047
1124
|
@attachment.assign(@file)
|
1048
|
-
assert
|
1125
|
+
assert @attachment.queued_for_write.present?
|
1049
1126
|
@attachment.clear
|
1050
1127
|
assert @attachment.queued_for_write.blank?
|
1051
1128
|
end
|
1052
1129
|
end
|
1053
1130
|
|
1054
1131
|
context "with a file assigned in the database" do
|
1055
|
-
|
1132
|
+
before do
|
1056
1133
|
@attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
|
1057
1134
|
@attachment.stubs(:instance_read).with(:content_type).returns("image/png")
|
1058
1135
|
@attachment.stubs(:instance_read).with(:file_size).returns(12345)
|
@@ -1062,54 +1139,57 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1062
1139
|
@attachment.stubs(:instance_read).with(:updated_at).returns(dtnow)
|
1063
1140
|
end
|
1064
1141
|
|
1065
|
-
|
1142
|
+
it "returns the proper path when filename has a single .'s" do
|
1066
1143
|
assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.png"), File.expand_path(@attachment.path)
|
1067
1144
|
end
|
1068
1145
|
|
1069
|
-
|
1146
|
+
it "returns the proper path when filename has multiple .'s" do
|
1070
1147
|
@attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
|
1071
1148
|
assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.old.png"), File.expand_path(@attachment.path)
|
1072
1149
|
end
|
1073
1150
|
|
1074
1151
|
context "when expecting three styles" do
|
1075
|
-
|
1076
|
-
styles
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1152
|
+
before do
|
1153
|
+
rebuild_class styles: {
|
1154
|
+
large: ["400x400", :png],
|
1155
|
+
medium: ["100x100", :gif],
|
1156
|
+
small: ["32x32#", :jpg]
|
1157
|
+
}
|
1158
|
+
@instance = Dummy.new
|
1159
|
+
@instance.stubs(:id).returns 123
|
1160
|
+
@file = File.new(fixture_file("5k.png"), 'rb')
|
1161
|
+
@attachment = @instance.avatar
|
1082
1162
|
end
|
1083
1163
|
|
1084
1164
|
context "and assigned a file" do
|
1085
|
-
|
1165
|
+
before do
|
1086
1166
|
now = Time.now
|
1087
1167
|
Time.stubs(:now).returns(now)
|
1088
1168
|
@attachment.assign(@file)
|
1089
1169
|
end
|
1090
1170
|
|
1091
|
-
|
1171
|
+
it "is dirty" do
|
1092
1172
|
assert @attachment.dirty?
|
1093
1173
|
end
|
1094
1174
|
|
1095
1175
|
context "and saved" do
|
1096
|
-
|
1176
|
+
before do
|
1097
1177
|
@attachment.save
|
1098
1178
|
end
|
1099
1179
|
|
1100
|
-
|
1180
|
+
it "commits the files to disk" do
|
1101
1181
|
[:large, :medium, :small].each do |style|
|
1102
|
-
|
1182
|
+
expect(@attachment.path(style)).to exist
|
1103
1183
|
end
|
1104
1184
|
end
|
1105
1185
|
|
1106
|
-
|
1186
|
+
it "saves the files as the right formats and sizes" do
|
1107
1187
|
[[:large, 400, 61, "PNG"],
|
1108
1188
|
[:medium, 100, 15, "GIF"],
|
1109
1189
|
[:small, 32, 32, "JPEG"]].each do |style|
|
1110
1190
|
cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"]
|
1111
1191
|
out = `#{cmd}`
|
1112
|
-
width, height,
|
1192
|
+
width, height, _size, format = out.split(" ")
|
1113
1193
|
assert_equal style[1].to_s, width.to_s
|
1114
1194
|
assert_equal style[2].to_s, height.to_s
|
1115
1195
|
assert_equal style[3].to_s, format.to_s
|
@@ -1117,13 +1197,13 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1117
1197
|
end
|
1118
1198
|
|
1119
1199
|
context "and trying to delete" do
|
1120
|
-
|
1200
|
+
before do
|
1121
1201
|
@existing_names = @attachment.styles.keys.collect do |style|
|
1122
1202
|
@attachment.path(style)
|
1123
1203
|
end
|
1124
1204
|
end
|
1125
1205
|
|
1126
|
-
|
1206
|
+
it "deletes the files after assigning nil" do
|
1127
1207
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1128
1208
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1129
1209
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1134,7 +1214,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1134
1214
|
@existing_names.each{|f| assert_file_not_exists(f) }
|
1135
1215
|
end
|
1136
1216
|
|
1137
|
-
|
1217
|
+
it "deletes the files when you call #clear and #save" do
|
1138
1218
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1139
1219
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1140
1220
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1145,7 +1225,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1145
1225
|
@existing_names.each{|f| assert_file_not_exists(f) }
|
1146
1226
|
end
|
1147
1227
|
|
1148
|
-
|
1228
|
+
it "deletes the files when you call #delete" do
|
1149
1229
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1150
1230
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1151
1231
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1156,11 +1236,11 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1156
1236
|
end
|
1157
1237
|
|
1158
1238
|
context "when keeping old files" do
|
1159
|
-
|
1239
|
+
before do
|
1160
1240
|
@attachment.options[:keep_old_files] = true
|
1161
1241
|
end
|
1162
1242
|
|
1163
|
-
|
1243
|
+
it "keeps the files after assigning nil" do
|
1164
1244
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1165
1245
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1166
1246
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1171,7 +1251,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1171
1251
|
@existing_names.each{|f| assert_file_exists(f) }
|
1172
1252
|
end
|
1173
1253
|
|
1174
|
-
|
1254
|
+
it "keeps the files when you call #clear and #save" do
|
1175
1255
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1176
1256
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1177
1257
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1182,7 +1262,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1182
1262
|
@existing_names.each{|f| assert_file_exists(f) }
|
1183
1263
|
end
|
1184
1264
|
|
1185
|
-
|
1265
|
+
it "keeps the files when you call #delete" do
|
1186
1266
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1187
1267
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1188
1268
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1199,19 +1279,19 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1199
1279
|
end
|
1200
1280
|
|
1201
1281
|
context "when trying a nonexistant storage type" do
|
1202
|
-
|
1203
|
-
rebuild_model :
|
1282
|
+
before do
|
1283
|
+
rebuild_model storage: :not_here
|
1204
1284
|
end
|
1205
1285
|
|
1206
|
-
|
1207
|
-
|
1286
|
+
it "is not able to find the module" do
|
1287
|
+
assert_raises(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
|
1208
1288
|
end
|
1209
1289
|
end
|
1210
1290
|
end
|
1211
1291
|
|
1212
1292
|
context "An attachment with only a avatar_file_name column" do
|
1213
|
-
|
1214
|
-
ActiveRecord::Base.connection.create_table :dummies, :
|
1293
|
+
before do
|
1294
|
+
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
|
1215
1295
|
table.column :avatar_file_name, :string
|
1216
1296
|
end
|
1217
1297
|
rebuild_class
|
@@ -1219,41 +1299,41 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1219
1299
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1220
1300
|
end
|
1221
1301
|
|
1222
|
-
|
1302
|
+
after { @file.close }
|
1223
1303
|
|
1224
|
-
|
1304
|
+
it "does not error when assigned an attachment" do
|
1225
1305
|
assert_nothing_raised { @dummy.avatar = @file }
|
1226
1306
|
end
|
1227
1307
|
|
1228
|
-
|
1308
|
+
it "does not return the time when sent #avatar_updated_at" do
|
1229
1309
|
@dummy.avatar = @file
|
1230
1310
|
assert_nil @dummy.avatar.updated_at
|
1231
1311
|
end
|
1232
1312
|
|
1233
|
-
|
1313
|
+
it "returns the right value when sent #avatar_file_size" do
|
1234
1314
|
@dummy.avatar = @file
|
1235
1315
|
assert_equal File.size(@file), @dummy.avatar.size
|
1236
1316
|
end
|
1237
1317
|
|
1238
1318
|
context "and avatar_created_at column" do
|
1239
|
-
|
1319
|
+
before do
|
1240
1320
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
|
1241
1321
|
rebuild_class
|
1242
1322
|
@dummy = Dummy.new
|
1243
1323
|
end
|
1244
1324
|
|
1245
|
-
|
1325
|
+
it "does not error when assigned an attachment" do
|
1246
1326
|
assert_nothing_raised { @dummy.avatar = @file }
|
1247
1327
|
end
|
1248
1328
|
|
1249
|
-
|
1329
|
+
it "returns the creation time when sent #avatar_created_at" do
|
1250
1330
|
now = Time.now
|
1251
1331
|
Time.stubs(:now).returns(now)
|
1252
1332
|
@dummy.avatar = @file
|
1253
1333
|
assert_equal now.to_i, @dummy.avatar.created_at
|
1254
1334
|
end
|
1255
1335
|
|
1256
|
-
|
1336
|
+
it "returns the creation time when sent #avatar_created_at and the entry has been updated" do
|
1257
1337
|
creation = 2.hours.ago
|
1258
1338
|
now = Time.now
|
1259
1339
|
Time.stubs(:now).returns(creation)
|
@@ -1264,7 +1344,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1264
1344
|
assert_not_equal now.to_i, @dummy.avatar.created_at
|
1265
1345
|
end
|
1266
1346
|
|
1267
|
-
|
1347
|
+
it "sets changed? to true on attachment assignment" do
|
1268
1348
|
@dummy.avatar = @file
|
1269
1349
|
@dummy.save!
|
1270
1350
|
@dummy.avatar = @file
|
@@ -1273,17 +1353,17 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1273
1353
|
end
|
1274
1354
|
|
1275
1355
|
context "and avatar_updated_at column" do
|
1276
|
-
|
1356
|
+
before do
|
1277
1357
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
|
1278
1358
|
rebuild_class
|
1279
1359
|
@dummy = Dummy.new
|
1280
1360
|
end
|
1281
1361
|
|
1282
|
-
|
1362
|
+
it "does not error when assigned an attachment" do
|
1283
1363
|
assert_nothing_raised { @dummy.avatar = @file }
|
1284
1364
|
end
|
1285
1365
|
|
1286
|
-
|
1366
|
+
it "returns the right value when sent #avatar_updated_at" do
|
1287
1367
|
now = Time.now
|
1288
1368
|
Time.stubs(:now).returns(now)
|
1289
1369
|
@dummy.avatar = @file
|
@@ -1291,45 +1371,51 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1291
1371
|
end
|
1292
1372
|
end
|
1293
1373
|
|
1294
|
-
|
1374
|
+
it "does not calculate fingerprint" do
|
1375
|
+
Digest::MD5.stubs(:file)
|
1376
|
+
@dummy.avatar = @file
|
1377
|
+
expect(Digest::MD5).to have_received(:file).never
|
1378
|
+
end
|
1379
|
+
|
1380
|
+
it "does not assign fingerprint" do
|
1295
1381
|
@dummy.avatar = @file
|
1296
1382
|
assert_nil @dummy.avatar.fingerprint
|
1297
1383
|
end
|
1298
1384
|
|
1299
1385
|
context "and avatar_content_type column" do
|
1300
|
-
|
1386
|
+
before do
|
1301
1387
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
|
1302
1388
|
rebuild_class
|
1303
1389
|
@dummy = Dummy.new
|
1304
1390
|
end
|
1305
1391
|
|
1306
|
-
|
1392
|
+
it "does not error when assigned an attachment" do
|
1307
1393
|
assert_nothing_raised { @dummy.avatar = @file }
|
1308
1394
|
end
|
1309
1395
|
|
1310
|
-
|
1396
|
+
it "returns the right value when sent #avatar_content_type" do
|
1311
1397
|
@dummy.avatar = @file
|
1312
1398
|
assert_equal "image/png", @dummy.avatar.content_type
|
1313
1399
|
end
|
1314
1400
|
end
|
1315
1401
|
|
1316
1402
|
context "and avatar_file_size column" do
|
1317
|
-
|
1403
|
+
before do
|
1318
1404
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
|
1319
1405
|
rebuild_class
|
1320
1406
|
@dummy = Dummy.new
|
1321
1407
|
end
|
1322
1408
|
|
1323
|
-
|
1409
|
+
it "does not error when assigned an attachment" do
|
1324
1410
|
assert_nothing_raised { @dummy.avatar = @file }
|
1325
1411
|
end
|
1326
1412
|
|
1327
|
-
|
1413
|
+
it "returns the right value when sent #avatar_file_size" do
|
1328
1414
|
@dummy.avatar = @file
|
1329
1415
|
assert_equal File.size(@file), @dummy.avatar.size
|
1330
1416
|
end
|
1331
1417
|
|
1332
|
-
|
1418
|
+
it "returns the right value when saved, reloaded, and sent #avatar_file_size" do
|
1333
1419
|
@dummy.avatar = @file
|
1334
1420
|
@dummy.save
|
1335
1421
|
@dummy = Dummy.find(@dummy.id)
|
@@ -1338,33 +1424,63 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1338
1424
|
end
|
1339
1425
|
|
1340
1426
|
context "and avatar_fingerprint column" do
|
1341
|
-
|
1427
|
+
before do
|
1342
1428
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_fingerprint, :string
|
1343
1429
|
rebuild_class
|
1344
1430
|
@dummy = Dummy.new
|
1345
1431
|
end
|
1346
1432
|
|
1347
|
-
|
1433
|
+
it "does not error when assigned an attachment" do
|
1348
1434
|
assert_nothing_raised { @dummy.avatar = @file }
|
1349
1435
|
end
|
1350
1436
|
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1437
|
+
context "with explicitly set digest" do
|
1438
|
+
before do
|
1439
|
+
rebuild_class adapter_options: { hash_digest: Digest::SHA256 }
|
1440
|
+
@dummy = Dummy.new
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
it "returns the right value when sent #avatar_fingerprint" do
|
1444
|
+
@dummy.avatar = @file
|
1445
|
+
assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
|
1446
|
+
@dummy.avatar_fingerprint
|
1447
|
+
end
|
1448
|
+
|
1449
|
+
it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
|
1450
|
+
@dummy.avatar = @file
|
1451
|
+
@dummy.save
|
1452
|
+
@dummy = Dummy.find(@dummy.id)
|
1453
|
+
assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
|
1454
|
+
@dummy.avatar_fingerprint
|
1455
|
+
end
|
1354
1456
|
end
|
1355
1457
|
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1458
|
+
context "with the default digest" do
|
1459
|
+
before do
|
1460
|
+
rebuild_class # MD5 is the default
|
1461
|
+
@dummy = Dummy.new
|
1462
|
+
end
|
1463
|
+
|
1464
|
+
it "returns the right value when sent #avatar_fingerprint" do
|
1465
|
+
@dummy.avatar = @file
|
1466
|
+
assert_equal "aec488126c3b33c08a10c3fa303acf27",
|
1467
|
+
@dummy.avatar_fingerprint
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
|
1471
|
+
@dummy.avatar = @file
|
1472
|
+
@dummy.save
|
1473
|
+
@dummy = Dummy.find(@dummy.id)
|
1474
|
+
assert_equal "aec488126c3b33c08a10c3fa303acf27",
|
1475
|
+
@dummy.avatar_fingerprint
|
1476
|
+
end
|
1361
1477
|
end
|
1362
1478
|
end
|
1363
1479
|
end
|
1364
1480
|
|
1365
1481
|
context "an attachment with delete_file option set to false" do
|
1366
|
-
|
1367
|
-
rebuild_model :
|
1482
|
+
before do
|
1483
|
+
rebuild_model preserve_files: true
|
1368
1484
|
@dummy = Dummy.new
|
1369
1485
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1370
1486
|
@dummy.avatar = @file
|
@@ -1373,27 +1489,27 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1373
1489
|
@path = @attachment.path
|
1374
1490
|
end
|
1375
1491
|
|
1376
|
-
|
1492
|
+
after { @file.close }
|
1377
1493
|
|
1378
|
-
|
1494
|
+
it "does not delete the files from storage when attachment is destroyed" do
|
1379
1495
|
@attachment.destroy
|
1380
1496
|
assert_file_exists(@path)
|
1381
1497
|
end
|
1382
1498
|
|
1383
|
-
|
1499
|
+
it "clears out attachment data when attachment is destroyed" do
|
1384
1500
|
@attachment.destroy
|
1385
1501
|
assert !@attachment.exists?
|
1386
1502
|
assert_nil @dummy.avatar_file_name
|
1387
1503
|
end
|
1388
1504
|
|
1389
|
-
|
1505
|
+
it "does not delete the file when model is destroyed" do
|
1390
1506
|
@dummy.destroy
|
1391
1507
|
assert_file_exists(@path)
|
1392
1508
|
end
|
1393
1509
|
end
|
1394
1510
|
|
1395
1511
|
context "An attached file" do
|
1396
|
-
|
1512
|
+
before do
|
1397
1513
|
rebuild_model
|
1398
1514
|
@dummy = Dummy.new
|
1399
1515
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
@@ -1403,22 +1519,30 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1403
1519
|
@path = @attachment.path
|
1404
1520
|
end
|
1405
1521
|
|
1406
|
-
|
1522
|
+
after { @file.close }
|
1407
1523
|
|
1408
|
-
|
1524
|
+
it "is not deleted when the model fails to destroy" do
|
1409
1525
|
@dummy.stubs(:destroy).raises(Exception)
|
1410
1526
|
|
1411
|
-
|
1527
|
+
assert_raises Exception do
|
1412
1528
|
@dummy.destroy
|
1413
1529
|
end
|
1414
1530
|
|
1415
1531
|
assert_file_exists(@path)
|
1416
1532
|
end
|
1417
1533
|
|
1418
|
-
|
1534
|
+
it "is deleted when the model is destroyed" do
|
1419
1535
|
@dummy.destroy
|
1420
1536
|
assert_file_not_exists(@path)
|
1421
1537
|
end
|
1422
|
-
end
|
1423
1538
|
|
1539
|
+
it "is not deleted when transaction rollbacks after model is destroyed" do
|
1540
|
+
ActiveRecord::Base.transaction do
|
1541
|
+
@dummy.destroy
|
1542
|
+
raise ActiveRecord::Rollback
|
1543
|
+
end
|
1544
|
+
|
1545
|
+
assert_file_exists(@path)
|
1546
|
+
end
|
1547
|
+
end
|
1424
1548
|
end
|