22from src .vcon .dialog import Dialog
33import hashlib
44import base64
5+ import requests
6+ from unittest .mock import Mock , patch
7+ from datetime import datetime
58
69
710class TestDialog :
@@ -346,7 +349,7 @@ def test_handles_invalid_datetime_string(self):
346349 Dialog (type = "text" , start = "invalid-datetime" , parties = [1 , 2 , 3 ])
347350
348351 # Converts start time to ISO 8601 string if provided as datetime
349- def test_convert_start_time_to_iso_string (self ):
352+ def test_convert_datetime_to_iso_string (self ):
350353 from datetime import datetime
351354 from src .vcon .dialog import Dialog
352355 from unittest .mock import patch
@@ -365,7 +368,7 @@ def test_convert_start_time_to_iso_string(self):
365368 assert dialog .start == "2022-09-15T10:30:00"
366369
367370 # Converts start time to ISO 8601 string if provided as string
368- def test_convert_start_time_to_iso_string (self ):
371+ def test_convert_string_to_iso_string (self ):
369372 from datetime import datetime
370373 from src .vcon .dialog import Dialog
371374 from unittest .mock import patch
@@ -379,3 +382,63 @@ def test_convert_start_time_to_iso_string(self):
379382 dialog = Dialog (type = "text" , start = start_time , parties = [1 , 2 , 3 ])
380383
381384 assert dialog .start == expected_iso_time
385+
386+ def test_to_inline_data_binary (self ):
387+ # Create some fake binary audio data
388+ fake_binary_data = (
389+ b"\x52 \x49 \x46 \x46 \x24 \x08 \x00 \x00 \x57 \x41 \x56 \x45 " # WAV header snippet
390+ )
391+
392+ # Mock the requests.get response
393+ mock_response = Mock ()
394+ mock_response .status_code = 200
395+ mock_response .content = fake_binary_data
396+ mock_response .headers = {"Content-Type" : "audio/x-wav" }
397+
398+ # Create a dialog with external data
399+ dialog = Dialog (
400+ type = "audio" ,
401+ start = datetime .now (),
402+ parties = [1 , 2 ],
403+ url = "http://example.com/audio.wav" ,
404+ )
405+
406+ # Mock the requests.get call
407+ with patch ("requests.get" , return_value = mock_response ):
408+ dialog .to_inline_data ()
409+
410+ # Verify the conversion was successful
411+ assert not hasattr (dialog , "url" ) # URL should be removed
412+ assert dialog .mimetype == "audio/x-wav"
413+ assert dialog .filename == "audio.wav"
414+ assert dialog .encoding == "base64url"
415+ assert dialog .alg == "sha256"
416+
417+ # Decode the base64url body and verify it matches original content
418+ decoded_body = base64 .urlsafe_b64decode (dialog .body .encode ())
419+ assert decoded_body == fake_binary_data
420+
421+ # Verify the signature matches the content
422+ expected_signature = base64 .urlsafe_b64encode (
423+ hashlib .sha256 (fake_binary_data ).digest ()
424+ ).decode ()
425+ assert dialog .signature == expected_signature
426+
427+ def test_to_inline_data_failed_request (self ):
428+ # Create a dialog with external data
429+ dialog = Dialog (
430+ type = "audio" ,
431+ start = datetime .now (),
432+ parties = [1 , 2 ],
433+ url = "http://example.com/audio.wav" ,
434+ )
435+
436+ # Mock a failed request
437+ mock_response = Mock ()
438+ mock_response .status_code = 404
439+
440+ # Verify that the conversion raises an exception
441+ with patch ("requests.get" , return_value = mock_response ):
442+ with pytest .raises (Exception ) as exc_info :
443+ dialog .to_inline_data ()
444+ assert "Failed to fetch external data: 404" in str (exc_info .value )
0 commit comments