Module: HasHelpers::FactoryApi
- Defined in:
- lib/has_helpers/factory_api.rb
Constant Summary collapse
- HEADER =
"X-Factory-Api-Key"
Class Method Summary collapse
- .api_key ⇒ Object
- .authenticated?(provided_key) ⇒ Boolean
- .enabled? ⇒ Boolean
-
.load_rails_helper_if_present! ⇒ Object
Load the host application's rails_helper (and thus its factory definitions) if available.
Class Method Details
.api_key ⇒ Object
18 19 20 |
# File 'lib/has_helpers/factory_api.rb', line 18 def self.api_key ENV["TEST_FACTORY_API_KEY"] end |
.authenticated?(provided_key) ⇒ Boolean
22 23 24 25 26 27 28 |
# File 'lib/has_helpers/factory_api.rb', line 22 def self.authenticated?(provided_key) key = api_key return false if key.blank? || provided_key.blank? return false unless key.bytesize == provided_key.bytesize ActiveSupport::SecurityUtils.secure_compare(provided_key, key) end |
.enabled? ⇒ Boolean
12 13 14 15 16 |
# File 'lib/has_helpers/factory_api.rb', line 12 def self.enabled? return false if Rails.env.production? Rails.env.test? && ENV.fetch("TEST_FACTORY_API_ENABLED", "false") == "true" end |
.load_rails_helper_if_present! ⇒ Object
Load the host application's rails_helper (and thus its factory definitions) if available. Safe to call multiple times; it will only attempt once per process.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/has_helpers/factory_api.rb', line 32 def self.load_rails_helper_if_present! return if @rails_helper_loaded return unless defined?(::Rails) return unless ::Rails.respond_to?(:env) && ::Rails.env.test? return unless ::Rails.respond_to?(:root) && ::Rails.root rails_helper_path = ::Rails.root.join("spec/rails_helper.rb") return unless rails_helper_path.file? require rails_helper_path.to_s @rails_helper_loaded = true rescue LoadError # noop end |