diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index e89ca97ac..88807db6d 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -32,7 +32,8 @@ class DebatesController < ApplicationController end def update - if @debate.update(debate_params) + @debate.assign_attributes(debate_params) + if @debate.save_with_captcha redirect_to @debate, notice: t('flash.actions.update.notice', resource_name: 'Debate') else load_featured_tags diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 984f3ea2e..9309cf312 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -51,6 +51,26 @@ feature 'Debates' do expect(page).to have_content I18n.l(Date.today) end + scenario 'Captcha is required for debate creation' do + login_as(create(:user)) + + visit new_debate_path + fill_in 'debate_title', with: "Great title" + fill_in 'debate_description', with: 'Very important issue...' + fill_in 'debate_captcha', with: "wrongText!" + check 'debate_terms_of_service' + + click_button "Create Debate" + + expect(page).to_not have_content "Debate was successfully created." + expect(page).to have_content "1 error" + + fill_in 'debate_captcha', with: correct_captcha_text + click_button "Create Debate" + + expect(page).to have_content "Debate was successfully created." + end + scenario 'JS injection is prevented but safe html is respected' do author = create(:user) login_as(author) @@ -152,6 +172,7 @@ feature 'Debates' do fill_in 'debate_title', with: "End child poverty" fill_in 'debate_description', with: "Let's..." + fill_in 'debate_captcha', with: correct_captcha_text click_button "Update Debate" @@ -160,6 +181,26 @@ feature 'Debates' do expect(page).to have_content "Let's..." end + scenario 'Captcha is required to update a debate' do + debate = create(:debate) + login_as(debate.author) + + visit edit_debate_path(debate) + expect(current_path).to eq(edit_debate_path(debate)) + + fill_in 'debate_title', with: "New title" + fill_in 'debate_captcha', with: "wrong!" + click_button "Update Debate" + + expect(page).to_not have_content "Debate was successfully updated." + expect(page).to have_content "1 error" + + fill_in 'debate_captcha', with: correct_captcha_text + click_button "Update Debate" + + expect(page).to have_content "Debate was successfully updated." + end + describe 'Limiting tags shown' do let(:all_tags) { ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"] diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index c496c471e..a15902dc7 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -85,6 +85,7 @@ feature 'Tags' do expect(page).to have_selector("input[value='Economía']") fill_in 'debate_tag_list', with: "Economía, Hacienda" + fill_in 'debate_captcha', with: correct_captcha_text click_button 'Update Debate' expect(page).to have_content 'Debate was successfully updated.' @@ -101,6 +102,7 @@ feature 'Tags' do visit edit_debate_path(debate) fill_in 'debate_tag_list', with: "" + fill_in 'debate_captcha', with: correct_captcha_text click_button 'Update Debate' expect(page).to have_content 'Debate was successfully updated.' diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index c1ad724bb..7647c9a64 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -53,7 +53,7 @@ module CommonActions end def correct_captcha_text - SimpleCaptcha::SimpleCaptchaData.first.value + SimpleCaptcha::SimpleCaptchaData.last.value end def avatar(name)