resizing 1.2.2 → 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/Dockerfile +73 -0
- data/.devcontainer/compose.yaml +61 -0
- data/.devcontainer/devcontainer-lock.json +9 -0
- data/.devcontainer/devcontainer.json +32 -0
- data/.devcontainer/post-create.sh +41 -0
- data/.github/copilot-instructions.md +1 -0
- data/.github/dependabot.yml +23 -0
- data/.github/labeler.yml +28 -0
- data/.github/release.yml +35 -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 +22 -0
- data/.github/workflows/lint.yml +37 -0
- data/.github/workflows/secret-scan.yml +29 -0
- data/.github/workflows/test.yml +51 -13
- data/.github/workflows/verify-lockfile.yml +28 -0
- data/AGENTS.md +1 -0
- data/CHANGELOG.md +3 -0
- data/CLAUDE.md +68 -0
- data/Gemfile +12 -3
- data/README.ja.md +241 -0
- data/README.md +196 -2
- data/Rakefile +5 -7
- data/bin/setup +3 -0
- data/docker-compose.yml +7 -0
- data/git-hooks/pre-push +6 -0
- data/git-hooks/pre-push-ruby.sh +53 -0
- data/lib/resizing/carrier_wave/callback_file.rb +88 -0
- data/lib/resizing/carrier_wave/storage/file.rb +105 -38
- data/lib/resizing/carrier_wave/storage/remote.rb +25 -16
- data/lib/resizing/carrier_wave.rb +109 -6
- data/lib/resizing/client.rb +47 -4
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +40 -10
- data/lib/resizing/mock_client.rb +48 -9
- data/lib/resizing/public_id.rb +3 -2
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +4 -1
- data/resizing.gemspec +3 -3
- 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 +495 -9
- data/test/resizing/carrier_wave/storage/remote_test.rb +151 -12
- data/test/resizing/carrier_wave_cache_callbacks_test.rb +224 -0
- data/test/resizing/carrier_wave_callback_file_test.rb +77 -0
- 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 +214 -30
- data/test/resizing/carrier_wave_uploader_test.rb +225 -0
- data/test/resizing/client_test.rb +419 -30
- data/test/resizing/configuration_test.rb +235 -10
- data/test/resizing/http_clientable_test.rb +27 -0
- data/test/resizing/mock_client_test.rb +145 -1
- data/test/resizing/public_id_test.rb +96 -0
- data/test/resizing_module_test.rb +126 -0
- data/test/resizing_test.rb +8 -0
- data/test/test_helper.rb +318 -9
- 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 +47 -9
|
@@ -190,6 +190,21 @@ class ResizingModuleTest < Minitest::Test
|
|
|
190
190
|
assert_equal 'image_id', result['id']
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
+
def test_confirm_delegates_to_client
|
|
194
|
+
Resizing.configure = {
|
|
195
|
+
image_host: 'https://img.example.com',
|
|
196
|
+
project_id: 'project123',
|
|
197
|
+
secret_token: 'token123',
|
|
198
|
+
enable_mock: true
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
result = Resizing.confirm('image_id')
|
|
202
|
+
|
|
203
|
+
assert_instance_of Hash, result
|
|
204
|
+
assert_equal 'image_id', result['id']
|
|
205
|
+
assert_equal false, result['temporary']
|
|
206
|
+
end
|
|
207
|
+
|
|
193
208
|
def test_metadata_delegates_to_client
|
|
194
209
|
Resizing.configure = {
|
|
195
210
|
image_host: 'https://img.example.com',
|
|
@@ -203,4 +218,115 @@ class ResizingModuleTest < Minitest::Test
|
|
|
203
218
|
assert_instance_of Hash, result
|
|
204
219
|
assert_equal 'image_id', result['id']
|
|
205
220
|
end
|
|
221
|
+
|
|
222
|
+
def test_post_delegates_to_client
|
|
223
|
+
Resizing.configure = {
|
|
224
|
+
image_host: 'https://img.example.com',
|
|
225
|
+
project_id: 'project123',
|
|
226
|
+
secret_token: 'token123',
|
|
227
|
+
enable_mock: true
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
result = Resizing.post('test/data/images/sample1.jpg', content_type: 'image/jpeg')
|
|
231
|
+
|
|
232
|
+
assert_instance_of Hash, result
|
|
233
|
+
assert_equal '87263920-2081-498e-a107-9625f4fde01b', result['id']
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def test_client_raises_configuration_error_when_not_configured
|
|
237
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.client }
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def test_api_methods_raise_configuration_error_when_not_configured
|
|
241
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.post('dummy', content_type: 'image/jpeg') }
|
|
242
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.put('id', 'dummy', content_type: 'image/jpeg') }
|
|
243
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.delete('id') }
|
|
244
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.confirm('id') }
|
|
245
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.metadata('id', {}) }
|
|
246
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.url_from_image_id('id') }
|
|
247
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.generate_identifier }
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def test_configure_setter_raises_for_invalid_hash
|
|
251
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.configure = { project_id: 'only-project' } }
|
|
252
|
+
assert_raises(Resizing::ConfigurationError) { Resizing.configure = { secret_token: 'only-token' } }
|
|
253
|
+
assert_raises(Resizing::ConfigurationError) do
|
|
254
|
+
Resizing.configure = { project_id: 'p', secret_token: 's', host: 'deprecated-host' }
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def test_configure_setter_replaces_previous_configuration
|
|
259
|
+
Resizing.configure = { project_id: 'first', secret_token: 'token' }
|
|
260
|
+
Resizing.configure = { project_id: 'second', secret_token: 'token' }
|
|
261
|
+
|
|
262
|
+
assert_equal 'second', Resizing.configure.project_id
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def test_client_reflects_latest_configuration
|
|
266
|
+
Resizing.configure = { project_id: 'p', secret_token: 's', enable_mock: false }
|
|
267
|
+
assert_instance_of Resizing::Client, Resizing.client
|
|
268
|
+
|
|
269
|
+
Resizing.configure = { project_id: 'p', secret_token: 's', enable_mock: true }
|
|
270
|
+
assert_instance_of Resizing::MockClient, Resizing.client
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def test_separate_public_id_extracts_project_id_image_id_and_version
|
|
274
|
+
matched = Resizing.separate_public_id(
|
|
275
|
+
'/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c/v1Id850q34fgsaer23w'
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
assert_equal '098a2a0d-c387-4135-a071-1254d6d7e70a', matched[:project_id]
|
|
279
|
+
assert_equal '28c49144-c00d-4cb5-8619-98ce95977b9c', matched[:image_id]
|
|
280
|
+
assert_equal '1Id850q34fgsaer23w', matched[:version]
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def test_separate_public_id_without_version
|
|
284
|
+
matched = Resizing.separate_public_id(
|
|
285
|
+
'/projects/098a2a0d-c387-4135-a071-1254d6d7e70a/upload/images/28c49144-c00d-4cb5-8619-98ce95977b9c'
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
assert_equal '28c49144-c00d-4cb5-8619-98ce95977b9c', matched[:image_id]
|
|
289
|
+
assert_nil matched[:version]
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def test_separate_public_id_returns_nil_for_unrelated_string
|
|
293
|
+
assert_nil Resizing.separate_public_id('/foo/bar')
|
|
294
|
+
assert_nil Resizing.separate_public_id('')
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def test_generate_identifier_returns_new_value_each_time
|
|
298
|
+
Resizing.configure = { project_id: '098a2a0d-c387-4135-a071-1254d6d7e70a', secret_token: 'token' }
|
|
299
|
+
|
|
300
|
+
first = Resizing.generate_identifier
|
|
301
|
+
second = Resizing.generate_identifier
|
|
302
|
+
|
|
303
|
+
refute_equal first, second
|
|
304
|
+
# 生成した identifier は PublicId として解釈できる
|
|
305
|
+
assert_equal '098a2a0d-c387-4135-a071-1254d6d7e70a', Resizing::PublicId.new(first).project_id
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def test_url_from_image_id_uses_default_image_host_when_not_specified
|
|
309
|
+
Resizing.configure = { project_id: 'project123', secret_token: 'token123' }
|
|
310
|
+
|
|
311
|
+
assert_equal 'https://img.resizing.net/projects/project123/upload/images/image456', Resizing.url_from_image_id('image456')
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Issue #95: 実体のないファイルを指す autoload が残っていると
|
|
315
|
+
# その定数を参照した瞬間に LoadError になるため、
|
|
316
|
+
# 全 autoload 先が実際にロードできることを確認する
|
|
317
|
+
def test_all_autoload_targets_are_loadable
|
|
318
|
+
source = File.read(File.expand_path('../lib/resizing.rb', __dir__))
|
|
319
|
+
const_names = source.scan(/autoload\s+:(\w+),/).flatten
|
|
320
|
+
|
|
321
|
+
refute_empty const_names
|
|
322
|
+
|
|
323
|
+
const_names.each do |const_name|
|
|
324
|
+
assert Resizing.const_get(const_name), "Resizing::#{const_name} should be loadable"
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def test_video_constant_is_not_registered
|
|
329
|
+
refute_includes Resizing.constants, :Video
|
|
330
|
+
assert_raises(NameError) { Resizing.const_get(:Video) }
|
|
331
|
+
end
|
|
206
332
|
end
|
data/test/resizing_test.rb
CHANGED
|
@@ -6,4 +6,12 @@ class ResizingTest < Minitest::Test
|
|
|
6
6
|
def test_that_it_has_a_version_number
|
|
7
7
|
refute_nil ::Resizing::VERSION
|
|
8
8
|
end
|
|
9
|
+
|
|
10
|
+
def test_version_follows_semantic_versioning
|
|
11
|
+
assert_match(/\A\d+\.\d+\.\d+(\.pre)?\z/, ::Resizing::VERSION)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_version_is_frozen_string
|
|
15
|
+
assert ::Resizing::VERSION.frozen?
|
|
16
|
+
end
|
|
9
17
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -5,6 +5,13 @@ require 'simplecov-cobertura'
|
|
|
5
5
|
|
|
6
6
|
SimpleCov.start do
|
|
7
7
|
add_filter '/test/'
|
|
8
|
+
# bundle install で vendor/ 配下に入る gem を計測対象から除外し、
|
|
9
|
+
# カバレッジ率が lib の実態を表すようにする
|
|
10
|
+
add_filter '/vendor/'
|
|
11
|
+
# version.rb is loaded by the gemspec before SimpleCov starts, so it always shows
|
|
12
|
+
# 0% coverage. Exclude it so that release PRs do not fail the patch check.
|
|
13
|
+
add_filter '/lib/resizing/version.rb'
|
|
14
|
+
track_files 'lib/**/*.rb'
|
|
8
15
|
|
|
9
16
|
if ENV['CI']
|
|
10
17
|
formatter SimpleCov::Formatter::CoberturaFormatter
|
|
@@ -20,6 +27,8 @@ SimpleCov.start do
|
|
|
20
27
|
end
|
|
21
28
|
|
|
22
29
|
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
|
30
|
+
require 'digest'
|
|
31
|
+
require 'socket'
|
|
23
32
|
require 'time'
|
|
24
33
|
require 'timecop'
|
|
25
34
|
require 'vcr'
|
|
@@ -47,6 +56,46 @@ VCR.configure do |c|
|
|
|
47
56
|
end
|
|
48
57
|
end
|
|
49
58
|
|
|
59
|
+
# Resizing::Client の代わりに使う偽クライアント (delete / confirm の応答を固定し、呼び出しを記録する)
|
|
60
|
+
class FakeApiClient
|
|
61
|
+
attr_reader :deleted_ids, :confirmed_ids
|
|
62
|
+
|
|
63
|
+
def initialize(response)
|
|
64
|
+
@response = response
|
|
65
|
+
@deleted_ids = []
|
|
66
|
+
@confirmed_ids = []
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def delete(image_id)
|
|
70
|
+
@deleted_ids << image_id
|
|
71
|
+
@response.dup
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def confirm(image_id)
|
|
75
|
+
@confirmed_ids << image_id
|
|
76
|
+
@response.dup
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Resizing::Client を FakeApiClient に差し替えるヘルパー
|
|
81
|
+
module FakeApiClientAssertions
|
|
82
|
+
# Resizing::Client.new が FakeApiClient を返す状態でブロックを実行する
|
|
83
|
+
#
|
|
84
|
+
# @param response [Hash] 偽クライアントの delete が返す固定レスポンス
|
|
85
|
+
# @yield [FakeApiClient] 差し替えた偽クライアント
|
|
86
|
+
# @return [Object] ブロックの戻り値
|
|
87
|
+
#
|
|
88
|
+
# @example
|
|
89
|
+
# with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
|
|
90
|
+
# file.delete
|
|
91
|
+
# assert_equal [IMAGE_ID], fake.deleted_ids
|
|
92
|
+
# end
|
|
93
|
+
def with_fake_api_client(response)
|
|
94
|
+
fake = FakeApiClient.new(response)
|
|
95
|
+
Resizing::Client.stub(:new, fake) { yield fake }
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
50
99
|
# VCRカセットのリクエストが実際に使用されたかを検証するヘルパー
|
|
51
100
|
module VCRRequestAssertions
|
|
52
101
|
# VCRカセット内でブロックを実行し、カセットのインタラクションがすべて使用されたことを確認
|
|
@@ -129,15 +178,172 @@ module VCRRequestAssertions
|
|
|
129
178
|
end
|
|
130
179
|
end
|
|
131
180
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
181
|
+
# 全テストで共通の Resizing の設定と、アップロードのテスト用ヘルパー
|
|
182
|
+
#
|
|
183
|
+
# 設定値と識別子は test/vcr/ のカセットに記録されているホスト・プロジェクト・
|
|
184
|
+
# レスポンスと揃えてあるので、変更するときはカセットも合わせて録り直すこと
|
|
185
|
+
module ResizingTestConfiguration
|
|
186
|
+
CONFIGURATION_TEMPLATE = {
|
|
187
|
+
image_host: 'http://192.168.56.101:5000',
|
|
188
|
+
project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
|
|
189
|
+
secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
|
|
190
|
+
open_timeout: 10,
|
|
191
|
+
response_timeout: 20
|
|
192
|
+
}.freeze
|
|
193
|
+
|
|
194
|
+
# carrier_wave_test/save カセットが返す画像の public id
|
|
195
|
+
UPLOADED_IDENTIFIER = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/' \
|
|
196
|
+
'14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
197
|
+
|
|
198
|
+
def configuration_template
|
|
199
|
+
CONFIGURATION_TEMPLATE
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# @param overrides [Hash] 既定の設定を上書きする値
|
|
203
|
+
def configure_resizing(overrides = {})
|
|
204
|
+
Resizing.configure = CONFIGURATION_TEMPLATE.merge(overrides)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def expect_identifier
|
|
208
|
+
UPLOADED_IDENTIFIER
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def expect_url
|
|
212
|
+
"#{CONFIGURATION_TEMPLATE[:image_host]}#{UPLOADED_IDENTIFIER}"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# アップロード用のサンプル画像
|
|
216
|
+
# 実際に POST が発行されるので VCR カセット内で使うこと
|
|
217
|
+
def sample_uploaded_file
|
|
218
|
+
file = File.open('test/data/images/sample1.jpg', 'r')
|
|
219
|
+
ActionDispatch::Http::UploadedFile.new(
|
|
220
|
+
filename: File.basename(file.path),
|
|
221
|
+
type: 'image/jpeg',
|
|
222
|
+
tempfile: file
|
|
223
|
+
)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# モデルにサンプル画像を添付するヘルパーメソッド
|
|
227
|
+
# VCRカセット内で呼び出す必要がある
|
|
228
|
+
def attach_sample_image(model)
|
|
229
|
+
model.resizing_picture = sample_uploaded_file
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# `remote_<column>_url=` でダウンロードされたのと同じ形のファイル
|
|
233
|
+
# (CarrierWave::Downloader::RemoteFile) を、HTTP を発行せずに組み立てる
|
|
234
|
+
#
|
|
235
|
+
# @param url [String] ダウンロード元 URL (ファイル名の決定に使われる)
|
|
236
|
+
# @param content_type [String] レスポンスの Content-Type
|
|
237
|
+
# @param body [String] レスポンスボディ (既定はサンプル画像)
|
|
238
|
+
def sample_remote_file(url, content_type:, body: File.binread('test/data/images/sample1.jpg'))
|
|
239
|
+
response = Net::HTTPOK.new('1.1', '200', 'OK')
|
|
240
|
+
response['Content-Type'] = content_type
|
|
241
|
+
response.instance_variable_set(:@body, body)
|
|
242
|
+
response.instance_variable_set(:@read, true)
|
|
243
|
+
response.uri = URI.parse(url)
|
|
244
|
+
::CarrierWave::Downloader::RemoteFile.new(response)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# `remote_<column>_url=` のダウンロードを差し替えて、与えた RemoteFile を返すようにする
|
|
248
|
+
# (テストからは外部 HTTP を発行しないため)
|
|
249
|
+
#
|
|
250
|
+
# @param remote_file [CarrierWave::Downloader::RemoteFile] ダウンロード結果として返すファイル
|
|
251
|
+
def with_stubbed_download(remote_file, &block)
|
|
252
|
+
downloader = Object.new
|
|
253
|
+
downloader.define_singleton_method(:download) { |_url, _headers = {}| remote_file }
|
|
254
|
+
::CarrierWave::Downloader::Base.stub(:new, downloader, &block)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Test database setup
|
|
259
|
+
#
|
|
260
|
+
# By default each test process creates its own database and drops it when the
|
|
261
|
+
# run finishes, so that several test processes sharing one MySQL server do not
|
|
262
|
+
# overwrite each other's tables and records.
|
|
263
|
+
#
|
|
264
|
+
# The server can be overridden with MYSQL_HOST / MYSQL_PORT / MYSQL_USER /
|
|
265
|
+
# MYSQL_PASSWORD (the dev container runs MySQL in a separate container).
|
|
266
|
+
# Setting MYSQL_DATABASE uses that database as is and never drops it; give each
|
|
267
|
+
# concurrent process a different name in that case.
|
|
268
|
+
module TestDatabase
|
|
269
|
+
BASE_NAME = 'resizing_gem_test'
|
|
270
|
+
|
|
271
|
+
module_function
|
|
272
|
+
|
|
273
|
+
# Host identifier mixed into the per-process database name, so that PIDs from
|
|
274
|
+
# different hosts do not collide on a shared MySQL server
|
|
275
|
+
def host_token
|
|
276
|
+
@host_token ||= Digest::MD5.hexdigest(Socket.gethostname)[0, 8]
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def per_process_name_pattern
|
|
280
|
+
/\A#{Regexp.escape(BASE_NAME)}_#{host_token}_(\d+)\z/
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def database_name
|
|
284
|
+
@database_name ||= ENV['MYSQL_DATABASE'] || "#{BASE_NAME}_#{host_token}_#{Process.pid}"
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Whether the database belongs to this process alone and can be dropped
|
|
288
|
+
def ephemeral?
|
|
289
|
+
ENV['MYSQL_DATABASE'].nil?
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def server_config
|
|
293
|
+
{
|
|
294
|
+
adapter: 'mysql2',
|
|
295
|
+
host: ENV.fetch('MYSQL_HOST', '127.0.0.1'),
|
|
296
|
+
port: Integer(ENV.fetch('MYSQL_PORT', '3306')),
|
|
297
|
+
encoding: 'utf8',
|
|
298
|
+
username: ENV.fetch('MYSQL_USER', 'root'),
|
|
299
|
+
password: ENV.fetch('MYSQL_PASSWORD', 'secret')
|
|
300
|
+
}
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def setup
|
|
304
|
+
ActiveRecord::Base.establish_connection(server_config)
|
|
305
|
+
drop_abandoned_databases if ephemeral?
|
|
306
|
+
ActiveRecord::Base.connection.execute("create database if not exists `#{database_name}`")
|
|
307
|
+
ActiveRecord::Base.establish_connection(server_config.merge(database: database_name))
|
|
308
|
+
|
|
309
|
+
# Minitest.after_run, not at_exit: at_exit handlers run in reverse order of
|
|
310
|
+
# registration, so this one would drop the database before the tests run
|
|
311
|
+
Minitest.after_run { drop_database } if ephemeral?
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def drop_database
|
|
315
|
+
ActiveRecord::Base.establish_connection(server_config)
|
|
316
|
+
ActiveRecord::Base.connection.execute("drop database if exists `#{database_name}`")
|
|
317
|
+
rescue StandardError => e
|
|
318
|
+
warn "failed to drop test database #{database_name}: #{e.message}"
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Clean up databases left behind by processes that were killed before they
|
|
322
|
+
# could drop their own
|
|
323
|
+
def drop_abandoned_databases
|
|
324
|
+
connection = ActiveRecord::Base.connection
|
|
325
|
+
|
|
326
|
+
connection.select_values('show databases').each do |database|
|
|
327
|
+
matched = per_process_name_pattern.match(database)
|
|
328
|
+
next if matched.nil? || process_alive?(matched[1].to_i)
|
|
329
|
+
|
|
330
|
+
connection.execute("drop database if exists `#{database}`")
|
|
331
|
+
end
|
|
332
|
+
rescue StandardError => e
|
|
333
|
+
warn "failed to drop abandoned test databases: #{e.message}"
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def process_alive?(pid)
|
|
337
|
+
Process.kill(0, pid)
|
|
338
|
+
true
|
|
339
|
+
rescue Errno::ESRCH
|
|
340
|
+
false
|
|
341
|
+
rescue Errno::EPERM
|
|
342
|
+
true
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
TestDatabase.setup
|
|
141
347
|
|
|
142
348
|
ActiveRecord::Schema.define do
|
|
143
349
|
self.verbose = false
|
|
@@ -186,10 +392,113 @@ class ResizingUploaderWithDefaultURL < CarrierWave::Uploader::Base
|
|
|
186
392
|
end
|
|
187
393
|
end
|
|
188
394
|
|
|
395
|
+
# before :cache コールバック (拡張子 / content_type / サイズのチェック) のテスト用アップローダ
|
|
396
|
+
class ResizingUploaderWithExtensionAllowlist < CarrierWave::Uploader::Base
|
|
397
|
+
include Resizing::CarrierWave
|
|
398
|
+
|
|
399
|
+
def extension_allowlist
|
|
400
|
+
%w[png]
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
class ResizingUploaderWithExtensionDenylist < CarrierWave::Uploader::Base
|
|
405
|
+
include Resizing::CarrierWave
|
|
406
|
+
|
|
407
|
+
def extension_denylist
|
|
408
|
+
%w[jpg]
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# 拡張子の判定が content_type から行われることのテスト用アップローダ
|
|
413
|
+
# (sample1.jpg / image/jpeg を受け付け、image/png は拒否する)
|
|
414
|
+
class ResizingUploaderWithJpegAllowlist < CarrierWave::Uploader::Base
|
|
415
|
+
include Resizing::CarrierWave
|
|
416
|
+
|
|
417
|
+
def extension_allowlist
|
|
418
|
+
%w[jpg jpeg]
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
class ResizingUploaderWithContentTypeAllowlist < CarrierWave::Uploader::Base
|
|
423
|
+
include Resizing::CarrierWave
|
|
424
|
+
|
|
425
|
+
def content_type_allowlist
|
|
426
|
+
['image/png']
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
class ResizingUploaderWithSizeRange < CarrierWave::Uploader::Base
|
|
431
|
+
include Resizing::CarrierWave
|
|
432
|
+
|
|
433
|
+
def size_range
|
|
434
|
+
0..10
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# process! が発火しないことのテスト用アップローダ
|
|
439
|
+
# convert / transformation は配信 URL の transform を組み立てるための宣言であり
|
|
440
|
+
# uploader のメソッドとしては存在しないので、process! が走ると NoMethodError になる
|
|
441
|
+
class ResizingUploaderWithUndefinedProcessor < CarrierWave::Uploader::Base
|
|
442
|
+
include Resizing::CarrierWave
|
|
443
|
+
|
|
444
|
+
process convert: 'png'
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# Uploader that changes the expiry of the temporary image uploaded on assignment
|
|
448
|
+
class ResizingUploaderWithCacheExpiresIn < CarrierWave::Uploader::Base
|
|
449
|
+
include Resizing::CarrierWave
|
|
450
|
+
|
|
451
|
+
def cache_expires_in
|
|
452
|
+
3600
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
|
|
189
456
|
class TestModel < ::ActiveRecord::Base
|
|
190
457
|
mount_uploader :resizing_picture, ResizingUploader
|
|
191
458
|
end
|
|
192
459
|
|
|
460
|
+
class TestModelWithExtensionAllowlist < ::ActiveRecord::Base
|
|
461
|
+
self.table_name = 'test_models'
|
|
462
|
+
|
|
463
|
+
mount_uploader :resizing_picture, ResizingUploaderWithExtensionAllowlist
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
class TestModelWithExtensionDenylist < ::ActiveRecord::Base
|
|
467
|
+
self.table_name = 'test_models'
|
|
468
|
+
|
|
469
|
+
mount_uploader :resizing_picture, ResizingUploaderWithExtensionDenylist
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
class TestModelWithJpegAllowlist < ::ActiveRecord::Base
|
|
473
|
+
self.table_name = 'test_models'
|
|
474
|
+
|
|
475
|
+
mount_uploader :resizing_picture, ResizingUploaderWithJpegAllowlist
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
class TestModelWithContentTypeAllowlist < ::ActiveRecord::Base
|
|
479
|
+
self.table_name = 'test_models'
|
|
480
|
+
|
|
481
|
+
mount_uploader :resizing_picture, ResizingUploaderWithContentTypeAllowlist
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
class TestModelWithSizeRange < ::ActiveRecord::Base
|
|
485
|
+
self.table_name = 'test_models'
|
|
486
|
+
|
|
487
|
+
mount_uploader :resizing_picture, ResizingUploaderWithSizeRange
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
class TestModelWithUndefinedProcessor < ::ActiveRecord::Base
|
|
491
|
+
self.table_name = 'test_models'
|
|
492
|
+
|
|
493
|
+
mount_uploader :resizing_picture, ResizingUploaderWithUndefinedProcessor
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
class TestModelWithCacheExpiresIn < ::ActiveRecord::Base
|
|
497
|
+
self.table_name = 'test_models'
|
|
498
|
+
|
|
499
|
+
mount_uploader :resizing_picture, ResizingUploaderWithCacheExpiresIn
|
|
500
|
+
end
|
|
501
|
+
|
|
193
502
|
class TestJPGModel < ::ActiveRecord::Base
|
|
194
503
|
mount_uploader :resizing_picture, ResizingJPGUploader
|
|
195
504
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
http_interactions:
|
|
3
|
+
- request:
|
|
4
|
+
method: put
|
|
5
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/confirm
|
|
6
|
+
body:
|
|
7
|
+
encoding: US-ASCII
|
|
8
|
+
string: ''
|
|
9
|
+
headers:
|
|
10
|
+
User-Agent:
|
|
11
|
+
- Faraday v1.0.1
|
|
12
|
+
response:
|
|
13
|
+
status:
|
|
14
|
+
code: 200
|
|
15
|
+
message: OK
|
|
16
|
+
headers:
|
|
17
|
+
content-type:
|
|
18
|
+
- application/json; charset=utf-8
|
|
19
|
+
body:
|
|
20
|
+
encoding: UTF-8
|
|
21
|
+
string: '{"id":"14ea7aac-a194-4330-931f-6b562aec413d","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","latest_version_id":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"","size":848590,"created_at":"2020-10-11T04:56:48.340Z","updated_at":"2020-10-11T04:57:10.120Z","filename":"sample1.jpg","width":4032,"height":3016,"format":"jpeg","version":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","temporary":false,"expires_at":null}'
|
|
22
|
+
recorded_at: Sun, 11 Oct 2020 04:57:10 GMT
|
|
23
|
+
recorded_with: VCR 6.0.0
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
http_interactions:
|
|
3
|
+
- request:
|
|
4
|
+
method: get
|
|
5
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/metadata
|
|
6
|
+
body:
|
|
7
|
+
encoding: US-ASCII
|
|
8
|
+
string: ''
|
|
9
|
+
headers:
|
|
10
|
+
User-Agent:
|
|
11
|
+
- Faraday v1.0.1
|
|
12
|
+
response:
|
|
13
|
+
status:
|
|
14
|
+
code: 200
|
|
15
|
+
message: OK
|
|
16
|
+
headers:
|
|
17
|
+
content-type:
|
|
18
|
+
- application/json; charset=utf-8
|
|
19
|
+
body:
|
|
20
|
+
encoding: UTF-8
|
|
21
|
+
string: '{"id":"14ea7aac-a194-4330-931f-6b562aec413d","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","latest_version_id":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"","size":848590,"created_at":"2020-10-11T04:56:48.340Z","updated_at":"2020-10-11T04:56:48.340Z","filename":"sample1.jpg","width":4032,"height":3016,"format":"jpeg","version":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","temporary":true,"expires_at":"2020-10-12T04:56:48.340Z"}'
|
|
22
|
+
recorded_at: Sun, 11 Oct 2020 04:57:05 GMT
|
|
23
|
+
recorded_with: VCR 6.0.0
|
|
@@ -47,6 +47,6 @@ http_interactions:
|
|
|
47
47
|
- chunked
|
|
48
48
|
body:
|
|
49
49
|
encoding: UTF-8
|
|
50
|
-
string: '{"id":"14ea7aac-a194-4330-931f-6b562aec413d","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","latest_version_id":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"","size":848590,"created_at":"2020-10-11T04:56:48.340Z","updated_at":"2020-10-11T04:56:48.340Z","filename":"sample1.jpg","width":4032,"height":3016,"format":"jpeg","version":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e"}'
|
|
50
|
+
string: '{"id":"14ea7aac-a194-4330-931f-6b562aec413d","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","latest_version_id":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"","size":848590,"created_at":"2020-10-11T04:56:48.340Z","updated_at":"2020-10-11T04:56:48.340Z","filename":"sample1.jpg","width":4032,"height":3016,"format":"jpeg","version":"_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e","temporary":true,"expires_at":"2020-10-12T04:56:48.340Z"}'
|
|
51
51
|
recorded_at: Sun, 11 Oct 2020 04:56:48 GMT
|
|
52
52
|
recorded_with: VCR 6.0.0
|
|
@@ -18,7 +18,7 @@ http_interactions:
|
|
|
18
18
|
- application/json; charset=utf-8
|
|
19
19
|
body:
|
|
20
20
|
encoding: UTF-8
|
|
21
|
-
string: '{"id":"old-image-id-1111-1111-111111111111","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v1","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111/v1"}'
|
|
21
|
+
string: '{"id":"old-image-id-1111-1111-111111111111","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v1","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111/v1","temporary":true,"expires_at":"2020-10-12T04:56:48.000Z"}'
|
|
22
22
|
recorded_at: Sun, 11 Oct 2020 04:56:48 GMT
|
|
23
23
|
- request:
|
|
24
24
|
method: post
|
|
@@ -38,7 +38,47 @@ http_interactions:
|
|
|
38
38
|
- application/json; charset=utf-8
|
|
39
39
|
body:
|
|
40
40
|
encoding: UTF-8
|
|
41
|
-
string: '{"id":"new-image-id-2222-2222-222222222222","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v2","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/v2"}'
|
|
41
|
+
string: '{"id":"new-image-id-2222-2222-222222222222","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v2","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/v2","temporary":true,"expires_at":"2020-10-12T04:56:49.000Z"}'
|
|
42
|
+
recorded_at: Sun, 11 Oct 2020 04:56:49 GMT
|
|
43
|
+
- request:
|
|
44
|
+
method: put
|
|
45
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111/confirm
|
|
46
|
+
body:
|
|
47
|
+
encoding: US-ASCII
|
|
48
|
+
string: ''
|
|
49
|
+
headers:
|
|
50
|
+
User-Agent:
|
|
51
|
+
- Faraday v1.0.1
|
|
52
|
+
response:
|
|
53
|
+
status:
|
|
54
|
+
code: 200
|
|
55
|
+
message: OK
|
|
56
|
+
headers:
|
|
57
|
+
content-type:
|
|
58
|
+
- application/json; charset=utf-8
|
|
59
|
+
body:
|
|
60
|
+
encoding: UTF-8
|
|
61
|
+
string: '{"id":"old-image-id-1111-1111-111111111111","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v1","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/old-image-id-1111-1111-111111111111/v1","temporary":false,"expires_at":null}'
|
|
62
|
+
recorded_at: Sun, 11 Oct 2020 04:56:48 GMT
|
|
63
|
+
- request:
|
|
64
|
+
method: put
|
|
65
|
+
uri: http://192.168.56.101:5000/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/confirm
|
|
66
|
+
body:
|
|
67
|
+
encoding: US-ASCII
|
|
68
|
+
string: ''
|
|
69
|
+
headers:
|
|
70
|
+
User-Agent:
|
|
71
|
+
- Faraday v1.0.1
|
|
72
|
+
response:
|
|
73
|
+
status:
|
|
74
|
+
code: 200
|
|
75
|
+
message: OK
|
|
76
|
+
headers:
|
|
77
|
+
content-type:
|
|
78
|
+
- application/json; charset=utf-8
|
|
79
|
+
body:
|
|
80
|
+
encoding: UTF-8
|
|
81
|
+
string: '{"id":"new-image-id-2222-2222-222222222222","project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f","content_type":"image/jpeg","version":"v2","public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/v2","temporary":false,"expires_at":null}'
|
|
42
82
|
recorded_at: Sun, 11 Oct 2020 04:56:49 GMT
|
|
43
83
|
- request:
|
|
44
84
|
method: delete
|