Rescue error and add specs (#7292)

This commit is contained in:
Alex 2020-04-15 10:34:00 -04:00 committed by GitHub
parent b10668a454
commit 83e92cbfd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 1 deletions

View file

@ -77,7 +77,12 @@ class ApplicationController < ActionController::Base
end
def log_image_data_to_datadog
images = Array.wrap(params.dig("user", "profile_image") || params["image"])
images = Array.wrap(
params.dig("user", "profile_image") ||
params.dig("podcast", "image") ||
params.dig("organization", "profile_image") ||
params["image"],
)
raise if images.empty?

View file

@ -1,5 +1,6 @@
class OrganizationsController < ApplicationController
after_action :verify_authorized
rescue_from Errno::ENAMETOOLONG, with: :log_image_data_to_datadog
def create
@tab = "organization"

View file

@ -5,6 +5,8 @@ class PodcastsController < ApplicationController
# method "current_user.add_role()" we have no control of
around_action :skip_bullet, only: %i[create], if: -> { defined?(Bullet) }
rescue_from Errno::ENAMETOOLONG, with: :log_image_data_to_datadog
def new
@podcast = Podcast.new
@podcasts = Podcast.available.order("title asc")

View file

@ -42,4 +42,19 @@ RSpec.describe "OrganizationsUpdate", type: :request do
put "/organizations/#{invalid_id}", params: { organization: { id: invalid_id, text_color_hex: "#111111" } }
end.to raise_error(ActiveRecord::RecordNotFound)
end
it "catches error if image file name is too long" do
organization = user.organizations.first
allow(Organization).to receive(:find_by).and_return(organization)
allow(organization).to receive(:update).and_raise(Errno::ENAMETOOLONG)
allow(DatadogStatsClient).to receive(:increment)
expect do
put "/organizations/#{org_id}", params: { organization: { id: org_id, profile_image: fixture_file_upload("files/800x600.png", "image/png") } }
end.to raise_error(Errno::ENAMETOOLONG)
tags = hash_including(tags: instance_of(Array))
expect(DatadogStatsClient).to have_received(:increment).with("image_upload_error", tags)
end
end

View file

@ -74,5 +74,20 @@ RSpec.describe "Podcast Create", type: :request do
post podcasts_path, params: { podcast: valid_attributes }
expect(response.body).to include("Suggest a Podcast")
end
it "catches error if image file name is too long" do
podcast = create(:podcast)
allow(Podcast).to receive(:new).and_return(podcast)
allow(podcast).to receive(:save).and_raise(Errno::ENAMETOOLONG)
allow(DatadogStatsClient).to receive(:increment)
expect do
post podcasts_path, params: { podcast: valid_attributes }
end.to raise_error(Errno::ENAMETOOLONG)
tags = hash_including(tags: instance_of(Array))
expect(DatadogStatsClient).to have_received(:increment).with("image_upload_error", tags)
end
end
end

View file

@ -49,6 +49,23 @@ RSpec.describe "UserOrganization", type: :request do
end
end
it "catches error if image file name is too long" do
sign_in user
org_params = build(:organization).attributes
org_params["profile_image"] = fixture_file_upload("files/800x600.png", "image/png")
allow(Organization).to receive(:new).and_return(organization)
allow(organization).to receive(:save).and_raise(Errno::ENAMETOOLONG)
allow(DatadogStatsClient).to receive(:increment)
expect do
post "/organizations", params: { organization: org_params }
end.to raise_error(Errno::ENAMETOOLONG)
tags = hash_including(tags: instance_of(Array))
expect(DatadogStatsClient).to have_received(:increment).with("image_upload_error", tags)
end
context "when leaving an org" do
let(:org_member) { create(:user, :org_member) }