| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5f46cfd commit 45b8930
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,11 +44,11 @@ properly closed when you exit a ``with`` block: | |||
| 44 | 44 | netrc authentication | |
| 45 | 45 | -------------------- | |
| 46 | 46 | ||
| 47 | - python-gitlab reads credentials from ``.netrc`` files via the ``requests`` backend by default, | ||
| 48 | - which may override authentication headers you set on your client. | ||
| 47 | + python-gitlab reads credentials from ``.netrc`` files via the ``requests`` backend | ||
| 48 | + only if you do not provide any other type of authentication yourself. | ||
| 49 | 49 | ||
| 50 | - For more granular control, you can disable this `Using a custom session`_ | ||
| 51 | - and explicitly setting ``trust_env=False`` as described in the ``requests`` documentation. | ||
| 50 | + If you'd like to disable reading netrc files altogether, you can follow `Using a custom session`_ | ||
| 51 | + and explicitly set ``trust_env=False`` as described in the ``requests`` documentation. | ||
| 52 | 52 | ||
| 53 | 53 | .. code-block:: python | |
| 54 | 54 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,22 @@ | |||
| 1 | + import pathlib | ||
| 2 | + | ||
| 1 | 3 | import pytest | |
| 2 | 4 | import requests | |
| 5 | + import responses | ||
| 3 | 6 | from requests import PreparedRequest | |
| 4 | 7 | ||
| 5 | 8 | from gitlab import Gitlab | |
| 6 | 9 | from gitlab._backends import JobTokenAuth, OAuthTokenAuth, PrivateTokenAuth | |
| 7 | 10 | from gitlab.config import GitlabConfigParser | |
| 8 | 11 | ||
| 9 | 12 | ||
| 13 | + @pytest.fixture | ||
| 14 | + def netrc(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path): | ||
| 15 | + netrc_file = tmp_path / ".netrc" | ||
| 16 | + netrc_file.write_text("machine localhost login test password test") | ||
| 17 | + monkeypatch.setenv("NETRC", str(netrc_file)) | ||
| 18 | + | ||
| 19 | + | ||
| 10 | 20 | def test_invalid_auth_args(): | |
| 11 | 21 | with pytest.raises(ValueError): | |
| 12 | 22 | Gitlab( | |
@@ -101,6 +111,30 @@ def test_http_auth(): | |||
| 101 | 111 | assert "JOB-TOKEN" not in p.headers | |
| 102 | 112 | ||
| 103 | 113 | ||
| 114 | + @responses.activate | ||
| 115 | + def test_with_no_auth_uses_netrc_file(netrc): | ||
| 116 | + responses.get( | ||
| 117 | + url="http://localhost/api/v4/test", | ||
| 118 | + match=[ | ||
| 119 | + responses.matchers.header_matcher({"Authorization": "Basic dGVzdDp0ZXN0"}) | ||
| 120 | + ], | ||
| 121 | + ) | ||
| 122 | + | ||
| 123 | + gl = Gitlab("http://localhost") | ||
| 124 | + gl.http_get("/test") | ||
| 125 | + | ||
| 126 | + | ||
| 127 | + @responses.activate | ||
| 128 | + def test_with_auth_ignores_netrc_file(netrc): | ||
| 129 | + responses.get( | ||
| 130 | + url="http://localhost/api/v4/test", | ||
| 131 | + match=[responses.matchers.header_matcher({"Authorization": "Bearer test"})], | ||
| 132 | + ) | ||
| 133 | + | ||
| 134 | + gl = Gitlab("http://localhost", oauth_token="test") | ||
| 135 | + gl.http_get("/test") | ||
| 136 | + | ||
| 137 | + | ||
| 104 | 138 | @pytest.mark.parametrize( | |
| 105 | 139 | "options,config,expected_private_token,expected_oauth_token,expected_job_token", | |
| 106 | 140 | [ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments