|
72 | 72 | to_core_task_push_notification_config, |
73 | 73 | to_core_task_status, |
74 | 74 | to_core_task_status_update_event, |
| 75 | + core_to_compat_task_model, |
| 76 | + compat_task_model_to_core, |
| 77 | + core_to_compat_push_notification_config_model, |
| 78 | + compat_push_notification_config_model_to_core, |
75 | 79 | ) |
| 80 | +from a2a.server.models import PushNotificationConfigModel, TaskModel |
| 81 | +from cryptography.fernet import Fernet |
76 | 82 | from a2a.types import a2a_pb2 as pb2_v10 |
77 | 83 |
|
78 | 84 |
|
@@ -1911,3 +1917,100 @@ def test_to_core_part_unknown_part(): |
1911 | 1917 | assert not core_part.HasField('data') |
1912 | 1918 | assert not core_part.HasField('raw') |
1913 | 1919 | assert not core_part.HasField('url') |
| 1920 | + |
| 1921 | + |
| 1922 | +def test_task_db_conversion(): |
| 1923 | + v10_task = pb2_v10.Task( |
| 1924 | + id='task-123', |
| 1925 | + context_id='ctx-456', |
| 1926 | + status=pb2_v10.TaskStatus( |
| 1927 | + state=pb2_v10.TaskState.TASK_STATE_WORKING, |
| 1928 | + ), |
| 1929 | + metadata={'m1': 'v1'}, |
| 1930 | + ) |
| 1931 | + owner = 'owner-789' |
| 1932 | + |
| 1933 | + # Test Core -> Model |
| 1934 | + model = core_to_compat_task_model(v10_task, owner) |
| 1935 | + assert model.id == 'task-123' |
| 1936 | + assert model.context_id == 'ctx-456' |
| 1937 | + assert model.owner == owner |
| 1938 | + assert model.protocol_version == '0.3' |
| 1939 | + assert model.status['state'] == 'working' |
| 1940 | + assert model.task_metadata == {'m1': 'v1'} |
| 1941 | + |
| 1942 | + # Test Model -> Core |
| 1943 | + v10_restored = compat_task_model_to_core(model) |
| 1944 | + assert v10_restored.id == v10_task.id |
| 1945 | + assert v10_restored.context_id == v10_task.context_id |
| 1946 | + assert v10_restored.status.state == v10_task.status.state |
| 1947 | + assert v10_restored.metadata == v10_task.metadata |
| 1948 | + |
| 1949 | + |
| 1950 | +def test_push_notification_config_db_conversion(): |
| 1951 | + task_id = 'task-123' |
| 1952 | + v10_config = pb2_v10.TaskPushNotificationConfig( |
| 1953 | + id='pnc-1', |
| 1954 | + url='https://example.com/push', |
| 1955 | + token='secret-token', |
| 1956 | + ) |
| 1957 | + owner = 'owner-789' |
| 1958 | + |
| 1959 | + # Test Core -> Model (No encryption) |
| 1960 | + model = core_to_compat_push_notification_config_model( |
| 1961 | + task_id, v10_config, owner |
| 1962 | + ) |
| 1963 | + assert model.task_id == task_id |
| 1964 | + assert model.config_id == 'pnc-1' |
| 1965 | + assert model.owner == owner |
| 1966 | + assert model.protocol_version == '0.3' |
| 1967 | + |
| 1968 | + import json |
| 1969 | + |
| 1970 | + data = json.loads(model.config_data.decode('utf-8')) |
| 1971 | + assert data['url'] == 'https://example.com/push' |
| 1972 | + assert data['token'] == 'secret-token' |
| 1973 | + |
| 1974 | + # Test Model -> Core |
| 1975 | + v10_restored = compat_push_notification_config_model_to_core( |
| 1976 | + model.config_data.decode('utf-8'), task_id |
| 1977 | + ) |
| 1978 | + assert v10_restored.id == v10_config.id |
| 1979 | + assert v10_restored.url == v10_config.url |
| 1980 | + assert v10_restored.token == v10_config.token |
| 1981 | + |
| 1982 | + |
| 1983 | +def test_push_notification_config_persistence_conversion_with_encryption(): |
| 1984 | + task_id = 'task-123' |
| 1985 | + v10_config = pb2_v10.TaskPushNotificationConfig( |
| 1986 | + id='pnc-1', |
| 1987 | + url='https://example.com/push', |
| 1988 | + token='secret-token', |
| 1989 | + ) |
| 1990 | + owner = 'owner-789' |
| 1991 | + key = Fernet.generate_key() |
| 1992 | + fernet = Fernet(key) |
| 1993 | + |
| 1994 | + # Test Core -> Model (With encryption) |
| 1995 | + model = core_to_compat_push_notification_config_model( |
| 1996 | + task_id, v10_config, owner, fernet=fernet |
| 1997 | + ) |
| 1998 | + assert ( |
| 1999 | + model.config_data != v10_config.SerializeToString() |
| 2000 | + ) # Should be encrypted |
| 2001 | + |
| 2002 | + # Decrypt and verify |
| 2003 | + decrypted_data = fernet.decrypt(model.config_data) |
| 2004 | + import json |
| 2005 | + |
| 2006 | + data = json.loads(decrypted_data.decode('utf-8')) |
| 2007 | + assert data['url'] == 'https://example.com/push' |
| 2008 | + assert data['token'] == 'secret-token' |
| 2009 | + |
| 2010 | + # Test Model -> Core |
| 2011 | + v10_restored = compat_push_notification_config_model_to_core( |
| 2012 | + decrypted_data.decode('utf-8'), task_id |
| 2013 | + ) |
| 2014 | + assert v10_restored.id == v10_config.id |
| 2015 | + assert v10_restored.url == v10_config.url |
| 2016 | + assert v10_restored.token == v10_config.token |
0 commit comments