|
| 1 | +import json |
| 2 | +import os |
| 3 | +import urllib.parse |
| 4 | +import uuid |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from click.testing import CliRunner |
| 9 | +from pytest_httpx import HTTPXMock |
| 10 | + |
| 11 | +from tests.cli.utils.project_details import ProjectDetails |
| 12 | +from uipath._cli.cli_invoke import invoke # type: ignore |
| 13 | +from uipath._cli.middlewares import MiddlewareResult |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def entrypoint(): |
| 18 | + return "entrypoint.py" |
| 19 | + |
| 20 | + |
| 21 | +def _create_env_file(mock_env_vars: dict[str, str]): |
| 22 | + """Create the environment file.""" |
| 23 | + with open(".env", "w") as f: |
| 24 | + for key, value in mock_env_vars.items(): |
| 25 | + f.write(f"{key}={value}\n") |
| 26 | + |
| 27 | + |
| 28 | +class TestInvoke: |
| 29 | + class TestFileInput: |
| 30 | + def test_invoke_input_file_not_found( |
| 31 | + self, |
| 32 | + runner: CliRunner, |
| 33 | + temp_dir: str, |
| 34 | + entrypoint: str, |
| 35 | + ): |
| 36 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 37 | + result = runner.invoke(invoke, [entrypoint, "--file", "not-here.json"]) |
| 38 | + assert result.exit_code != 0 |
| 39 | + assert "Error: Invalid value for '-f' / '--file'" in result.output |
| 40 | + |
| 41 | + def test_invoke_invalid_input_file( |
| 42 | + self, |
| 43 | + runner: CliRunner, |
| 44 | + temp_dir: str, |
| 45 | + entrypoint: str, |
| 46 | + ): |
| 47 | + file_name = "not-json.txt" |
| 48 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 49 | + file_path = os.path.join(temp_dir, file_name) |
| 50 | + with open(file_path, "w") as f: |
| 51 | + f.write("file content") |
| 52 | + result = runner.invoke(invoke, [entrypoint, "--file", file_path]) |
| 53 | + assert result.exit_code == 1 |
| 54 | + assert "Input file extension must be '.json'." in result.output |
| 55 | + |
| 56 | + def test_invoke_successful( |
| 57 | + self, |
| 58 | + runner: CliRunner, |
| 59 | + temp_dir: str, |
| 60 | + entrypoint: str, |
| 61 | + mock_env_vars: dict[str, str], |
| 62 | + httpx_mock: HTTPXMock, |
| 63 | + project_details: ProjectDetails, |
| 64 | + ): |
| 65 | + base_url = mock_env_vars.get("UIPATH_URL") |
| 66 | + job_key = uuid.uuid4() |
| 67 | + my_workspace_folder_id = "123" |
| 68 | + my_workspace_feed_id = "042e669a-c95f-46a3-87b0-9e5a98d7cf8a" |
| 69 | + release_id = "123" |
| 70 | + odata_top_filter = 25 |
| 71 | + personal_workspace_response_data = { |
| 72 | + "PersonalWorskpaceFeedId": my_workspace_feed_id, |
| 73 | + "PersonalWorkspace": {"Id": my_workspace_folder_id}, |
| 74 | + } |
| 75 | + list_release_response_data = { |
| 76 | + "value": [ |
| 77 | + { |
| 78 | + "ProcessVersion": project_details.version, |
| 79 | + "Id": release_id, |
| 80 | + "Key": "9d17b737-1283-4ebe-b1f5-7d88967b94e4", |
| 81 | + } |
| 82 | + ] |
| 83 | + } |
| 84 | + start_job_response_data = { |
| 85 | + "value": [{"Key": f"{job_key}", "other_key": "other_value"}] |
| 86 | + } |
| 87 | + # mock get release info |
| 88 | + httpx_mock.add_response( |
| 89 | + url=f"{base_url}/orchestrator_/odata/Releases/UiPath.Server.Configuration.OData.ListReleases?$select=Id,Key,ProcessVersion&$top={odata_top_filter}&$filter=ProcessKey%20eq%20%27{urllib.parse.quote(project_details.name)}%27", |
| 90 | + status_code=200, |
| 91 | + text=json.dumps(list_release_response_data), |
| 92 | + ) |
| 93 | + # mock get personal workspace info |
| 94 | + httpx_mock.add_response( |
| 95 | + url=f"{base_url}/orchestrator_/odata/Users/UiPath.Server.Configuration.OData.GetCurrentUserExtended?$expand=PersonalWorkspace", |
| 96 | + status_code=200, |
| 97 | + text=json.dumps(personal_workspace_response_data), |
| 98 | + ) |
| 99 | + # mock start job response |
| 100 | + httpx_mock.add_response( |
| 101 | + url=f"{base_url}/orchestrator_/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs", |
| 102 | + status_code=201, |
| 103 | + text=json.dumps(start_job_response_data), |
| 104 | + ) |
| 105 | + |
| 106 | + file_name = "input.json" |
| 107 | + json_content = """ |
| 108 | + { |
| 109 | + "input_key": "input_value" |
| 110 | + }""" |
| 111 | + |
| 112 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 113 | + _create_env_file(mock_env_vars) |
| 114 | + |
| 115 | + file_path = os.path.join(temp_dir, file_name) |
| 116 | + with open(file_path, "w") as f: |
| 117 | + f.write(json_content) |
| 118 | + with open("pyproject.toml", "w") as f: |
| 119 | + f.write(project_details.to_toml()) |
| 120 | + result = runner.invoke(invoke, [entrypoint, "{}"]) |
| 121 | + assert result.exit_code == 0 |
| 122 | + assert "Starting job ..." in result.output |
| 123 | + assert "Job started successfully!" in result.output |
| 124 | + assert ( |
| 125 | + f"{base_url}/orchestrator_/jobs(sidepanel:sidepanel/jobs/{job_key}/details)?fid={my_workspace_folder_id}" |
| 126 | + in result.output |
| 127 | + ) |
| 128 | + |
| 129 | + def test_invoke_personal_workspace_info_error( |
| 130 | + self, |
| 131 | + runner: CliRunner, |
| 132 | + temp_dir: str, |
| 133 | + entrypoint: str, |
| 134 | + mock_env_vars: dict[str, str], |
| 135 | + httpx_mock: HTTPXMock, |
| 136 | + ): |
| 137 | + base_url = mock_env_vars.get("UIPATH_URL") |
| 138 | + # mock start job response |
| 139 | + httpx_mock.add_response( |
| 140 | + url=f"{base_url}/orchestrator_/odata/Users/UiPath.Server.Configuration.OData.GetCurrentUserExtended?$expand=PersonalWorkspace", |
| 141 | + status_code=401, |
| 142 | + ) |
| 143 | + |
| 144 | + file_name = "input.json" |
| 145 | + json_content = """ |
| 146 | + { |
| 147 | + "input_key": "input_value" |
| 148 | + }""" |
| 149 | + |
| 150 | + with runner.isolated_filesystem(temp_dir=temp_dir): |
| 151 | + _create_env_file(mock_env_vars) |
| 152 | + |
| 153 | + file_path = os.path.join(temp_dir, file_name) |
| 154 | + with open(file_path, "w") as f: |
| 155 | + f.write(json_content) |
| 156 | + with patch("uipath._cli.cli_run.Middlewares.next") as mock_middleware: |
| 157 | + mock_middleware.return_value = MiddlewareResult( |
| 158 | + should_continue=False, |
| 159 | + info_message="Execution succeeded", |
| 160 | + error_message=None, |
| 161 | + should_include_stacktrace=False, |
| 162 | + ) |
| 163 | + result = runner.invoke(invoke, [entrypoint, "{}"]) |
| 164 | + assert result.exit_code == 1 |
| 165 | + assert ( |
| 166 | + "Failed to fetch user info. Please try reauthenticating." |
| 167 | + in result.output |
| 168 | + ) |
0 commit comments