From c9acceb41402f08188ae2a79d8c41bcaef7b8a50 Mon Sep 17 00:00:00 2001 From: Oliver Valls <199462+tramuntanal@users.noreply.github.com> Date: Wed, 20 Mar 2024 08:14:10 +0100 Subject: [PATCH] Fix document number field duplication during verification (#24) * Add binstubs for rspec and rubocop * Avoid duplication of document number field in verification form * Bump module version --- CHANGELOG.MD | 3 +++ Gemfile.lock | 2 +- app/services/file_authorization_handler.rb | 11 +++++--- bin/rspec | 27 +++++++++++++++++++ bin/rubocop | 27 +++++++++++++++++++ .../file_authorization_handler/version.rb | 2 +- .../file_authorization_handler_spec.rb | 8 ++++++ 7 files changed, 74 insertions(+), 6 deletions(-) create mode 100755 bin/rspec create mode 100755 bin/rubocop diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 0923410..54e923d 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,5 +1,8 @@ # CHANGELOG +## v0.27.1.7 + - FIX: Fix previous version. Accept :document_number as decidim-initiatives sends it, but don't declare it as a argument as it duplicates the verification input for the document. + ## v0.27.1.6 - FIX: Add a hack in `file_authorization_handler` order to sign initiatives because in decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb send to handler_for a param named document_number. diff --git a/Gemfile.lock b/Gemfile.lock index f0b600f..7f3b539 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - decidim-file_authorization_handler (0.27.1.6) + decidim-file_authorization_handler (0.27.1.7) decidim (~> 0.27.1) decidim-admin (~> 0.27.1) rails (>= 5.2) diff --git a/app/services/file_authorization_handler.rb b/app/services/file_authorization_handler.rb index b906dc3..8d60753 100644 --- a/app/services/file_authorization_handler.rb +++ b/app/services/file_authorization_handler.rb @@ -5,9 +5,6 @@ class FileAuthorizationHandler < Decidim::AuthorizationHandler # This is the input (from the user) to validate against attribute :id_document, String - # This is a hack in order to sign initiatives because in decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb - # send to handler_for a param named document_number. - attribute :document_number, String attribute :birthdate, Decidim::Attributes::LocalizedDate # This is the validation to perform @@ -16,6 +13,12 @@ class FileAuthorizationHandler < Decidim::AuthorizationHandler validates :birthdate, presence: true validate :censed + # Customized constructor to support decidim-initiatives. + def initialize(params) + params[:id_document] = params.delete(:document_number) unless params.has_key?(:id_document) + super(params) + end + def metadata @metadata ||= begin meta = { birthdate: census_for_user&.birthdate&.strftime("%Y/%m/%d") } @@ -53,7 +56,7 @@ def census_for_user return unless organization @census_for_user ||= Decidim::FileAuthorizationHandler::CensusDatum - .search_id_document(organization, id_document || document_number) + .search_id_document(organization, id_document) end def organization diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..757e79b --- /dev/null +++ b/bin/rspec @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rspec' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rspec-core", "rspec") diff --git a/bin/rubocop b/bin/rubocop new file mode 100755 index 0000000..2b1fa1f --- /dev/null +++ b/bin/rubocop @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rubocop' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rubocop", "rubocop") diff --git a/lib/decidim/file_authorization_handler/version.rb b/lib/decidim/file_authorization_handler/version.rb index 0ed17be..f374d8a 100644 --- a/lib/decidim/file_authorization_handler/version.rb +++ b/lib/decidim/file_authorization_handler/version.rb @@ -7,6 +7,6 @@ module FileAuthorizationHandler # Uses the latest matching Decidim version for # - major, minor and patch # - the optional extra number is related to this module's patches - VERSION = "#{DECIDIM_VERSION}.6".freeze + VERSION = "#{DECIDIM_VERSION}.7".freeze end end diff --git a/spec/services/decidim/file_authorization_handler/file_authorization_handler_spec.rb b/spec/services/decidim/file_authorization_handler/file_authorization_handler_spec.rb index 5471928..d486257 100644 --- a/spec/services/decidim/file_authorization_handler/file_authorization_handler_spec.rb +++ b/spec/services/decidim/file_authorization_handler/file_authorization_handler_spec.rb @@ -15,6 +15,14 @@ Digest::SHA256.hexdigest("#{handler.census_for_user&.id_document}-#{organization.id}-#{Rails.application.secrets.secret_key_base}") end + it "accepts either id_document or document_number in the constructor" do + expect(handler.id_document).to eq(dni) + + handler = described_class.new(user:, document_number: dni, birthdate: date) + .with_context(current_organization: organization) + expect(handler.id_document).to eq(dni) + end + context "without extras in CensusDatum" do let(:census_datum) do create(:census_datum, id_document: encoded_dni,