|
| 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 | [
|
|
0 commit comments