resizing 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.devcontainer/compose.yaml +7 -4
- data/.github/dependabot.yml +23 -0
- data/.github/tools/actionlint/Dockerfile +3 -0
- data/.github/tools/gitleaks/Dockerfile +2 -0
- data/.github/workflows/actionlint.yml +27 -0
- data/.github/workflows/actions.lock +54 -0
- data/.github/workflows/labeler.yml +3 -1
- data/.github/workflows/lint.yml +2 -2
- data/.github/workflows/secret-scan.yml +29 -0
- data/.github/workflows/test.yml +37 -6
- data/.github/workflows/verify-lockfile.yml +28 -0
- data/CLAUDE.md +2 -2
- data/Gemfile +3 -0
- data/README.ja.md +241 -0
- data/README.md +83 -1
- data/docker-compose.yml +7 -0
- data/git-hooks/pre-push-ruby.sh +16 -11
- data/lib/resizing/carrier_wave/storage/file.rb +21 -2
- data/lib/resizing/carrier_wave/storage/remote.rb +18 -13
- data/lib/resizing/carrier_wave.rb +75 -7
- data/lib/resizing/client.rb +47 -3
- data/lib/resizing/mock_client.rb +25 -2
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +4 -0
- data/test/rails_app/app/views/users/new.html.erb +6 -0
- data/test/rails_app/rails_app.rb +86 -0
- data/test/resizing/carrier_wave/storage/file_test.rb +40 -0
- data/test/resizing/carrier_wave/storage/remote_test.rb +66 -9
- data/test/resizing/carrier_wave_rails_integration_test.rb +94 -0
- data/test/resizing/carrier_wave_temporary_image_test.rb +280 -0
- data/test/resizing/carrier_wave_test.rb +15 -6
- data/test/resizing/carrier_wave_uploader_test.rb +14 -0
- data/test/resizing/client_test.rb +99 -9
- data/test/resizing/mock_client_test.rb +34 -0
- data/test/resizing_module_test.rb +16 -0
- data/test/test_helper.rb +23 -2
- data/test/vcr/carrier_wave_test/confirm.yml +23 -0
- data/test/vcr/carrier_wave_test/retrieve_from_cache.yml +23 -0
- data/test/vcr/carrier_wave_test/save.yml +1 -1
- data/test/vcr/carrier_wave_test/update_image.yml +42 -2
- metadata +16 -2
|
@@ -150,6 +150,46 @@ module Resizing
|
|
|
150
150
|
end
|
|
151
151
|
end
|
|
152
152
|
|
|
153
|
+
def test_store_with_temporary_option_posts_temporary_image
|
|
154
|
+
model = TestModel.new
|
|
155
|
+
file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
|
|
156
|
+
posted = []
|
|
157
|
+
response = { 'public_id' => IDENTIFIER, 'content_type' => 'image/jpeg' }
|
|
158
|
+
spy = lambda do |_content, options|
|
|
159
|
+
posted << options
|
|
160
|
+
response
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
Resizing.stub(:post, spy) do
|
|
164
|
+
file.store(::File.open('test/data/images/sample1.jpg', 'r'), temporary: true, expires_in: 60)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
assert_equal true, posted.first[:temporary]
|
|
168
|
+
assert_equal 60, posted.first[:expires_in]
|
|
169
|
+
assert_equal 'image/jpeg', posted.first[:content_type]
|
|
170
|
+
assert_equal 'sample1.jpg', posted.first[:filename]
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def test_confirm_confirms_image_of_public_id
|
|
174
|
+
model = TestModel.new
|
|
175
|
+
file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
|
|
176
|
+
|
|
177
|
+
with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
|
|
178
|
+
assert_equal({ 'id' => IMAGE_ID }, file.confirm)
|
|
179
|
+
assert_equal [IMAGE_ID], fake.confirmed_ids
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def test_confirm_does_nothing_without_public_id
|
|
184
|
+
model = TestModel.new
|
|
185
|
+
file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
|
|
186
|
+
|
|
187
|
+
with_fake_api_client({}) do |fake|
|
|
188
|
+
assert_nil file.confirm
|
|
189
|
+
assert_empty fake.confirmed_ids
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
153
193
|
def test_delete_with_valid_public_id
|
|
154
194
|
VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
|
|
155
195
|
model = TestModel.new
|
|
@@ -21,6 +21,18 @@ module Resizing
|
|
|
21
21
|
# NOP
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
# ブロック内で Resizing.post に渡されたオプションを記録する (リクエストはそのまま発行する)
|
|
25
|
+
def capture_post_options(&block)
|
|
26
|
+
posted = []
|
|
27
|
+
original = Resizing.method(:post)
|
|
28
|
+
spy = lambda do |file, options|
|
|
29
|
+
posted << options
|
|
30
|
+
original.call(file, options)
|
|
31
|
+
end
|
|
32
|
+
Resizing.stub(:post, spy, &block)
|
|
33
|
+
posted
|
|
34
|
+
end
|
|
35
|
+
|
|
24
36
|
def test_storage_has_required_methods
|
|
25
37
|
assert_respond_to @storage, :store!
|
|
26
38
|
assert_respond_to @storage, :remove!
|
|
@@ -30,9 +42,16 @@ module Resizing
|
|
|
30
42
|
assert_respond_to @storage, :clean_cache!
|
|
31
43
|
end
|
|
32
44
|
|
|
33
|
-
def
|
|
34
|
-
|
|
35
|
-
assert_nil
|
|
45
|
+
def test_retrieve_from_cache_returns_nil_for_blank_identifier
|
|
46
|
+
assert_nil @storage.retrieve_from_cache!(nil)
|
|
47
|
+
assert_nil @storage.retrieve_from_cache!('')
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_retrieve_from_cache_returns_file_without_request
|
|
51
|
+
result = @storage.retrieve_from_cache!(IDENTIFIER)
|
|
52
|
+
|
|
53
|
+
assert_instance_of Resizing::CarrierWave::Storage::File, result
|
|
54
|
+
assert_equal IDENTIFIER, result.public_id.to_s
|
|
36
55
|
end
|
|
37
56
|
|
|
38
57
|
def test_delete_dir_does_nothing
|
|
@@ -79,18 +98,56 @@ module Resizing
|
|
|
79
98
|
assert_equal IDENTIFIER, @uploader.model.read_attribute(:resizing_picture)
|
|
80
99
|
end
|
|
81
100
|
|
|
82
|
-
def
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
def test_store_uploads_without_temporary_option
|
|
102
|
+
posted = capture_post_options do
|
|
103
|
+
VCR.use_cassette('carrier_wave_test/save', record: :none) do
|
|
104
|
+
@storage.store!(::File.open('test/data/images/sample1.jpg', 'r'))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
86
107
|
|
|
87
|
-
|
|
88
|
-
|
|
108
|
+
refute posted.first.key?(:temporary)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def test_store_confirms_cached_file_instead_of_uploading_it
|
|
112
|
+
cached = Resizing::CarrierWave::Storage::File.new(@uploader, IDENTIFIER)
|
|
113
|
+
|
|
114
|
+
result = with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
|
|
115
|
+
stored = @storage.store!(cached)
|
|
116
|
+
assert_equal [IMAGE_ID], fake.confirmed_ids
|
|
117
|
+
stored
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
assert_same cached, result
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def test_cache_uploads_temporary_image
|
|
124
|
+
# Resizing にはキャッシュ用のストレージがないので、一時画像としてアップロードする
|
|
125
|
+
posted = capture_post_options do
|
|
126
|
+
VCR.use_cassette 'carrier_wave_test/save', record: :none do
|
|
127
|
+
file = @storage.cache!(::File.open('test/data/images/sample1.jpg', 'r'))
|
|
128
|
+
|
|
129
|
+
assert_instance_of Resizing::CarrierWave::Storage::File, file
|
|
130
|
+
assert_equal IDENTIFIER, file.public_id.to_s
|
|
131
|
+
end
|
|
89
132
|
end
|
|
90
133
|
|
|
134
|
+
assert_equal true, posted.first[:temporary]
|
|
135
|
+
assert_nil posted.first[:expires_in]
|
|
91
136
|
assert_equal IDENTIFIER, @uploader.model.read_attribute(:resizing_picture)
|
|
92
137
|
end
|
|
93
138
|
|
|
139
|
+
def test_cache_sends_cache_expires_in_of_uploader
|
|
140
|
+
storage = Remote.new(TestModelWithCacheExpiresIn.new.resizing_picture)
|
|
141
|
+
|
|
142
|
+
posted = capture_post_options do
|
|
143
|
+
VCR.use_cassette 'carrier_wave_test/save', record: :none do
|
|
144
|
+
storage.cache!(::File.open('test/data/images/sample1.jpg', 'r'))
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
assert_equal 3600, posted.first[:expires_in]
|
|
149
|
+
end
|
|
150
|
+
|
|
94
151
|
def test_retrieve_returns_new_file_instance_each_time
|
|
95
152
|
first = @storage.retrieve!(IDENTIFIER)
|
|
96
153
|
second = @storage.retrieve!(IDENTIFIER)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require_relative '../rails_app/rails_app'
|
|
5
|
+
|
|
6
|
+
module Resizing
|
|
7
|
+
# README の「Making uploads work across form redisplays」の例を、Rails アプリに組み込んだ形で確認する
|
|
8
|
+
# (フォームの表示 → バリデーションエラーで再表示 → image_cache を送り返して保存)
|
|
9
|
+
class CarrierWaveRailsIntegrationTest < ActionDispatch::IntegrationTest
|
|
10
|
+
include VCRRequestAssertions
|
|
11
|
+
include ResizingTestConfiguration
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
User.delete_all
|
|
15
|
+
configure_resizing
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_new_form_is_multipart_with_empty_image_cache
|
|
19
|
+
get '/users/new'
|
|
20
|
+
|
|
21
|
+
assert_response :ok
|
|
22
|
+
assert_includes response.body, 'enctype="multipart/form-data"'
|
|
23
|
+
assert_equal '', image_cache_in(response.body)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_form_shown_again_after_validation_error_keeps_temporary_image
|
|
27
|
+
assert_vcr_no_requests 'carrier_wave_test/confirm' do
|
|
28
|
+
assert_vcr_requests_made 'carrier_wave_test/save' do
|
|
29
|
+
post '/users', params: { user: { name: '', image: sample_upload, image_cache: '' } }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
assert_response :unprocessable_entity
|
|
34
|
+
assert_equal expect_identifier, image_cache_in(response.body)
|
|
35
|
+
assert_equal 0, User.count
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_resubmitting_image_cache_saves_and_confirms_the_image
|
|
39
|
+
assert_vcr_requests_made 'carrier_wave_test/retrieve_from_cache' do
|
|
40
|
+
assert_vcr_requests_made 'carrier_wave_test/confirm' do
|
|
41
|
+
post '/users', params: { user: { name: 'Alice', image_cache: expect_identifier } }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
user = User.last
|
|
46
|
+
assert_redirected_to "/users/#{user.id}"
|
|
47
|
+
assert_equal expect_identifier, user.read_attribute(:image)
|
|
48
|
+
assert_equal "#{expect_url}/", user.image_url
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# 再表示されたフォームの image_cache をそのまま送り返すだけで、画像を選び直さずに保存できる
|
|
52
|
+
def test_image_cache_from_form_shown_again_is_enough_to_save
|
|
53
|
+
assert_vcr_requests_made 'carrier_wave_test/save' do
|
|
54
|
+
post '/users', params: { user: { name: '', image: sample_upload, image_cache: '' } }
|
|
55
|
+
end
|
|
56
|
+
assert_response :unprocessable_entity
|
|
57
|
+
image_cache = image_cache_in(response.body)
|
|
58
|
+
|
|
59
|
+
assert_vcr_requests_made 'carrier_wave_test/retrieve_from_cache' do
|
|
60
|
+
assert_vcr_requests_made 'carrier_wave_test/confirm' do
|
|
61
|
+
post '/users', params: { user: { name: 'Alice', image_cache: image_cache } }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
user = User.last
|
|
66
|
+
assert_redirected_to "/users/#{user.id}"
|
|
67
|
+
assert_equal expect_identifier, user.read_attribute(:image)
|
|
68
|
+
assert_equal "#{expect_url}/", user.image_url
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_image_cache_left_out_of_strong_parameters_is_dropped
|
|
72
|
+
assert_vcr_no_requests 'carrier_wave_test/retrieve_from_cache' do
|
|
73
|
+
post '/users_without_cache', params: { user: { name: 'Bob', image_cache: expect_identifier } }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
user = User.last
|
|
77
|
+
assert_redirected_to "/users/#{user.id}"
|
|
78
|
+
assert_nil user.read_attribute(:image)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def sample_upload
|
|
84
|
+
Rack::Test::UploadedFile.new('test/data/images/sample1.jpg', 'image/jpeg')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# name 属性と value 属性の順序に依存せずに hidden field の値を取り出す
|
|
88
|
+
def image_cache_in(html)
|
|
89
|
+
tag = html[/<input[^>]*name="user\[image_cache\]"[^>]*>/]
|
|
90
|
+
assert tag, 'image_cache hidden field should be rendered'
|
|
91
|
+
tag[/value="([^"]*)"/, 1].to_s
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
# Resizing::CarrierWave の一時画像を使ったキャッシュの扱いをテストする
|
|
7
|
+
#
|
|
8
|
+
# - 代入 (cache!) では一時画像としてアップロードし、確定はしない
|
|
9
|
+
# - 保存 (コミット後の store!) で一時画像を確定する
|
|
10
|
+
# - フォームの再表示で送り返された `<column>_cache` から一時画像を復元する
|
|
11
|
+
class CarrierWaveTemporaryImageTest < Minitest::Test
|
|
12
|
+
include VCRRequestAssertions
|
|
13
|
+
include ResizingTestConfiguration
|
|
14
|
+
|
|
15
|
+
IMAGE_ID = '14ea7aac-a194-4330-931f-6b562aec413d'
|
|
16
|
+
OTHER_PROJECT_IDENTIFIER = '/projects/00000000-0000-0000-0000-000000000000/upload/images/' \
|
|
17
|
+
"#{IMAGE_ID}/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e".freeze
|
|
18
|
+
|
|
19
|
+
def setup
|
|
20
|
+
TestModel.delete_all
|
|
21
|
+
configure_resizing
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def teardown; end
|
|
25
|
+
|
|
26
|
+
# ============================================================
|
|
27
|
+
# 代入時のアップロード
|
|
28
|
+
# ============================================================
|
|
29
|
+
|
|
30
|
+
def test_assignment_uploads_image_as_temporary
|
|
31
|
+
model = TestModel.new
|
|
32
|
+
|
|
33
|
+
posted = capture_post_options do
|
|
34
|
+
assert_vcr_requests_made('carrier_wave_test/save') { attach_sample_image(model) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
assert_equal 1, posted.size
|
|
38
|
+
assert_equal true, posted.first[:temporary]
|
|
39
|
+
assert_nil posted.first[:expires_in]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_assignment_sends_cache_expires_in_of_uploader
|
|
43
|
+
model = TestModelWithCacheExpiresIn.new
|
|
44
|
+
|
|
45
|
+
posted = capture_post_options do
|
|
46
|
+
assert_vcr_requests_made('carrier_wave_test/save') { attach_sample_image(model) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
assert_equal true, posted.first[:temporary]
|
|
50
|
+
assert_equal 3600, posted.first[:expires_in]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_assignment_does_not_confirm_image
|
|
54
|
+
model = TestModel.new
|
|
55
|
+
|
|
56
|
+
assert_vcr_no_requests 'carrier_wave_test/confirm' do
|
|
57
|
+
assert_vcr_requests_made('carrier_wave_test/save') { attach_sample_image(model) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# ============================================================
|
|
64
|
+
# 保存時の確定
|
|
65
|
+
# ============================================================
|
|
66
|
+
|
|
67
|
+
def test_save_confirms_temporary_image
|
|
68
|
+
model = prepare_model
|
|
69
|
+
|
|
70
|
+
assert_vcr_requests_made 'carrier_wave_test/confirm' do
|
|
71
|
+
model.save!
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
assert_equal expect_identifier, TestModel.find(model.id).read_attribute(:resizing_picture)
|
|
75
|
+
assert_nil model.resizing_picture_cache
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_rolled_back_save_does_not_confirm_image
|
|
79
|
+
# 保存がロールバックされた場合は確定しない (一時画像は期限で削除される)
|
|
80
|
+
model = prepare_model
|
|
81
|
+
|
|
82
|
+
assert_vcr_no_requests 'carrier_wave_test/confirm' do
|
|
83
|
+
ActiveRecord::Base.transaction do
|
|
84
|
+
model.save!
|
|
85
|
+
raise ActiveRecord::Rollback
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_saving_again_does_not_confirm_stored_image_again
|
|
91
|
+
model = prepare_model
|
|
92
|
+
VCR.use_cassette('carrier_wave_test/confirm', record: :none) { model.save! }
|
|
93
|
+
|
|
94
|
+
assert_vcr_no_requests 'carrier_wave_test/confirm' do
|
|
95
|
+
model.save!
|
|
96
|
+
TestModel.find(model.id).save!
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def test_confirm_failure_is_raised_on_save
|
|
101
|
+
# 期限切れなどで確定できなかった場合は、画像のないレコードを黙って残さずに例外にする
|
|
102
|
+
model = prepare_model
|
|
103
|
+
error = Resizing::APIError.new('expired')
|
|
104
|
+
|
|
105
|
+
Resizing::Client.stub(:new, failing_confirm_client(error)) do
|
|
106
|
+
raised = assert_raises(Resizing::APIError) { model.save! }
|
|
107
|
+
assert_same error, raised
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def test_store_with_new_file_uploads_temporary_image_and_confirms_it
|
|
112
|
+
uploader = TestModel.new.resizing_picture
|
|
113
|
+
|
|
114
|
+
posted = capture_post_options do
|
|
115
|
+
assert_vcr_requests_made 'carrier_wave_test/confirm' do
|
|
116
|
+
assert_vcr_requests_made('carrier_wave_test/save') { uploader.store!(sample_uploaded_file) }
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
assert_equal true, posted.first[:temporary]
|
|
121
|
+
assert_equal expect_identifier, uploader.identifier
|
|
122
|
+
refute uploader.cached?
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# ============================================================
|
|
126
|
+
# <column>_cache による一時画像の復元
|
|
127
|
+
# ============================================================
|
|
128
|
+
|
|
129
|
+
def test_cache_returns_public_id_of_temporary_image
|
|
130
|
+
model = prepare_model
|
|
131
|
+
|
|
132
|
+
assert_equal expect_identifier, model.resizing_picture_cache
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def test_cache_is_nil_for_model_loaded_from_db
|
|
136
|
+
model = TestModel.new
|
|
137
|
+
model.send(:write_attribute, :resizing_picture, expect_identifier)
|
|
138
|
+
model.save!
|
|
139
|
+
|
|
140
|
+
assert_nil TestModel.find(model.id).resizing_picture_cache
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def test_cache_name_restores_temporary_image
|
|
144
|
+
model = TestModel.new
|
|
145
|
+
|
|
146
|
+
assert_vcr_requests_made 'carrier_wave_test/retrieve_from_cache' do
|
|
147
|
+
model.resizing_picture_cache = expect_identifier
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
151
|
+
assert_equal "#{expect_url}/", model.resizing_picture_url
|
|
152
|
+
assert_equal expect_identifier, model.resizing_picture_cache
|
|
153
|
+
|
|
154
|
+
# 復元した一時画像は保存時に確定する
|
|
155
|
+
assert_vcr_requests_made 'carrier_wave_test/confirm' do
|
|
156
|
+
model.save!
|
|
157
|
+
end
|
|
158
|
+
assert_equal expect_identifier, TestModel.find(model.id).read_attribute(:resizing_picture)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def test_cache_name_replaces_image_of_persisted_model
|
|
162
|
+
model = TestModel.new
|
|
163
|
+
model.send(:write_attribute, :resizing_picture, '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old/v1')
|
|
164
|
+
model.save!
|
|
165
|
+
model = TestModel.find(model.id)
|
|
166
|
+
|
|
167
|
+
VCR.use_cassette('carrier_wave_test/retrieve_from_cache', record: :none) do
|
|
168
|
+
model.resizing_picture_cache = expect_identifier
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def test_assigning_public_id_of_temporary_image_restores_it
|
|
175
|
+
# 文字列の代入も CarrierWave は cache name として扱う
|
|
176
|
+
model = TestModel.new
|
|
177
|
+
|
|
178
|
+
assert_vcr_requests_made 'carrier_wave_test/retrieve_from_cache' do
|
|
179
|
+
model.resizing_picture = expect_identifier
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def test_cache_name_uses_public_id_returned_by_resizing
|
|
186
|
+
# 送られてきた version ではなく、Resizing が返した public_id を書き込む
|
|
187
|
+
model = TestModel.new
|
|
188
|
+
tampered = expect_identifier.sub(/v[^v]+\z/, 'vtampered')
|
|
189
|
+
|
|
190
|
+
VCR.use_cassette('carrier_wave_test/retrieve_from_cache', record: :none) do
|
|
191
|
+
model.resizing_picture_cache = tampered
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def test_cache_name_of_confirmed_image_is_ignored
|
|
198
|
+
# 確定済みの画像は他のレコードの画像かもしれないので、cache name では付け替えない
|
|
199
|
+
assert_cache_name_ignored(metadata: { 'public_id' => expect_identifier, 'temporary' => false })
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def test_cache_name_of_expired_or_deleted_image_is_ignored
|
|
203
|
+
# 期限切れの一時画像には削除前でも 404 が返る
|
|
204
|
+
assert_cache_name_ignored(
|
|
205
|
+
metadata: { 'error' => 'ActiveRecord::RecordNotFound', 'message' => "#{IMAGE_ID} expired" }
|
|
206
|
+
)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def test_cache_name_of_other_project_is_ignored_without_request
|
|
210
|
+
assert_cache_name_ignored(cache_name: OTHER_PROJECT_IDENTIFIER)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def test_malformed_cache_name_is_ignored_without_request
|
|
214
|
+
assert_cache_name_ignored(cache_name: 'not-a-public-id')
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def test_retrieve_from_cache_raises_invalid_parameter_for_invalid_cache_name
|
|
218
|
+
uploader = TestModel.new.resizing_picture
|
|
219
|
+
|
|
220
|
+
assert_raises(::CarrierWave::InvalidParameter) { uploader.retrieve_from_cache!('not-a-public-id') }
|
|
221
|
+
assert_raises(::CarrierWave::InvalidParameter) { uploader.retrieve_from_cache!('') }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def test_blank_cache_name_keeps_assigned_image
|
|
225
|
+
model = prepare_model
|
|
226
|
+
|
|
227
|
+
model.resizing_picture_cache = ''
|
|
228
|
+
|
|
229
|
+
assert_equal expect_identifier, model.read_attribute(:resizing_picture)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
private
|
|
233
|
+
|
|
234
|
+
def prepare_model
|
|
235
|
+
model = TestModel.new
|
|
236
|
+
VCR.use_cassette('carrier_wave_test/save', record: :none) { attach_sample_image(model) }
|
|
237
|
+
model
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# ブロック内で Resizing.post に渡されたオプションを記録する (リクエストはそのまま発行する)
|
|
241
|
+
def capture_post_options(&block)
|
|
242
|
+
posted = []
|
|
243
|
+
original = Resizing.method(:post)
|
|
244
|
+
spy = lambda do |file, options|
|
|
245
|
+
posted << options
|
|
246
|
+
original.call(file, options)
|
|
247
|
+
end
|
|
248
|
+
Resizing.stub(:post, spy, &block)
|
|
249
|
+
posted
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def failing_confirm_client(error)
|
|
253
|
+
client = Object.new
|
|
254
|
+
client.define_singleton_method(:confirm) { |_image_id| raise error }
|
|
255
|
+
client
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# cache name を受け付けず、モデルに画像が付かないこと
|
|
259
|
+
# metadata を渡した場合はその応答を返し、渡さない場合はリクエストしないことを確認する
|
|
260
|
+
def assert_cache_name_ignored(cache_name: expect_identifier, metadata: nil)
|
|
261
|
+
model = TestModel.new
|
|
262
|
+
looked_up = []
|
|
263
|
+
responder = lambda do |image_id, _options|
|
|
264
|
+
flunk "metadata must not be requested for #{cache_name}" if metadata.nil?
|
|
265
|
+
|
|
266
|
+
looked_up << image_id
|
|
267
|
+
metadata
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
Resizing.stub(:metadata, responder) do
|
|
271
|
+
model.resizing_picture_cache = cache_name
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
assert_equal(metadata.nil? ? [] : [IMAGE_ID], looked_up)
|
|
275
|
+
assert_nil model.read_attribute(:resizing_picture)
|
|
276
|
+
assert model.resizing_picture.blank?
|
|
277
|
+
assert_nil model.resizing_picture_cache
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
end
|
|
@@ -12,9 +12,16 @@ module Resizing
|
|
|
12
12
|
TestJPGModel.delete_all
|
|
13
13
|
|
|
14
14
|
configure_resizing
|
|
15
|
+
|
|
16
|
+
# 画像を代入したモデルを保存すると、コミット時に一時画像の確定 (PUT confirm) が発行される
|
|
17
|
+
# このクラスのテストは確定以外のリクエストを対象にしているので、確定は外側のこのカセットで応答する
|
|
18
|
+
# (確定そのものは CarrierWaveTemporaryImageTest でテストする)
|
|
19
|
+
VCR.insert_cassette 'carrier_wave_test/confirm', record: :none, allow_playback_repeats: true
|
|
15
20
|
end
|
|
16
21
|
|
|
17
|
-
def teardown
|
|
22
|
+
def teardown
|
|
23
|
+
VCR.eject_cassette
|
|
24
|
+
end
|
|
18
25
|
|
|
19
26
|
def test_remove_resizing_picture
|
|
20
27
|
model = prepare_model TestModel
|
|
@@ -254,8 +261,10 @@ module Resizing
|
|
|
254
261
|
# 画像更新時に古い画像を削除する DELETE リクエストが発行されることを確認
|
|
255
262
|
# VCR カセット update_image.yml には以下が含まれる:
|
|
256
263
|
# 1. POST (古い画像のアップロード) -> old-image-id を返す
|
|
257
|
-
# 2.
|
|
258
|
-
# 3.
|
|
264
|
+
# 2. PUT confirm (古い画像の確定)
|
|
265
|
+
# 3. POST (新しい画像のアップロード) -> new-image-id を返す
|
|
266
|
+
# 4. PUT confirm (新しい画像の確定)
|
|
267
|
+
# 5. DELETE (古い画像の削除) -> old-image-id を削除
|
|
259
268
|
|
|
260
269
|
assert_vcr_requests_made 'carrier_wave_test/update_image' do
|
|
261
270
|
model = TestModel.new
|
|
@@ -276,7 +285,7 @@ module Resizing
|
|
|
276
285
|
new_public_id = model.read_attribute(:resizing_picture)
|
|
277
286
|
assert_match(/new-image-id/, new_public_id)
|
|
278
287
|
end
|
|
279
|
-
# カセット内の全リクエスト(POST x2, DELETE x1)が発行されたことを確認
|
|
288
|
+
# カセット内の全リクエスト(POST x2, PUT confirm x2, DELETE x1)が発行されたことを確認
|
|
280
289
|
end
|
|
281
290
|
|
|
282
291
|
# ============================================================
|
|
@@ -534,7 +543,7 @@ module Resizing
|
|
|
534
543
|
def test_save_after_assignment_does_not_upload_again
|
|
535
544
|
model = prepare_model TestModel
|
|
536
545
|
|
|
537
|
-
# 代入時に既にアップロード済みなので、save! で再度 POST は発行されない
|
|
546
|
+
# 代入時に既にアップロード済みなので、save! で再度 POST は発行されない (一時画像の確定だけ行う)
|
|
538
547
|
assert_vcr_no_requests 'carrier_wave_test/save' do
|
|
539
548
|
model.save!
|
|
540
549
|
end
|
|
@@ -596,7 +605,7 @@ module Resizing
|
|
|
596
605
|
# ============================================================
|
|
597
606
|
|
|
598
607
|
def test_updated_image_is_persisted_and_old_image_deleted
|
|
599
|
-
# update_image カセット: POST(old) -> POST(new) -> DELETE(old)
|
|
608
|
+
# update_image カセット: POST(old) -> confirm(old) -> POST(new) -> confirm(new) -> DELETE(old)
|
|
600
609
|
model = nil
|
|
601
610
|
assert_vcr_requests_made 'carrier_wave_test/update_image' do
|
|
602
611
|
model = TestModel.new
|
|
@@ -186,6 +186,20 @@ module Resizing
|
|
|
186
186
|
|
|
187
187
|
assert_nil uploader.store_versions!
|
|
188
188
|
assert_nil uploader.remove_versions!
|
|
189
|
+
assert_nil uploader.retrieve_versions_from_cache!(IDENTIFIER)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def test_cache_expires_in_defaults_to_nil
|
|
193
|
+
assert_nil TestModel.new.resizing_picture.cache_expires_in
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def test_cached_file_is_not_deleted_after_storing
|
|
197
|
+
# 一時画像は確定するとそのまま保存済みの画像になるので、保存後に削除してはいけない
|
|
198
|
+
assert_equal false, TestModel.new.resizing_picture.delete_tmp_file_after_storage
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def test_cache_name_is_nil_when_nothing_is_cached
|
|
202
|
+
assert_nil TestModel.new.resizing_picture.cache_name
|
|
189
203
|
end
|
|
190
204
|
|
|
191
205
|
def test_rename_raises_not_implemented_error
|