Skip to content

Commit 6f5b0e7

Browse files
committed
feat(client): warn user on misconfigured URL in auth()
1 parent 31c9c2f commit 6f5b0e7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

gitlab/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,14 @@ def _merge_auth(
363363
return (None, None, None)
364364

365365
def auth(self) -> None:
366-
"""Performs an authentication using private token.
366+
"""Performs an authentication using private token. Warns the user if a
367+
potentially misconfigured URL is detected on the client or server side.
367368
368369
The `user` attribute will hold a `gitlab.objects.CurrentUser` object on
369370
success.
370371
"""
371372
self.user = self._objects.CurrentUserManager(self).get()
373+
self._check_url(self.user.web_url, path=self.user.username)
372374

373375
def version(self) -> Tuple[str, str]:
374376
"""Returns the version and revision of the gitlab server.

tests/unit/test_gitlab.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ def resp_get_user():
3737
return {
3838
"method": responses.GET,
3939
"url": "http://localhost/api/v4/user",
40-
"json": {"id": 1, "username": "username"},
40+
"json": {
41+
"id": 1,
42+
"username": "username",
43+
"web_url": "http://localhost/username",
44+
},
4145
"content_type": "application/json",
4246
"status": 200,
4347
}
@@ -254,6 +258,24 @@ def test_gitlab_token_auth(gl, resp_get_user):
254258
assert isinstance(gl.user, gitlab.v4.objects.CurrentUser)
255259

256260

261+
@responses.activate
262+
def test_gitlab_auth_with_mismatching_url_warns():
263+
responses.add(
264+
method=responses.GET,
265+
url="http://first.example.com/api/v4/user",
266+
json={
267+
"username": "test-user",
268+
"web_url": "http://second.example.com/test-user",
269+
},
270+
content_type="application/json",
271+
status=200,
272+
)
273+
gl = gitlab.Gitlab("http://first.example.com")
274+
275+
with pytest.warns(UserWarning):
276+
gl.auth()
277+
278+
257279
def test_gitlab_default_url():
258280
gl = gitlab.Gitlab()
259281
assert gl.url == gitlab.const.DEFAULT_URL

0 commit comments

Comments
 (0)