Skip to content

Commit 0040b43

Browse files
nejchJohnVillalovos
authored andcommitted
feat(client): warn user on misconfigured URL in auth()
1 parent af21a18 commit 0040b43

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

gitlab/client.py

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

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

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

tests/functional/cli/test_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def test_private_token_overrides_job_token(
100100
# CLI first calls .auth() when private token is present
101101
resp_auth_with_token = copy.deepcopy(resp_get_project_with_token)
102102
resp_auth_with_token.update(url=f"{DEFAULT_URL}/api/v4/user")
103+
resp_auth_with_token["json"].update(username="user", web_url=f"{DEFAULT_URL}/user")
103104

104105
responses.add(**resp_get_project_with_token)
105106
responses.add(**resp_auth_with_token)

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)