adds poll and poll question's specs

This commit is contained in:
rgarcia
2016-11-17 22:48:00 +01:00
parent 626a8e9154
commit 4955daf886
6 changed files with 136 additions and 21 deletions

View File

@@ -11,8 +11,8 @@ class Polls::QuestionsController < ApplicationController
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order) @comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments) set_comment_flags(@comment_tree.comments)
#@question_answer = @question.answers.where(author_id: current_user.try(:id)).first question_answer = @question.partial_results.where(author_id: current_user.try(:id)).first
@answers_by_question_id = {@question.id => params[:answer]} @answers_by_question_id = {@question.id => question_answer.try(:answer)}
end end
def answer def answer

View File

@@ -47,8 +47,7 @@ class Poll::Question < ActiveRecord::Base
end end
def answerable_by?(user) def answerable_by?(user)
true poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id))
#poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id))
end end
def self.answerable_by(user) def self.answerable_by(user)

View File

@@ -1,23 +1,46 @@
<div class="enquiries-answers"> <div class="poll-question-answers">
<% if can? :answer, question %> <% if can? :answer, question %>
<div class="small-12 small-centered text-center column"> <div class="small-12 small-centered text-center column">
<% question.valid_answers.each do |answer| %> <% question.valid_answers.each do |answer| %>
<% if @answers_by_question_id[question.id] == answer %> <% if @answers_by_question_id[question.id] == answer %>
<span class="button answered-fixme-decabeza"> <span class="button" title="<%= t("poll_questions.show.voted", answer: answer)%>">
<%= answer %> <%= answer %>
</span> </span>
<% else %> <% else %>
<%= link_to answer, <%= link_to answer,
answer_poll_question_path(poll_id: question.poll_id, id: question.id, answer: answer), answer_question_path(question, answer: answer),
method: :post, method: :post,
remote: true, remote: true,
title: t("poll_questions.show.vote_answer", answer: answer),
class: "button secondary hollow" %> class: "button secondary hollow" %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>
<% else %> <% else %>
<% question.valid_answers.each do |answer| %> <div class="small-12 column">
<span class="button deactivated-fixme-decabeza"><%= answer %></span> <% if current_user.nil? %>
<div class="callout primary">
<%= t("poll_questions.show.not_logged_in",
signin: link_to(t("poll_questions.show.signin"), new_user_session_path, class: "probe-message"),
signup: link_to(t("poll_questions.show.signup"), new_user_registration_path, class: "probe-message")).html_safe %>
</div>
<% elsif current_user.unverified? %>
<div class="callout warning">
<%= t('poll_questions.show.cant_answer_verify_html',
verify_link: link_to(t('poll_questions.show.verify_link'), verification_path)) %>
</div>
<% else %>
<div class="callout warning">
<%= t('poll_questions.show.cant_answer_wrong_geozone') %>
</div>
<% end %> <% end %>
<div class="row">
<div class="small-12 small-centered text-center column">
<% question.valid_answers.each do |answer| %>
<span class="button disabled"><%= answer %></span>
<% end %>
</div>
</div>
</div>
<% end %> <% end %>
</div> </div>

View File

@@ -52,7 +52,7 @@ feature 'Admin enquiries' do
expect(page).to have_content(summary) expect(page).to have_content(summary)
end end
scenario 'Create from successful proposal', :focus do scenario 'Create from successful proposal' do
geozones = create_list(:geozone, 3) geozones = create_list(:geozone, 3)
proposal = create(:proposal, :successful) proposal = create(:proposal, :successful)

View File

