Skip to content

Commit

Permalink
feat: Validate business avatar (joemasilotti#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Daddario authored Dec 22, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6f4e71a commit b214e7a
Showing 7 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/models/concerns/avatarable.rb
Original file line number Diff line number Diff line change
@@ -3,5 +3,8 @@ module Avatarable

included do
has_one_attached :avatar

validates :avatar, content_type: ["image/png", "image/jpeg", "image/jpg"],
max_file_size: 2.megabytes
end
end
2 changes: 0 additions & 2 deletions app/models/developer.rb
Original file line number Diff line number Diff line change
@@ -19,8 +19,6 @@ class Developer < ApplicationRecord
validates :name, presence: true
validates :hero, presence: true
validates :bio, presence: true
validates :avatar, content_type: ["image/png", "image/jpeg", "image/jpg"],
max_file_size: 2.megabytes
validates :cover_image, content_type: ["image/png", "image/jpeg", "image/jpg"],
max_file_size: 10.megabytes
validates :preferred_max_hourly_rate, allow_nil: true, numericality: {greater_than_or_equal_to: :preferred_min_hourly_rate}, if: -> { preferred_min_hourly_rate.present? }
6 changes: 6 additions & 0 deletions test/fixtures/active_storage/attachments.yml
Original file line number Diff line number Diff line change
@@ -9,3 +9,9 @@ two:
record_type: Developer
record_id: <%= ActiveRecord::FixtureSet.identify(:available) %>
blob_id: <%= ActiveRecord::FixtureSet.identify(:two) %>

three:
name: avatar
record_type: Business
record_id: <%= ActiveRecord::FixtureSet.identify(:with_conversation) %>
blob_id: <%= ActiveRecord::FixtureSet.identify(:three) %>
4 changes: 4 additions & 0 deletions test/fixtures/active_storage/blobs.yml
Original file line number Diff line number Diff line change
@@ -5,3 +5,7 @@ one: <%= ActiveStorage::FixtureSet.blob(
two: <%= ActiveStorage::FixtureSet.blob(
filename: "mountains.jpg"
) %>

three: <%= ActiveStorage::FixtureSet.blob(
filename: "basecamp.png"
) %>
Binary file added test/fixtures/files/basecamp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions test/integration/businesses_test.rb
Original file line number Diff line number Diff line change
@@ -34,11 +34,13 @@ class BusinessesTest < ActionDispatch::IntegrationTest
end

test "successful business creation" do
sign_in users(:empty)
user = users(:empty)
sign_in user

assert_difference "Business.count", 1 do
post businesses_path, params: valid_business_params
end
assert_equal "basecamp.png", user.business.avatar.filename.to_s
assert_redirected_to developers_path
end

@@ -123,7 +125,8 @@ def valid_business_params
business: {
name: "Business Owner",
company: "Business, LLC",
bio: "We're in the business for business."
bio: "We're in the business for business.",
avatar: fixture_file_upload("basecamp.png", "image/png")
}
}
end
34 changes: 31 additions & 3 deletions test/models/business_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "test_helper"

class BusinessTest < ActiveSupport::TestCase
setup do
@business = businesses(:with_conversation)
end

test "successful business creation sends a notification to the admin" do
user = users(:empty)

@@ -13,9 +17,33 @@ class BusinessTest < ActiveSupport::TestCase
end

test "conversations relationship doesn't include blocked ones" do
business = businesses(:with_conversation)
assert @business.conversations.include?(conversations(:one))
refute @business.conversations.include?(conversations(:blocked))
end

test "should accept avatars of valid file formats" do
valid_formats = %w[image/png image/jpeg image/jpg]

valid_formats.each do |file_format|
@business.avatar.stub :content_type, file_format do
assert @business.valid?, "#{file_format} should be a valid"
end
end
end

test "should reject avatars of invalid file formats" do
invalid_formats = %w[image/bmp image/gif video/mp4]

invalid_formats.each do |file_format|
@business.avatar.stub :content_type, file_format do
refute @business.valid?, "#{file_format} should be an invalid format"
end
end
end

assert business.conversations.include?(conversations(:one))
refute business.conversations.include?(conversations(:blocked))
test "should enforce a maximum avatar file size" do
@business.avatar.blob.stub :byte_size, 3.megabytes do
refute @business.valid?
end
end
end

0 comments on commit b214e7a

Please sign in to comment.