Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 02dace8

Browse files
committed
updated tests
1 parent a8efb6b commit 02dace8

2 files changed

Lines changed: 5 additions & 80 deletions

File tree

google/auth/crypt/rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, private_key, key_id=None):
100100
if module_str.startswith(RSA_KEY_MODULE_PREFIX):
101101
impl_lib = _python_rsa
102102
elif module_str.startswith(CRYPTOGRAPHY_KEY_MODULE_PREFIX):
103-
impl_lib = _cryptography_rs
103+
impl_lib = _cryptography_rsa
104104
else:
105105
raise ValueError(f"unrecognized private key type: {private_key}")
106106
self._impl = impl_lib.RSASigner(private_key, key_id=key_id)

tests/crypt/test_rsa.py

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import pytest
2121
import rsa as rsa_lib
2222

23-
from google.auth import exceptions
2423
from google.auth.crypt import _cryptography_rsa
2524
from google.auth.crypt import _python_rsa
2625
from google.auth.crypt import rsa
@@ -64,17 +63,6 @@ def test_init_with_unknown_key(self):
6463
with pytest.raises(ValueError):
6564
rsa.RSAVerifier(unknown_key)
6665

67-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
68-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
69-
def test_init_with_missing_dependencies(self):
70-
with pytest.raises(exceptions.MissingOptionalDependencyError) as e:
71-
rsa.RSAVerifier(RSA_PUBLIC_KEY)
72-
assert "RSAVerifier requires `cryptography`" in str(e)
73-
assert (
74-
"`rsa` is also supported for legacy compatibility, but is deprecated"
75-
in str(e)
76-
)
77-
7866
def test_verify_delegates(self):
7967
verifier = rsa.RSAVerifier(CRYPTOGRAPHY_PUBLIC_KEY)
8068

@@ -87,7 +75,6 @@ def test_verify_delegates(self):
8775
mock_verify.assert_called_once_with(b"message", b"signature")
8876

8977
@mock.patch("google.auth.crypt.rsa._cryptography_rsa")
90-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
9178
def test_from_string_cryptography(self, mock_crypto):
9279
expected_verifier = mock.Mock()
9380
mock_crypto.RSAVerifier.from_string.return_value = expected_verifier
@@ -97,30 +84,6 @@ def test_from_string_cryptography(self, mock_crypto):
9784
assert result == expected_verifier
9885
mock_crypto.RSAVerifier.from_string.assert_called_once_with(PUBLIC_KEY_BYTES)
9986

100-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
101-
@mock.patch("google.auth.crypt.rsa._python_rsa")
102-
def test_from_string_python_rsa(self, mock_python_rsa):
103-
expected_verifier = mock.Mock()
104-
mock_python_rsa.RSAVerifier.from_string.return_value = expected_verifier
105-
106-
result = rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
107-
108-
assert result == expected_verifier
109-
mock_python_rsa.RSAVerifier.from_string.assert_called_once_with(
110-
PUBLIC_KEY_BYTES
111-
)
112-
113-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
114-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
115-
def test_from_string_missing_deps(self):
116-
with pytest.raises(exceptions.MissingOptionalDependencyError) as e:
117-
rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
118-
assert "RSAVerifier requires `cryptography`" in str(e)
119-
assert (
120-
"`rsa` is also supported for legacy compatibility, but is deprecated"
121-
in str(e)
122-
)
123-
12487

12588
class TestRSASigner:
12689
def test_init_with_cryptography_key(self):
@@ -145,17 +108,6 @@ def test_init_with_unknown_key(self):
145108
with pytest.raises(ValueError):
146109
rsa.RSASigner(unknown_key)
147110

148-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
149-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
150-
def test_init_with_missing_dependencies(self):
151-
with pytest.raises(exceptions.MissingOptionalDependencyError) as e:
152-
rsa.RSASigner(RSA_PRIVATE_KEY)
153-
assert "RSASigner requires `cryptography`" in str(e)
154-
assert (
155-
"`rsa` is also supported for legacy compatibility, but is deprecated"
156-
in str(e)
157-
)
158-
159111
def test_sign_delegates(self):
160112
signer = rsa.RSASigner(RSA_PRIVATE_KEY)
161113

@@ -167,7 +119,6 @@ def test_sign_delegates(self):
167119
mock_sign.assert_called_once_with(b"message")
168120

169121
@mock.patch("google.auth.crypt.rsa._cryptography_rsa")
170-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
171122
def test_from_string_delegates_to_cryptography(self, mock_crypto):
172123
expected_signer = mock.Mock()
173124
mock_crypto.RSASigner.from_string.return_value = expected_signer
@@ -179,31 +130,6 @@ def test_from_string_delegates_to_cryptography(self, mock_crypto):
179130
PRIVATE_KEY_BYTES, key_id="123"
180131
)
181132

182-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
183-
@mock.patch("google.auth.crypt.rsa._python_rsa")
184-
def test_from_string_delegates_to_python_rsa(self, mock_python_rsa):
185-
expected_signer = mock.Mock()
186-
mock_python_rsa.RSASigner.from_string.return_value = expected_signer
187-
188-
result = rsa.RSASigner.from_string(PRIVATE_KEY_BYTES, key_id="123")
189-
190-
assert result == expected_signer
191-
mock_python_rsa.RSASigner.from_string.assert_called_once_with(
192-
PRIVATE_KEY_BYTES, key_id="123"
193-
)
194-
195-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
196-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
197-
def test_from_string_missing_deps(self):
198-
with pytest.raises(exceptions.MissingOptionalDependencyError) as e:
199-
rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
200-
assert "RSASigner requires `cryptography`" in str(e)
201-
assert (
202-
"`rsa` is also supported for legacy compatibility, but is deprecated"
203-
in str(e)
204-
)
205-
206-
@mock.patch("google.auth.crypt.rsa._python_rsa", None)
207133
def test_end_to_end_cryptography_lib(self):
208134
signer = rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
209135
message = b"Hello World"
@@ -214,13 +140,12 @@ def test_end_to_end_cryptography_lib(self):
214140
assert isinstance(verifier, _cryptography_rsa.RSAVerifier)
215141
assert isinstance(signer, _cryptography_rsa.RSASigner)
216142

217-
@mock.patch("google.auth.crypt.rsa._cryptography_rsa", None)
218143
def test_end_to_end_rsa_lib(self):
219-
signer = rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
144+
signer = rsa.RSASigner(RSA_PRIVATE_KEY)
220145
message = b"Hello World"
221146
sig = signer.sign(message)
222-
verifier = rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
147+
verifier = rsa.RSAVerifier(RSA_PUBLIC_KEY)
223148
result = verifier.verify(message, sig)
224149
assert bool(result) is True
225-
assert isinstance(verifier, _python_rsa.RSAVerifier)
226-
assert isinstance(signer, _python_rsa.RSASigner)
150+
assert isinstance(verifier._impl, _python_rsa.RSAVerifier)
151+
assert isinstance(signer._impl, _python_rsa.RSASigner)

0 commit comments

Comments
 (0)