Skip to content

Commit 854027c

Browse files
committed
adds file validation for organization logo to avoids crash
1 parent d85df1d commit 854027c

File tree

7 files changed

+27
-3
lines changed

7 files changed

+27
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ gem 'sidekiq', '~> 6.5'
2525
gem 'sidekiq-cron', '~> 1.9.1'
2626
gem 'aws-sdk-s3', '~> 1.94', require: false
2727
gem 'image_processing', '~> 1.12'
28+
gem 'active_storage_validations', '~> 1.1.3'
2829

2930
# Assets
3031
gem 'jquery-rails', '~> 4.4.0'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ GEM
3939
erubi (~> 1.4)
4040
rails-dom-testing (~> 2.0)
4141
rails-html-sanitizer (~> 1.1, >= 1.2.0)
42+
active_storage_validations (1.1.3)
43+
activejob (>= 5.2.0)
44+
activemodel (>= 5.2.0)
45+
activestorage (>= 5.2.0)
46+
activesupport (>= 5.2.0)
4247
activeadmin (2.9.0)
4348
arbre (~> 1.2, >= 1.2.1)
4449
formtastic (>= 3.1, < 5.0)
@@ -447,6 +452,7 @@ PLATFORMS
447452
ruby
448453

449454
DEPENDENCIES
455+
active_storage_validations (~> 1.1.3)
450456
activeadmin (~> 2.9.0)
451457
aws-sdk-s3 (~> 1.94)
452458
bootsnap (~> 1.12.0)

app/helpers/application_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def gravatar_url(user, size = 32)
2727
def organization_logo
2828
org = @organization || @current_organization
2929

30-
return unless org && org.logo.attached?
30+
return unless org && org.logo.attached? && org.errors.details[:logo].blank?
3131
return if "#{controller_name}##{action_name}".in? %w(organizations#index pages#show)
3232

3333
content_tag(:div, class: "row organization-logo") do

app/models/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Organization < ApplicationRecord
2626
has_many :petitions, dependent: :delete_all
2727

2828
validates :name, presence: true, uniqueness: true
29+
validates :logo, content_type: /\Aimage\/.*\z/
2930

3031
before_validation :ensure_url
3132
after_create :create_account

app/views/organizations/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<%= f.input :address %>
1010
<%= f.input :neighborhood %>
1111
<%= f.input :city %>
12-
<%= f.input :logo %>
12+
<%= f.input :logo, input_html: { accept: "image/*" } %>
1313
<%= f.button :submit %>
1414
<% end %>

config/database.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
defaults: &defaults
22
adapter: postgresql
3-
username: <%= ENV['DATABASE_USER'] || ENV["POSTGRES_USER"] %>
3+
username: postgres
44
template: 'template0'
55
encoding: 'UTF8'
6+
host: localhost
7+
port: 5441
68

79
development:
810
<<: *defaults

spec/models/organization_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
RSpec.describe Organization do
22
let(:organization) { Fabricate(:organization) }
33

4+
describe "logo validation" do
5+
it "validates content_type" do
6+
temp_file = Tempfile.new('test.txt')
7+
organization.logo.attach(io: File.open(temp_file.path), filename: 'test.txt')
8+
9+
expect(organization).to be_invalid
10+
11+
temp_file = Tempfile.new('test.png')
12+
organization.logo.attach(io: File.open(temp_file.path), filename: 'test.png')
13+
14+
expect(organization).to be_valid
15+
end
16+
end
17+
418
describe '#display_id' do
519
subject { organization.display_id }
620

0 commit comments

Comments
 (0)