Include BugHerd script only if configured, and with configured API key

This commit is contained in:
Rohan Mitchell
2016-05-04 12:13:03 +10:00
parent 9ac6de4215
commit a11696b85e
3 changed files with 77 additions and 12 deletions

View File

@@ -1,18 +1,8 @@
- if Rails.env.staging? or Rails.env.production?
- if (Rails.env.staging? || Rails.env.production?) && Spree::Config.bugherd_api_key.present?
:javascript
(function (d, t) {
var bh = d.createElement(t), s = d.getElementsByTagName(t)[0];
bh.type = 'text/javascript';
bh.src = '//www.bugherd.com/sidebarv2.js?apikey=4ftxjbgwx7y6ssykayr04w';
bh.src = '//www.bugherd.com/sidebarv2.js?apikey=#{Spree::Config.bugherd_api_key}';
s.parentNode.insertBefore(bh, s);
})(document, 'script');
-#- elsif Rails.env.production?
-#:javascript
-#(function (d, t) {
-#var bh = d.createElement(t), s = d.getElementsByTagName(t)[0];
-#bh.type = 'text/javascript';
-#bh.src = '//www.bugherd.com/sidebarv2.js?apikey=xro3uv55objies58o2wrua';
-#s.parentNode.insertBefore(bh, s);
-#})(document, 'script');

View File

@@ -0,0 +1,57 @@
require 'spec_helper'
feature 'External services' do
include AuthenticationWorkflow
include WebHelper
describe "bugherd" do
describe "limiting inclusion by environment" do
before { Spree::Config.bugherd_api_key = 'abc123' }
it "is not included in test" do
visit root_path
expect(script_content(with: 'bugherd')).to be_nil
end
it "is not included in dev" do
Rails.env.stub(:development?) { true }
visit root_path
expect(script_content(with: 'bugherd')).to be_nil
end
it "is included in staging" do
Rails.env.stub(:staging?) { true }
visit root_path
expect(script_content(with: 'bugherd')).not_to be_nil
end
it "is included in production" do
Rails.env.stub(:production?) { true }
visit root_path
expect(script_content(with: 'bugherd')).not_to be_nil
end
end
context "in an environment where BugHerd is displayed" do
before { Rails.env.stub(:staging?) { true } }
context "when there is no API key set" do
before { Spree::Config.bugherd_api_key = nil }
it "does not include the BugHerd script" do
visit root_path
expect(script_content(with: 'bugherd')).to be_nil
end
end
context "when an API key is set" do
before { Spree::Config.bugherd_api_key = 'abc123' }
it "includes the BugHerd script, with the correct API key" do
visit root_path
expect(script_content(with: 'bugherd')).to include 'abc123'
end
end
end
end
end

View File

@@ -115,6 +115,24 @@ module WebHelper
DirtyFormDialog.new(page)
end
# Fetch the content of a script block
# eg. script_content with: 'my-script.com'
# Returns nil if not found
# Raises an exception if multiple matching blocks are found
def script_content(opts={})
elems = page.all('script', visible: false)
elems = elems.to_a.select { |e| e.text(:all).include? opts[:with] } if opts[:with]
if elems.none?
nil
elsif elems.many?
raise "Multiple results returned for script_content"
else
elems.first.text(:all)
end
end
# http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara
# Do not use this without good reason. Capybara's built-in waiting is very effective.
def wait_until(secs=nil)