From ee6dcb80d4c831caf25270135c6204f65bb76132 Mon Sep 17 00:00:00 2001 From: unikounio Date: Fri, 10 Jan 2025 19:09:24 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Entry.determine=5Fmime=5Ftype=E3=81=AE?= =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E3=82=B9=E3=83=9A=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/entry_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index 4c2499a..c7bc55e 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -25,4 +25,45 @@ expect(entry.errors[:image]).to include('対応していない形式のファイルです') end end + + describe '.determine_mime_type' do + context 'when content_type is application/octet-stream' do + it 'returns image/jpeg for .jpg' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'image.jpg') + expect(described_class.determine_mime_type(file)).to eq('image/jpeg') + end + + it 'returns image/png for .png' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'image.png') + expect(described_class.determine_mime_type(file)).to eq('image/png') + end + + it 'returns image/webp for .webp' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'image.webp') + expect(described_class.determine_mime_type(file)).to eq('image/webp') + end + + it 'returns image/heic for .heic' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'image.heic') + expect(described_class.determine_mime_type(file)).to eq('image/heic') + end + + it 'returns image/heif for .heif' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'image.heif') + expect(described_class.determine_mime_type(file)).to eq('image/heif') + end + + it 'returns application/octet-stream for unsupported extension' do + file = Struct.new(:content_type, :original_filename).new('application/octet-stream', 'file.txt') + expect(described_class.determine_mime_type(file)).to eq('application/octet-stream') + end + end + + context 'when content_type is not application/octet-stream' do + it 'returns the original content_type' do + file = Struct.new(:content_type, :original_filename).new('image/jpeg', 'image.jpeg') + expect(described_class.determine_mime_type(file)).to eq('image/jpeg') + end + end + end end From 5e7f78d8052fdd3cfb7a4b5c8254abfeb5b88634 Mon Sep 17 00:00:00 2001 From: unikounio Date: Fri, 10 Jan 2025 19:18:49 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Entry.validate=5Fmime=5Ftype=E3=81=AE?= =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E3=82=B9=E3=83=9A=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/entry_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index c7bc55e..0aa5bda 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -26,6 +26,32 @@ end end + describe '.validate_mime_type' do + let(:allowed_file) { Struct.new(:content_type, :original_filename).new('image/jpeg', 'image.jpg') } + + context 'when all files have allowed MIME types' do + it 'returns nil' do + files = [allowed_file] + expect(described_class.validate_mime_type(files)).to be_nil + end + end + + context 'when some files have disallowed MIME types' do + it 'returns an error message' do + disallowed_file = Struct.new(:content_type, :original_filename).new('text/plain', 'file.txt') + files = [allowed_file, disallowed_file] + expect(described_class.validate_mime_type(files)).to eq('対応していない形式のファイルが含まれています。画面を更新してやり直してください。') + end + end + + context 'when files array is empty' do + it 'returns nil' do + files = [] + expect(described_class.validate_mime_type(files)).to be_nil + end + end + end + describe '.determine_mime_type' do context 'when content_type is application/octet-stream' do it 'returns image/jpeg for .jpg' do