Skip to content

Commit 89a8dbf

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 89a8dbf

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

tests/unit/test_network_auth.py

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

9292

93+
class MDBA_KeyringV1(MultiDomainBasicAuth):
94+
_keyring = KeyringModuleV1()
95+
96+
9397
@pytest.mark.parametrize('url, expect', (
9498
("http://example.com/path1", (None, None)),
9599
# path1 URLs will be resolved by netloc
@@ -99,20 +103,16 @@ def set_password(self, system, username, password):
99103
("http://example.com/path2/path3", (None, None)),
100104
("http://[email protected]/path2/path3", ("foo", "foo!url")),
101105
))
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"])
106+
def test_keyring_get_password(url, expect):
107+
auth = MDBA_KeyringV1(index_urls=["http://example.com/path2"])
106108

107109
actual = auth._get_new_credentials(url, allow_netrc=False,
108110
allow_keyring=True)
109111
assert actual == expect
110112

111113

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

117117
def ask_input(prompt):
118118
assert prompt == "User for example.com: "
@@ -124,9 +124,7 @@ def ask_input(prompt):
124124

125125

126126
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()
127+
auth = MDBA_KeyringV1()
130128

131129
def ask_input(prompt):
132130
assert prompt == "User for unknown.com: "
@@ -143,10 +141,8 @@ def ask_password(prompt):
143141
assert actual == ("user", "fake_password", True)
144142

145143

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"])
144+
def test_keyring_get_password_username_in_index():
145+
auth = MDBA_KeyringV1(index_urls=["http://[email protected]/path2"])
150146
get = functools.partial(
151147
auth._get_new_credentials,
152148
allow_netrc=False,
@@ -164,9 +160,7 @@ def test_keyring_get_password_username_in_index(monkeypatch):
164160
))
165161
def test_keyring_set_password(monkeypatch, response_status, creds,
166162
expect_save):
167-
keyring = KeyringModuleV1()
168-
monkeypatch.setattr('pip._internal.network.auth.keyring', keyring)
169-
auth = MultiDomainBasicAuth(prompting=True)
163+
auth = MDBA_KeyringV1(prompting=True)
170164
monkeypatch.setattr(auth, '_get_url_and_credentials',
171165
lambda u: (u, None, None))
172166
monkeypatch.setattr(auth, '_prompt_for_password', lambda *a: creds)
@@ -203,6 +197,7 @@ def _send(sent_req, **kwargs):
203197

204198
auth.handle_401(resp)
205199

200+
keyring = auth.get_keyring()
206201
if expect_save:
207202
assert keyring.saved_passwords == [("example.com", creds[0], creds[1])]
208203
else:
@@ -228,16 +223,17 @@ def get_credential(self, system, username):
228223
return None
229224

230225

226+
class MDBA_KeyringV2(MultiDomainBasicAuth):
227+
_keyring = KeyringModuleV2()
228+
229+
231230
@pytest.mark.parametrize('url, expect', (
232231
("http://example.com/path1", ("username", "netloc")),
233232
("http://example.com/path2/path3", ("username", "url")),
234233
("http://[email protected]/path2/path3", ("username", "url")),
235234
))
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"])
235+
def test_keyring_get_credential(url, expect):
236+
auth = MDBA_KeyringV1(index_urls=["http://example.com/path2"])
241237

242238
assert auth._get_new_credentials(
243239
url, allow_netrc=False, allow_keyring=True
@@ -255,11 +251,13 @@ def get_credential(self, system, username):
255251
raise Exception("This keyring is broken!")
256252

257253

258-
def test_broken_keyring_disables_keyring(monkeypatch):
259-
keyring_broken = KeyringModuleBroken()
260-
monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring_broken)
254+
class MDBA_KeyringBroken(MultiDomainBasicAuth):
255+
_keyring = KeyringModuleBroken()
261256

257+
258+
def test_broken_keyring_disables_keyring(monkeypatch):
262259
auth = MultiDomainBasicAuth(index_urls=["http://example.com/"])
260+
keyring_broken = auth.get_keyring()
263261

264262
assert keyring_broken._call_count == 0
265263
for i in range(5):

0 commit comments

Comments
 (0)