Skip to content

Commit 2949004

Browse files
committed
test_network_auth: Refactor not to monkeypatch network.auth.keyring
Instead, we can inherit from `MultiDomainBasicAuth` and override the `get_keyring` class method.
1 parent 184f64f commit 2949004

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

tests/unit/test_network_auth.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ def set_password(self, system, username, password):
9090
self.saved_passwords.append((system, username, password))
9191

9292

93+
class MDBA_KeyringV1(MultiDomainBasicAuth):
94+
@classmethod
95+
def get_keyring(cls):
96+
if hasattr(cls, '_keyring'):
97+
return cls._keyring
98+
99+
cls._keyring = KeyringModuleV1()
100+
return cls._keyring
101+
102+
93103
@pytest.mark.parametrize('url, expect', (
94104
("http://example.com/path1", (None, None)),
95105
# path1 URLs will be resolved by netloc
@@ -99,20 +109,16 @@ def set_password(self, system, username, password):
99109
("http://example.com/path2/path3", (None, None)),
100110
("http://[email protected]/path2/path3", ("foo", "foo!url")),
101111
))
102-
def test_keyring_get_password(monkeypatch, url, expect):
103-
keyring = KeyringModuleV1()
104-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
105-
auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"])
112+
def test_keyring_get_password(url, expect):
113+
auth = MDBA_KeyringV1(index_urls=["http://example.com/path2"])
106114

107115
actual = auth._get_new_credentials(url, allow_netrc=False,
108116
allow_keyring=True)
109117
assert actual == expect
110118

111119

112120
def test_keyring_get_password_after_prompt(monkeypatch):
113-
keyring = KeyringModuleV1()
114-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
115-
auth = MultiDomainBasicAuth()
121+
auth = MDBA_KeyringV1()
116122

117123
def ask_input(prompt):
118124
assert prompt == "User for example.com: "
@@ -124,9 +130,7 @@ def ask_input(prompt):
124130

125131

126132
def test_keyring_get_password_after_prompt_when_none(monkeypatch):
127-
keyring = KeyringModuleV1()
128-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
129-
auth = MultiDomainBasicAuth()
133+
auth = MDBA_KeyringV1()
130134

131135
def ask_input(prompt):
132136
assert prompt == "User for unknown.com: "
@@ -143,10 +147,8 @@ def ask_password(prompt):
143147
assert actual == ("user", "fake_password", True)
144148

145149

146-
def test_keyring_get_password_username_in_index(monkeypatch):
147-
keyring = KeyringModuleV1()
148-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
149-
auth = MultiDomainBasicAuth(index_urls=["http://[email protected]/path2"])
150+
def test_keyring_get_password_username_in_index():
151+
auth = MDBA_KeyringV1(index_urls=["http://[email protected]/path2"])
150152
get = functools.partial(
151153
auth._get_new_credentials,
152154
allow_netrc=False,
@@ -164,9 +166,7 @@ def test_keyring_get_password_username_in_index(monkeypatch):
164166
))
165167
def test_keyring_set_password(monkeypatch, response_status, creds,
166168
expect_save):
167-
keyring = KeyringModuleV1()
168-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
169-
auth = MultiDomainBasicAuth(prompting=True)
169+
auth = MDBA_KeyringV1(prompting=True)
170170
monkeypatch.setattr(auth, '_get_url_and_credentials',
171171
lambda u: (u, None, None))
172172
monkeypatch.setattr(auth, '_prompt_for_password', lambda *a: creds)
@@ -203,6 +203,7 @@ def _send(sent_req, **kwargs):
203203

204204
auth.handle_401(resp)
205205

206+
keyring = auth.get_keyring()
206207
if expect_save:
207208
assert keyring.saved_passwords == [("example.com", creds[0], creds[1])]
208209
else:
@@ -228,16 +229,23 @@ def get_credential(self, system, username):
228229
return None
229230

230231

232+
class MDBA_KeyringV2(MultiDomainBasicAuth):
233+
@classmethod
234+
def get_keyring(cls):
235+
if hasattr(cls, '_keyring'):
236+
return cls._keyring
237+
238+
cls._keyring = KeyringModuleV2()
239+
return cls._keyring
240+
241+
231242
@pytest.mark.parametrize('url, expect', (
232243
("http://example.com/path1", ("username", "netloc")),
233244
("http://example.com/path2/path3", ("username", "url")),
234245
("http://[email protected]/path2/path3", ("username", "url")),
235246
))
236-
def test_keyring_get_credential(monkeypatch, url, expect):
237-
monkeypatch.setattr(
238-
pip._internal.network.auth, 'keyring', KeyringModuleV2()
239-
)
240-
auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"])
247+
def test_keyring_get_credential(url, expect):
248+
auth = MDBA_KeyringV1(index_urls=["http://example.com/path2"])
241249

242250
assert auth._get_new_credentials(
243251
url, allow_netrc=False, allow_keyring=True
@@ -255,11 +263,19 @@ def get_credential(self, system, username):
255263
raise Exception("This keyring is broken!")
256264

257265

258-
def test_broken_keyring_disables_keyring(monkeypatch):
259-
keyring_broken = KeyringModuleBroken()
260-
monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring_broken)
266+
class MDBA_KeyringBroken(MultiDomainBasicAuth):
267+
@classmethod
268+
def get_keyring(cls):
269+
if hasattr(cls, '_keyring'):
270+
return cls._keyring
261271

272+
cls._keyring = KeyringModuleBroken()
273+
return cls._keyring
274+
275+
276+
def test_broken_keyring_disables_keyring(monkeypatch):
262277
auth = MultiDomainBasicAuth(index_urls=["http://example.com/"])
278+
keyring_broken = auth.get_keyring()
263279

264280
assert keyring_broken._call_count == 0
265281
for i in range(5):

0 commit comments

Comments
 (0)