From 4dd1e42ad7ddb6780486b181861bc63732024771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Fri, 15 Sep 2023 13:48:17 +0200 Subject: [PATCH] allow valuators to assign templates --- Gemfile.lock | 346 +++++++++--------- .../proposal_answer_templates_controller.rb | 6 +- .../templates/admin/extra_permissions.rb | 38 ++ lib/decidim/reporting_proposals/version.rb | 2 +- .../templates/admin/extra_permissions_spec.rb | 47 +++ .../valuator_answers_with_templates_spec.rb | 57 +++ 6 files changed, 321 insertions(+), 175 deletions(-) create mode 100644 app/permissions/decidim/templates/admin/extra_permissions.rb create mode 100644 spec/permissions/templates/admin/extra_permissions_spec.rb create mode 100644 spec/system/admin/valuator_answers_with_templates_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 65b209f3..8c9ad843 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,40 +12,40 @@ PATH GEM remote: https://rubygems.org/ specs: - actioncable (6.1.7.4) - actionpack (= 6.1.7.4) - activesupport (= 6.1.7.4) + actioncable (6.1.7.6) + actionpack (= 6.1.7.6) + activesupport (= 6.1.7.6) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailbox (6.1.7.4) - actionpack (= 6.1.7.4) - activejob (= 6.1.7.4) - activerecord (= 6.1.7.4) - activestorage (= 6.1.7.4) - activesupport (= 6.1.7.4) + actionmailbox (6.1.7.6) + actionpack (= 6.1.7.6) + activejob (= 6.1.7.6) + activerecord (= 6.1.7.6) + activestorage (= 6.1.7.6) + activesupport (= 6.1.7.6) mail (>= 2.7.1) - actionmailer (6.1.7.4) - actionpack (= 6.1.7.4) - actionview (= 6.1.7.4) - activejob (= 6.1.7.4) - activesupport (= 6.1.7.4) + actionmailer (6.1.7.6) + actionpack (= 6.1.7.6) + actionview (= 6.1.7.6) + activejob (= 6.1.7.6) + activesupport (= 6.1.7.6) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (6.1.7.4) - actionview (= 6.1.7.4) - activesupport (= 6.1.7.4) + actionpack (6.1.7.6) + actionview (= 6.1.7.6) + activesupport (= 6.1.7.6) rack (~> 2.0, >= 2.0.9) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (6.1.7.4) - actionpack (= 6.1.7.4) - activerecord (= 6.1.7.4) - activestorage (= 6.1.7.4) - activesupport (= 6.1.7.4) + actiontext (6.1.7.6) + actionpack (= 6.1.7.6) + activerecord (= 6.1.7.6) + activestorage (= 6.1.7.6) + activesupport (= 6.1.7.6) nokogiri (>= 1.8.5) - actionview (6.1.7.4) - activesupport (= 6.1.7.4) + actionview (6.1.7.6) + activesupport (= 6.1.7.6) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) @@ -53,22 +53,22 @@ GEM active_link_to (1.0.5) actionpack addressable - activejob (6.1.7.4) - activesupport (= 6.1.7.4) + activejob (6.1.7.6) + activesupport (= 6.1.7.6) globalid (>= 0.3.6) - activemodel (6.1.7.4) - activesupport (= 6.1.7.4) - activerecord (6.1.7.4) - activemodel (= 6.1.7.4) - activesupport (= 6.1.7.4) - activestorage (6.1.7.4) - actionpack (= 6.1.7.4) - activejob (= 6.1.7.4) - activerecord (= 6.1.7.4) - activesupport (= 6.1.7.4) + activemodel (6.1.7.6) + activesupport (= 6.1.7.6) + activerecord (6.1.7.6) + activemodel (= 6.1.7.6) + activesupport (= 6.1.7.6) + activestorage (6.1.7.6) + actionpack (= 6.1.7.6) + activejob (= 6.1.7.6) + activerecord (= 6.1.7.6) + activesupport (= 6.1.7.6) marcel (~> 1.0) mini_mime (>= 1.1.0) - activesupport (6.1.7.4) + activesupport (6.1.7.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) @@ -76,7 +76,7 @@ GEM zeitwerk (~> 2.3) acts_as_list (0.9.19) activerecord (>= 3.0) - addressable (2.8.4) + addressable (2.8.5) public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) axe-core-api (4.7.0) @@ -149,12 +149,12 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - commonmarker (0.23.9) + commonmarker (0.23.10) concurrent-ruby (1.2.2) crack (0.4.5) rexml crass (1.0.6) - css_parser (1.14.0) + css_parser (1.16.0) addressable date (3.3.3) date_validator (0.12.0) @@ -163,59 +163,59 @@ GEM db-query-matchers (0.10.0) activesupport (>= 4.0, < 7) rspec (~> 3.0) - decidim (0.27.3) - decidim-accountability (= 0.27.3) - decidim-admin (= 0.27.3) - decidim-api (= 0.27.3) - decidim-assemblies (= 0.27.3) - decidim-blogs (= 0.27.3) - decidim-budgets (= 0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) - decidim-debates (= 0.27.3) - decidim-forms (= 0.27.3) - decidim-generators (= 0.27.3) - decidim-meetings (= 0.27.3) - decidim-pages (= 0.27.3) - decidim-participatory_processes (= 0.27.3) - decidim-proposals (= 0.27.3) - decidim-sortitions (= 0.27.3) - decidim-surveys (= 0.27.3) - decidim-system (= 0.27.3) - decidim-templates (= 0.27.3) - decidim-verifications (= 0.27.3) - decidim-accountability (0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) - decidim-admin (0.27.3) + 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) active_link_to (~> 1.0) - decidim-core (= 0.27.3) + decidim-core (= 0.27.4) devise (~> 4.7) devise-i18n (~> 1.2) devise_invitable (~> 2.0) - decidim-api (0.27.3) + decidim-api (0.27.4) graphql (~> 1.12, < 1.13) graphql-docs (~> 2.1.0) rack-cors (~> 1.0) - decidim-assemblies (0.27.3) - decidim-core (= 0.27.3) - decidim-blogs (0.27.3) - decidim-admin (= 0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) - decidim-budgets (0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) + 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-bulletin_board (0.23.0) byebug (~> 11.0) graphlient (~> 0.5.0) jwt (~> 2.2.2) rails (~> 6.0, >= 5.0.0) wisper (~> 2.0.0) - decidim-comments (0.27.3) - decidim-core (= 0.27.3) + decidim-comments (0.27.4) + decidim-core (= 0.27.4) redcarpet (~> 3.5, >= 3.5.1) - decidim-core (0.27.3) + decidim-core (0.27.4) active_link_to (~> 1.0) acts_as_list (~> 0.9) batch-loader (~> 1.2) @@ -225,7 +225,7 @@ GEM cells-rails (~> 0.1.3) charlock_holmes (~> 0.7) date_validator (~> 0.12.0) - decidim-api (= 0.27.3) + decidim-api (= 0.27.4) devise (~> 4.7) devise-i18n (~> 1.2) diffy (~> 3.3) @@ -238,7 +238,7 @@ GEM hashdiff (>= 0.4.0, < 2.0.0) invisible_captcha (~> 0.12) kaminari (~> 1.2, >= 1.2.1) - loofah (~> 2.3.1) + loofah (~> 2.19.0) mime-types (>= 1.16, < 4.0) mini_magick (~> 4.9) mustache (~> 1.1.0) @@ -266,15 +266,15 @@ GEM webpacker (= 6.0.0.rc.5) webpush (~> 1.1) wisper (~> 2.0) - decidim-debates (0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) - decidim-dev (0.27.3) + decidim-debates (0.27.4) + decidim-comments (= 0.27.4) + decidim-core (= 0.27.4) + decidim-dev (0.27.4) axe-core-rspec (~> 4.1.0) byebug (~> 11.0) capybara (~> 3.24) db-query-matchers (~> 0.10.0) - decidim (= 0.27.3) + decidim (= 0.27.4) erb_lint (~> 0.0.35) factory_bot_rails (~> 4.8) i18n-tasks (~> 0.9.18) @@ -297,53 +297,53 @@ GEM w3c_rspec_validators (~> 0.3.0) webmock (~> 3.6) wisper-rspec (~> 1.0) - decidim-elections (0.27.3) + decidim-elections (0.27.4) decidim-bulletin_board (= 0.23) - decidim-core (= 0.27.3) - decidim-forms (= 0.27.3) - decidim-proposals (= 0.27.3) + decidim-core (= 0.27.4) + decidim-forms (= 0.27.4) + decidim-proposals (= 0.27.4) rack-attack (~> 6.0) voting_schemes-dummy (= 0.23) voting_schemes-electionguard (= 0.23) - decidim-forms (0.27.3) - decidim-core (= 0.27.3) + decidim-forms (0.27.4) + decidim-core (= 0.27.4) wicked_pdf (~> 2.1) wkhtmltopdf-binary (~> 0.12) - decidim-generators (0.27.3) - decidim-core (= 0.27.3) - decidim-meetings (0.27.3) - decidim-core (= 0.27.3) - decidim-forms (= 0.27.3) + 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) icalendar (~> 2.5) - decidim-pages (0.27.3) - decidim-core (= 0.27.3) - decidim-participatory_processes (0.27.3) - decidim-core (= 0.27.3) - decidim-proposals (0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) + 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) doc2text (~> 0.4.5) redcarpet (~> 3.5, >= 3.5.1) - decidim-sortitions (0.27.3) - decidim-admin (= 0.27.3) - decidim-comments (= 0.27.3) - decidim-core (= 0.27.3) - decidim-proposals (= 0.27.3) - decidim-surveys (0.27.3) - decidim-core (= 0.27.3) - decidim-forms (= 0.27.3) - decidim-templates (= 0.27.3) - decidim-system (0.27.3) + 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) active_link_to (~> 1.0) - decidim-core (= 0.27.3) + decidim-core (= 0.27.4) devise (~> 4.7) devise-i18n (~> 1.2) devise_invitable (~> 2.0) - decidim-templates (0.27.3) - decidim-core (= 0.27.3) - decidim-forms (= 0.27.3) - decidim-verifications (0.27.3) - decidim-core (= 0.27.3) + 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) declarative-builder (0.1.0) declarative-option (< 0.2.0) declarative-option (0.1.0) @@ -388,8 +388,8 @@ GEM temple erubi (1.12.0) escape_utils (1.3.0) - excon (0.100.0) - execjs (2.8.1) + excon (0.103.0) + execjs (2.9.0) extended-markdown-filter (0.7.0) html-pipeline (~> 2.9) factory_bot (4.11.1) @@ -443,8 +443,8 @@ GEM railties (>= 4.1, < 7.1) gemoji (3.0.1) geocoder (1.8.2) - globalid (1.1.0) - activesupport (>= 5.0) + globalid (1.2.1) + activesupport (>= 6.1) graphlient (0.5.0) faraday (>= 1.0) faraday_middleware @@ -482,7 +482,7 @@ GEM rails-i18n rainbow (>= 2.2.2, < 4.0) terminal-table (>= 1.5.1) - icalendar (2.8.0) + icalendar (2.9.0) ice_cube (~> 0.16) ice_cube (0.16.4) ice_nine (0.11.2) @@ -521,7 +521,7 @@ GEM listen (3.8.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - loofah (2.3.1) + loofah (2.19.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.8.1) @@ -538,22 +538,22 @@ GEM mixlib-config (>= 2.2.1, < 4) mixlib-shellout method_source (1.0.0) - mime-types (3.4.1) + mime-types (3.5.1) mime-types-data (~> 3.2015) - mime-types-data (3.2023.0218.1) + mime-types-data (3.2023.0808) mini_magick (4.12.0) - mini_mime (1.1.2) - minitest (5.18.1) + mini_mime (1.1.5) + minitest (5.20.0) mixlib-cli (2.1.8) mixlib-config (3.0.27) tomlrb mixlib-shellout (3.2.7) chef-utils - msgpack (1.7.1) + msgpack (1.7.2) multi_xml (0.6.0) multipart-post (2.3.0) mustache (1.1.1) - net-imap (0.3.6) + net-imap (0.3.7) date net-protocol net-pop (0.1.2) @@ -624,52 +624,52 @@ GEM actionmailer (>= 3) net-smtp premailer (~> 1.7, >= 1.7.9) - public_suffix (5.0.1) - puma (5.6.6) + public_suffix (5.0.3) + puma (5.6.7) nio4r (~> 2.0) racc (1.7.1) - rack (2.2.7) - rack-attack (6.6.1) - rack (>= 1.0, < 3) + rack (2.2.8) + rack-attack (6.7.0) + rack (>= 1.0, < 4) rack-cors (1.1.1) rack (>= 2.0.0) - rack-protection (3.0.6) - rack - rack-proxy (0.7.6) + rack-protection (3.1.0) + rack (~> 2.2, >= 2.2.4) + rack-proxy (0.7.7) rack rack-test (2.1.0) rack (>= 1.3) - rails (6.1.7.4) - actioncable (= 6.1.7.4) - actionmailbox (= 6.1.7.4) - actionmailer (= 6.1.7.4) - actionpack (= 6.1.7.4) - actiontext (= 6.1.7.4) - actionview (= 6.1.7.4) - activejob (= 6.1.7.4) - activemodel (= 6.1.7.4) - activerecord (= 6.1.7.4) - activestorage (= 6.1.7.4) - activesupport (= 6.1.7.4) + rails (6.1.7.6) + actioncable (= 6.1.7.6) + actionmailbox (= 6.1.7.6) + actionmailer (= 6.1.7.6) + actionpack (= 6.1.7.6) + actiontext (= 6.1.7.6) + actionview (= 6.1.7.6) + activejob (= 6.1.7.6) + activemodel (= 6.1.7.6) + activerecord (= 6.1.7.6) + activestorage (= 6.1.7.6) + activesupport (= 6.1.7.6) bundler (>= 1.15.0) - railties (= 6.1.7.4) + railties (= 6.1.7.6) sprockets-rails (>= 2.0.0) rails-controller-testing (1.0.5) actionpack (>= 5.0.1.rc1) actionview (>= 5.0.1.rc1) activesupport (>= 5.0.1.rc1) - rails-dom-testing (2.1.1) + rails-dom-testing (2.2.0) activesupport (>= 5.0.0) minitest nokogiri (>= 1.6) - rails-html-sanitizer (1.4.3) - loofah (~> 2.3) + rails-html-sanitizer (1.5.0) + loofah (~> 2.19, >= 2.19.1) rails-i18n (6.0.0) i18n (>= 0.7, < 2) railties (>= 6.0.0, < 7) - railties (6.1.7.4) - actionpack (= 6.1.7.4) - activesupport (= 6.1.7.4) + railties (6.1.7.6) + actionpack (= 6.1.7.6) + activesupport (= 6.1.7.6) method_source rake (>= 12.2) thor (~> 1.0) @@ -690,7 +690,7 @@ GEM responders (3.1.0) actionpack (>= 5.2) railties (>= 5.2) - rexml (3.2.5) + rexml (3.2.6) rspec (3.12.0) rspec-core (~> 3.12.0) rspec-expectations (~> 3.12.0) @@ -706,7 +706,7 @@ GEM rspec-html-matchers (0.9.4) nokogiri (~> 1) rspec (>= 3.0.0.a, < 4) - rspec-mocks (3.12.5) + rspec-mocks (3.12.6) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-rails (4.1.2) @@ -780,20 +780,20 @@ GEM spring-watcher-listen (2.1.0) listen (>= 2.7, < 4.0) spring (>= 4) - sprockets (4.2.0) + sprockets (4.2.1) concurrent-ruby (~> 1.0) rack (>= 2.2.4, < 4) sprockets-rails (3.4.2) actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) - ssrf_filter (1.1.1) + ssrf_filter (1.1.2) temple (0.10.2) terminal-table (3.0.2) unicode-display_width (>= 1.1.1, < 3) thor (1.2.2) thread_safe (0.3.6) - tilt (2.2.0) + tilt (2.3.0) timeout (0.4.0) tomlrb (2.0.3) tzinfo (2.0.6) @@ -822,12 +822,12 @@ GEM rexml (~> 3.2) warden (1.2.9) rack (>= 2.0.9) - web-console (4.2.0) + web-console (4.2.1) actionview (>= 6.0.0) activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) - webmock (3.18.1) + webmock (3.19.1) addressable (>= 2.8.0) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) @@ -839,17 +839,17 @@ GEM webpush (1.1.0) hkdf (~> 0.2) jwt (~> 2.0) - websocket-driver (0.7.5) + websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - wicked_pdf (2.6.3) + wicked_pdf (2.7.0) activesupport wisper (2.0.1) wisper-rspec (1.1.0) wkhtmltopdf-binary (0.12.6.6) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (2.6.8) + zeitwerk (2.6.11) PLATFORMS x86_64-linux @@ -858,11 +858,11 @@ DEPENDENCIES bootsnap (~> 1.7) byebug (~> 11.0) codecov - decidim (= 0.27.3) - decidim-dev (= 0.27.3) - decidim-elections (= 0.27.3) + decidim (= 0.27.4) + decidim-dev (= 0.27.4) + decidim-elections (= 0.27.4) decidim-reporting_proposals! - decidim-templates (= 0.27.3) + decidim-templates (= 0.27.4) faker (~> 2.14) letter_opener_web (~> 2.0) listen (~> 3.1) diff --git a/app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb b/app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb index 7b3c3213..71a14794 100644 --- a/app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb +++ b/app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb @@ -7,6 +7,10 @@ class ProposalAnswerTemplatesController < Decidim::Templates::Admin::Application include Decidim::TranslatableAttributes include Decidim::Paginable + def permission_class_chain + [::Decidim::Templates::Admin::ExtraPermissions] + super + end + helper_method :availability_option_as_text, :availability_options_for_select rescue_from ActiveRecord::RecordNotFound do |exception| @@ -57,7 +61,7 @@ def destroy end def fetch - enforce_permission_to :read, :template, template: template + enforce_permission_to :read, :template, template: template, proposal: proposal response_object = { state: template.field_values["internal_state"], diff --git a/app/permissions/decidim/templates/admin/extra_permissions.rb b/app/permissions/decidim/templates/admin/extra_permissions.rb new file mode 100644 index 00000000..982e8ac5 --- /dev/null +++ b/app/permissions/decidim/templates/admin/extra_permissions.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Decidim + module Templates + module Admin + class ExtraPermissions < Decidim::DefaultPermissions + # this should go to the standard templates/permissions class but to avoid hacking it we put it here + # to be removed when proposal templates is accepted into core + def permissions + return permission_action if permission_action.scope != :admin + return permission_action unless user + return permission_action if context[:current_organization] != user.organization + + # return permission_action if context[:current_organization] != user.organization + allow! if user_has_a_role? && (permission_action.subject == :template && permission_action.action == :read) + + super + end + + private + + def participatory_space + @participatory_space ||= context[:proposal].try(:participatory_space) + end + + def user_roles + @user_roles ||= participatory_space.try(:user_roles) + end + + def user_has_a_role? + return unless user_roles + + user_roles.exists?(user: user) + end + end + end + end +end diff --git a/lib/decidim/reporting_proposals/version.rb b/lib/decidim/reporting_proposals/version.rb index 55129bd7..7c690e39 100644 --- a/lib/decidim/reporting_proposals/version.rb +++ b/lib/decidim/reporting_proposals/version.rb @@ -3,7 +3,7 @@ module Decidim module ReportingProposals VERSION = "0.5.0" - DECIDIM_VERSION = "0.27.3" + DECIDIM_VERSION = "0.27.4" COMPAT_DECIDIM_VERSION = [">= 0.27.0", "< 0.28"].freeze end diff --git a/spec/permissions/templates/admin/extra_permissions_spec.rb b/spec/permissions/templates/admin/extra_permissions_spec.rb new file mode 100644 index 00000000..4ce305f0 --- /dev/null +++ b/spec/permissions/templates/admin/extra_permissions_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "spec_helper" + +module Decidim::Templates::Admin + describe ExtraPermissions do + subject { described_class.new(user, permission_action, context).permissions.allowed? } + + let(:organization) { create :organization } + let(:participatory_process) { create :participatory_process, organization: organization } + let(:user) { create :user, :confirmed, :admin_terms_accepted, organization: organization } + let!(:valuator_role) { create :participatory_process_user_role, role: :valuator, user: user, participatory_process: participatory_process } + let(:current_component) { create(:proposal_component, participatory_space: participatory_process) } + let(:proposal) { create :proposal, component: current_component } + let(:context) do + { + proposal: proposal, + current_organization: organization, + current_component: current_component, + current_settings: component_settings, + component_settings: component_settings + } + end + let(:component_settings) do + double( + proposal_answering_enabled: true + ) + end + let(:permission_action) { Decidim::PermissionAction.new(**action) } + + describe "template reading" do + let(:action) do + { scope: :admin, action: :read, subject: :template } + end + + context "when everything is OK" do + it { is_expected.to be true } + end + + context "when user has no admin role" do + let(:valuator_role) { nil } + + it_behaves_like "permission is not set" + end + end + end +end diff --git a/spec/system/admin/valuator_answers_with_templates_spec.rb b/spec/system/admin/valuator_answers_with_templates_spec.rb new file mode 100644 index 00000000..7c0b24b1 --- /dev/null +++ b/spec/system/admin/valuator_answers_with_templates_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/templates/test/factories" + +describe "Valuator answers with templates", type: :system do + let(:organization) { create :organization } + let(:user) { create :user, :confirmed, :admin_terms_accepted, organization: organization } + let!(:valuator_role) { create :participatory_process_user_role, role: :valuator, user: user, participatory_process: participatory_process } + let(:participatory_process) { create :participatory_process, organization: organization } + let!(:proposal_component) { create :component, manifest_name: :proposals, participatory_space: participatory_process } + let!(:reporting_component) { create :component, manifest_name: :reporting_proposals, settings: settings, default_step_settings: settings, participatory_space: participatory_process } + let(:settings) do + { proposal_answering_enabled: true } + end + let(:component) { reporting_component } + let!(:template) { create(:template, :proposal_answer, description: { en: description }, field_values: values, organization: organization, templatable: reporting_component) } + let!(:proposal) { create(:proposal, component: component) } + let!(:valuation_assignment) { create(:valuation_assignment, proposal: proposal, valuator_role: valuator_role) } + + before do + switch_to_host(organization.host) + login_as user, scope: :user + visit Decidim::EngineRouter.admin_proxy(component).root_path + find("a", class: "action-icon--show-proposal").click + end + + describe "using a proposal_answer_template" do + let(:description) { "Some meaningful answer" } + let(:values) do + { internal_state: "evaluating" } + end + + it "uses the template" do + within ".edit_proposal_answer" do + select template.name["en"], from: :proposal_answer_template_chooser + expect(page).to have_content(description) + click_button "Answer" + end + + expect(page).to have_admin_callout("Proposal successfully answered") + + within find("tr", text: proposal.title["en"]) do + expect(page).to have_content("Evaluating") + end + end + + it "shows an error message if the template is removed" do + within ".edit_proposal_answer" do + template.destroy! + select template.name["en"], from: :proposal_answer_template_chooser + expect(page).not_to have_content(description) + expect(page).to have_content("Couldn't find this template") + end + end + end +end