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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/Dockerfile +73 -0
  3. data/.devcontainer/compose.yaml +61 -0
  4. data/.devcontainer/devcontainer-lock.json +9 -0
  5. data/.devcontainer/devcontainer.json +32 -0
  6. data/.devcontainer/post-create.sh +41 -0
  7. data/.github/copilot-instructions.md +1 -0
  8. data/.github/dependabot.yml +23 -0
  9. data/.github/labeler.yml +28 -0
  10. data/.github/release.yml +35 -0
  11. data/.github/tools/actionlint/Dockerfile +3 -0
  12. data/.github/tools/gitleaks/Dockerfile +2 -0
  13. data/.github/workflows/actionlint.yml +27 -0
  14. data/.github/workflows/actions.lock +54 -0
  15. data/.github/workflows/labeler.yml +22 -0
  16. data/.github/workflows/lint.yml +37 -0
  17. data/.github/workflows/secret-scan.yml +29 -0
  18. data/.github/workflows/test.yml +51 -13
  19. data/.github/workflows/verify-lockfile.yml +28 -0
  20. data/AGENTS.md +1 -0
  21. data/CHANGELOG.md +3 -0
  22. data/CLAUDE.md +68 -0
  23. data/Gemfile +12 -3
  24. data/README.ja.md +241 -0
  25. data/README.md +196 -2
  26. data/Rakefile +5 -7
  27. data/bin/setup +3 -0
  28. data/docker-compose.yml +7 -0
  29. data/git-hooks/pre-push +6 -0
  30. data/git-hooks/pre-push-ruby.sh +53 -0
  31. data/lib/resizing/carrier_wave/callback_file.rb +88 -0
  32. data/lib/resizing/carrier_wave/storage/file.rb +105 -38
  33. data/lib/resizing/carrier_wave/storage/remote.rb +25 -16
  34. data/lib/resizing/carrier_wave.rb +109 -6
  35. data/lib/resizing/client.rb +47 -4
  36. data/lib/resizing/configurable.rb +1 -1
  37. data/lib/resizing/configuration.rb +40 -10
  38. data/lib/resizing/mock_client.rb +48 -9
  39. data/lib/resizing/public_id.rb +3 -2
  40. data/lib/resizing/version.rb +1 -1
  41. data/lib/resizing.rb +4 -1
  42. data/resizing.gemspec +3 -3
  43. data/test/rails_app/app/views/users/new.html.erb +6 -0
  44. data/test/rails_app/rails_app.rb +86 -0
  45. data/test/resizing/carrier_wave/storage/file_test.rb +495 -9
  46. data/test/resizing/carrier_wave/storage/remote_test.rb +151 -12
  47. data/test/resizing/carrier_wave_cache_callbacks_test.rb +224 -0
  48. data/test/resizing/carrier_wave_callback_file_test.rb +77 -0
  49. data/test/resizing/carrier_wave_rails_integration_test.rb +94 -0
  50. data/test/resizing/carrier_wave_temporary_image_test.rb +280 -0
  51. data/test/resizing/carrier_wave_test.rb +214 -30
  52. data/test/resizing/carrier_wave_uploader_test.rb +225 -0
  53. data/test/resizing/client_test.rb +419 -30
  54. data/test/resizing/configuration_test.rb +235 -10
  55. data/test/resizing/http_clientable_test.rb +27 -0
  56. data/test/resizing/mock_client_test.rb +145 -1
  57. data/test/resizing/public_id_test.rb +96 -0
  58. data/test/resizing_module_test.rb +126 -0
  59. data/test/resizing_test.rb +8 -0
  60. data/test/test_helper.rb +318 -9
  61. data/test/vcr/carrier_wave_test/confirm.yml +23 -0
  62. data/test/vcr/carrier_wave_test/retrieve_from_cache.yml +23 -0
  63. data/test/vcr/carrier_wave_test/save.yml +1 -1
  64. data/test/vcr/carrier_wave_test/update_image.yml +42 -2
  65. metadata +47 -9
