|
1 | 1 | from typing import Optional |
2 | 2 | from unittest import mock |
3 | | -from uuid import UUID |
| 3 | +from uuid import UUID, uuid4 |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
@@ -317,6 +317,87 @@ def test_analytics_enabled_after_setting_explicitly( |
317 | 317 | ) |
318 | 318 |
|
319 | 319 |
|
| 320 | +@pytest.mark.unit |
| 321 | +def test_cloud_context_init_with_system_user_no_workspaces( |
| 322 | + unset_gx_env_variables: None, |
| 323 | + monkeypatch, |
| 324 | + mocker, |
| 325 | +): |
| 326 | + monkeypatch.setattr(ENV_CONFIG, "gx_analytics_enabled", True) # Enable usage stats |
| 327 | + |
| 328 | + user_id = uuid4() |
| 329 | + organization_id = uuid4() |
| 330 | + workspace_id = uuid4() |
| 331 | + |
| 332 | + # Mock cloud API response for system user with no workspaces |
| 333 | + def mock_request_cloud_backend(*args, **kwargs): |
| 334 | + mock_response = mocker.MagicMock() |
| 335 | + mock_response.json.return_value = { |
| 336 | + "user_id": str(user_id), |
| 337 | + "workspaces": [], |
| 338 | + } |
| 339 | + return mock_response |
| 340 | + |
| 341 | + mock_config = { |
| 342 | + "cloud_base_url": "https://api.test.greatexpectations.io", |
| 343 | + "cloud_access_token": "test_token_123", |
| 344 | + "cloud_organization_id": str(organization_id), |
| 345 | + "cloud_workspace_id": str(workspace_id), |
| 346 | + } |
| 347 | + |
| 348 | + mock_data_context_config = { |
| 349 | + "config_version": 4.0, |
| 350 | + "datasources": {}, |
| 351 | + "stores": {}, |
| 352 | + "expectations_store_name": "expectations_store", |
| 353 | + "validation_results_store_name": "validation_results_store", |
| 354 | + "checkpoint_store_name": "checkpoint_store", |
| 355 | + "data_docs_sites": {}, |
| 356 | + "analytics_enabled": True, |
| 357 | + } |
| 358 | + |
| 359 | + with ( |
| 360 | + mock.patch( |
| 361 | + "great_expectations.data_context.data_context.cloud_data_context.CloudDataContext.retrieve_data_context_config_from_cloud", |
| 362 | + return_value=mock_data_context_config, |
| 363 | + ), |
| 364 | + mock.patch( |
| 365 | + "great_expectations.data_context.data_context.cloud_data_context.CloudDataContext._save_project_config" |
| 366 | + ), |
| 367 | + mock.patch( |
| 368 | + "great_expectations.data_context.data_context.cloud_data_context.CloudDataContext._check_if_latest_version" |
| 369 | + ), |
| 370 | + mock.patch( |
| 371 | + "great_expectations.data_context.data_context.cloud_data_context.CloudDataContext._request_cloud_backend", |
| 372 | + side_effect=mock_request_cloud_backend, |
| 373 | + ), |
| 374 | + mock.patch( |
| 375 | + "great_expectations.data_context.data_context.cloud_data_context.init_analytics" |
| 376 | + ) as mock_init, |
| 377 | + mock.patch("posthog.capture"), |
| 378 | + ): |
| 379 | + # This should not raise an error for system users with no workspaces |
| 380 | + gx.get_context( |
| 381 | + mode="cloud", |
| 382 | + cloud_base_url=mock_config["cloud_base_url"], |
| 383 | + cloud_access_token=mock_config["cloud_access_token"], |
| 384 | + cloud_organization_id=mock_config["cloud_organization_id"], |
| 385 | + cloud_workspace_id=mock_config["cloud_workspace_id"], |
| 386 | + ) |
| 387 | + |
| 388 | + # Verify analytics initialization was called with the system user's ID |
| 389 | + mock_init.assert_called_once_with( |
| 390 | + enable=True, |
| 391 | + user_id=user_id, |
| 392 | + data_context_id=mock.ANY, |
| 393 | + organization_id=organization_id, |
| 394 | + oss_id=mock.ANY, |
| 395 | + cloud_mode=True, |
| 396 | + mode="cloud", |
| 397 | + user_agent_str=None, |
| 398 | + ) |
| 399 | + |
| 400 | + |
320 | 401 | @pytest.mark.parametrize("initial_user_agent_str", [None, "old user agent string"]) |
321 | 402 | @pytest.mark.parametrize("new_user_agent_str", [None, "new user agent string"]) |
322 | 403 | @pytest.mark.unit |
|
0 commit comments