Skip to content

Commit cb9bd61

Browse files
CopilotSubterrane
andcommitted
Add SAML2 compatibility module with helpful error messages
Co-authored-by: Subterrane <5290140+Subterrane@users.noreply.github.com>
1 parent 62b1918 commit cb9bd61

File tree

8 files changed

+124
-36
lines changed

8 files changed

+124
-36
lines changed

=2.0.2

Whitespace-only changes.

=2.11.0

Whitespace-only changes.

=2.5.3

Whitespace-only changes.

=3.1.11

Whitespace-only changes.

=67.7.1

Lines changed: 0 additions & 36 deletions
This file was deleted.

onelogin/saml2/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# coding: utf-8
2+
3+
"""
4+
SAML2 Authentication Module
5+
6+
This module provides compatibility for users expecting to import
7+
OneLogin_Saml2_Auth from onelogin.saml2.auth. However, OneLogin_Saml2_Auth is
8+
actually provided by the 'python3-saml' package, not this onelogin package.
9+
10+
This onelogin package is for OneLogin API management, while python3-saml is
11+
for SAML2 authentication integration.
12+
"""

onelogin/saml2/auth.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# coding: utf-8
2+
3+
"""
4+
SAML2 Authentication Module
5+
6+
OneLogin_Saml2_Auth is not part of this package. This package (onelogin) is
7+
for OneLogin API management. For SAML2 authentication, you need the
8+
'python3-saml' package.
9+
10+
To fix this import error:
11+
12+
1. Install the correct package:
13+
pip install python3-saml
14+
15+
2. Import from the correct module:
16+
from onelogin.saml2.auth import OneLogin_Saml2_Auth
17+
18+
OR use the newer syntax:
19+
from onelogin.saml2.auth import Auth as OneLogin_Saml2_Auth
20+
21+
For more information, see: https://github.com/onelogin/python3-saml
22+
"""
23+
24+
25+
class ImportError(Exception):
26+
"""Custom import error with helpful message"""
27+
pass
28+
29+
30+
def __getattr__(name):
31+
"""
32+
Intercept attempts to import OneLogin_Saml2_Auth and provide helpful
33+
error message.
34+
"""
35+
if name == "OneLogin_Saml2_Auth":
36+
raise ImportError(
37+
"OneLogin_Saml2_Auth is not available in this package.\n\n"
38+
"This package (onelogin) is for OneLogin API management.\n"
39+
"For SAML2 authentication, you need the 'python3-saml' "
40+
"package.\n\n"
41+
"To fix this:\n"
42+
"1. Install the correct package: pip install python3-saml\n"
43+
"2. Import from: from onelogin.saml2.auth import "
44+
"OneLogin_Saml2_Auth\n\n"
45+
"Note: You may need to uninstall this 'onelogin' package if you "
46+
"only need SAML2 auth:\n"
47+
"pip uninstall onelogin\n\n"
48+
"For more information: https://github.com/onelogin/python3-saml"
49+
)
50+
51+
raise AttributeError(
52+
f"module 'onelogin.saml2.auth' has no attribute '{name}'"
53+
)
54+
55+
56+
# Make module attributes available for inspection
57+
__all__ = []
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# coding: utf-8
2+
3+
"""
4+
Test import behavior for SAML2 compatibility module
5+
"""
6+
7+
import pytest
8+
from onelogin.saml2.auth import ImportError
9+
10+
11+
class TestSaml2ImportCompatibility:
12+
"""Test the SAML2 import compatibility module"""
13+
14+
def test_import_onelogin_saml2_auth_raises_helpful_error(self):
15+
"""Test that importing OneLogin_Saml2_Auth raises a helpful error message"""
16+
17+
# Attempt to import OneLogin_Saml2_Auth
18+
with pytest.raises(ImportError) as exc_info:
19+
from onelogin.saml2.auth import OneLogin_Saml2_Auth
20+
21+
# Verify the error message contains helpful guidance
22+
error_message = str(exc_info.value)
23+
assert "OneLogin_Saml2_Auth is not available in this package" in error_message
24+
assert "python3-saml" in error_message
25+
assert "pip install python3-saml" in error_message
26+
assert "https://github.com/onelogin/python3-saml" in error_message
27+
28+
def test_getattr_for_nonexistent_attribute_raises_attribute_error(self):
29+
"""Test that accessing non-existent attributes raises AttributeError"""
30+
31+
import onelogin.saml2.auth
32+
with pytest.raises(AttributeError) as exc_info:
33+
getattr(onelogin.saml2.auth, 'NonExistentClass')
34+
35+
error_message = str(exc_info.value)
36+
assert "module 'onelogin.saml2.auth' has no attribute 'NonExistentClass'" in error_message
37+
38+
def test_saml2_module_can_be_imported(self):
39+
"""Test that the saml2 module itself can be imported without error"""
40+
41+
import onelogin.saml2
42+
# Should not raise any errors
43+
44+
def test_saml2_auth_module_can_be_imported(self):
45+
"""Test that the saml2.auth module itself can be imported without error"""
46+
47+
import onelogin.saml2.auth
48+
# Should not raise any errors
49+
50+
def test_saml2_auth_module_has_empty_all_list(self):
51+
"""Test that __all__ is defined and empty"""
52+
53+
import onelogin.saml2.auth
54+
assert hasattr(onelogin.saml2.auth, '__all__')
55+
assert onelogin.saml2.auth.__all__ == []

0 commit comments

Comments
 (0)