Skip to content

Commit

Permalink
Check if Rails is fully initialized (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-obukhov authored Sep 23, 2024
1 parent 2003c94 commit 62ca537
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
rails_logger = double("Rails.logger")
stub_const("Rails", double(logger: 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)

expect(subject.logger).to be_a(Proc)
expect { subject.logger.call("test") }.to output("test\n").to_stdout
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)
expect { subject.logger.call("test") }.to output("test\n").to_stdout
end
end
end
end
end

0 comments on commit 62ca537

Please sign in to comment.