diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index 185da4a25..d832cf40a 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -11,10 +11,12 @@ class SiteCustomization::Image < ApplicationRecord "logo_email" => [400, 80] }.freeze + VALID_MIME_TYPES = %w[image/jpeg image/png].freeze + has_attachment :image validates :name, presence: true, uniqueness: true, inclusion: { in: ->(*) { VALID_IMAGES.keys }} - validates :image, file_content_type: { allow: ["image/png", "image/jpeg"], if: -> { image.attached? }} + validates :image, file_content_type: { allow: ->(*) { VALID_MIME_TYPES }, if: -> { image.attached? }} validate :check_image def self.all_images diff --git a/spec/models/site_customization/image_spec.rb b/spec/models/site_customization/image_spec.rb index 945046144..55534b535 100644 --- a/spec/models/site_customization/image_spec.rb +++ b/spec/models/site_customization/image_spec.rb @@ -44,4 +44,18 @@ describe SiteCustomization::Image do expect(map).not_to be_valid end end + + it "dynamically validates the valid mime types" do + stub_const("#{SiteCustomization::Image}::VALID_MIME_TYPES", ["image/gif"]) + + gif = build(:site_customization_image, + name: "logo_header", + image: fixture_file_upload("logo_header.gif")) + expect(gif).to be_valid + + png = build(:site_customization_image, + name: "logo_header", + image: fixture_file_upload("logo_header.png")) + expect(png).not_to be_valid + end end