"""Tests for Derived Roles commands.""" import os from pathlib import Path from typing import Generator import pytest from tests.integration.utils import run_command @pytest.fixture() def yml_path(tmp_path: Path) -> Generator[str, None, None]: """Create an empty file for the tests.""" d = tmp_path / "testdata" d.mkdir() yml_path = str(d / "my_roles.yaml") yield yml_path if os.path.isfile(yml_path): os.remove(yml_path) def test_by_tenant_command(tmp_path: Path) -> None: """Test the by_tenant command.""" d = tmp_path / "testdata" d.mkdir() yml_path = str(d / "my_roles.yaml") run_command( [ "uv", "run", "pdpcli", "derived_roles", "by_tenant", "--filepath", yml_path, "--name", "my_dream_jobs", "--role", "dog_whisperer", "--role", "cat_scratcher", ] ) expected_lines = [] actual_lines = [] with open( "tests/integration/cli/commands/testdata/expected_my_roles.yaml" ) as expected: expected_lines = expected.readlines() with open(yml_path) as actual: actual_lines = actual.readlines() assert len(actual_lines) == 42 assert actual_lines == expected_lines def test_by_tenant_command__invalid_name_error(yml_path: str) -> None: """Test the by_tenant command with an invalid --name value.""" stdout, _ = run_command( [ "uv", "run", "pdpcli", "derived_roles", "by_tenant", "--filepath", yml_path, "--role", "dog_whisperer", "--name", "my_dream_jobs_", # invalid derived role name ], expected_returncode=2, ) assert ( "The NAME of the collection of derived roles must be a lowercase and snake_case string" # noqa: E501 in stdout ) def test_by_tenant_command__invalid_dirpath() -> None: """Test the by_tenant command with an invalid filepath.""" stdout, _ = run_command( [ "uv", "run", "pdpcli", "derived_roles", "by_tenant", "--filepath", "/no/such/path/gameover.yml", # the file path does not exist. "--role", "dog_whisperer", "--name", "my_dream_jobs", ], expected_returncode=2, ) assert "The FILEPATH directory does not exist" in stdout def test_by_tenant_command__invalid_file_basename(tmp_path: Path) -> None: """Test the by_tenant command with an invalid filename.""" d = tmp_path / "testdata" d.mkdir() yml_path = str(d / "1234.foo") # invalid filename stdout, _ = run_command( [ "uv", "run", "pdpcli", "derived_roles", "by_tenant", "--filepath", yml_path, "--role", "dog_whisperer", "--name", "my_dream_jobs", ], expected_returncode=2, ) assert ( "The filename must be a lowercase and snake_case string with yml extension" in stdout ) def test_by_tenant_command__invalid_rolename(yml_path: str) -> None: """Test the by_tenant command with an invalid --role value.""" stdout, _ = run_command( [ "uv", "run", "pdpcli", "derived_roles", "by_tenant", "--filepath", yml_path, "--role", "1234!!", # invalid role name "--name", "my_dream_jobs", ], expected_returncode=2, ) assert "A role must be a lowercase and snake_case string." in stdout