Rescue error and add specs (#7292)
This commit is contained in:
parent
b10668a454
commit
83e92cbfd3
6 changed files with 56 additions and 1 deletions
|
|
@ -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?
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue