|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +import pytest |
| 4 | + |
| 5 | +import machineid |
| 6 | +from askui.telemetry.machineid import ( |
| 7 | + get_machine_id, |
| 8 | + _read_machine_id_from_file, |
| 9 | + _write_machine_id_to_file, |
| 10 | + _is_valid_uuid4, |
| 11 | + _MACHINE_ID_FILE_PATH, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def reset_machine_id(): |
| 17 | + """Reset cached machine ID before each test.""" |
| 18 | + import askui.telemetry.machineid |
| 19 | + askui.telemetry.machineid._machine_id = None |
| 20 | + yield |
| 21 | + askui.telemetry.machineid._machine_id = None |
| 22 | + |
| 23 | + |
| 24 | +def test_read_machine_id_from_file(mocker): |
| 25 | + mocker.patch("os.path.exists", return_value=True) |
| 26 | + mock_file = mocker.mock_open(read_data="test-uuid-1234\n") |
| 27 | + mocker.patch("builtins.open", mock_file) |
| 28 | + |
| 29 | + result = _read_machine_id_from_file() |
| 30 | + assert result == "test-uuid-1234" |
| 31 | + mock_file.assert_called_once_with(_MACHINE_ID_FILE_PATH, "r") |
| 32 | + |
| 33 | + |
| 34 | +def test_read_machine_id_from_nonexistent_file(mocker): |
| 35 | + mocker.patch("os.path.exists", return_value=False) |
| 36 | + result = _read_machine_id_from_file() |
| 37 | + assert result is None |
| 38 | + |
| 39 | + |
| 40 | +def test_read_machine_id_file_error(mocker): |
| 41 | + mocker.patch("os.path.exists", return_value=True) |
| 42 | + mocker.patch("builtins.open", side_effect=IOError("Test IO Error")) |
| 43 | + result = _read_machine_id_from_file() |
| 44 | + assert result is None |
| 45 | + |
| 46 | + |
| 47 | +def test_write_machine_id_to_file(mocker): |
| 48 | + mocker.patch("os.makedirs") |
| 49 | + mock_file = mocker.mock_open() |
| 50 | + mocker.patch("builtins.open", mock_file) |
| 51 | + |
| 52 | + result = _write_machine_id_to_file("test-uuid-5678") |
| 53 | + assert result is True |
| 54 | + os.makedirs.assert_called_once_with(os.path.dirname(_MACHINE_ID_FILE_PATH), exist_ok=True) |
| 55 | + mock_file.assert_called_once_with(_MACHINE_ID_FILE_PATH, "w") |
| 56 | + mock_file().write.assert_called_once_with("test-uuid-5678") |
| 57 | + |
| 58 | + |
| 59 | +def test_write_machine_id_file_error(mocker): |
| 60 | + mocker.patch("os.makedirs", side_effect=OSError("Test OS Error")) |
| 61 | + result = _write_machine_id_to_file("test-uuid-5678") |
| 62 | + assert result is False |
| 63 | + |
| 64 | + |
| 65 | +def test_is_valid_uuid4(): |
| 66 | + # Valid UUID4 |
| 67 | + valid_uuid = str(uuid.uuid4()) |
| 68 | + assert _is_valid_uuid4(valid_uuid) is True |
| 69 | + |
| 70 | + # Invalid UUIDs |
| 71 | + assert _is_valid_uuid4("not-a-uuid") is False |
| 72 | + assert _is_valid_uuid4("12345678-1234-1234-1234-123456789012") is False # Valid format but not v4 |
| 73 | + assert _is_valid_uuid4("") is False |
| 74 | + |
| 75 | + |
| 76 | +def test_get_machine_id_from_file(mocker): |
| 77 | + valid_uuid = str(uuid.uuid4()).lower() |
| 78 | + mocker.patch("askui.telemetry.machineid._read_machine_id_from_file", return_value=valid_uuid) |
| 79 | + mock_write = mocker.patch("askui.telemetry.machineid._write_machine_id_to_file") |
| 80 | + mock_hashed_id = mocker.patch("machineid.hashed_id") |
| 81 | + |
| 82 | + result = get_machine_id() |
| 83 | + |
| 84 | + assert result == valid_uuid |
| 85 | + mock_hashed_id.assert_not_called() |
| 86 | + mock_write.assert_not_called() |
| 87 | + |
| 88 | + |
| 89 | +def test_get_machine_id_from_system(mocker): |
| 90 | + mocker.patch("askui.telemetry.machineid._read_machine_id_from_file", return_value=None) |
| 91 | + mock_write = mocker.patch("askui.telemetry.machineid._write_machine_id_to_file") |
| 92 | + mock_hashed_id = mocker.patch("machineid.hashed_id") |
| 93 | + mock_hashed_id.return_value = "system-machine-id" |
| 94 | + result = get_machine_id() |
| 95 | + mock_hashed_id.assert_called_once_with(app_id="askui") |
| 96 | + mock_write.assert_called_once() |
| 97 | + assert _is_valid_uuid4(result) |
| 98 | + |
| 99 | + |
| 100 | +def test_get_machine_id_fallback_to_random(mocker): |
| 101 | + mocker.patch("askui.telemetry.machineid._read_machine_id_from_file", return_value=None) |
| 102 | + mock_write = mocker.patch("askui.telemetry.machineid._write_machine_id_to_file") |
| 103 | + mock_hashed_id = mocker.patch("machineid.hashed_id") |
| 104 | + mock_hashed_id.side_effect = machineid.MachineIdNotFound() |
| 105 | + random_uuid = uuid.uuid4() |
| 106 | + mocker.patch("uuid.uuid4", return_value=random_uuid) |
| 107 | + result = get_machine_id() |
| 108 | + assert result == str(random_uuid).lower() |
| 109 | + mock_write.assert_called_once_with(str(random_uuid).lower()) |
| 110 | + |
| 111 | + |
| 112 | +def test_get_machine_id_caching(mocker): |
| 113 | + valid_uuid = str(uuid.uuid4()).lower() |
| 114 | + mock_read = mocker.patch("askui.telemetry.machineid._read_machine_id_from_file", return_value=valid_uuid) |
| 115 | + result1 = get_machine_id() |
| 116 | + assert result1 == valid_uuid |
| 117 | + mock_read.reset_mock() |
| 118 | + result2 = get_machine_id() |
| 119 | + assert result2 == valid_uuid |
| 120 | + mock_read.assert_not_called() |
0 commit comments