diff options
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/issues_controller_spec.rb | 89 | ||||
-rw-r--r-- | spec/controllers/registrations_controller_spec.rb | 2 |
2 files changed, 74 insertions, 17 deletions
diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index 5f27f336f72..4b89381eb96 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -326,7 +326,7 @@ describe Projects::IssuesController do end describe 'POST #create' do - def post_new_issue(attrs = {}) + def post_new_issue(issue_attrs = {}, additional_params = {}) sign_in(user) project = create(:empty_project, :public) project.team << [user, :developer] @@ -334,8 +334,8 @@ describe Projects::IssuesController do post :create, { namespace_id: project.namespace.to_param, project_id: project.to_param, - issue: { title: 'Title', description: 'Description' }.merge(attrs) - } + issue: { title: 'Title', description: 'Description' }.merge(issue_attrs) + }.merge(additional_params) project.issues.first end @@ -378,24 +378,81 @@ describe Projects::IssuesController do context 'Akismet is enabled' do before do + stub_application_setting(recaptcha_enabled: true) allow_any_instance_of(SpamService).to receive(:check_for_spam?).and_return(true) - allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true) end - def post_spam_issue - post_new_issue(title: 'Spam Title', description: 'Spam lives here') - end + context 'when an issue is not identified as a spam' do + before do + allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false) + allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(false) + end - it 'rejects an issue recognized as spam' do - expect{ post_spam_issue }.not_to change(Issue, :count) - expect(response).to render_template(:new) + it 'does not create an issue' do + expect { post_new_issue(title: '') }.not_to change(Issue, :count) + end end - it 'creates a spam log' do - post_spam_issue - spam_logs = SpamLog.all - expect(spam_logs.count).to eq(1) - expect(spam_logs[0].title).to eq('Spam Title') + context 'when an issue is identified as a spam' do + before { allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true) } + + context 'when captcha is not verified' do + def post_spam_issue + post_new_issue(title: 'Spam Title', description: 'Spam lives here') + end + + before { allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false) } + + it 'rejects an issue recognized as a spam' do + expect { post_spam_issue }.not_to change(Issue, :count) + end + + it 'creates a spam log' do + post_spam_issue + spam_logs = SpamLog.all + + expect(spam_logs.count).to eq(1) + expect(spam_logs.first.title).to eq('Spam Title') + expect(spam_logs.first.recaptcha_verified).to be_falsey + end + + it 'does not create an issue when it is not valid' do + expect { post_new_issue(title: '') }.not_to change(Issue, :count) + end + + it 'does not create an issue when recaptcha is not enabled' do + stub_application_setting(recaptcha_enabled: false) + + expect { post_spam_issue }.not_to change(Issue, :count) + end + end + + context 'when captcha is verified' do + let!(:spam_logs) { create_list(:spam_log, 2, user: user, title: 'Title') } + + def post_verified_issue + post_new_issue({}, { spam_log_id: spam_logs.last.id, recaptcha_verification: true } ) + end + + before do + allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(true) + end + + it 'accepts an issue after recaptcha is verified' do + expect { post_verified_issue }.to change(Issue, :count) + end + + it 'marks spam log as recaptcha_verified' do + expect { post_verified_issue }.to change { SpamLog.last.recaptcha_verified }.from(false).to(true) + end + + it 'does not mark spam log as recaptcha_verified when it does not belong to current_user' do + spam_log = create(:spam_log) + + expect { post_new_issue({}, { spam_log_id: spam_log.id, recaptcha_verification: true } ) }. + not_to change { SpamLog.last.recaptcha_verified } + end + end end end @@ -405,7 +462,7 @@ describe Projects::IssuesController do end it 'creates a user agent detail' do - expect{ post_new_issue }.to change(UserAgentDetail, :count).by(1) + expect { post_new_issue }.to change(UserAgentDetail, :count).by(1) end end diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 42fbfe89368..8cc216445eb 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -44,7 +44,7 @@ describe RegistrationsController do post(:create, user_params) expect(response).to render_template(:new) - expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please re-solve the reCAPTCHA.' + expect(flash[:alert]).to include 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.' end it 'redirects to the dashboard when the recaptcha is solved' do |