@@ -1,4 +1,3 @@
# coding: utf-8
require 'rails_helper' require 'rails_helper'
feature 'Polls' do feature 'Polls' do
@@ -95,7 +94,7 @@ feature 'Polls' do
expect(page).to_not have_link('Chewbacca') expect(page).to_not have_link('Chewbacca')
end end
scenario 'Level 2 users in an incoming question' do scenario 'Level 2 users in an incoming poll' do
incoming_poll = create(:poll, :incoming) incoming_poll = create(:poll, :incoming)
create(:poll_question, poll: incoming_poll, geozone_ids: [geozone.id], valid_answers: 'Rey, Finn') create(:poll_question, poll: incoming_poll, geozone_ids: [geozone.id], valid_answers: 'Rey, Finn')
login_as(create(:user, :level_two, geozone: geozone)) login_as(create(:user, :level_two, geozone: geozone))
@@ -110,7 +109,7 @@ feature 'Polls' do
expect(page).to have_content('This poll has not yet started') expect(page).to have_content('This poll has not yet started')
end end
scenario 'Level 2 users in an expired question' do scenario 'Level 2 users in an expired poll' do
expired_poll = create(:poll, :expired) expired_poll = create(:poll, :expired)
create(:poll_question, poll: expired_poll, geozone_ids: [geozone.id], valid_answers: 'Luke, Leia') create(:poll_question, poll: expired_poll, geozone_ids: [geozone.id], valid_answers: 'Luke, Leia')
login_as(create(:user, :level_two, geozone: geozone)) login_as(create(:user, :level_two, geozone: geozone))
@@ -161,6 +160,7 @@ feature 'Polls' do
question = create(:poll_question, poll: poll, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca') question = create(:poll_question, poll: poll, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone) user = create(:user, :level_two, geozone: geozone)
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca') create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
login_as user login_as user
visit poll_path(poll) visit poll_path(poll)
@@ -182,6 +182,4 @@ feature 'Polls' do
end end
end end
end end

View File

@@ -0,0 +1,95 @@
require 'rails_helper'
feature 'Poll Questions' do
scenario 'Lists enquiries from proposals before regular enquiries' do
poll = create(:poll)
normal_question = create(:poll_question, poll: poll)
proposal_question = create(:poll_question, proposal: create(:proposal), poll: poll)
visit poll_path(poll)
expect(proposal_question.title).to appear_before(normal_question.title)
end
context 'Answering' do
let(:geozone) { create(:geozone) }
scenario 'Non-logged in users' do
question = create(:poll_question, valid_answers: 'Han Solo, Chewbacca')
visit question_path(question)
expect(page).to have_content('Han Solo')
expect(page).to have_content('Chewbacca')
expect(page).to have_content('You must Sign in or Sign up to participate')
expect(page).to_not have_link('Han Solo')
expect(page).to_not have_link('Chewbacca')
end
scenario 'Level 1 users' do
question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, geozone: geozone))
visit question_path(question)
expect(page).to have_content('Han Solo')
expect(page).to have_content('Chewbacca')
expect(page).to have_content('You must verify your account in order to answer')
expect(page).to_not have_link('Han Solo')
expect(page).to_not have_link('Chewbacca')
end
scenario 'Level 2 users in an enquiry for a geozone which is not theirs' do
question = create(:poll_question, geozone_ids: [], valid_answers: 'Vader, Palpatine')
login_as(create(:user, :level_two))
visit question_path(question)
expect(page).to have_content('Vader')
expect(page).to have_content('Palpatine')
expect(page).to_not have_link('Vader')
expect(page).to_not have_link('Palpatine')
end
scenario 'Level 2 users who can answer' do
question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
login_as(create(:user, :level_two, geozone: geozone))
visit question_path(question)
expect(page).to have_link('Han Solo')
expect(page).to have_link('Chewbacca')
end
scenario 'Level 2 users who have already answered' do
question = create(:poll_question, geozone_ids:[geozone.id], valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
create(:poll_partial_result, question: question, author: user, answer: 'Chewbacca')
login_as user
visit question_path(question)
expect(page).to have_link('Han Solo')
expect(page).to_not have_link('Chewbacca')
expect(page).to have_content('Chewbacca')
end
scenario 'Level 2 users answering', :js do
question = create(:poll_question, geozone_ids: [geozone.id], valid_answers: 'Han Solo, Chewbacca')
user = create(:user, :level_two, geozone: geozone)
login_as user
visit question_path(question)
click_link 'Han Solo'
expect(page).to_not have_link('Han Solo')
expect(page).to have_link('Chewbacca')
end
end
end