Skip to content

Commit

Permalink
Check if Rails is fully initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-obukhov committed Sep 6, 2024
1 parent 3fd021c commit 8204802
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/saml_idp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module SamlIdp
require 'saml_idp/metadata_builder'
require 'saml_idp/version'
require 'saml_idp/fingerprint'
require 'saml_idp/engine' if defined?(::Rails)
require 'saml_idp/engine' if defined?(::Rails::Engine)

def self.config
@config ||= SamlIdp::Configurator.new
Expand Down
2 changes: 1 addition & 1 deletion lib/saml_idp/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize
self.service_provider.persisted_metadata_getter = ->(id, service_provider) { }
self.session_expiry = 0
self.attributes = {}
self.logger = defined?(::Rails) ? Rails.logger : ->(msg) { puts msg }
self.logger = (defined?(::Rails) && Rails.respond_to?(:logger)) ? Rails.logger : ->(msg) { puts msg }
end

# formats
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/saml_idp/configurator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,34 @@ module SamlIdp
it 'has a valid session_expiry' do
expect(subject.session_expiry).to eq(0)
end

context "logger initialization" do
context 'when Rails has been properly initialized' do
it 'sets logger to Rails.logger' do
stub_const("Rails", Class.new)
allow(Rails).to receive(:respond_to?).with(:logger).and_return(true)
allow(Rails).to receive(:logger).and_return(double("Rails.logger"))

expect(subject.logger).to eq(Rails.logger)
end
end

context 'when Rails is not fully initialized' do
it 'sets logger to a lambda' do
stub_const("Rails", Class.new)
allow(Rails).to receive(:respond_to?).with(:logger).and_return(false)

expect(subject.logger).to be_a(Proc)
end
end

context 'when Rails is not defined' do
it 'sets logger to a lambda' do
hide_const("Rails")

expect(subject.logger).to be_a(Proc)
end
end
end
end
end

0 comments on commit 8204802

Please sign in to comment.