1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
import pathlib
import pytest
import requests
import responses
from requests import PreparedRequest
from gitlab import Gitlab
from gitlab._backends import JobTokenAuth, OAuthTokenAuth, PrivateTokenAuth
from gitlab.config import GitlabConfigParser
@pytest.fixture
def netrc(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
netrc_file = tmp_path / ".netrc"
netrc_file.write_text("machine localhost login test password test")
monkeypatch.setenv("NETRC", str(netrc_file))
def test_invalid_auth_args():
with pytest.raises(ValueError):
Gitlab(
"http://localhost",
api_version="4",
private_token="private_token",
oauth_token="bearer",
)
with pytest.raises(ValueError):
Gitlab(
"http://localhost",
api_version="4",
oauth_token="bearer",
http_username="foo",
http_password="bar",
)
with pytest.raises(ValueError):
Gitlab(
"http://localhost",
api_version="4",
private_token="private_token",
http_password="bar",
)
with pytest.raises(ValueError):
Gitlab(
"http://localhost",
api_version="4",
private_token="private_token",
http_username="foo",
)
def test_private_token_auth():
gl = Gitlab("http://localhost", private_token="private_token", api_version="4")
p = PreparedRequest()
p.prepare(url=gl.url, auth=gl._auth)
assert gl.private_token == "private_token"
assert gl.oauth_token is None
assert gl.job_token is None
assert isinstance(gl._auth, PrivateTokenAuth)
assert gl._auth.token == "private_token"
assert p.headers["PRIVATE-TOKEN"] == "private_token"
assert "JOB-TOKEN" not in p.headers
assert "Authorization" not in p.headers
def test_oauth_token_auth():
gl = Gitlab("http://localhost", oauth_token="oauth_token", api_version="4")
p = PreparedRequest()
p.prepare(url=gl.url, auth=gl._auth)
assert gl.private_token is None
assert gl.oauth_token == "oauth_token"
assert gl.job_token is None
assert isinstance(gl._auth, OAuthTokenAuth)
assert gl._auth.token == "oauth_token"
assert p.headers["Authorization"] == "Bearer oauth_token"
assert "PRIVATE-TOKEN" not in p.headers
assert "JOB-TOKEN" not in p.headers
def test_job_token_auth():
gl = Gitlab("http://localhost", job_token="CI_JOB_TOKEN", api_version="4")
p = PreparedRequest()
p.prepare(url=gl.url, auth=gl._auth)
assert gl.private_token is None
assert gl.oauth_token is None
assert gl.job_token == "CI_JOB_TOKEN"
assert isinstance(gl._auth, JobTokenAuth)
assert gl._auth.token == "CI_JOB_TOKEN"
assert p.headers["JOB-TOKEN"] == "CI_JOB_TOKEN"
assert "PRIVATE-TOKEN" not in p.headers
assert "Authorization" not in p.headers
def test_http_auth():
gl = Gitlab(
"http://localhost",
http_username="foo",
http_password="bar",
api_version="4",
)
p = PreparedRequest()
p.prepare(url=gl.url, auth=gl._auth)
assert gl.private_token is None
assert gl.oauth_token is None
assert gl.job_token is None
assert isinstance(gl._auth, requests.auth.HTTPBasicAuth)
assert gl._auth.username == "foo"
assert gl._auth.password == "bar"
assert p.headers["Authorization"] == "Basic Zm9vOmJhcg=="
assert "PRIVATE-TOKEN" not in p.headers
assert "JOB-TOKEN" not in p.headers
@responses.activate
def test_with_no_auth_uses_netrc_file(netrc):
responses.get(
url="http://localhost/api/v4/test",
match=[
responses.matchers.header_matcher({"Authorization": "Basic dGVzdDp0ZXN0"})
],
)
gl = Gitlab("http://localhost")
gl.http_get("/test")
@responses.activate
def test_with_auth_ignores_netrc_file(netrc):
responses.get(
url="http://localhost/api/v4/test",
match=[responses.matchers.header_matcher({"Authorization": "Bearer test"})],
)
gl = Gitlab("http://localhost", oauth_token="test")
gl.http_get("/test")
@pytest.mark.parametrize(
"options,config,expected_private_token,expected_oauth_token,expected_job_token",
[
(
{
"private_token": "options-private-token",
"oauth_token": "options-oauth-token",
"job_token": "options-job-token",
},
{
"private_token": "config-private-token",
"oauth_token": "config-oauth-token",
"job_token": "config-job-token",
},
"options-private-token",
None,
None,
),
(
{
"private_token": None,
"oauth_token": "options-oauth-token",
"job_token": "options-job-token",
},
{
"private_token": "config-private-token",
"oauth_token": "config-oauth-token",
"job_token": "config-job-token",
},
"config-private-token",
None,
None,
),
(
{
"private_token": None,
"oauth_token": None,
"job_token": "options-job-token",
},
{
"private_token": "config-private-token",
"oauth_token": "config-oauth-token",
"job_token": "config-job-token",
},
"config-private-token",
None,
None,
),
(
{
"private_token": None,
"oauth_token": None,
"job_token": None,
},
{
"private_token": "config-private-token",
"oauth_token": "config-oauth-token",
"job_token": "config-job-token",
},
"config-private-token",
None,
None,
),
(
{
"private_token": None,
"oauth_token": None,
"job_token": None,
},
{
"private_token": None,
"oauth_token": "config-oauth-token",
"job_token": "config-job-token",
},
None,
"config-oauth-token",
None,
),
(
{
"private_token": None,
"oauth_token": None,
"job_token": None,
},
{
"private_token": None,
"oauth_token": None,
"job_token": "config-job-token",
},
None,
None,
"config-job-token",
),
],
)
def test_merge_auth(
options,
config,
expected_private_token,
expected_oauth_token,
expected_job_token,
):
cp = GitlabConfigParser()
cp.private_token = config["private_token"]
cp.oauth_token = config["oauth_token"]
cp.job_token = config["job_token"]
private_token, oauth_token, job_token = Gitlab._merge_auth(options, cp)
assert private_token == expected_private_token
assert oauth_token == expected_oauth_token
assert job_token == expected_job_token
|