From b4c4bda8a7365028ff0d9988b86cb60822bffdf0 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 20 Oct 2015 18:46:47 +0200 Subject: [PATCH 1/5] refactors votes_spec --- spec/features/votes_spec.rb | 90 ++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index f340dedd6..75fe9ef0b 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -5,13 +5,13 @@ feature 'Votes' do background do @manuela = create(:user, verified_at: Time.now) @pablo = create(:user) - - login_as(@manuela) end feature 'Debates' do + background { login_as(@manuela) } scenario "Index shows user votes on debates" do + debate1 = create(:debate) debate2 = create(:debate) debate3 = create(:debate) @@ -178,49 +178,11 @@ feature 'Votes' do end end - scenario 'Not logged user trying to vote', :js do - debate = create(:debate) - visit "/" - click_link "Logout" - - visit debates_path - within("#debate_#{debate.id}") do - find("div.votes").hover - expect_message_you_need_to_sign_in - end - - visit debate_path(debate) - within("#debate_#{debate.id}") do - find("div.votes").hover - expect_message_you_need_to_sign_in - end - end - - scenario 'Anonymous user trying to vote', :js do - user = create(:user) - debate = create(:debate) - - Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 50) - debate.update(cached_anonymous_votes_total: 520, cached_votes_total: 1000) - - login_as(user) - - visit debates_path - within("#debate_#{debate.id}") do - find("div.votes").hover - expect_message_to_many_anonymous_votes - end - - visit debate_path(debate) - within("#debate_#{debate.id}") do - find("div.votes").hover - expect_message_to_many_anonymous_votes - end - end end feature 'Proposals' do + background { login_as(@manuela) } scenario "Index shows user votes on proposals" do proposal1 = create(:proposal) @@ -302,12 +264,24 @@ feature 'Votes' do end end - scenario 'Not logged user trying to vote', :js do - proposal = create(:proposal) + scenario 'Not logged user trying to vote debates', :js do + debate = create(:debate) - visit "/" - click_link "Logout" - expect(page).to have_content "Signed out successfully." + visit debates_path + within("#debate_#{debate.id}") do + find("div.votes").hover + expect_message_you_need_to_sign_in + end + + visit debate_path(debate) + within("#debate_#{debate.id}") do + find("div.votes").hover + expect_message_you_need_to_sign_in + end + end + + scenario 'Not logged user trying to vote proposals', :js do + proposal = create(:proposal) visit proposals_path within("#proposal_#{proposal.id}") do @@ -322,7 +296,29 @@ feature 'Votes' do end end - scenario "Anonymous user trying to vote", :js do + scenario 'Anonymous user trying to vote debates', :js do + user = create(:user) + debate = create(:debate) + + Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 50) + debate.update(cached_anonymous_votes_total: 520, cached_votes_total: 1000) + + login_as(user) + + visit debates_path + within("#debate_#{debate.id}") do + find("div.votes").hover + expect_message_to_many_anonymous_votes + end + + visit debate_path(debate) + within("#debate_#{debate.id}") do + find("div.votes").hover + expect_message_to_many_anonymous_votes + end + end + + scenario "Anonymous user trying to vote proposals", :js do user = create(:user) proposal = create(:proposal) From b27c91f40b591de59b87f07dad74926c0655f6d2 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 20 Oct 2015 18:59:35 +0200 Subject: [PATCH 2/5] disallow voting debates or proposals when user is nil in some tests --- app/models/debate.rb | 1 + app/models/proposal.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index 822a38b31..0b64b7b1e 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -72,6 +72,7 @@ class Debate < ActiveRecord::Base end def votable_by?(user) + return false unless user total_votes <= 100 || !user.unverified? || Setting.value_for('max_ratio_anon_votes_on_debates').to_i == 100 || diff --git a/app/models/proposal.rb b/app/models/proposal.rb index f7c1c5078..7d5a5bb17 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -55,7 +55,7 @@ class Proposal < ActiveRecord::Base end def votable_by?(user) - user.level_two_or_three_verified? + user && user.level_two_or_three_verified? end def register_vote(user, vote_value) From b6af10a91fa70e3d901b2bf8c03279b2623a894f Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 20 Oct 2015 19:08:52 +0200 Subject: [PATCH 3/5] refactors localization spec using expect instead of global state --- spec/features/localization_spec.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index ddea1e38b..810d07503 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -34,13 +34,10 @@ feature 'Localization' do end scenario 'Locale switcher not present if only one locale' do - initial_locales = I18n.available_locales - I18n.available_locales = [:en] + expect(I18n).to receive(:available_locales).and_return([:en]) visit '/' expect(page).to_not have_content('Language') expect(page).to_not have_css('div.locale') - - I18n.available_locales = initial_locales end end From 1e08217b91ff958d6f8e9d00e7a7b114c28c281d Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 20 Oct 2015 19:20:48 +0200 Subject: [PATCH 4/5] adds another refactor to votes --- spec/features/votes_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index 75fe9ef0b..07fcd3a34 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -105,7 +105,9 @@ feature 'Votes' do visit debate_path(@debate) find('.in-favor a').click + expect(page).to have_content "1 vote" find('.in-favor a').click + expect(page).to_not have_content "2 votes" within('.in-favor') do expect(page).to have_content "100%" @@ -114,8 +116,6 @@ feature 'Votes' do within('.against') do expect(page).to have_content "0%" end - - expect(page).to have_content "1 vote" end scenario 'Show' do @@ -222,9 +222,11 @@ feature 'Votes' do within('.supports') do find('.in-favor a').click - find('.in-favor a').click - expect(page).to have_content "1 support" + + find('.in-favor a').click + expect(page).to have_content "1 support" + expect(page).to_not have_content "2 supports" end end From 49e12041400031c1150a8b8241682e52d7267242 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 20 Oct 2015 19:44:46 +0200 Subject: [PATCH 5/5] refactors votes_spec --- spec/features/votes_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index 07fcd3a34..03aaa7e4c 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -224,9 +224,7 @@ feature 'Votes' do find('.in-favor a').click expect(page).to have_content "1 support" - find('.in-favor a').click - expect(page).to have_content "1 support" - expect(page).to_not have_content "2 supports" + expect(page).to_not have_selector ".in-favor a" end end