def test_keyring_get_credential(monkeypatch, url, expect): monkeypatch.setattr(pip._internal.download, 'keyring', KeyringModuleV2()) auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"]) assert auth._get_new_credentials(url, allow_netrc=False, allow_keyring=True) \ == expect
def test_keyring_get_credential(monkeypatch, url, expect): monkeypatch.setattr(pip._internal.download, 'keyring', KeyringModuleV2()) auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"]) assert auth._get_new_credentials(url, allow_netrc=False, allow_keyring=True) \ == expect
def test_get_credentials_uses_cached_credentials(): auth = MultiDomainBasicAuth() auth.passwords['example.com'] = ('user', 'pass') got = auth._get_url_and_credentials("http://*****:*****@example.com/path") expected = ('http://example.com/path', 'user', 'pass') assert got == expected
def test_parse_credentials(): auth = MultiDomainBasicAuth() assert auth.parse_credentials("foo:[email protected]") == ('foo', 'bar') assert auth.parse_credentials("*****@*****.**") == ('foo', None) assert auth.parse_credentials("example.com") == (None, None) # URL-encoded reserved characters: assert auth.parse_credentials("user%3Aname:%23%40%[email protected]") \ == ("user:name", "#@^")
def test_keyring_get_password_after_prompt(monkeypatch): monkeypatch.setattr('pip._internal.download.keyring', KeyringModuleV1()) auth = MultiDomainBasicAuth() def ask_input(prompt): assert prompt == "User for example.com: " return "user" monkeypatch.setattr('pip._internal.download.ask_input', ask_input) actual = auth._prompt_for_password("example.com") assert actual == ("user", "user!netloc", False)
def test_keyring_get_password_after_prompt(monkeypatch): monkeypatch.setattr('pip._internal.download.keyring', KeyringModuleV1()) auth = MultiDomainBasicAuth() def ask_input(prompt): assert prompt == "User for example.com: " return "user" monkeypatch.setattr('pip._internal.download.ask_input', ask_input) actual = auth._prompt_for_password("example.com") assert actual == ("user", "user!netloc", False)
def test_keyring_set_password(monkeypatch, response_status, creds, expect_save): keyring = KeyringModuleV1() monkeypatch.setattr('pip._internal.download.keyring', keyring) auth = MultiDomainBasicAuth(prompting=True) monkeypatch.setattr(auth, '_get_url_and_credentials', lambda u: (u, None, None)) monkeypatch.setattr(auth, '_prompt_for_password', lambda *a: creds) if creds[2]: # when _prompt_for_password indicates to save, we should save def should_save_password_to_keyring(*a): return True else: # when _prompt_for_password indicates not to save, we should # never call this function def should_save_password_to_keyring(*a): assert False, ("_should_save_password_to_keyring should not be " + "called") monkeypatch.setattr(auth, '_should_save_password_to_keyring', should_save_password_to_keyring) req = MockRequest("https://example.com") resp = MockResponse(b"") resp.url = req.url connection = MockConnection() def _send(sent_req, **kwargs): assert sent_req is req assert "Authorization" in sent_req.headers r = MockResponse(b"") r.status_code = response_status return r connection._send = _send resp.request = req resp.status_code = 401 resp.connection = connection auth.handle_401(resp) if expect_save: assert keyring.saved_passwords == [("example.com", creds[0], creds[1])] else: assert keyring.saved_passwords == []
def test_keyring_set_password(monkeypatch, response_status, creds, expect_save): keyring = KeyringModuleV1() monkeypatch.setattr('pip._internal.download.keyring', keyring) auth = MultiDomainBasicAuth(prompting=True) monkeypatch.setattr(auth, '_get_url_and_credentials', lambda u: (u, None, None)) monkeypatch.setattr(auth, '_prompt_for_password', lambda *a: creds) if creds[2]: # when _prompt_for_password indicates to save, we should save def should_save_password_to_keyring(*a): return True else: # when _prompt_for_password indicates not to save, we should # never call this function def should_save_password_to_keyring(*a): assert False, ("_should_save_password_to_keyring should not be " + "called") monkeypatch.setattr(auth, '_should_save_password_to_keyring', should_save_password_to_keyring) req = MockRequest("https://example.com") resp = MockResponse(b"") resp.url = req.url connection = MockConnection() def _send(sent_req, **kwargs): assert sent_req is req assert "Authorization" in sent_req.headers r = MockResponse(b"") r.status_code = response_status return r connection._send = _send resp.request = req resp.status_code = 401 resp.connection = connection auth.handle_401(resp) if expect_save: assert keyring.saved_passwords == [("example.com", creds[0], creds[1])] else: assert keyring.saved_passwords == []
def test_keyring_get_password_username_in_index(monkeypatch): monkeypatch.setattr('pip._internal.download.keyring', KeyringModuleV1()) auth = MultiDomainBasicAuth(index_urls=["http://[email protected]/path2"]) get = functools.partial(auth._get_new_credentials, allow_netrc=False, allow_keyring=True) assert get("http://example.com/path2/path3") == ("user", "user!url") assert get("http://example.com/path4/path1") == (None, None)
def test_get_index_url_credentials(): auth = MultiDomainBasicAuth(index_urls=["http://*****:*****@example.com/path"]) get = functools.partial(auth._get_new_credentials, allow_netrc=False, allow_keyring=False) # Check resolution of indexes assert get("http://example.com/path/path2") == ('foo', 'bar') assert get("http://example.com/path3/path2") == (None, None)
def test_get_credentials_parses_correctly(input_url, url, username, password): auth = MultiDomainBasicAuth() get = auth._get_url_and_credentials # Check URL parsing assert get(input_url) == (url, username, password) assert ( # There are no credentials in the URL (username is None and password is None) or # Credentials were found and "cached" appropriately auth.passwords['example.com'] == (username, password))
def test_get_credentials(): auth = MultiDomainBasicAuth() get = auth._get_url_and_credentials # Check URL parsing assert get("http://*****:*****@example.com/path") \ == ('http://example.com/path', 'foo', 'bar') assert auth.passwords['example.com'] == ('foo', 'bar') auth.passwords['example.com'] = ('user', 'pass') assert get("http://*****:*****@example.com/path") \ == ('http://example.com/path', 'user', 'pass')
def test_parse_credentials(): auth = MultiDomainBasicAuth() assert auth.parse_credentials("foo:[email protected]") == ('foo', 'bar') assert auth.parse_credentials("*****@*****.**") == ('foo', None) assert auth.parse_credentials("example.com") == (None, None) # URL-encoded reserved characters: assert auth.parse_credentials("user%3Aname:%23%40%[email protected]") \ == ("user:name", "#@^")
def test_get_env_credentials_only_password(request, caplog): def reset_env(): del os.environ['PIP_PASSWORD'] request.addfinalizer(reset_env) os.environ['PIP_PASSWORD'] = '******' auth = MultiDomainBasicAuth() get = functools.partial(auth._get_new_credentials, allow_netrc=False, allow_keyring=False) # Check resolution of credentials from env assert get("http://envexample.com/pathx") == (None, None) assert "but not 'PIP_USERNAME'" in caplog.text
def test_get_env_credentials(request, caplog): def reset_env(): del os.environ['PIP_USERNAME'] del os.environ['PIP_PASSWORD'] request.addfinalizer(reset_env) os.environ['PIP_USERNAME'] = '******' os.environ['PIP_PASSWORD'] = '******' with caplog.at_level(logging.DEBUG): auth = MultiDomainBasicAuth() get = functools.partial(auth._get_new_credentials, allow_netrc=False, allow_keyring=False) # Check resolution of credentials from env assert get("http://envexample.com/pathx") == ('foo', 'bar') assert "Found credentials in environment" in caplog.text