From 8dbad5c3d2ab927e6351ce19111c98a3c5244b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Nov 2019 20:36:41 +0100 Subject: [PATCH 1/7] Simplify tests for stats and results permissions Now these tests look like the other ability tests. --- spec/models/abilities/everyone_spec.rb | 86 ++++---------------------- 1 file changed, 12 insertions(+), 74 deletions(-) diff --git a/spec/models/abilities/everyone_spec.rb b/spec/models/abilities/everyone_spec.rb index 6e2ef637e..899639a60 100644 --- a/spec/models/abilities/everyone_spec.rb +++ b/spec/models/abilities/everyone_spec.rb @@ -31,81 +31,19 @@ describe Abilities::Everyone do it { should_not be_able_to(:create, LocalCensusRecords::Import) } it { should_not be_able_to(:show, LocalCensusRecords::Import) } - context "when accessing poll results" do - let(:results_enabled) { true } - let(:poll) { create(:poll, :expired, results_enabled: results_enabled) } + it { should be_able_to(:results, create(:poll, :expired, results_enabled: true)) } + it { should_not be_able_to(:results, create(:poll, :expired, results_enabled: false)) } + it { should_not be_able_to(:results, create(:poll, :current, results_enabled: true)) } - it { should be_able_to(:results, poll) } + it { should be_able_to(:stats, create(:poll, :expired, stats_enabled: true)) } + it { should_not be_able_to(:stats, create(:poll, :expired, stats_enabled: false)) } + it { should_not be_able_to(:stats, create(:poll, :current, stats_enabled: true)) } - context "and results disabled" do - let(:results_enabled) { false } + it { should be_able_to(:read_results, create(:budget, :finished, results_enabled: true)) } + it { should_not be_able_to(:read_results, create(:budget, :finished, results_enabled: false)) } + it { should_not be_able_to(:read_results, create(:budget, :reviewing_ballots, results_enabled: true)) } - it { should_not be_able_to(:results, poll) } - end - - context "and not expired" do - let(:poll) { create(:poll, :current, results_enabled: true) } - - it { should_not be_able_to(:results, poll) } - end - end - - context "when accessing poll stats" do - let(:stats_enabled) { true } - let(:poll) { create(:poll, :expired, stats_enabled: stats_enabled) } - - it { should be_able_to(:stats, poll) } - - context "and stats disabled" do - let(:stats_enabled) { false } - - it { should_not be_able_to(:stats, poll) } - end - - context "and not expired" do - let(:poll) { create(:poll, :current, stats_enabled: true) } - - it { should_not be_able_to(:stats, poll) } - end - end - - context "when accessing budget results" do - context "budget is not finished" do - let(:budget) { create(:budget, :reviewing_ballots, results_enabled: true) } - - it { should_not be_able_to(:read_results, budget) } - end - - context "budget is finished" do - let(:budget) { create(:budget, :finished) } - - it { should be_able_to(:read_results, budget) } - end - - context "results disabled" do - let(:budget) { create(:budget, :finished, results_enabled: false) } - - it { should_not be_able_to(:read_results, budget) } - end - end - - context "when accessing budget stats" do - context "supports phase is not finished" do - let(:budget) { create(:budget, :selecting, stats_enabled: true) } - - it { should_not be_able_to(:read_stats, budget) } - end - - context "supports phase is finished" do - let(:budget) { create(:budget, :valuating, stats_enabled: true) } - - it { should be_able_to(:read_stats, budget) } - end - - context "stats disabled" do - let(:budget) { create(:budget, :valuating, stats_enabled: false) } - - it { should_not be_able_to(:read_stats, budget) } - end - end + it { should be_able_to(:read_stats, create(:budget, :valuating, stats_enabled: true)) } + it { should_not be_able_to(:read_stats, create(:budget, :valuating, stats_enabled: false)) } + it { should_not be_able_to(:read_stats, create(:budget, :selecting, stats_enabled: true)) } end From 2029d7baa50c02ac32c35185b431f98e8b7ff511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Nov 2019 20:49:28 +0100 Subject: [PATCH 2/7] Limit admin access to poll stats and results There's no reason to allow administrators to check stats and results for a poll when it isn't finished or when results and stats are not enabled. Now admins have the same permissions as everyone else. --- app/models/abilities/administrator.rb | 2 +- spec/models/abilities/administrator_spec.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index b997e56a4..25dcb60c7 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -75,7 +75,7 @@ module Abilities can [:index, :create, :edit, :update, :destroy], Geozone - can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments, :results, :stats], Poll + can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments], Poll can [:read, :create, :update, :destroy, :available], Poll::Booth can [:search, :create, :index, :destroy], ::Poll::Officer can [:create, :destroy, :manage], ::Poll::BoothAssignment diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index c904dcb91..bbf8f91fe 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -6,7 +6,6 @@ describe Abilities::Administrator do let(:user) { administrator.user } let(:administrator) { create(:administrator) } - let(:poll) { create(:poll, :current, stats_enabled: false, results_enabled: false) } let(:other_user) { create(:user) } let(:hidden_user) { create(:user, :hidden) } @@ -89,9 +88,6 @@ describe Abilities::Administrator do it { should_not be_able_to(:destroy, budget_investment_document) } it { should be_able_to(:manage, Dashboard::Action) } - it { should be_able_to(:stats, poll) } - it { should be_able_to(:results, poll) } - it { should be_able_to(:read, Poll::Question) } it { should be_able_to(:create, Poll::Question) } it { should be_able_to(:update, Poll::Question) } From 864f750d92832bf3fb78637ecc361b498ba84f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Nov 2019 21:00:09 +0100 Subject: [PATCH 3/7] Remove duplication in poll permissions We were checking for `expired?` and `results_enabled?` in views and helpers, when we've already defined a rule for accessing stats and results for a poll. This way we also fix a bug when stats were enabled but the poll wasn't finished. In this scenario, the link pointed to the stats page, but when clicking it we'd get a "you don't have permission" message. Now the link doesn't point to the stats page anymore. --- app/helpers/polls_helper.rb | 8 ++------ app/views/polls/_poll_subnav.html.erb | 6 +++--- spec/features/polls/polls_spec.rb | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index f4f57ec5c..a3de04ccf 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -49,19 +49,15 @@ module PollsHelper end def link_to_poll(text, poll) - if poll.results_enabled? + if can?(:results, poll) link_to text, results_poll_path(id: poll.slug || poll.id) - elsif poll.stats_enabled? + elsif can?(:stats, poll) link_to text, stats_poll_path(id: poll.slug || poll.id) else link_to text, poll_path(id: poll.slug || poll.id) end end - def show_stats_or_results? - @poll.expired? && (@poll.results_enabled? || @poll.stats_enabled?) - end - def results_menu? controller_name == "polls" && action_name == "results" end diff --git a/app/views/polls/_poll_subnav.html.erb b/app/views/polls/_poll_subnav.html.erb index e50d64e0a..1fdc2d71d 100644 --- a/app/views/polls/_poll_subnav.html.erb +++ b/app/views/polls/_poll_subnav.html.erb @@ -1,8 +1,8 @@ -<% if show_stats_or_results? %> +<% if can?(:stats, @poll) || can?(:results, @poll) %>