From d508fb69a7a8295c67deeba343ac2ca07263b3dc Mon Sep 17 00:00:00 2001 From: Antti Hukkanen Date: Mon, 11 Mar 2024 15:55:00 +0200 Subject: [PATCH] Fix image attachments through the form --- .../admin/result_form_extensions.rb | 26 +++++++++- .../admin_validates_result_images_spec.rb | 49 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spec/requests/admin_validates_result_images_spec.rb diff --git a/app/forms/concerns/decidim/accountability_simple/admin/result_form_extensions.rb b/app/forms/concerns/decidim/accountability_simple/admin/result_form_extensions.rb index 92eb193..756113a 100644 --- a/app/forms/concerns/decidim/accountability_simple/admin/result_form_extensions.rb +++ b/app/forms/concerns/decidim/accountability_simple/admin/result_form_extensions.rb @@ -29,11 +29,33 @@ module ResultFormExtensions validates :main_image, passthru: { to: Decidim::Accountability::Result, - with: { component: ->(form) { form.current_component } } + with: { + # When the image validations are done through the validation + # endpoint, the component is unknown and would cause the + # validations to fail because the component would not exist. + component: lambda do |form| + Decidim::Component.new( + participatory_space: Decidim::ParticipatoryProcess.new( + organization: form.current_organization + ) + ) + end + } } validates :list_image, passthru: { to: Decidim::Accountability::Result, - with: { component: ->(form) { form.current_component } } + with: { + # When the image validations are done through the validation + # endpoint, the component is unknown and would cause the + # validations to fail because the component would not exist. + component: lambda do |form| + Decidim::Component.new( + participatory_space: Decidim::ParticipatoryProcess.new( + organization: form.current_organization + ) + ) + end + } } validates_locations_for Decidim::Accountability::Result diff --git a/spec/requests/admin_validates_result_images_spec.rb b/spec/requests/admin_validates_result_images_spec.rb new file mode 100644 index 0000000..7f7bd7e --- /dev/null +++ b/spec/requests/admin_validates_result_images_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe "Admin validates result images" do # rubocop:disable RSpec/DescribeClass + include Decidim::ComponentPathHelper + + let(:user) { create(:user, :confirmed, :admin) } + + let(:headers) { { "HOST" => user.organization.host } } + + before do + login_as user, scope: :user + end + + describe "POST create" do + let(:request_path) { Decidim::Core::Engine.routes.url_helpers.upload_validations_path } + + let(:blob) do + ActiveStorage::Blob.create_and_upload!( + io: File.open(Decidim::Dev.asset("city.jpeg")), + filename: "city.jpeg", + content_type: "image/jpeg" + ) + end + + %w(main_image list_image).each do |property| + context "with #{property}" do + let(:params) do + { + resource_class: "Decidim::Accountability::Result", + property: property, + blob: blob.signed_id, + form_class: "Decidim::Accountability::Admin::ResultForm" + } + end + + it "validates the image" do + post(request_path, params: params, headers: headers) + + expect(response).to have_http_status(:ok) + + messages = JSON.parse(response.body) + expect(messages).to be_empty + end + end + end + end +end