diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1b3fd278..727bc1d5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -70,3 +70,5 @@ jobs: - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 + with: + token: ${{ env.CODECOV_TOKEN }} diff --git a/.rubocop_ruby.yml b/.rubocop_ruby.yml index fe783a56..69d23584 100644 --- a/.rubocop_ruby.yml +++ b/.rubocop_ruby.yml @@ -71,11 +71,6 @@ AllCops: # Otherwise we fallback to the oldest officially supported Ruby version (2.0). TargetRubyVersion: 3.0 - RSpec: - Patterns: - - "(?:^|/)spec/" - - "(?:^|/)test/" - # Indent private/protected/public as deep as method definitions Layout/AccessModifierIndentation: EnforcedStyle: indent diff --git a/Gemfile.lock b/Gemfile.lock index d435e400..cb20d4bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - decidim-action_delegator (0.8.1) + decidim-action_delegator (0.8.2) decidim-admin (>= 0.27.0, < 0.28) decidim-consultations (>= 0.27.0, < 0.28) decidim-core (>= 0.27.0, < 0.28) @@ -82,7 +82,7 @@ GEM gyoku (>= 0.4.0) nokogiri ast (2.4.2) - axe-core-api (4.7.0) + axe-core-api (4.9.1) dumb_delegator virtus axe-core-rspec (4.1.0) @@ -104,22 +104,23 @@ GEM html_tokenizer (~> 0.0.6) parser (>= 2.4) smart_properties + bigdecimal (3.1.8) bindex (0.8.1) bootsnap (1.13.0) msgpack (~> 1.2) browser (2.7.1) builder (3.2.4) byebug (11.1.3) - capybara (3.39.2) + capybara (3.40.0) addressable matrix mini_mime (>= 0.1.3) - nokogiri (~> 1.8) + nokogiri (~> 1.11) rack (>= 1.6.0) rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) - carrierwave (2.2.4) + carrierwave (2.2.6) activemodel (>= 5.0.0) activesupport (>= 5.0.0) addressable (~> 2.6) @@ -139,7 +140,7 @@ GEM actionpack (>= 5.0) cells (>= 4.1.6, < 5.0.0) charlock_holmes (0.7.7) - chef-utils (18.3.0) + chef-utils (18.4.12) concurrent-ruby childprocess (4.1.0) codecov (0.6.0) @@ -155,7 +156,8 @@ GEM coffee-script-source (1.12.2) commonmarker (0.23.10) concurrent-ruby (1.2.2) - crack (0.4.5) + crack (1.0.0) + bigdecimal rexml crass (1.0.6) css_parser (1.16.0) @@ -167,67 +169,67 @@ GEM db-query-matchers (0.10.0) activesupport (>= 4.0, < 7) rspec (~> 3.0) - decidim (0.27.4) - decidim-accountability (= 0.27.4) - decidim-admin (= 0.27.4) - decidim-api (= 0.27.4) - decidim-assemblies (= 0.27.4) - decidim-blogs (= 0.27.4) - decidim-budgets (= 0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-debates (= 0.27.4) - decidim-forms (= 0.27.4) - decidim-generators (= 0.27.4) - decidim-meetings (= 0.27.4) - decidim-pages (= 0.27.4) - decidim-participatory_processes (= 0.27.4) - decidim-proposals (= 0.27.4) - decidim-sortitions (= 0.27.4) - decidim-surveys (= 0.27.4) - decidim-system (= 0.27.4) - decidim-templates (= 0.27.4) - decidim-verifications (= 0.27.4) - decidim-accountability (0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-admin (0.27.4) + decidim (0.27.6) + decidim-accountability (= 0.27.6) + decidim-admin (= 0.27.6) + decidim-api (= 0.27.6) + decidim-assemblies (= 0.27.6) + decidim-blogs (= 0.27.6) + decidim-budgets (= 0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-debates (= 0.27.6) + decidim-forms (= 0.27.6) + decidim-generators (= 0.27.6) + decidim-meetings (= 0.27.6) + decidim-pages (= 0.27.6) + decidim-participatory_processes (= 0.27.6) + decidim-proposals (= 0.27.6) + decidim-sortitions (= 0.27.6) + decidim-surveys (= 0.27.6) + decidim-system (= 0.27.6) + decidim-templates (= 0.27.6) + decidim-verifications (= 0.27.6) + decidim-accountability (0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-admin (0.27.6) active_link_to (~> 1.0) - decidim-core (= 0.27.4) + decidim-core (= 0.27.6) devise (~> 4.7) devise-i18n (~> 1.2) - devise_invitable (~> 2.0) - decidim-api (0.27.4) + devise_invitable (~> 2.0, >= 2.0.9) + decidim-api (0.27.6) + decidim-core (= 0.27.6) graphql (~> 1.12, < 1.13) graphql-docs (~> 2.1.0) rack-cors (~> 1.0) - decidim-assemblies (0.27.4) - decidim-core (= 0.27.4) - decidim-blogs (0.27.4) - decidim-admin (= 0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-budgets (0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-comments (0.27.4) - decidim-core (= 0.27.4) + decidim-assemblies (0.27.6) + decidim-core (= 0.27.6) + decidim-blogs (0.27.6) + decidim-admin (= 0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-budgets (0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-comments (0.27.6) + decidim-core (= 0.27.6) redcarpet (~> 3.5, >= 3.5.1) - decidim-consultations (0.27.4) - decidim-admin (= 0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-core (0.27.4) + decidim-consultations (0.27.6) + decidim-admin (= 0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-core (0.27.6) active_link_to (~> 1.0) acts_as_list (~> 0.9) batch-loader (~> 1.2) browser (~> 2.7) - carrierwave (~> 2.2.1) + carrierwave (~> 2.2.5, >= 2.2.5) cells-erb (~> 0.1.0) cells-rails (~> 0.1.3) charlock_holmes (~> 0.7) date_validator (~> 0.12.0) - decidim-api (= 0.27.4) devise (~> 4.7) devise-i18n (~> 1.2) diffy (~> 3.3) @@ -243,7 +245,6 @@ GEM loofah (~> 2.19.0) mime-types (>= 1.16, < 4.0) mini_magick (~> 4.9) - mustache (~> 1.1.0) omniauth (~> 2.0) omniauth-facebook (~> 5.0) omniauth-google-oauth2 (~> 1.0) @@ -268,15 +269,15 @@ GEM webpacker (= 6.0.0.rc.5) webpush (~> 1.1) wisper (~> 2.0) - decidim-debates (0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-dev (0.27.4) + decidim-debates (0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-dev (0.27.6) axe-core-rspec (~> 4.1.0) byebug (~> 11.0) capybara (~> 3.24) db-query-matchers (~> 0.10.0) - decidim (= 0.27.4) + decidim (= 0.27.6) erb_lint (~> 0.0.35) factory_bot_rails (~> 4.8) i18n-tasks (~> 0.9.18) @@ -299,45 +300,44 @@ GEM w3c_rspec_validators (~> 0.3.0) webmock (~> 3.6) wisper-rspec (~> 1.0) - decidim-forms (0.27.4) - decidim-core (= 0.27.4) + decidim-forms (0.27.6) + decidim-core (= 0.27.6) wicked_pdf (~> 2.1) wkhtmltopdf-binary (~> 0.12) - decidim-generators (0.27.4) - decidim-core (= 0.27.4) - decidim-meetings (0.27.4) - decidim-core (= 0.27.4) - decidim-forms (= 0.27.4) + decidim-generators (0.27.6) + decidim-core (= 0.27.6) + decidim-meetings (0.27.6) + decidim-core (= 0.27.6) + decidim-forms (= 0.27.6) icalendar (~> 2.5) - decidim-pages (0.27.4) - decidim-core (= 0.27.4) - decidim-participatory_processes (0.27.4) - decidim-core (= 0.27.4) - decidim-proposals (0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) + decidim-pages (0.27.6) + decidim-core (= 0.27.6) + decidim-participatory_processes (0.27.6) + decidim-core (= 0.27.6) + decidim-proposals (0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) doc2text (~> 0.4.5) redcarpet (~> 3.5, >= 3.5.1) - decidim-sortitions (0.27.4) - decidim-admin (= 0.27.4) - decidim-comments (= 0.27.4) - decidim-core (= 0.27.4) - decidim-proposals (= 0.27.4) - decidim-surveys (0.27.4) - decidim-core (= 0.27.4) - decidim-forms (= 0.27.4) - decidim-templates (= 0.27.4) - decidim-system (0.27.4) + decidim-sortitions (0.27.6) + decidim-admin (= 0.27.6) + decidim-comments (= 0.27.6) + decidim-core (= 0.27.6) + decidim-proposals (= 0.27.6) + decidim-surveys (0.27.6) + decidim-core (= 0.27.6) + decidim-forms (= 0.27.6) + decidim-system (0.27.6) active_link_to (~> 1.0) - decidim-core (= 0.27.4) + decidim-core (= 0.27.6) devise (~> 4.7) devise-i18n (~> 1.2) - devise_invitable (~> 2.0) - decidim-templates (0.27.4) - decidim-core (= 0.27.4) - decidim-forms (= 0.27.4) - decidim-verifications (0.27.4) - decidim-core (= 0.27.4) + devise_invitable (~> 2.0, >= 2.0.9) + decidim-templates (0.27.6) + decidim-core (= 0.27.6) + decidim-forms (= 0.27.6) + decidim-verifications (0.27.6) + decidim-core (= 0.27.6) declarative-builder (0.1.0) declarative-option (< 0.2.0) declarative-option (0.1.0) @@ -357,13 +357,13 @@ GEM warden (~> 1.2.3) devise-i18n (1.11.0) devise (>= 4.9.0) - devise_invitable (2.0.8) + devise_invitable (2.0.9) actionmailer (>= 5.0) devise (>= 4.6) - diff-lcs (1.5.0) + diff-lcs (1.5.1) diffy (3.4.2) - doc2text (0.4.6) - nokogiri (>= 1.13.2, < 1.15.0) + doc2text (0.4.7) + nokogiri (>= 1.13.2, < 1.17.0) rubyzip (~> 2.3.0) docile (1.4.0) doorkeeper (5.6.6) @@ -433,12 +433,12 @@ GEM rexml (~> 3.0) hashdiff (1.0.1) hashie (5.0.0) - highline (2.1.0) + highline (3.0.1) hkdf (0.3.0) html-pipeline (2.14.3) activesupport (>= 2) nokogiri (>= 1.4) - html_tokenizer (0.0.7) + html_tokenizer (0.0.8) htmlentities (4.3.4) httpi (3.0.1) rack @@ -454,7 +454,7 @@ GEM rails-i18n rainbow (>= 2.2.2, < 4.0) terminal-table (>= 1.5.1) - icalendar (2.9.0) + icalendar (2.10.1) ice_cube (~> 0.16) ice_cube (0.16.4) ice_nine (0.11.2) @@ -463,7 +463,7 @@ GEM ruby-vips (>= 2.0.17, < 3) invisible_captcha (0.13.0) rails (>= 3.2.0) - json (2.6.3) + json (2.7.2) jwt (2.5.0) kaminari (1.2.2) activesupport (>= 4.1.0) @@ -503,7 +503,7 @@ GEM net-smtp marcel (1.0.2) matrix (0.4.2) - mdl (0.12.0) + mdl (0.13.0) kramdown (~> 2.3) kramdown-parser-gfm (~> 1.1) mixlib-cli (~> 2.1, >= 2.1.1) @@ -523,7 +523,6 @@ GEM chef-utils msgpack (1.6.0) multi_xml (0.6.0) - mustache (1.1.1) net-imap (0.3.7) date net-protocol @@ -600,7 +599,7 @@ GEM net-smtp premailer (~> 1.7, >= 1.7.9) public_suffix (5.0.3) - puma (5.6.7) + puma (5.6.8) nio4r (~> 2.0) racc (1.7.1) rack (2.2.8) @@ -666,24 +665,24 @@ GEM actionpack (>= 5.2) railties (>= 5.2) rexml (3.2.5) - rspec (3.12.0) - rspec-core (~> 3.12.0) - rspec-expectations (~> 3.12.0) - rspec-mocks (~> 3.12.0) - rspec-cells (0.3.8) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-cells (0.3.9) cells (>= 4.0.0, < 6.0.0) - rspec-rails (>= 3.0.0, < 6.1.0) - rspec-core (3.12.2) - rspec-support (~> 3.12.0) - rspec-expectations (3.12.3) + rspec-rails (>= 3.0.0, < 6.2.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.12.0) + rspec-support (~> 3.13.0) rspec-html-matchers (0.9.4) nokogiri (~> 1) rspec (>= 3.0.0.a, < 4) - rspec-mocks (3.12.6) + rspec-mocks (3.13.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.12.0) + rspec-support (~> 3.13.0) rspec-rails (4.1.2) actionpack (>= 4.2) activesupport (>= 4.2) @@ -694,7 +693,7 @@ GEM rspec-support (~> 3.10) rspec-retry (0.6.2) rspec-core (> 3.3) - rspec-support (3.12.1) + rspec-support (3.13.1) rspec_junit_formatter (0.3.0) rspec-core (>= 2, < 4, != 2.12.0) rubocop (1.28.2) @@ -817,7 +816,7 @@ GEM activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) - webmock (3.19.1) + webmock (3.23.1) addressable (>= 2.8.0) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) @@ -832,11 +831,11 @@ GEM websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - wicked_pdf (2.7.0) + wicked_pdf (2.8.0) activesupport wisper (2.0.1) wisper-rspec (1.1.0) - wkhtmltopdf-binary (0.12.6.6) + wkhtmltopdf-binary (0.12.6.7) xpath (3.2.0) nokogiri (~> 1.8) zeitwerk (2.6.11) @@ -851,10 +850,10 @@ DEPENDENCIES bootsnap (~> 1.4) byebug (~> 11.0) codecov - decidim (= 0.27.4) + decidim (= 0.27.6) decidim-action_delegator! - decidim-consultations (= 0.27.4) - decidim-dev (= 0.27.4) + decidim-consultations (= 0.27.6) + decidim-dev (= 0.27.6) faker (~> 2.14) letter_opener_web (~> 2.0) listen (~> 3.1) diff --git a/app/controllers/decidim/action_delegator/admin/delegations_controller.rb b/app/controllers/decidim/action_delegator/admin/delegations_controller.rb index 04cd4184..e2902b83 100644 --- a/app/controllers/decidim/action_delegator/admin/delegations_controller.rb +++ b/app/controllers/decidim/action_delegator/admin/delegations_controller.rb @@ -25,7 +25,7 @@ def new def create enforce_permission_to :create, :delegation - @form = DelegationForm.from_params(params) + @form = form(DelegationForm).from_params(params) CreateDelegation.call(@form, current_user, current_setting) do on(:ok) do diff --git a/app/controllers/decidim/action_delegator/admin/manage_delegations_controller.rb b/app/controllers/decidim/action_delegator/admin/manage_delegations_controller.rb index 25f035f8..8a53614c 100644 --- a/app/controllers/decidim/action_delegator/admin/manage_delegations_controller.rb +++ b/app/controllers/decidim/action_delegator/admin/manage_delegations_controller.rb @@ -22,15 +22,14 @@ def create enforce_permission_to :create, :delegation @csv_file = params[:csv_file] - redirect_to seting_manage_delegations_path && return if @csv_file.blank? + redirect_to(new_setting_manage_delegation_path) && return if @csv_file.blank? - importer_type = "DelegationsCsvImporter" csv_file = @csv_file.read.force_encoding("utf-8").encode("utf-8") - @import_summary = Decidim::ActionDelegator::Admin::ImportCsvJob.perform_now(importer_type, csv_file, current_user, current_setting) + @import_summary = Decidim::ActionDelegator::Admin::ImportCsvJob.perform_now("DelegationsCsvImporter", csv_file, current_user, current_setting) flash[:notice] = t(".success") - redirect_to decidim_admin_action_delegator.setting_delegations_path(current_setting) + redirect_to setting_delegations_path(current_setting) end private diff --git a/app/controllers/decidim/action_delegator/admin/manage_participants_controller.rb b/app/controllers/decidim/action_delegator/admin/manage_participants_controller.rb index 53858969..5f33f5fb 100644 --- a/app/controllers/decidim/action_delegator/admin/manage_participants_controller.rb +++ b/app/controllers/decidim/action_delegator/admin/manage_participants_controller.rb @@ -24,9 +24,8 @@ def create @csv_file = params[:csv_file] redirect_to seting_manage_participants_path && return if @csv_file.blank? - importer_type = "ParticipantsCsvImporter" csv_file = @csv_file.read.force_encoding("utf-8").encode("utf-8") - @import_summary = Decidim::ActionDelegator::Admin::ImportCsvJob.perform_later(importer_type, csv_file, current_user, current_setting) + @import_summary = Decidim::ActionDelegator::Admin::ImportCsvJob.perform_later("ParticipantsCsvImporter", csv_file, current_user, current_setting) flash[:notice] = t(".success") diff --git a/app/forms/decidim/action_delegator/admin/delegation_form.rb b/app/forms/decidim/action_delegator/admin/delegation_form.rb index 281a61bf..00488c01 100644 --- a/app/forms/decidim/action_delegator/admin/delegation_form.rb +++ b/app/forms/decidim/action_delegator/admin/delegation_form.rb @@ -18,15 +18,19 @@ class DelegationForm < Form validate :grantee_exists def granter - User.find_by(id: granter_id) || User.find_by(email: granter_email) + User.find_by(id: granter_id, organization: current_organization) || User.find_by(email: granter_email, organization: current_organization) end def grantee - User.find_by(id: grantee_id) || User.find_by(email: grantee_email) + User.find_by(id: grantee_id, organization: current_organization) || User.find_by(email: grantee_email, organization: current_organization) end private + def current_organization + context&.current_organization + end + def granter_exists return if granter.present? diff --git a/app/models/decidim/action_delegator/delegation.rb b/app/models/decidim/action_delegator/delegation.rb index a195a9a7..fddb98db 100644 --- a/app/models/decidim/action_delegator/delegation.rb +++ b/app/models/decidim/action_delegator/delegation.rb @@ -16,6 +16,10 @@ class Delegation < ApplicationRecord message: I18n.t("delegations.create.error_granter_unique", scope: "decidim.action_delegator.admin") } + validate :grantee_is_not_granter + validate :granter_and_grantee_belongs_to_same_organization + validate :granter_is_same_organization_as_consultation + delegate :consultation, to: :setting before_destroy { |record| throw(:abort) if record.grantee_voted? } @@ -32,6 +36,27 @@ def grantee_voted? granter_votes&.detect { |vote| vote.versions.exists?(whodunnit: grantee&.id) } ? true : false end end + + private + + def grantee_is_not_granter + return unless granter == grantee + + errors.add(:grantee, :invalid) + end + + def granter_and_grantee_belongs_to_same_organization + return unless granter.organization != grantee.organization + + errors.add(:grantee, :invalid) + end + + def granter_is_same_organization_as_consultation + return unless setting && setting.consultation + return unless consultation.organization != granter.organization + + errors.add(:granter, :invalid) + end end end end diff --git a/app/models/decidim/action_delegator/participant.rb b/app/models/decidim/action_delegator/participant.rb index 47af7909..8caf623c 100644 --- a/app/models/decidim/action_delegator/participant.rb +++ b/app/models/decidim/action_delegator/participant.rb @@ -25,6 +25,8 @@ class Participant < ApplicationRecord validates :email, uniqueness: { scope: :setting }, if: -> { email.present? } validates :phone, uniqueness: { scope: :setting }, if: -> { phone.present? } + validate :user_belongs_to_organization + # sets the decidim user if found before_save :set_decidim_user @@ -36,7 +38,7 @@ def user end def user_from_metadata - @user_from_metadata ||= if setting.email_required? + @user_from_metadata ||= if setting&.email_required? Decidim::User.find_by(email: email, organization: setting.organization) else Decidim::Authorization.find_by(unique_id: uniq_ids)&.user @@ -96,6 +98,13 @@ def voted? def set_decidim_user self.decidim_user = user_from_metadata if decidim_user.blank? end + + def user_belongs_to_organization + return unless decidim_user && setting && setting.consultation + return if decidim_user.organization == organization + + errors.add(:decidim_user, :invalid) + end end end end diff --git a/app/services/decidim/action_delegator/delegations_csv_importer.rb b/app/services/decidim/action_delegator/delegations_csv_importer.rb index 70c7f9ee..2bbd923b 100644 --- a/app/services/decidim/action_delegator/delegations_csv_importer.rb +++ b/app/services/decidim/action_delegator/delegations_csv_importer.rb @@ -23,7 +23,7 @@ def extract_params(row) grantee_email: row["to"].to_s.strip.downcase } - @form = form(Decidim::ActionDelegator::Admin::DelegationForm).from_params(params, setting: @current_setting) + @form = form(Decidim::ActionDelegator::Admin::DelegationForm).from_params(params, setting: @current_setting, current_organization: @current_setting.organization) { granter_id: @form.granter&.id, @@ -31,8 +31,8 @@ def extract_params(row) } end - def delegation_exists?(params) - @delegation = Delegation.find_by(granter_id: params[:granter_id], grantee_id: params[:grantee_id]) + def delegation_exists?(_params) + @delegation = Delegation.find_by(granter: @form.granter, grantee: @form.grantee) @delegation.present? end diff --git a/details.csv b/details.csv index 862c818c..6e30a2e2 100644 --- a/details.csv +++ b/details.csv @@ -1 +1 @@ -,,,Email can't be blank, Phone can't be blank +granter@example.org,grantee@example.org,Granter email The granter (from) is not registered into the platform diff --git a/lib/decidim/action_delegator/version.rb b/lib/decidim/action_delegator/version.rb index 67eff501..deab1ed0 100644 --- a/lib/decidim/action_delegator/version.rb +++ b/lib/decidim/action_delegator/version.rb @@ -3,8 +3,8 @@ module Decidim # This holds the decidim-action_delegator version. module ActionDelegator - VERSION = "0.8.1" - DECIDIM_VERSION = "0.27.4" + VERSION = "0.8.2" + DECIDIM_VERSION = "0.27.6" COMPAT_DECIDIM_VERSION = [">= 0.27.0", "< 0.28"].freeze end end diff --git a/spec/commands/decidim/action_delegator/admin/create_delegation_spec.rb b/spec/commands/decidim/action_delegator/admin/create_delegation_spec.rb index fcd73554..f1c6c572 100644 --- a/spec/commands/decidim/action_delegator/admin/create_delegation_spec.rb +++ b/spec/commands/decidim/action_delegator/admin/create_delegation_spec.rb @@ -6,7 +6,8 @@ subject { described_class.new(form, current_user, current_setting) } let(:current_user) { create(:user, organization: organization) } - let(:current_setting) { create(:setting, max_grants: 1) } + let(:consultation) { create(:consultation, organization: organization) } + let(:current_setting) { create(:setting, max_grants: 1, consultation: consultation) } let(:organization) { create(:organization) } let(:granter) { create(:user, organization: organization) } let(:grantee) { create(:user, organization: organization) } @@ -69,4 +70,12 @@ expect { subject.call }.to broadcast(:error) end end + + context "when granter is not in the same organization" do + let(:granter) { create(:user) } + + it "broadcasts :error" do + expect { subject.call }.to broadcast(:error) + end + end end diff --git a/spec/controllers/decidim/action_delegator/admin/delegations_controller_spec.rb b/spec/controllers/decidim/action_delegator/admin/delegations_controller_spec.rb index 96ca267c..bc732b1b 100644 --- a/spec/controllers/decidim/action_delegator/admin/delegations_controller_spec.rb +++ b/spec/controllers/decidim/action_delegator/admin/delegations_controller_spec.rb @@ -78,7 +78,23 @@ module ActionDelegator context "when the setting belongs to another organization" do let(:setting) { create(:setting) } - it "does not destroy the delegation" do + it "does not create the delegation" do + expect { post :create, params: params }.not_to change(Delegation, :count) + end + end + + context "when the granter belong to another organization" do + let(:granter) { create(:user) } + + it "does not create the delegation" do + expect { post :create, params: params }.not_to change(Delegation, :count) + end + end + + context "when the grantee belong to another organization" do + let(:grantee) { create(:user) } + + it "does not create the delegation" do expect { post :create, params: params }.not_to change(Delegation, :count) end end diff --git a/spec/controllers/decidim/action_delegator/admin/manage_delegations_controller_spec.rb b/spec/controllers/decidim/action_delegator/admin/manage_delegations_controller_spec.rb index 06a85d25..ad202bd5 100644 --- a/spec/controllers/decidim/action_delegator/admin/manage_delegations_controller_spec.rb +++ b/spec/controllers/decidim/action_delegator/admin/manage_delegations_controller_spec.rb @@ -5,17 +5,16 @@ module Decidim module ActionDelegator module Admin - describe ManageDelegationsController do + describe ManageDelegationsController, type: :controller do routes { Decidim::ActionDelegator::AdminEngine.routes } let(:organization) { create(:organization) } - let(:current_user) { create(:user, :confirmed, :admin, organization: organization) } + let(:user) { create(:user, :confirmed, :admin, organization: organization) } let(:consultation) { create(:consultation, organization: organization) } before do request.env["decidim.current_organization"] = organization - request.env["decidim.current_setting"] = setting - sign_in current_user + sign_in user end describe "GET #new" do @@ -33,6 +32,47 @@ module Admin expect(assigns(:errors)).to eq [] end end + + describe "POST #create" do + let(:setting) { create(:setting, consultation: consultation) } + let(:csv_file) { fixture_file_upload("spec/fixtures/valid_delegations.csv", "text/csv") } + let!(:granter) { create(:user, :confirmed, email: "granter@example.org", organization: organization) } + let!(:grantee) { create(:user, :confirmed, email: "grantee@example.org", organization: organization) } + + before do + post :create, params: { setting_id: setting.id, csv_file: csv_file } + end + + it "creates the delegatiosn" do + expect(flash[:notice]).to eq I18n.t("decidim.action_delegator.admin.manage_delegations.create.success") + expect(response).to redirect_to(setting_delegations_path(setting)) + perform_enqueued_jobs do + expect(Decidim::ActionDelegator::Delegation.count).to eq(1) + expect(Decidim::ActionDelegator::Delegation.first.granter).to eq(granter) + expect(Decidim::ActionDelegator::Delegation.first.grantee).to eq(grantee) + end + end + + context "when granter is in another organization" do + let(:granter) { create(:user, :confirmed, email: "granter@example.org") } + + it "does not create the delegation" do + expect(flash[:notice]).to eq I18n.t("decidim.action_delegator.admin.manage_delegations.create.success") + expect(response).to redirect_to(setting_delegations_path(setting)) + perform_enqueued_jobs do + expect(Decidim::ActionDelegator::Delegation.count).to eq(0) + end + end + end + + context "with invalid params" do + let(:csv_file) { nil } + + it "redirects to the setting manage delegations path" do + expect(response).to redirect_to(new_setting_manage_delegation_path) + end + end + end end end end diff --git a/spec/forms/decidim/action_delegator/admin/delegation_form_spec.rb b/spec/forms/decidim/action_delegator/admin/delegation_form_spec.rb index 0a18a52e..d2b92f0a 100644 --- a/spec/forms/decidim/action_delegator/admin/delegation_form_spec.rb +++ b/spec/forms/decidim/action_delegator/admin/delegation_form_spec.rb @@ -3,7 +3,7 @@ require "spec_helper" describe Decidim::ActionDelegator::Admin::DelegationForm do - subject { described_class.from_params(attributes) } + subject { described_class.from_params(attributes).with_context(current_organization: organization) } let(:organization) { create(:organization) } let(:granter) { create(:user, organization: organization) } @@ -23,6 +23,18 @@ context "when there's granter and grantee" do it { is_expected.to be_valid } + + context "when granter belongs to another organization" do + let(:granter) { create(:user) } + + it { is_expected.not_to be_valid } + end + + context "when grantee belongs to another organization" do + let(:grantee) { create(:user) } + + it { is_expected.not_to be_valid } + end end context "when granter is missing" do diff --git a/spec/mailers/decidim/action_delegator/import_mailer_spec.rb b/spec/mailers/decidim/action_delegator/import_mailer_spec.rb index 35d4d342..075514df 100644 --- a/spec/mailers/decidim/action_delegator/import_mailer_spec.rb +++ b/spec/mailers/decidim/action_delegator/import_mailer_spec.rb @@ -8,7 +8,7 @@ module ActionDelegator let(:organization) { create :organization } let(:current_user) { create :user, organization: organization } let(:current_setting) { create(:setting, consultation: consultation) } - let(:consultation) { create(:consultation) } + let(:consultation) { create(:consultation, organization: organization) } describe "#import participants" do let(:valid_csv_file) { File.open("spec/fixtures/valid_participants.csv") } @@ -77,8 +77,8 @@ module ActionDelegator let(:invalid_csv_file) { File.open("spec/fixtures/invalid_delegations.csv") } let!(:granter_email) { "granter@example.org" } let!(:grantee_email) { "grantee@example.org" } - let!(:granter) { create(:user, email: granter_email) } - let!(:grantee) { create(:user, email: grantee_email) } + let!(:granter) { create(:user, email: granter_email, organization: organization) } + let!(:grantee) { create(:user, email: grantee_email, organization: organization) } let(:params) do { diff --git a/spec/models/decidim/action_delegator/delegation_spec.rb b/spec/models/decidim/action_delegator/delegation_spec.rb index f22e1c8f..3aebb8ba 100644 --- a/spec/models/decidim/action_delegator/delegation_spec.rb +++ b/spec/models/decidim/action_delegator/delegation_spec.rb @@ -11,6 +11,34 @@ module ActionDelegator it { is_expected.to be_valid } it { is_expected.not_to be_grantee_voted } + context "when grantee is the same as the granter" do + let(:setting) { create(:setting) } + let(:grantee) { create(:user, organization: setting.organization) } + + subject { build(:delegation, setting: setting, grantee: grantee, granter: grantee) } + + it { is_expected.not_to be_valid } + end + + context "when users from different organizations" do + let(:grantee) { create(:user) } + + subject { build(:delegation, grantee: grantee) } + + it { is_expected.not_to be_valid } + end + + context "when users are from a different organization than the consultation" do + let(:consultation) { create(:consultation) } + let(:setting) { create(:setting, consultation: consultation) } + let(:grantee) { create(:user) } + let(:granter) { create(:user, organization: grantee.organization) } + + subject { build(:delegation, grantee: grantee, granter: granter, setting: setting) } + + it { is_expected.not_to be_valid } + end + describe ".granted_to?" do subject { delegation } diff --git a/spec/models/decidim/action_delegator/participant_spec.rb b/spec/models/decidim/action_delegator/participant_spec.rb index 29d1ef97..35467806 100644 --- a/spec/models/decidim/action_delegator/participant_spec.rb +++ b/spec/models/decidim/action_delegator/participant_spec.rb @@ -19,6 +19,12 @@ module ActionDelegator it { is_expected.to be_valid } it { is_expected.to belong_to(:setting) } + context "when user belongs to a different organization" do + let(:decidim_user) { create(:user) } + + it { is_expected.not_to be_valid } + end + it "belong_to a ponderation" do expect(subject.ponderation).to eq(ponderation) end diff --git a/spec/queries/decidim/action_delegator/consultation_delegations_spec.rb b/spec/queries/decidim/action_delegator/consultation_delegations_spec.rb index bb7d4653..8ae9839b 100644 --- a/spec/queries/decidim/action_delegator/consultation_delegations_spec.rb +++ b/spec/queries/decidim/action_delegator/consultation_delegations_spec.rb @@ -6,16 +6,18 @@ subject { described_class.new(consultation) } let(:consultation) { create(:consultation) } - let(:user) { create(:user) } + let(:other_consultation) { create(:consultation, organization: consultation.organization) } + let(:user) { create(:user, organization: consultation.organization) } let(:setting) { create(:setting, consultation: consultation) } + let(:other_setting) { create(:setting, consultation: other_consultation) } - let!(:consultation_delegation) { create(:delegation, setting: setting, grantee: user) } - let!(:organization_delegation) { create(:delegation, grantee: user) } + let!(:delegation) { create(:delegation, setting: setting, grantee: user) } + let!(:other_delegation) { create(:delegation, setting: other_setting, grantee: user) } let!(:other_user_delegation) { create(:delegation, setting: setting) } describe "#query" do it "returns delegations of the specified consultation only" do - expect(subject.query).to match_array([consultation_delegation, other_user_delegation]) + expect(subject.query).to match_array([delegation, other_user_delegation]) end end end diff --git a/spec/queries/decidim/action_delegator/delegation_votes_spec.rb b/spec/queries/decidim/action_delegator/delegation_votes_spec.rb index 510cb4af..29fb9ce8 100644 --- a/spec/queries/decidim/action_delegator/delegation_votes_spec.rb +++ b/spec/queries/decidim/action_delegator/delegation_votes_spec.rb @@ -5,17 +5,17 @@ module Decidim::ActionDelegator describe DelegationVotes do let(:organization) { create(:organization) } - let(:other_organization) { create(:organization) } - let(:consultation) { create(:consultation, organization: organization) } - + let(:other_consultation) { create(:consultation, organization: organization) } let(:question) { create(:question, consultation: consultation) } let(:other_question) { create(:question, consultation: consultation) } + let(:setting) { create(:setting, consultation: consultation) } + let(:other_setting) { create(:setting, consultation: other_consultation) } let(:granter) { create(:user, organization: organization) } - let!(:delegation) { create(:delegation, granter: granter) } - let!(:other_delegation) { create(:delegation, granter: granter) } + let!(:delegation) { create(:delegation, setting: setting, granter: granter) } + let!(:other_delegation) { create(:delegation, setting: other_setting, granter: granter) } before do create(:vote, author: granter, question: question) diff --git a/spec/queries/decidim/action_delegator/setting_delegations_spec.rb b/spec/queries/decidim/action_delegator/setting_delegations_spec.rb index cb7a6148..7a1e6486 100644 --- a/spec/queries/decidim/action_delegator/setting_delegations_spec.rb +++ b/spec/queries/decidim/action_delegator/setting_delegations_spec.rb @@ -7,6 +7,7 @@ let(:organization) { create(:organization) } let(:consultation) { create(:consultation, organization: organization) } + let(:other_consultation) { create(:consultation, organization: organization) } let(:granter) { create(:user, organization: organization) } let(:grantee) { create(:user, organization: organization) } let(:other_user) { create(:user, organization: organization) } @@ -14,7 +15,7 @@ let!(:delegation) { create(:delegation, setting: setting, granter: granter, grantee: grantee) } let!(:other_delegation) { create(:delegation, setting: other_setting, granter: granter, grantee: other_user) } let!(:setting) { create(:setting, consultation: consultation) } - let!(:other_setting) { create(:setting) } + let!(:other_setting) { create(:setting, consultation: other_consultation) } describe "#query" do it "returns the delegations of the specified setting only" do diff --git a/spec/services/decidim/action_delegator/delegations_csv_importer_spec.rb b/spec/services/decidim/action_delegator/delegations_csv_importer_spec.rb index 2a1aa7e7..f0f58df8 100644 --- a/spec/services/decidim/action_delegator/delegations_csv_importer_spec.rb +++ b/spec/services/decidim/action_delegator/delegations_csv_importer_spec.rb @@ -21,8 +21,8 @@ let!(:granter_email) { "granter@example.org" } let!(:grantee_email) { "grantee@example.org" } - let!(:granter) { create(:user, email: granter_email) } - let!(:grantee) { create(:user, email: grantee_email) } + let!(:granter) { create(:user, email: granter_email, organization: organization) } + let!(:grantee) { create(:user, email: grantee_email, organization: organization) } describe "#import!" do context "when the rows in the csv file are valid" do @@ -64,7 +64,7 @@ context "when delegation already exists" do subject { described_class.new(valid_csv_file, current_user, current_setting) } - let!(:delegation) { create(:delegation, granter_id: granter.id, grantee_id: grantee.id) } + let!(:delegation) { create(:delegation, setting: current_setting, granter_id: granter.id, grantee_id: grantee.id) } it "does not create a new delegation" do expect do