diff options
author | Nejc Habjan <hab.nejc@gmail.com> | 2021-01-31 17:58:20 +0100 |
---|---|---|
committer | Nejc Habjan <hab.nejc@gmail.com> | 2021-01-31 18:15:12 +0100 |
commit | c5a37e7e37a62372c250dfc8c0799e847eecbc30 (patch) | |
tree | 0c006b3e087a86b75d810cb2983d0c0acb6f0edd | |
parent | 4bb201b92ef0dcc14a7a9c83e5600ba5b118fc33 (diff) | |
download | gitlab-c5a37e7e37a62372c250dfc8c0799e847eecbc30.tar.gz |
test(api,cli): add tests for custom user agent
-rw-r--r-- | gitlab/tests/test_config.py | 33 | ||||
-rw-r--r-- | gitlab/tests/test_gitlab.py | 15 |
2 files changed, 46 insertions, 2 deletions
diff --git a/gitlab/tests/test_config.py b/gitlab/tests/test_config.py index 7fb03e0..7a9e239 100644 --- a/gitlab/tests/test_config.py +++ b/gitlab/tests/test_config.py @@ -21,10 +21,12 @@ import unittest import mock import io -from gitlab import config +from gitlab import config, USER_AGENT import pytest +custom_user_agent = "my-package/1.0.0" + valid_config = u"""[global] default = one ssl_verify = true @@ -51,6 +53,17 @@ url = https://four.url oauth_token = STUV """ +custom_user_agent_config = """[global] +default = one +user_agent = {} + +[one] +url = http://one.url +private_token = ABCDEF +""".format( + custom_user_agent +) + no_default_config = u"""[global] [there] url = http://there.url @@ -178,3 +191,21 @@ def test_valid_data(m_open, path_exists): assert "STUV" == cp.oauth_token assert 2 == cp.timeout assert True == cp.ssl_verify + + +@mock.patch("os.path.exists") +@mock.patch("builtins.open") +@pytest.mark.parametrize( + "config_string,expected_agent", + [ + (valid_config, USER_AGENT), + (custom_user_agent_config, custom_user_agent), + ], +) +def test_config_user_agent(m_open, path_exists, config_string, expected_agent): + fd = io.StringIO(config_string) + fd.close = mock.Mock(return_value=None) + m_open.return_value = fd + + cp = config.GitlabConfigParser() + assert cp.user_agent == expected_agent diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 553afb3..4a82207 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -18,9 +18,10 @@ import pickle +import pytest from httmock import HTTMock, response, urlmatch, with_httmock # noqa -from gitlab import Gitlab, GitlabList +from gitlab import Gitlab, GitlabList, USER_AGENT from gitlab.v4.objects import CurrentUser @@ -139,3 +140,15 @@ def test_gitlab_subclass_from_config(default_config): config_path = default_config gl = MyGitlab.from_config("one", [config_path]) assert isinstance(gl, MyGitlab) + + +@pytest.mark.parametrize( + "kwargs,expected_agent", + [ + ({}, USER_AGENT), + ({"user_agent": "my-package/1.0.0"}, "my-package/1.0.0"), + ], +) +def test_gitlab_user_agent(kwargs, expected_agent): + gl = Gitlab("http://localhost", **kwargs) + assert gl.headers["User-Agent"] == expected_agent |