Skip to content

Commit

Permalink
Fix document number field duplication during verification (CodiTramun…
Browse files Browse the repository at this point in the history
…tana#24)

* Add binstubs for rspec and rubocop

* Avoid duplication of document number field in verification form

* Bump module version
  • Loading branch information
tramuntanal authored Mar 20, 2024
1 parent 0ae3985 commit c9acceb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 7 additions & 4 deletions app/services/file_authorization_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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") }
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 27 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion lib/decidim/file_authorization_handler/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c9acceb

Please sign in to comment.