Skip to content

Commit 4289263

Browse files
committed
Adjust OneLogin namespace compatibility, defining Module instead Alias to Object. Fix compatibility with Logging
1 parent 7ae2af6 commit 4289263

File tree

4 files changed

+99
-11
lines changed

4 files changed

+99
-11
lines changed

UPGRADING.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ the class names themselves have intentionally been kept the same.
3333
Note that the project folder structure has also been updated accordingly. Notably, the directory
3434
`lib/onelogin/schemas` is now `lib/ruby_saml/schemas`.
3535

36-
For backward compatibility, the alias `OneLogin = Object` has been set, so `RubySaml::` will still work
37-
as before. This alias will be removed in RubySaml version `3.0.0`.
36+
For backward compatibility, a module is defined at lib/ruby_saml.rb, so `RubySaml::` will still work
37+
as before. This module will be removed in RubySaml version `3.0.0`.
38+
```
39+
unless defined?(::OneLogin)
40+
module OneLogin
41+
RubySaml = ::RubySaml
42+
end
43+
end
44+
```
3845

3946

4047
### Deprecation and removal of "XMLSecurity" namespace

lib/ruby_saml.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,17 @@
2020
require 'ruby_saml/utils'
2121
require 'ruby_saml/version'
2222

23-
# @deprecated This alias adds compatibility with v1.x and will be removed in v3.0.0
24-
OneLogin = Object
23+
# @deprecated This module adds compatibility with v1.x and will be removed in v3.0.0
24+
unless defined?(::OneLogin)
25+
module OneLogin
26+
RubySaml = ::RubySaml
27+
end
28+
end
29+
30+
unless defined?(::OneLogin::RubySaml::Logging)
31+
module OneLogin
32+
module RubySaml
33+
Logging = ::RubySaml::Logging
34+
end
35+
end
36+
end

test/logging_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,56 @@ class LoggingTest < Minitest::Test
5858
RubySaml::Logging.info('sup?')
5959
end
6060
end
61+
62+
describe "OneLogin::RubySaml::Logging compatibility" do
63+
it "defines OneLogin::RubySaml::Logging as an alias to RubySaml::Logging" do
64+
assert defined?(OneLogin::RubySaml::Logging),
65+
"Expected OneLogin::RubySaml::Logging to be defined"
66+
67+
assert_equal RubySaml::Logging.object_id,
68+
OneLogin::RubySaml::Logging.object_id,
69+
"Expected OneLogin::RubySaml::Logging to alias RubySaml::Logging"
70+
end
71+
72+
it "shares the same logger instance when set via RubySaml::Logging" do
73+
logger = mock('Logger')
74+
RubySaml::Logging.logger = logger
75+
76+
assert_same logger, OneLogin::RubySaml::Logging.logger
77+
end
78+
79+
it "shares the same logger instance when set via OneLogin::RubySaml::Logging" do
80+
logger = mock('Logger')
81+
OneLogin::RubySaml::Logging.logger = logger
82+
83+
assert_same logger, RubySaml::Logging.logger
84+
end
85+
86+
it "delegates to the configured logger when using the legacy constant" do
87+
logger = mock('Logger')
88+
OneLogin::RubySaml::Logging.logger = logger
89+
90+
logger.expects(:debug).with('hi mom')
91+
logger.expects(:info).with('sup?')
92+
93+
OneLogin::RubySaml::Logging.debug('hi mom')
94+
OneLogin::RubySaml::Logging.info('sup?')
95+
end
96+
97+
it "respects ENV['ruby-saml/testing'] and does not log when set (legacy constant)" do
98+
logger = mock('Logger')
99+
OneLogin::RubySaml::Logging.logger = logger
100+
101+
ENV["ruby-saml/testing"] = "1"
102+
103+
# No expectations on logger; any call would cause Mocha to fail the test.
104+
OneLogin::RubySaml::Logging.debug('hi mom')
105+
OneLogin::RubySaml::Logging.info('sup?')
106+
OneLogin::RubySaml::Logging.warn('hey')
107+
OneLogin::RubySaml::Logging.error('oops')
108+
109+
ENV.delete("ruby-saml/testing")
110+
end
111+
end
61112
end
62113
end
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@
22

33
require_relative 'test_helper'
44

5-
class OneloginAliasTest < Minitest::Test
5+
class OneLoginRubySamlCompatTest < Minitest::Test
66

7-
describe 'legacy OneLogin namespace alias' do
7+
describe 'legacy OneLogin namespace compatibility' do
88

9-
describe 'equality with Object' do
10-
it "should be equal" do
11-
assert_equal OneLogin, Object
12-
assert_equal ::OneLogin, Object
13-
assert_equal OneLogin::RubySaml, OneLogin::RubySaml
9+
describe 'namespace equality' do
10+
it "defines OneLogin::RubySaml as an alias to the RubySaml module" do
11+
assert defined?(OneLogin::RubySaml), "Expected OneLogin::RubySaml to be defined"
1412
assert_equal ::OneLogin::RubySaml, ::RubySaml
13+
assert_equal RubySaml.object_id, OneLogin::RubySaml.object_id, "Expected OneLogin::RubySaml to alias RubySaml"
14+
end
15+
16+
it "exposes Logging under OneLogin::RubySaml::Logging as an alias to RubySaml::Logging" do
17+
assert defined?(OneLogin::RubySaml::Logging), "Expected OneLogin::RubySaml::Logging to be defined"
18+
assert_equal ::OneLogin::RubySaml::Logging, ::RubySaml::Logging
19+
assert_equal RubySaml::Logging.object_id, OneLogin::RubySaml::Logging.object_id, "Expected OneLogin::RubySaml::Logging to alias RubySaml::Logging"
20+
end
21+
22+
it "shares the same logger instance between RubySaml::Logging and OneLogin::RubySaml::Logging" do
23+
logger = mock("Logger")
24+
25+
RubySaml::Logging.logger = logger
26+
assert_same logger, OneLogin::RubySaml::Logging.logger
27+
28+
other_logger = mock("OtherLogger")
29+
OneLogin::RubySaml::Logging.logger = other_logger
30+
assert_same other_logger, RubySaml::Logging.logger
31+
ensure
32+
RubySaml::Logging.logger = ::TEST_LOGGER
1533
end
1634
end
1735

0 commit comments

Comments
 (0)