|
| 1 | +import os |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from click.testing import CliRunner |
| 6 | + |
| 7 | +from uipath._cli.cli_auth import auth |
| 8 | + |
| 9 | +""" |
| 10 | +Unit tests for the 'uipath auth' command. |
| 11 | +
|
| 12 | +This test suite covers the following scenarios for the authentication logic: |
| 13 | +
|
| 14 | +1. **UIPATH_URL Environment Variable**: |
| 15 | + Ensures the `auth` command correctly uses the domain from the `UIPATH_URL` |
| 16 | + environment variable when no specific environment flag is used. |
| 17 | +
|
| 18 | +2. **--alpha Flag**: |
| 19 | + Verifies that the `--alpha` flag correctly uses the 'alpha' environment and |
| 20 | + overrides the `UIPATH_URL` environment variable if it is set. |
| 21 | +
|
| 22 | +3. **--staging Flag**: |
| 23 | + Verifies that the `--staging` flag correctly uses the 'staging' environment and |
| 24 | + overrides the `UIPATH_URL` environment variable if it is set. |
| 25 | +
|
| 26 | +4. **--cloud Flag**: |
| 27 | + Checks that the `--cloud` flag works as expected, using the 'cloud' environment, |
| 28 | + when no `UIPATH_URL` environment variable is set. |
| 29 | +
|
| 30 | +5. **Default Behavior**: |
| 31 | + Confirms that the command defaults to the 'cloud' environment when no flags |
| 32 | + or environment variables are provided. |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "scenario_name, cli_args, env_vars, expected_url_part, expected_select_tenant_return", |
| 38 | + [ |
| 39 | + ( |
| 40 | + "auth_with_uipath_url_env_variable", |
| 41 | + ["--force"], |
| 42 | + {"UIPATH_URL": "https://custom.automationsuite.org/org/tenant"}, |
| 43 | + "https://custom.automationsuite.org/identity_/connect/authorize", |
| 44 | + "https://custom.automationsuite.org/DefaultOrg/DefaultTenant", |
| 45 | + ), |
| 46 | + ( |
| 47 | + "auth_with_alpha_flag", |
| 48 | + ["--alpha", "--force"], |
| 49 | + {"UIPATH_URL": "https://custom.uipath.com/org/tenant"}, |
| 50 | + "https://alpha.uipath.com/identity_/connect/authorize", |
| 51 | + "https://alpha.uipath.com/DefaultOrg/DefaultTenant", |
| 52 | + ), |
| 53 | + ( |
| 54 | + "auth_with_staging_flag", |
| 55 | + ["--staging", "--force"], |
| 56 | + {"UIPATH_URL": "https://custom.uipath.com/org/tenant"}, |
| 57 | + "https://staging.uipath.com/identity_/connect/authorize", |
| 58 | + "https://staging.uipath.com/DefaultOrg/DefaultTenant", |
| 59 | + ), |
| 60 | + ( |
| 61 | + "auth_with_cloud_flag", |
| 62 | + ["--cloud", "--force"], |
| 63 | + {}, |
| 64 | + "https://cloud.uipath.com/identity_/connect/authorize", |
| 65 | + "https://cloud.uipath.com/DefaultOrg/DefaultTenant", |
| 66 | + ), |
| 67 | + ( |
| 68 | + "auth_default_to_cloud", |
| 69 | + ["--force"], |
| 70 | + {}, |
| 71 | + "https://cloud.uipath.com/identity_/connect/authorize", |
| 72 | + "https://cloud.uipath.com/DefaultOrg/DefaultTenant", |
| 73 | + ), |
| 74 | + ], |
| 75 | + ids=[ |
| 76 | + "uipath_url_env", |
| 77 | + "alpha_flag_overrides_env", |
| 78 | + "staging_flag_overrides_env", |
| 79 | + "cloud_flag", |
| 80 | + "default_to_cloud", |
| 81 | + ], |
| 82 | +) |
| 83 | +def test_auth_scenarios( |
| 84 | + scenario_name, cli_args, env_vars, expected_url_part, expected_select_tenant_return |
| 85 | +): |
| 86 | + """ |
| 87 | + Test 'uipath auth' with different configurations. |
| 88 | + """ |
| 89 | + runner = CliRunner() |
| 90 | + with ( |
| 91 | + patch("uipath._cli.cli_auth.webbrowser.open") as mock_open, |
| 92 | + patch("uipath._cli.cli_auth.HTTPServer") as mock_server, |
| 93 | + patch("uipath._cli.cli_auth.PortalService") as mock_portal_service, |
| 94 | + ): |
| 95 | + mock_server.return_value.start = AsyncMock( |
| 96 | + return_value={"access_token": "test_token"} |
| 97 | + ) |
| 98 | + mock_portal_service.return_value.__enter__.return_value.get_tenants_and_organizations.return_value = { |
| 99 | + "tenants": [{"name": "DefaultTenant", "id": "tenant-id"}], |
| 100 | + "organization": {"name": "DefaultOrg", "id": "org-id"}, |
| 101 | + } |
| 102 | + mock_portal_service.return_value.__enter__.return_value.select_tenant.return_value = expected_select_tenant_return |
| 103 | + |
| 104 | + with runner.isolated_filesystem(): |
| 105 | + for key, value in env_vars.items(): |
| 106 | + os.environ[key] = value |
| 107 | + |
| 108 | + result = runner.invoke(auth, cli_args) |
| 109 | + |
| 110 | + for key in env_vars: |
| 111 | + del os.environ[key] |
| 112 | + |
| 113 | + assert result.exit_code == 0, ( |
| 114 | + f"Scenario '{scenario_name}' failed with exit code {result.exit_code}: {result.output}" |
| 115 | + ) |
| 116 | + mock_open.assert_called_once() |
| 117 | + call_args = mock_open.call_args[0][0] |
| 118 | + assert expected_url_part in call_args |
0 commit comments