[deploy] Add recaptcha keys to as site-configurable keys (#10736)
* Add recaptcha keys to as site-configurable keys * Add recaptcha to site config view * Use site config key * Make ReCAPTCHA tag optional if keys are blank * Use proper capitalization of recaptcha Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> * Refactor logic for readability * Use proper capitalization of recaptcha Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> * Use proper capitalization of recaptcha Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> * Use proper capitalization of recaptcha Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
This commit is contained in:
parent
e920155a93
commit
6c20a70aef
8 changed files with 79 additions and 4 deletions
|
|
@ -58,6 +58,8 @@ module Admin
|
|||
allow_email_password_registration
|
||||
primary_brand_color_hex
|
||||
spam_trigger_terms
|
||||
recaptcha_site_key
|
||||
recaptcha_secret_key
|
||||
]
|
||||
|
||||
allowed_params = allowed_params |
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ class FeedbackMessagesController < ApplicationController
|
|||
params = feedback_message_params.merge(reporter_id: current_user&.id)
|
||||
@feedback_message = FeedbackMessage.new(params)
|
||||
|
||||
if recaptcha_verified? && @feedback_message.save
|
||||
if (recaptcha_disabled? || recaptcha_verified?) && @feedback_message.save
|
||||
Slack::Messengers::Feedback.call(
|
||||
user: current_user,
|
||||
type: feedback_message_params[:feedback_type],
|
||||
|
|
@ -30,8 +30,12 @@ class FeedbackMessagesController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
def recaptcha_disabled?
|
||||
SiteConfig.recaptcha_site_key.blank? && SiteConfig.recaptcha_secret_key.blank?
|
||||
end
|
||||
|
||||
def recaptcha_verified?
|
||||
recaptcha_params = { secret_key: ApplicationConfig["RECAPTCHA_SECRET"] }
|
||||
recaptcha_params = { secret_key: SiteConfig.recaptcha_secret_key }
|
||||
params["g-recaptcha-response"] && verify_recaptcha(recaptcha_params)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -215,6 +215,14 @@ module Constants
|
|||
description: "Determines the mininum for the periodic email digest",
|
||||
placeholder: 2
|
||||
},
|
||||
recaptcha_site_key: {
|
||||
description: "Site key for Google reCAPTCHA, used for reporting abuse.",
|
||||
placeholder: "..."
|
||||
},
|
||||
recaptcha_secret_key: {
|
||||
description: "Secret key for Google reCAPTCHA, used for reporting abuse.",
|
||||
placeholder: "..."
|
||||
},
|
||||
right_navbar_svg_icon: {
|
||||
description: "The SVG icon used to expand the right navbar navigation menu. Should be a max of 24x24px.",
|
||||
placeholder: "<svg ...></svg>"
|
||||
|
|
|
|||
|
|
@ -67,6 +67,10 @@ class SiteConfig < RailsSettings::Base
|
|||
# Google Analytics Tracking ID, e.g. UA-71991000-1
|
||||
field :ga_tracking_id, type: :string, default: ApplicationConfig["GA_TRACKING_ID"]
|
||||
|
||||
# Google ReCATPCHA keys
|
||||
field :recaptcha_site_key, type: :string, default: ApplicationConfig["RECAPTCHA_SITE"]
|
||||
field :recaptcha_secret_key, type: :string, default: ApplicationConfig["RECAPTCHA_SECRET"]
|
||||
|
||||
# Images
|
||||
field :main_social_image, type: :string
|
||||
field :favicon_url, type: :string, default: "favicon.ico"
|
||||
|
|
|
|||
|
|
@ -464,6 +464,32 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mt-3">
|
||||
<%= render partial: "card_header",
|
||||
locals: {
|
||||
header: "Google reCAPTCHA Keys",
|
||||
state: "collapse",
|
||||
target: "recaptchaContainer",
|
||||
expanded: "false"
|
||||
} %>
|
||||
<div id="recaptchaContainer" class="card-body collapse hide" aria-labelledby="recaptchaContainer">
|
||||
<div class="form-group">
|
||||
<%= admin_config_label :recaptcha_site_key, "reCAPTCHA Site Key" %>
|
||||
<%= f.text_field :recaptcha_site_key,
|
||||
class: "form-control",
|
||||
value: SiteConfig.recaptcha_site_key %>
|
||||
<div class="alert alert-info"><%= Constants::SiteConfig::DETAILS[:recaptcha_site_key][:description] %></div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<%= admin_config_label :recaptcha_secret_key, "reCAPTCHA Secret Key" %>
|
||||
<%= f.text_field :recaptcha_secret_key,
|
||||
class: "form-control",
|
||||
value: SiteConfig.recaptcha_secret_key %>
|
||||
<div class="alert alert-info"><%= Constants::SiteConfig::DETAILS[:recaptcha_secret_key][:description] %></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mt-3">
|
||||
<%= render partial: "card_header",
|
||||
locals: {
|
||||
|
|
|
|||
|
|
@ -67,9 +67,11 @@
|
|||
<%= f.text_area :message, class: "crayons-textfield", placeholder: "...", value: @previous_message, required: true %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= recaptcha_tags site_key: ApplicationConfig["RECAPTCHA_SITE"] %>
|
||||
<% if SiteConfig.recaptcha_secret_key.present? && SiteConfig.recaptcha_site_key.present? %>
|
||||
<div class="recaptcha-tag-container">
|
||||
<%= recaptcha_tags site_key: SiteConfig.recaptcha_site_key %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div><button type="submit" name="commit" class="crayons-btn">Send Feedback</button></div>
|
||||
<% end %>
|
||||
|
|
|
|||
|
|
@ -551,6 +551,17 @@ RSpec.describe "/admin/config", type: :request do
|
|||
confirmation: confirmation_message }
|
||||
expect(SiteConfig.spam_trigger_terms).to eq(["hey", "pokemon go hack"])
|
||||
end
|
||||
|
||||
it "updates recaptcha_site_key and recaptcha_secret_key" do
|
||||
site_key = "hi-ho"
|
||||
secret_key = "lets-go"
|
||||
post "/admin/config", params: {
|
||||
site_config: { recaptcha_site_key: site_key, recaptcha_secret_key: secret_key },
|
||||
confirmation: confirmation_message
|
||||
}
|
||||
expect(SiteConfig.recaptcha_site_key).to eq site_key
|
||||
expect(SiteConfig.recaptcha_secret_key).to eq secret_key
|
||||
end
|
||||
end
|
||||
|
||||
describe "Social Media" do
|
||||
|
|
|
|||
|
|
@ -44,6 +44,24 @@ RSpec.describe "feedback_messages", type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
context "with no recaptcha keys set" do
|
||||
before do
|
||||
allow(SiteConfig).to receive(:recaptcha_secret_key).and_return("")
|
||||
allow(SiteConfig).to receive(:recaptcha_site_key).and_return("")
|
||||
end
|
||||
|
||||
it "does not show the recaptcha tag" do
|
||||
get "/report-abuse"
|
||||
expect(response.body).not_to include("recaptcha-tag-container")
|
||||
end
|
||||
|
||||
it "creates a feedback message" do
|
||||
expect do
|
||||
post feedback_messages_path, params: valid_abuse_report_params, headers: headers
|
||||
end.to change(FeedbackMessage, :count).by(1)
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid recaptcha" do
|
||||
it "rerenders page" do
|
||||
post "/feedback_messages", params: valid_abuse_report_params, headers: headers
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue