adds controller check for anonymous votes

This commit is contained in:
Juanjo Bazán
2015-08-31 14:51:46 +02:00
parent 3a15895617
commit bbf96259fc
6 changed files with 26 additions and 5 deletions

View File

@@ -53,7 +53,7 @@ class DebatesController < ApplicationController
end end
def vote def vote
@debate.vote_by(voter: current_user, vote: params[:value]) @debate.vote_by(voter: current_user, vote: params[:value]) if @debate.votable_by?(current_user)
set_debate_votes(@debate) set_debate_votes(@debate)
end end

View File

@@ -72,6 +72,7 @@ class Debate < ActiveRecord::Base
end end
def anonymous_votes_ratio def anonymous_votes_ratio
return 0 if cached_votes_total == 0
(cached_anonymous_votes_total.to_f / cached_votes_total) * 100 (cached_anonymous_votes_total.to_f / cached_votes_total) * 100
end end

View File

@@ -12,7 +12,6 @@ describe DebatesController do
end end
describe 'POST create' do describe 'POST create' do
it 'should create an ahoy event' do it 'should create an ahoy event' do
sign_in create(:user) sign_in create(:user)
@@ -22,4 +21,26 @@ describe DebatesController do
expect(Ahoy::Event.last.properties['debate_id']).to eq Debate.last.id expect(Ahoy::Event.last.properties['debate_id']).to eq Debate.last.id
end end
end end
describe "Vote with too many anonymous votes" do
it 'should allow vote if user is allowed' do
Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 100)
debate = create(:debate)
sign_in create(:user)
expect do
xhr :post, :vote, id: debate.id, value: 'yes'
end.to change { debate.reload.votes_for.size }.by(1)
end
it 'should not allow vote if user is not allowed' do
Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 0)
debate = create(:debate)
sign_in create(:user)
expect do
xhr :post, :vote, id: debate.id, value: 'yes'
end.to_not change { debate.reload.votes_for.size }
end
end
end end

View File

@@ -5,7 +5,7 @@ feature 'Votes' do
feature 'Debates' do feature 'Debates' do
background do background do
@manuela = create(:user) @manuela = create(:user, verified_at: Time.now)
@pablo = create(:user) @pablo = create(:user)
@debate = create(:debate) @debate = create(:debate)

View File

@@ -81,7 +81,7 @@ describe Debate do
describe "#votable_by?" do describe "#votable_by?" do
let(:debate) { create(:debate) } let(:debate) { create(:debate) }
before(:all) do before(:each) do
Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 50) Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 50)
end end

View File

@@ -21,7 +21,6 @@ RSpec.configure do |config|
config.before(:each) do |example| config.before(:each) do |example|
DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner.start DatabaseCleaner.start
load "#{Rails.root}/db/seeds.rb" load "#{Rails.root}/db/seeds.rb"
end end