|
| 1 | +import json |
| 2 | +import os |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from click.testing import CliRunner |
| 7 | + |
| 8 | +from datahub.cli.session_cli import session |
| 9 | +from datahub.ingestion.graph.config import DatahubClientConfig |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_sessions_file(tmp_path): |
| 14 | + # Create a temporary sessions file for testing |
| 15 | + test_sessions_file = tmp_path / "sessions.json" |
| 16 | + with patch( |
| 17 | + "datahub.cli.session_cli.DATAHUB_SESSIONS_PATH", str(test_sessions_file) |
| 18 | + ): |
| 19 | + yield test_sessions_file |
| 20 | + # Cleanup |
| 21 | + if test_sessions_file.exists(): |
| 22 | + test_sessions_file.unlink() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def sample_config(): |
| 27 | + return DatahubClientConfig(server="http://localhost:8080", token="test-token") |
| 28 | + |
| 29 | + |
| 30 | +def test_create_session(mock_sessions_file): |
| 31 | + runner = CliRunner() |
| 32 | + with runner.isolated_filesystem(): |
| 33 | + # Test creating a new profile |
| 34 | + result = runner.invoke( |
| 35 | + session, |
| 36 | + ["create"], |
| 37 | + input="http://localhost:8080\ntest-token\ntest-profile\n", |
| 38 | + ) |
| 39 | + assert result.exit_code == 0 |
| 40 | + assert "Created profile: test-profile" in result.output |
| 41 | + |
| 42 | + # Verify the session was saved |
| 43 | + with open(mock_sessions_file) as f: |
| 44 | + saved_sessions = json.load(f) |
| 45 | + assert "test-profile" in saved_sessions |
| 46 | + assert saved_sessions["test-profile"]["server"] == "http://localhost:8080" |
| 47 | + assert saved_sessions["test-profile"]["token"] == "test-token" |
| 48 | + |
| 49 | + |
| 50 | +def test_list_sessions_empty_data(mock_sessions_file): |
| 51 | + runner = CliRunner() |
| 52 | + # Create an empty sessions file |
| 53 | + os.makedirs(os.path.dirname(mock_sessions_file), exist_ok=True) |
| 54 | + with open(mock_sessions_file, "w") as f: |
| 55 | + json.dump({}, f) |
| 56 | + |
| 57 | + result = runner.invoke(session, ["list"]) |
| 58 | + assert result.exit_code == 0 |
| 59 | + assert "No profiles found" in result.output |
| 60 | + |
| 61 | + |
| 62 | +def test_list_sessions(mock_sessions_file, sample_config): |
| 63 | + runner = CliRunner() |
| 64 | + # Setup test data |
| 65 | + sessions_data = {"test-profile": sample_config.dict()} |
| 66 | + os.makedirs(os.path.dirname(mock_sessions_file), exist_ok=True) |
| 67 | + with open(mock_sessions_file, "w") as f: |
| 68 | + json.dump(sessions_data, f) |
| 69 | + |
| 70 | + result = runner.invoke(session, ["list"]) |
| 71 | + assert result.exit_code == 0 |
| 72 | + assert "test-profile" in result.output |
| 73 | + assert "http://localhost:8080" in result.output |
| 74 | + |
| 75 | + |
| 76 | +def test_delete_session(mock_sessions_file, sample_config): |
| 77 | + runner = CliRunner() |
| 78 | + # Setup test data |
| 79 | + sessions_data = {"test-profile": sample_config.dict()} |
| 80 | + os.makedirs(os.path.dirname(mock_sessions_file), exist_ok=True) |
| 81 | + with open(mock_sessions_file, "w") as f: |
| 82 | + json.dump(sessions_data, f) |
| 83 | + |
| 84 | + # Test deleting existing profile |
| 85 | + result = runner.invoke(session, ["delete", "test-profile"]) |
| 86 | + assert result.exit_code == 0 |
| 87 | + assert "Deleted profile: test-profile" in result.output |
| 88 | + |
| 89 | + # Verify profile was deleted |
| 90 | + with open(mock_sessions_file) as f: |
| 91 | + saved_sessions = json.load(f) |
| 92 | + assert "test-profile" not in saved_sessions |
| 93 | + |
| 94 | + |
| 95 | +def test_delete_unknown_session(mock_sessions_file, sample_config): |
| 96 | + runner = CliRunner() |
| 97 | + # Setup test data with a known profile |
| 98 | + sessions_data = {"test-profile": sample_config.dict()} |
| 99 | + os.makedirs(os.path.dirname(mock_sessions_file), exist_ok=True) |
| 100 | + with open(mock_sessions_file, "w") as f: |
| 101 | + json.dump(sessions_data, f) |
| 102 | + |
| 103 | + # Test deleting non-existent profile |
| 104 | + result = runner.invoke(session, ["delete", "unknown-profile"]) |
| 105 | + assert result.exit_code == 0 |
| 106 | + assert "Profile unknown-profile not found" in result.output |
| 107 | + |
| 108 | + # Verify original profile still exists |
| 109 | + with open(mock_sessions_file) as f: |
| 110 | + saved_sessions = json.load(f) |
| 111 | + assert "test-profile" in saved_sessions |
| 112 | + |
| 113 | + |
| 114 | +def test_use_session(mock_sessions_file, sample_config): |
| 115 | + runner = CliRunner() |
| 116 | + # Setup test data |
| 117 | + sessions_data = {"test-profile": sample_config.dict()} |
| 118 | + os.makedirs(os.path.dirname(mock_sessions_file), exist_ok=True) |
| 119 | + with open(mock_sessions_file, "w") as f: |
| 120 | + json.dump(sessions_data, f) |
| 121 | + |
| 122 | + with patch("datahub.cli.session_cli.persist_raw_datahub_config") as mock_persist: |
| 123 | + result = runner.invoke(session, ["use", "test-profile"]) |
| 124 | + assert result.exit_code == 0 |
| 125 | + assert "Using profile test-profile" in result.output |
| 126 | + mock_persist.assert_called_once_with(sample_config.dict()) |
| 127 | + |
| 128 | + |
| 129 | +def test_save_session(mock_sessions_file, sample_config): |
| 130 | + runner = CliRunner() |
| 131 | + with patch( |
| 132 | + "datahub.cli.session_cli.load_client_config", return_value=sample_config |
| 133 | + ): |
| 134 | + result = runner.invoke(session, ["save", "--profile", "new-profile"]) |
| 135 | + assert result.exit_code == 0 |
| 136 | + assert "Saved current datahubenv as profile: new-profile" in result.output |
| 137 | + |
| 138 | + # Verify the session was saved |
| 139 | + with open(mock_sessions_file) as f: |
| 140 | + saved_sessions = json.load(f) |
| 141 | + assert "new-profile" in saved_sessions |
| 142 | + assert saved_sessions["new-profile"]["server"] == "http://localhost:8080" |
| 143 | + assert saved_sessions["new-profile"]["token"] == "test-token" |
| 144 | + |
| 145 | + |
| 146 | +def test_create_session_with_password(mock_sessions_file): |
| 147 | + runner = CliRunner() |
| 148 | + with runner.isolated_filesystem(), patch( |
| 149 | + "datahub.cli.session_cli.generate_access_token", |
| 150 | + return_value=("username", "generated-token"), |
| 151 | + ): |
| 152 | + result = runner.invoke( |
| 153 | + session, |
| 154 | + ["create", "--use-password"], |
| 155 | + input="http://localhost:8080\nusername\npassword\ntest-profile\n", |
| 156 | + ) |
| 157 | + assert result.exit_code == 0 |
| 158 | + assert "Created profile: test-profile" in result.output |
| 159 | + |
| 160 | + # Verify the session was saved with generated token |
| 161 | + with open(mock_sessions_file) as f: |
| 162 | + saved_sessions = json.load(f) |
| 163 | + assert saved_sessions["test-profile"]["token"] == "generated-token" |
0 commit comments