diff options
author | Nejc Habjan <hab.nejc@gmail.com> | 2021-11-27 20:28:15 +0100 |
---|---|---|
committer | Nejc Habjan <hab.nejc@gmail.com> | 2021-11-27 21:08:47 +0100 |
commit | 6b892e3dcb18d0f43da6020b08fd4ba891da3670 (patch) | |
tree | e97beaaca3b5b94764f78e0fd99df0c53fd19f8b /tests/functional | |
parent | 70b9870f929c4db32fd2e1406db2122de9958bfd (diff) | |
download | gitlab-test/cli-coverage.tar.gz |
test(cli): improve basic CLI coveragetest/cli-coverage
Diffstat (limited to 'tests/functional')
-rw-r--r-- | tests/functional/api/test_users.py | 12 | ||||
-rw-r--r-- | tests/functional/cli/test_cli.py | 49 | ||||
-rw-r--r-- | tests/functional/conftest.py | 20 | ||||
-rw-r--r-- | tests/functional/fixtures/invalid_auth.cfg | 3 | ||||
-rw-r--r-- | tests/functional/fixtures/invalid_version.cfg | 3 |
5 files changed, 68 insertions, 19 deletions
diff --git a/tests/functional/api/test_users.py b/tests/functional/api/test_users.py index 1ef237c..edbbca1 100644 --- a/tests/functional/api/test_users.py +++ b/tests/functional/api/test_users.py @@ -3,23 +3,17 @@ GitLab API: https://docs.gitlab.com/ee/api/users.html https://docs.gitlab.com/ee/api/users.html#delete-authentication-identity-from-user """ -import pytest import requests -@pytest.fixture(scope="session") -def avatar_path(test_dir): - return test_dir / "fixtures" / "avatar.png" - - -def test_create_user(gl, avatar_path): +def test_create_user(gl, fixture_dir): user = gl.users.create( { "email": "foo@bar.com", "username": "foo", "name": "foo", "password": "foo_password", - "avatar": open(avatar_path, "rb"), + "avatar": open(fixture_dir / "avatar.png", "rb"), } ) @@ -29,7 +23,7 @@ def test_create_user(gl, avatar_path): avatar_url = user.avatar_url.replace("gitlab.test", "localhost:8080") uploaded_avatar = requests.get(avatar_url).content - assert uploaded_avatar == open(avatar_path, "rb").read() + assert uploaded_avatar == open(fixture_dir / "avatar.png", "rb").read() def test_block_user(gl, user): diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py new file mode 100644 index 0000000..c4e76a7 --- /dev/null +++ b/tests/functional/cli/test_cli.py @@ -0,0 +1,49 @@ +import json + +from gitlab import __version__ + + +def test_main_entrypoint(script_runner, gitlab_config): + ret = script_runner.run("python", "-m", "gitlab", "--config-file", gitlab_config) + assert ret.returncode == 2 + + +def test_version(script_runner): + ret = script_runner.run("gitlab", "--version") + assert ret.stdout.strip() == __version__ + + +def test_invalid_config(script_runner): + ret = script_runner.run("gitlab", "--gitlab", "invalid") + assert not ret.success + assert not ret.stdout + + +def test_invalid_config_prints_help(script_runner): + ret = script_runner.run("gitlab", "--gitlab", "invalid", "--help") + assert ret.success + assert ret.stdout + + +def test_invalid_api_version(script_runner, monkeypatch, fixture_dir): + monkeypatch.setenv("PYTHON_GITLAB_CFG", str(fixture_dir / "invalid_version.cfg")) + ret = script_runner.run("gitlab", "--gitlab", "test", "project", "list") + assert not ret.success + assert ret.stderr.startswith("Unsupported API version:") + + +def test_invalid_auth_config(script_runner, monkeypatch, fixture_dir): + monkeypatch.setenv("PYTHON_GITLAB_CFG", str(fixture_dir / "invalid_auth.cfg")) + ret = script_runner.run("gitlab", "--gitlab", "test", "project", "list") + assert not ret.success + assert "401" in ret.stderr + + +def test_fields(gitlab_cli, project_file): + cmd = "-o", "json", "--fields", "default_branch", "project", "list" + + ret = gitlab_cli(cmd) + assert ret.success + + content = json.loads(ret.stdout.strip()) + assert ["default_branch" in item for item in content] diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index b6fb9ed..854a273 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -9,6 +9,11 @@ import pytest import gitlab +@pytest.fixture(scope="session") +def fixture_dir(test_dir): + return test_dir / "functional" / "fixtures" + + def reset_gitlab(gl): # previously tools/reset_gitlab.py for project in gl.projects.list(): @@ -27,7 +32,7 @@ def reset_gitlab(gl): def set_token(container, rootdir): - set_token_rb = rootdir / "fixtures" / "set_token.rb" + set_token_rb = rootdir / "set_token.rb" with open(set_token_rb, "r") as f: set_token_command = f.read().strip() @@ -68,13 +73,8 @@ def temp_dir(): @pytest.fixture(scope="session") -def test_dir(pytestconfig): - return pytestconfig.rootdir / "tests" / "functional" - - -@pytest.fixture(scope="session") -def docker_compose_file(test_dir): - return test_dir / "fixtures" / "docker-compose.yml" +def docker_compose_file(fixture_dir): + return fixture_dir / "docker-compose.yml" @pytest.fixture(scope="session") @@ -129,7 +129,7 @@ def wait_for_sidekiq(gl): @pytest.fixture(scope="session") -def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, test_dir): +def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, fixture_dir): config_file = temp_dir / "python-gitlab.cfg" port = docker_services.port_for("gitlab", 80) @@ -137,7 +137,7 @@ def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, test_dir timeout=200, pause=5, check=lambda: check_is_alive("gitlab-test") ) - token = set_token("gitlab-test", rootdir=test_dir) + token = set_token("gitlab-test", rootdir=fixture_dir) config = f"""[global] default = local diff --git a/tests/functional/fixtures/invalid_auth.cfg b/tests/functional/fixtures/invalid_auth.cfg new file mode 100644 index 0000000..3d61d67 --- /dev/null +++ b/tests/functional/fixtures/invalid_auth.cfg @@ -0,0 +1,3 @@ +[test] +url = https://gitlab.com +private_token = abc123 diff --git a/tests/functional/fixtures/invalid_version.cfg b/tests/functional/fixtures/invalid_version.cfg new file mode 100644 index 0000000..31059a2 --- /dev/null +++ b/tests/functional/fixtures/invalid_version.cfg @@ -0,0 +1,3 @@ +[test] +api_version = 3 +url = https://gitlab.example.com |