from types import SimpleNamespace import pytest from dx_productivity.trial_task_center import cad_identity as identity def environment(**changes): return {'DXPART_DESKTOP': 'dx-test', 'DXPART_CAD_PID': '123', 'DXPART_CAD_GUID': '{owned}', 'DXPART_SESSION_TOKEN': 'a' * 32, **changes} def test_partial_identity_is_rejected_instead_of_using_interactive_cad(): with pytest.raises(ValueError, match='incomplete'): identity.CadBinding.from_environment({'DXPART_DESKTOP': 'dx-test'}) def test_identity_checks_desktop_before_reading_com(monkeypatch): binding = identity.CadBinding.from_environment(environment()) monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'Default') with pytest.raises(ValueError, match='desktop'): identity.verify_application(object(), binding) def test_foreign_com_guid_is_rejected_before_project_access(monkeypatch): binding = identity.CadBinding.from_environment(environment()) monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test') with pytest.raises(ValueError, match='GUID'): identity.verify_application(SimpleNamespace(Guid=lambda: '{foreign}'), binding) def test_foreign_view_pid_is_rejected(monkeypatch): binding = identity.CadBinding.from_environment(environment()) monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test') monkeypatch.setattr(identity, 'window_pid', lambda _: 456) app = SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7) with pytest.raises(ValueError, match='PID'): identity.verify_application(app, binding) def test_owned_app_matches_certificate_and_window_pid(monkeypatch): binding = identity.CadBinding.from_environment(environment()) monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test') monkeypatch.setattr(identity, 'window_pid', lambda _: 123) assert identity.verify_application(SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7), binding) == binding def test_no_identity_retains_explicit_legacy_helper_compatibility(): assert identity.CadBinding.from_environment({}) is None def test_closed_project_does_not_treat_stale_view_handle_as_current_pid(monkeypatch): binding = identity.CadBinding.from_environment(environment()) monkeypatch.setattr(identity, 'current_desktop_name', lambda: 'dx-test') monkeypatch.setattr(identity, 'window_pid', lambda _: 456) app = SimpleNamespace(Guid=lambda: '{owned}', ActiveViewHandle=7) assert identity.verify_application(app, binding, require_view=False) == binding with pytest.raises(ValueError, match='PID'): identity.verify_application(app, binding) @pytest.mark.parametrize('pid', ['0', '-1', 'abc']) def test_invalid_pid_cannot_become_an_owned_cad(pid): with pytest.raises(ValueError): identity.CadBinding.from_environment(environment(DXPART_CAD_PID=pid))