diff --git a/app/views/polls/_callout.html.erb b/app/components/polls/callout_component.html.erb similarity index 92% rename from app/views/polls/_callout.html.erb rename to app/components/polls/callout_component.html.erb index 432d8c984..62b7616bb 100644 --- a/app/views/polls/_callout.html.erb +++ b/app/components/polls/callout_component.html.erb @@ -1,4 +1,4 @@ -<% unless can?(:answer, @poll) %> +<% unless can?(:answer, poll) %> <% if current_user.nil? %>
<%= sanitize(t("polls.show.cant_answer_not_logged_in", @@ -10,7 +10,7 @@ <%= sanitize(t("polls.show.cant_answer_verify", verify_link: link_to(t("polls.show.verify_link"), verification_path))) %>
- <% elsif @poll.expired? %> + <% elsif poll.expired? %>
<%= t("polls.show.cant_answer_expired") %>
diff --git a/app/components/polls/callout_component.rb b/app/components/polls/callout_component.rb new file mode 100644 index 000000000..f639115aa --- /dev/null +++ b/app/components/polls/callout_component.rb @@ -0,0 +1,8 @@ +class Polls::CalloutComponent < ApplicationComponent + attr_reader :poll + use_helpers :can?, :current_user, :link_to_signin, :link_to_signup + + def initialize(poll) + @poll = poll + end +end diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 79d010a63..e63ccec50 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -16,7 +16,7 @@
- <%= render "callout" %> + <%= render Polls::CalloutComponent.new(@poll) %> <% if @poll.voted_in_booth?(current_user) %>
diff --git a/spec/components/polls/callout_component_spec.rb b/spec/components/polls/callout_component_spec.rb new file mode 100644 index 000000000..40866e636 --- /dev/null +++ b/spec/components/polls/callout_component_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +describe Polls::CalloutComponent do + it "asks anonymous users to sign in" do + render_inline Polls::CalloutComponent.new(create(:poll)) + + expect(page).to have_content "You must sign in or sign up to participate" + end + + it "shows a message to level 2 users when a poll has finished" do + sign_in(create(:user, :level_two)) + + render_inline Polls::CalloutComponent.new(create(:poll, :expired)) + + expect(page).to have_content "This poll has finished" + end + + it "asks unverified users to verify their account" do + sign_in(create(:user, :incomplete_verification)) + + render_inline Polls::CalloutComponent.new(create(:poll)) + + expect(page).to have_content "You must verify your account in order to answer" + expect(page).not_to have_content "You have already participated in this poll. " \ + "If you vote again it will be overwritten" + end +end diff --git a/spec/components/polls/form_component_spec.rb b/spec/components/polls/form_component_spec.rb index 754b7f72e..c6dd44b0b 100644 --- a/spec/components/polls/form_component_spec.rb +++ b/spec/components/polls/form_component_spec.rb @@ -20,6 +20,19 @@ describe Polls::FormComponent do expect(page).to have_button "Vote", disabled: true end + it "renders disabled answers to unverified users" do + sign_in(create(:user, :incomplete_verification)) + + render_inline Polls::FormComponent.new(web_vote) + + page.find("fieldset[disabled]") do |fieldset| + expect(fieldset).to have_field "Yes" + expect(fieldset).to have_field "No" + end + + expect(page).to have_button "Vote", disabled: true + end + context "expired poll" do let(:poll) { create(:poll, :expired) } diff --git a/spec/system/polls/polls_spec.rb b/spec/system/polls/polls_spec.rb index 49043ccf2..5d9c55e3d 100644 --- a/spec/system/polls/polls_spec.rb +++ b/spec/system/polls/polls_spec.rb @@ -168,14 +168,6 @@ describe "Polls" do end end - scenario "Non-logged in users" do - create(:poll_question, :yes_no, poll: poll) - - visit poll_path(poll) - - expect(page).to have_content("You must sign in or sign up to participate") - end - scenario "Level 1 users" do poll.update!(geozone_restricted_to: [geozone]) create(:poll_question, :yes_no, poll: poll) @@ -186,17 +178,6 @@ describe "Polls" do expect(page).to have_content("You must verify your account in order to answer") end - scenario "Level 2 users in an expired poll" do - expired_poll = create(:poll, :expired) - create(:poll_question, :yes_no, poll: expired_poll) - - login_as(create(:user, :level_two, geozone: geozone)) - - visit poll_path(expired_poll) - - expect(page).to have_content("This poll has finished") - end - scenario "Level 2 users answering" do poll.update!(geozone_restricted_to: [geozone]) create(:poll_question, :yes_no, poll: poll, title: "Do you agree?") diff --git a/spec/system/polls/voter_spec.rb b/spec/system/polls/voter_spec.rb index 2dd53d315..0a4f2947f 100644 --- a/spec/system/polls/voter_spec.rb +++ b/spec/system/polls/voter_spec.rb @@ -28,22 +28,6 @@ describe "Voter" do "If you vote again it will be overwritten." end - scenario "Voting via web as unverified user" do - user = create(:user, :incomplete_verification) - - login_as user - visit poll_path(poll) - - within_fieldset "Is this question stupid?" do - expect(page).to have_field "Yes", type: :radio, disabled: true - expect(page).to have_field "No", type: :radio, disabled: true - end - - expect(page).to have_content "You must verify your account in order to answer" - expect(page).not_to have_content "You have already participated in this poll. " \ - "If you vote again it will be overwritten" - end - scenario "Voting in booth" do admin_user = admin.user login_through_form_as_officer(officer)