@@ -1,21 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'test_helper'
4
+ require 'tempfile'
4
5
 
5
6
  module Resizing
6
7
  module CarrierWave
7
8
  module Storage
8
9
  class FileTest < Minitest::Test
10
+ include VCRRequestAssertions
11
+ include FakeApiClientAssertions
12
+ include ResizingTestConfiguration
13
+
9
14
  def setup
10
- @configuration_template = {
11
- image_host: 'http://192.168.56.101:5000',
12
- video_host: 'http://192.168.56.101:5000',
13
- project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
14
- secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
15
- open_timeout: 10,
16
- response_timeout: 20
17
- }
18
- Resizing.configure = @configuration_template
15
+ configure_resizing
19
16
  end
20
17
 
21
18
  def teardown
@@ -153,6 +150,46 @@ module Resizing
153
150
  end
154
151
  end
155
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
+
156
193
  def test_delete_with_valid_public_id
157
194
  VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
158
195
  model = TestModel.new
@@ -165,6 +202,455 @@ module Resizing
165
202
  file.delete
166
203
  end
167
204
  end
205
+
206
+ # ============================================================
207
+ # delete の詳細な挙動 (Resizing::Client を偽クライアントに差し替えて検証)
208
+ # ============================================================
209
+
210
+ IDENTIFIER = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
211
+ IMAGE_ID = '14ea7aac-a194-4330-931f-6b562aec413d'
212
+ OTHER_IDENTIFIER = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/new-image-id-2222-2222-222222222222/v2'
213
+ NOT_FOUND_RESPONSE = {
214
+ 'error' => 'ActiveRecord::RecordNotFound',
215
+ 'message' => "Upload::Image##{IMAGE_ID} not found(no record)"
216
+ }.freeze
217
+
218
+ def model_with_column(value)
219
+ model = TestModel.new
220
+ model.send(:write_attribute, :resizing_picture, value)
221
+ model
222
+ end
223
+
224
+ def test_delete_clears_column_when_deleted_image_is_current
225
+ model = model_with_column(IDENTIFIER)
226
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
227
+
228
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
229
+ file.delete
230
+ assert_equal [IMAGE_ID], fake.deleted_ids
231
+ end
232
+
233
+ assert_nil model.read_attribute(:resizing_picture)
234
+ end
235
+
236
+ def test_delete_keeps_column_when_deleting_previous_image_after_update
237
+ # 画像更新時: カラムは新しい画像を指しているが、削除対象は古い画像なのでカラムは消さない
238
+ model = model_with_column(OTHER_IDENTIFIER)
239
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
240
+
241
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
242
+ file.delete
243
+ assert_equal [IMAGE_ID], fake.deleted_ids
244
+ end
245
+
246
+ assert_equal OTHER_IDENTIFIER, model.read_attribute(:resizing_picture)
247
+ end
248
+
249
+ def test_delete_treats_not_found_as_success_and_clears_column
250
+ model = model_with_column(IDENTIFIER)
251
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
252
+
253
+ with_fake_api_client(NOT_FOUND_RESPONSE) do
254
+ assert_nil file.delete
255
+ end
256
+
257
+ assert_nil model.read_attribute(:resizing_picture)
258
+ end
259
+
260
+ def test_delete_treats_not_found_of_previous_image_without_clearing_column
261
+ model = model_with_column(OTHER_IDENTIFIER)
262
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
263
+
264
+ with_fake_api_client(NOT_FOUND_RESPONSE) { file.delete }
265
+
266
+ assert_equal OTHER_IDENTIFIER, model.read_attribute(:resizing_picture)
267
+ end
268
+
269
+ def test_delete_raises_api_error_when_response_id_does_not_match
270
+ model = model_with_column(IDENTIFIER)
271
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
272
+
273
+ with_fake_api_client({ 'id' => 'someone-else' }) do
274
+ assert_raises(Resizing::APIError) { file.delete }
275
+ end
276
+
277
+ # 失敗時はカラムを変更しない
278
+ assert_equal IDENTIFIER, model.read_attribute(:resizing_picture)
279
+ end
280
+
281
+ def test_delete_uses_column_value_when_constructed_without_identifier
282
+ # remove! の経路では識別子なしで File が生成され、モデルのカラム値から削除対象を決める
283
+ model = model_with_column(IDENTIFIER)
284
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
285
+
286
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
287
+ file.delete
288
+ assert_equal [IMAGE_ID], fake.deleted_ids
289
+ end
290
+
291
+ assert_nil model.read_attribute(:resizing_picture)
292
+ end
293
+
294
+ def test_delete_uses_previous_column_value_when_column_has_unsaved_change
295
+ # attribute_was: カラムが変更済み (保存前) の場合は変更前の値の画像を削除する
296
+ model = model_with_column(IDENTIFIER)
297
+ model.save!
298
+ model.send(:write_attribute, :resizing_picture, OTHER_IDENTIFIER)
299
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
300
+
301
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
302
+ file.delete
303
+ assert_equal [IMAGE_ID], fake.deleted_ids
304
+ end
305
+
306
+ # 削除したのは古い画像なので、新しい値はそのまま残る
307
+ assert_equal OTHER_IDENTIFIER, model.read_attribute(:resizing_picture)
308
+ end
309
+
310
+ def test_delete_does_not_write_column_of_destroyed_model
311
+ model = model_with_column(IDENTIFIER)
312
+ model.save!
313
+
314
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
315
+ ActiveRecord::Base.transaction do
316
+ model.destroy!
317
+ end
318
+ assert model.destroyed?
319
+ assert_equal [IMAGE_ID], fake.deleted_ids
320
+
321
+ # destroy 済み (frozen) のモデルに対して delete しても FrozenError にならない
322
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
323
+ file.delete
324
+ end
325
+ end
326
+
327
+ # ============================================================
328
+ # store の詳細な挙動 (Resizing.post を差し替えて送信内容を検証)
329
+ # ============================================================
330
+
331
+ POST_RESPONSE = {
332
+ 'id' => IMAGE_ID,
333
+ 'public_id' => IDENTIFIER,
334
+ 'content_type' => 'image/jpeg'
335
+ }.freeze
336
+
337
+ def capture_post(response = POST_RESPONSE, &block)
338
+ captured = {}
339
+ responder = lambda do |content, options|
340
+ captured[:content] = content
341
+ captured[:options] = options
342
+ response.dup
343
+ end
344
+ Resizing.stub(:post, responder, &block)
345
+ captured
346
+ end
347
+
348
+ def new_storage_file(model = TestModel.new)
349
+ Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
350
+ end
351
+
352
+ def test_store_posts_uploaded_file_with_its_content_type_and_original_filename
353
+ file = new_storage_file
354
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
355
+ filename: 'my photo.jpg',
356
+ type: 'image/jpeg',
357
+ tempfile: ::File.open('test/data/images/sample1.jpg', 'r')
358
+ )
359
+
360
+ captured = capture_post { file.store(uploaded_file) }
361
+
362
+ assert_equal 'image/jpeg', captured[:options][:content_type]
363
+ assert_equal 'my photo.jpg', captured[:options][:filename]
364
+ assert captured[:content].respond_to?(:read)
365
+ assert_equal 0, captured[:content].pos, 'IO should be rewound before upload'
366
+ end
367
+
368
+ def test_store_guesses_content_type_from_extension_for_plain_file
369
+ file = new_storage_file
370
+ source_file = ::File.open('test/data/images/sample1.jpg', 'r')
371
+ source_file.read(10) # 読み進めた状態でも巻き戻して送る
372
+
373
+ captured = capture_post { file.store(source_file) }
374
+
375
+ assert_equal 'image/jpeg', captured[:options][:content_type]
376
+ assert_equal 'sample1.jpg', captured[:options][:filename]
377
+ assert_equal 0, captured[:content].pos
378
+ end
379
+
380
+ # Issue #88: MIME::Types に登録のない拡張子でも例外にならず、
381
+ # application/octet-stream にフォールバックすること
382
+ def test_store_falls_back_to_octet_stream_for_unknown_extension
383
+ # 前提: この拡張子は MIME::Types に登録されていない
384
+ assert_empty MIME::Types.type_for('bookmark.url')
385
+
386
+ file = new_storage_file
387
+ tempfile = Tempfile.new(['bookmark', '.url'])
388
+ begin
389
+ tempfile.write("[InternetShortcut]\nURL=https://example.com/\n")
390
+ tempfile.rewind
391
+
392
+ captured = capture_post { file.store(tempfile) }
393
+
394
+ assert_equal 'application/octet-stream', captured[:options][:content_type]
395
+ assert_equal ::File.basename(tempfile.path), captured[:options][:filename]
396
+ ensure
397
+ tempfile.close!
398
+ end
399
+ end
400
+
401
+ def test_guess_content_type_falls_back_for_unknown_or_missing_path
402
+ file = new_storage_file
403
+
404
+ assert_equal 'image/jpeg', file.send(:guess_content_type, 'sample1.jpg')
405
+ assert_equal 'application/octet-stream', file.send(:guess_content_type, 'bookmark.url')
406
+ assert_equal 'application/octet-stream', file.send(:guess_content_type, 'no_extension')
407
+ assert_equal 'application/octet-stream', file.send(:guess_content_type, nil)
408
+ end
409
+
410
+ def test_store_sends_basename_only_as_filename
411
+ file = new_storage_file
412
+ uploaded_file = ActionDispatch::Http::UploadedFile.new(
413
+ filename: '/some/dir/photo.jpg',
414
+ type: 'image/jpeg',
415
+ tempfile: ::File.open('test/data/images/sample1.jpg', 'r')
416
+ )
417
+
418
+ captured = capture_post { file.store(uploaded_file) }
419
+
420
+ assert_equal 'photo.jpg', captured[:options][:filename]
421
+ end
422
+
423
+ def test_store_writes_public_id_into_model_column
424
+ model = TestModel.new
425
+ file = new_storage_file(model)
426
+
427
+ capture_post { file.store(::File.open('test/data/images/sample1.jpg', 'r')) }
428
+
429
+ assert_equal IDENTIFIER, model.read_attribute(:resizing_picture)
430
+ assert_equal IDENTIFIER, file.public_id.to_s
431
+ assert_equal IDENTIFIER, file.current_path
432
+ assert_equal IMAGE_ID, file.public_id.image_id
433
+ end
434
+
435
+ def test_store_uses_content_type_from_response
436
+ file = new_storage_file
437
+ response = POST_RESPONSE.merge('content_type' => 'image/webp')
438
+
439
+ capture_post(response) { file.store(::File.open('test/data/images/sample1.jpg', 'r')) }
440
+
441
+ assert_equal 'image/webp', file.content_type
442
+ end
443
+
444
+ def test_store_returns_true
445
+ file = new_storage_file
446
+ result = nil
447
+
448
+ capture_post { result = file.store(::File.open('test/data/images/sample1.jpg', 'r')) }
449
+
450
+ assert_equal true, result
451
+ end
452
+
453
+ def test_store_raises_for_storage_file_instance
454
+ model = TestModel.new
455
+ file = new_storage_file(model)
456
+ other = new_storage_file(model)
457
+
458
+ assert_raises(NotImplementedError) { file.store(other) }
459
+ end
460
+
461
+ # ============================================================
462
+ # current_path / name / public_id
463
+ # ============================================================
464
+
465
+ def test_current_path_falls_back_to_model_column
466
+ model = model_with_column(IDENTIFIER)
467
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
468
+
469
+ assert_equal IDENTIFIER, file.current_path
470
+ assert_equal IDENTIFIER, file.path
471
+ end
472
+
473
+ def test_current_path_prefers_identifier_over_model_column
474
+ model = model_with_column(OTHER_IDENTIFIER)
475
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
476
+
477
+ assert_equal IDENTIFIER, file.current_path
478
+ end
479
+
480
+ def test_name_reads_from_model_column_not_identifier
481
+ model = model_with_column(OTHER_IDENTIFIER)
482
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
483
+
484
+ assert_equal 'new-image-id-2222-2222-222222222222', file.name
485
+ end
486
+
487
+ def test_public_id_exposes_parsed_parts
488
+ model = TestModel.new
489
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
490
+
491
+ assert_equal IMAGE_ID, file.public_id.image_id
492
+ assert_equal 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f', file.public_id.project_id
493
+ assert_equal '_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e', file.public_id.version
494
+ end
495
+
496
+ def test_retrieve_replaces_public_id
497
+ model = TestModel.new
498
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
499
+
500
+ file.retrieve(OTHER_IDENTIFIER)
501
+
502
+ assert_equal OTHER_IDENTIFIER, file.public_id.to_s
503
+ assert_equal OTHER_IDENTIFIER, file.current_path
504
+ end
505
+
506
+ # ============================================================
507
+ # Issue #91: 未定義の `file` を参照していたメソッド群
508
+ # (size / content_type / read / exists? / attributes)
509
+ # ============================================================
510
+
511
+ # test/vcr/client/metadata.yml のカセットに対応する identifier
512
+ METADATA_IDENTIFIER = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo'
513
+ METADATA_SIZE = 848_590
514
+
515
+ def storage_file_for(identifier)
516
+ Resizing::CarrierWave::Storage::File.new(TestModel.new.resizing_picture, identifier)
517
+ end
518
+
519
+ def test_size_content_type_exists_and_attributes_come_from_metadata_api
520
+ file = storage_file_for(METADATA_IDENTIFIER)
521
+
522
+ # メタデータは1回だけ取得され、以降はメモ化される
523
+ assert_vcr_requests_made 'client/metadata' do
524
+ assert_equal METADATA_SIZE, file.size
525
+ assert_equal 'image/jpeg', file.content_type
526
+ assert file.exists?
527
+ assert_equal 'sample1.jpg', file.attributes['filename']
528
+ end
529
+ end
530
+
531
+ def test_metadata_backed_methods_do_not_raise_name_error_without_public_id
532
+ file = new_storage_file
533
+
534
+ # public_id がないのでリクエストは発行されず、既定値を返す
535
+ assert_vcr_no_requests 'client/metadata' do
536
+ assert_equal 0, file.size
537
+ assert_nil file.content_type
538
+ refute file.exists?
539
+ assert_nil file.attributes
540
+ end
541
+ end
542
+
543
+ def test_metadata_backed_methods_do_not_raise_when_api_fails
544
+ file = storage_file_for(IDENTIFIER)
545
+
546
+ Resizing.stub(:metadata, ->(*) { raise Resizing::APIError, 'boom' }) do
547
+ assert_equal 0, file.size
548
+ assert_nil file.content_type
549
+ refute file.exists?
550
+ assert_nil file.attributes
551
+ end
552
+ end
553
+
554
+ def test_metadata_not_found_response_is_treated_as_missing
555
+ file = storage_file_for(IDENTIFIER)
556
+ not_found = { 'error' => 'ActiveRecord::RecordNotFound' }
557
+
558
+ Resizing.stub(:metadata, ->(*) { not_found }) do
559
+ refute file.exists?
560
+ assert_equal 0, file.size
561
+ assert_nil file.content_type
562
+ end
563
+ end
564
+
565
+ def test_content_type_prefers_locally_known_value_over_metadata_api
566
+ file = new_storage_file
567
+
568
+ # store 済みで content_type が分かっている場合はメタデータを取りに行かない
569
+ assert_vcr_no_requests 'client/metadata' do
570
+ capture_post { file.store(::File.open('test/data/images/sample1.jpg', 'r')) }
571
+
572
+ assert_equal 'image/jpeg', file.content_type
573
+ end
574
+ end
575
+
576
+ def test_metadata_falls_back_to_model_column_when_built_without_identifier
577
+ model = model_with_column(METADATA_IDENTIFIER)
578
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
579
+
580
+ assert_vcr_requests_made 'client/metadata' do
581
+ assert_equal METADATA_SIZE, file.size
582
+ end
583
+ end
584
+
585
+ def test_retrieve_drops_cached_metadata_only_when_identifier_changes
586
+ file = storage_file_for(METADATA_IDENTIFIER)
587
+
588
+ assert_vcr_requests_made 'client/metadata' do
589
+ assert_equal METADATA_SIZE, file.size
590
+ # 同じ identifier での retrieve ではメタデータを再取得しない
591
+ file.retrieve(METADATA_IDENTIFIER)
592
+ assert_equal METADATA_SIZE, file.size
593
+ end
594
+
595
+ file.retrieve(IDENTIFIER)
596
+ Resizing.stub(:metadata, ->(*) { { 'size' => 1 } }) do
597
+ assert_equal 1, file.size
598
+ end
599
+ end
600
+
601
+ def test_read_raises_not_implemented_error_instead_of_name_error
602
+ file = storage_file_for(METADATA_IDENTIFIER)
603
+
604
+ error = assert_raises(NotImplementedError) { file.read }
605
+ assert_match(/Resizing/, error.message)
606
+ end
607
+
608
+ # Issue #91 の再現手順: DB から読み直したモデルの size が NameError にならないこと
609
+ # (CarrierWave の Uploader::Proxy#size は file.try(:size) を呼ぶ)
610
+ def test_uploader_proxy_size_and_content_type_for_model_loaded_from_db
611
+ model = model_with_column(METADATA_IDENTIFIER)
612
+ model.save!
613
+ loaded = TestModel.find(model.id)
614
+
615
+ assert_vcr_requests_made 'client/metadata' do
616
+ assert_equal METADATA_SIZE, loaded.resizing_picture.size
617
+ assert_equal 'image/jpeg', loaded.resizing_picture.content_type
618
+ end
619
+ end
620
+
621
+ # Issue #94: カラムに空文字が入っているレコードでも delete で例外にならない
622
+ def test_delete_does_nothing_when_column_is_empty_string
623
+ model = model_with_column('')
624
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture)
625
+
626
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
627
+ assert_nil file.delete
628
+ assert_empty fake.deleted_ids
629
+ end
630
+
631
+ assert_equal '', model.read_attribute(:resizing_picture)
632
+ end
633
+
634
+ def test_delete_does_nothing_when_identifier_is_empty_string
635
+ model = model_with_column('')
636
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, '')
637
+
638
+ with_fake_api_client({ 'id' => IMAGE_ID }) do |fake|
639
+ assert_nil file.delete
640
+ assert_empty fake.deleted_ids
641
+ end
642
+ end
643
+
644
+ def test_delete_with_valid_public_id_clears_model_column
645
+ VCR.use_cassette 'carrier_wave_test/remove_resizing_picture', record: :none do
646
+ model = model_with_column(IDENTIFIER)
647
+ file = Resizing::CarrierWave::Storage::File.new(model.resizing_picture, IDENTIFIER)
648
+
649
+ file.delete
650
+
651
+ assert_nil model.read_attribute(:resizing_picture)
652
+ end
653
+ end
168
654
  end
169
655
  end
170
656
  end