-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09c6a0c
commit 622e8e9
Showing
11 changed files
with
154 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This is the recaptcha submit handler for the record feedback modal. | ||
// Normally this is inline, but the Blacklight modal filters out inline scripts. | ||
// See: app/views/record_feedback/new.html.erb | ||
document.addEventListener('show.blacklight.blacklight-modal', () => { | ||
const form = document.forms.record_new_contact_form; | ||
if (!form || form.dataset.recaptchaInitialized) return; | ||
|
||
form.dataset.recaptchaInitialized = 'true'; | ||
form.addEventListener('submit', async function(e) { | ||
e.preventDefault(); | ||
if (typeof grecaptcha !== 'undefined' && grecaptcha) { | ||
const response = await grecaptcha.execute(this.dataset.recaptchaSiteKey, { action: 'record_feedback' }); | ||
const element = document.getElementById('g-recaptcha-response-data-record-feedback'); | ||
if (element) element.value = response; | ||
} | ||
this.submit(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,7 @@ | |
} | ||
} | ||
} | ||
|
||
.grecaptcha-badge { | ||
visibility: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# This unpleasantness allows us to include the upstream controller before overriding it | ||
spotlight_path = Gem::Specification.find_by_name('blacklight-spotlight').full_gem_path | ||
require_dependency File.join(spotlight_path, 'app/controllers/spotlight/contact_forms_controller') | ||
|
||
# Override the upstream controller to add recaptcha | ||
module Spotlight | ||
## | ||
# Controller for routing exhibit feedback from users | ||
class ContactFormsController | ||
def create | ||
return render 'new' unless @contact_form.valid? | ||
|
||
if verify_recaptcha(action: action) | ||
send_feedback | ||
else | ||
report_failure | ||
end | ||
end | ||
|
||
def action | ||
'feedback' | ||
end | ||
|
||
def send_feedback | ||
ContactMailer.report_problem(@contact_form).deliver_now | ||
redirect_back fallback_location: spotlight.new_exhibit_contact_form_path(current_exhibit), | ||
notice: t(:'helpers.submit.contact_form.created') | ||
end | ||
|
||
def report_failure | ||
redirect_back fallback_location: spotlight.new_exhibit_contact_form_path(current_exhibit), | ||
alert: t(:'helpers.submit.contact_form.error') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,25 @@ | |
end | ||
|
||
describe 'POST create' do | ||
it 'sends an email' do | ||
expect do | ||
context 'when recaptcha verification succeeds' do | ||
before do | ||
allow(controller).to receive(:verify_recaptcha).and_return(true) | ||
end | ||
|
||
it 'sends an email' do | ||
expect do | ||
post( | ||
:create, | ||
params: { | ||
exhibit_id: exhibit.id, | ||
id: 'abc123', | ||
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', honeypot_field_name => '' } | ||
} | ||
) | ||
end.to change { ActionMailer::Base.deliveries.count }.by(1) | ||
end | ||
|
||
it 'redirects back' do | ||
post( | ||
:create, | ||
params: { | ||
|
@@ -30,31 +47,33 @@ | |
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', honeypot_field_name => '' } | ||
} | ||
) | ||
end.to change { ActionMailer::Base.deliveries.count }.by(1) | ||
end | ||
expect(response).to redirect_to 'http://test.host/' | ||
end | ||
|
||
it 'redirects back' do | ||
post( | ||
:create, | ||
params: { | ||
exhibit_id: exhibit.id, | ||
id: 'abc123', | ||
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', honeypot_field_name => '' } | ||
} | ||
) | ||
expect(response).to redirect_to 'http://test.host/' | ||
it 'sets a flash message' do | ||
post( | ||
:create, | ||
params: { | ||
exhibit_id: exhibit.id, | ||
id: 'abc123', | ||
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', honeypot_field_name => '' } | ||
} | ||
) | ||
expect(flash[:notice]).to eq 'Thank you. Your feedback has been submitted.' | ||
end | ||
end | ||
|
||
it 'sets a flash message' do | ||
post( | ||
:create, | ||
params: { | ||
exhibit_id: exhibit.id, | ||
id: 'abc123', | ||
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', honeypot_field_name => '' } | ||
} | ||
) | ||
expect(flash[:notice]).to eq 'Thank you. Your feedback has been submitted.' | ||
context 'when recaptcha verification fails' do | ||
before do | ||
allow(controller).to receive(:verify_recaptcha).and_return(false) | ||
end | ||
|
||
it 'alerts the failure in the flash message' do | ||
post :create, params: { exhibit_id: exhibit.id, id: 'abc123', | ||
contact_form: { name: 'Joe Doe', email: '[email protected]', message: 'Great record!', | ||
honeypot_field_name => '' } } | ||
expect(flash[:alert]).to eq 'There was a problem submitting feedback.' | ||
end | ||
end | ||
end | ||
end |