@@ -698,11 +698,13 @@ def test_clean_messages(self, http_client_clean_messages):
698698 assert response .data [0 ].conversation == "cleaned example"
699699 assert response .data [1 ].conversation == "another example"
700700
701-
702701 def test_list_messages_select_param (self , http_client_list_response ):
703702 messages = Messages (http_client_list_response )
704703
705- messages .list (identifier = "abc-123" , query_params = {"select" : ["id" , "subject" , "from" , "to" ]})
704+ messages .list (
705+ identifier = "abc-123" ,
706+ query_params = {"select" : ["id" , "subject" , "from" , "to" ]},
707+ )
706708
707709 http_client_list_response ._execute .assert_called_once_with (
708710 "GET" ,
@@ -714,12 +716,18 @@ def test_list_messages_select_param(self, http_client_list_response):
714716 )
715717
716718 # Make sure query params are properly serialized
717- assert http_client_list_response ._execute .call_args [0 ][3 ] == {"select" : ["id" , "subject" , "from" , "to" ]}
719+ assert http_client_list_response ._execute .call_args [0 ][3 ] == {
720+ "select" : ["id" , "subject" , "from" , "to" ]
721+ }
718722
719723 def test_find_message_select_param (self , http_client_response ):
720724 messages = Messages (http_client_response )
721725
722- messages .find (identifier = "abc-123" , message_id = "message-123" , query_params = {"select" : ["id" , "subject" , "from" , "to" ]})
726+ messages .find (
727+ identifier = "abc-123" ,
728+ message_id = "message-123" ,
729+ query_params = {"select" : ["id" , "subject" , "from" , "to" ]},
730+ )
723731
724732 http_client_response ._execute .assert_called_once_with (
725733 "GET" ,
@@ -731,7 +739,9 @@ def test_find_message_select_param(self, http_client_response):
731739 )
732740
733741 # Make sure query params are properly serialized
734- assert http_client_response ._execute .call_args [0 ][3 ] == {"select" : ["id" , "subject" , "from" , "to" ]}
742+ assert http_client_response ._execute .call_args [0 ][3 ] == {
743+ "select" : ["id" , "subject" , "from" , "to" ]
744+ }
735745
736746 # New tests for tracking_options and raw_mime features
737747 def test_message_deserialization_with_tracking_options (self ):
@@ -747,8 +757,8 @@ def test_message_deserialization_with_tracking_options(self):
747757 "opens" : True ,
748758 "thread_replies" : False ,
749759 "links" : True ,
750- "label" : "Marketing Campaign"
751- }
760+ "label" : "Marketing Campaign" ,
761+ },
752762 }
753763
754764 message = Message .from_dict (message_json )
@@ -765,12 +775,15 @@ def test_message_deserialization_with_raw_mime(self):
765775 "grant_id" : "41009df5-bf11-4c97-aa18-b285b5f2e386" ,
766776 "id" : "5d3qmne77v32r8l4phyuksl2x" ,
767777 "object" : "message" ,
768- "raw_mime" : "TUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PXV0Zi04CgpIZWxsbyBXb3JsZCE="
778+ "raw_mime" : "TUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PXV0Zi04CgpIZWxsbyBXb3JsZCE=" ,
769779 }
770780
771781 message = Message .from_dict (message_json )
772782
773- assert message .raw_mime == "TUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PXV0Zi04CgpIZWxsbyBXb3JsZCE="
783+ assert (
784+ message .raw_mime
785+ == "TUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PXV0Zi04CgpIZWxsbyBXb3JsZCE="
786+ )
774787
775788 def test_message_deserialization_backwards_compatibility (self ):
776789 """Test that existing message deserialization still works (backwards compatibility)."""
@@ -792,7 +805,9 @@ def test_message_deserialization_backwards_compatibility(self):
792805 assert message .body == "Hello, I just sent a message using Nylas!"
793806 assert message .subject == "Hello from Nylas!"
794807
795- def test_list_messages_with_include_tracking_options_field (self , http_client_list_response ):
808+ def test_list_messages_with_include_tracking_options_field (
809+ self , http_client_list_response
810+ ):
796811 """Test listing messages with include_tracking_options field."""
797812 messages = Messages (http_client_list_response )
798813
@@ -828,7 +843,9 @@ def test_list_messages_with_raw_mime_field(self, http_client_list_response):
828843 overrides = None ,
829844 )
830845
831- def test_find_message_with_include_tracking_options_field (self , http_client_response ):
846+ def test_find_message_with_include_tracking_options_field (
847+ self , http_client_response
848+ ):
832849 """Test finding a message with include_tracking_options field."""
833850 messages = Messages (http_client_response )
834851
@@ -869,21 +886,18 @@ def test_find_message_with_raw_mime_field(self, http_client_response):
869886 def test_tracking_options_serialization (self ):
870887 """Test that tracking_options can be serialized to JSON."""
871888 from nylas .models .messages import TrackingOptions
872-
889+
873890 tracking_options = TrackingOptions (
874- opens = True ,
875- thread_replies = False ,
876- links = True ,
877- label = "Test Campaign"
891+ opens = True , thread_replies = False , links = True , label = "Test Campaign"
878892 )
879-
893+
880894 # Test serialization
881895 json_data = tracking_options .to_dict ()
882896 assert json_data ["opens" ] is True
883897 assert json_data ["thread_replies" ] is False
884898 assert json_data ["links" ] is True
885899 assert json_data ["label" ] == "Test Campaign"
886-
900+
887901 # Test deserialization
888902 tracking_options_from_dict = TrackingOptions .from_dict (json_data )
889903 assert tracking_options_from_dict .opens is True
@@ -931,7 +945,9 @@ def test_send_message_with_is_plaintext_false(self, http_client_response):
931945 overrides = None ,
932946 )
933947
934- def test_send_message_without_is_plaintext_backwards_compatibility (self , http_client_response ):
948+ def test_send_message_without_is_plaintext_backwards_compatibility (
949+ self , http_client_response
950+ ):
935951 """Test that existing code without is_plaintext still works (backwards compatibility)."""
936952 messages = Messages (http_client_response )
937953 request_body = {
@@ -949,4 +965,116 @@ def test_send_message_without_is_plaintext_backwards_compatibility(self, http_cl
949965 request_body = request_body ,
950966 data = None ,
951967 overrides = None ,
952- )
968+ )
969+
970+ def test_send_message_with_from_field_mapping (self , http_client_response ):
971+ """Test that from_ field is properly mapped to from field in request body."""
972+ messages = Messages (http_client_response )
973+ request_body = {
974+ "subject" : "Hello from Nylas!" ,
975+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
976+ "body" : "This is the body of my message." ,
977+ "from_" : [{
"name" :
"Daenerys Targaryen" ,
"email" :
"[email protected] " }],
978+ }
979+
980+ messages .send (identifier = "abc-123" , request_body = request_body )
981+
982+ # Verify that from_ was mapped to from and from_ was removed
983+ expected_request_body = {
984+ "subject" : "Hello from Nylas!" ,
985+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
986+ "body" : "This is the body of my message." ,
987+ "from" : [{
"name" :
"Daenerys Targaryen" ,
"email" :
"[email protected] " }],
988+ }
989+
990+ http_client_response ._execute .assert_called_once_with (
991+ method = "POST" ,
992+ path = "/v3/grants/abc-123/messages/send" ,
993+ request_body = expected_request_body ,
994+ data = None ,
995+ overrides = None ,
996+ )
997+
998+ def test_send_message_with_existing_from_field_unchanged (self , http_client_response ):
999+ """Test that existing from field is left unchanged when both from and from_ are present."""
1000+ messages = Messages (http_client_response )
1001+ request_body = {
1002+ "subject" : "Hello from Nylas!" ,
1003+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1004+ "body" : "This is the body of my message." ,
1005+ "from" : [{
"name" :
"Existing Sender" ,
"email" :
"[email protected] " }],
1006+ "from_" : [{
"name" :
"Daenerys Targaryen" ,
"email" :
"[email protected] " }],
1007+ }
1008+
1009+ messages .send (identifier = "abc-123" , request_body = request_body )
1010+
1011+ # Verify that the original from field is preserved and from_ is not processed
1012+ expected_request_body = {
1013+ "subject" : "Hello from Nylas!" ,
1014+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1015+ "body" : "This is the body of my message." ,
1016+ "from" : [{
"name" :
"Existing Sender" ,
"email" :
"[email protected] " }],
1017+ "from_" : [{
"name" :
"Daenerys Targaryen" ,
"email" :
"[email protected] " }],
1018+ }
1019+
1020+ http_client_response ._execute .assert_called_once_with (
1021+ method = "POST" ,
1022+ path = "/v3/grants/abc-123/messages/send" ,
1023+ request_body = expected_request_body ,
1024+ data = None ,
1025+ overrides = None ,
1026+ )
1027+
1028+ def test_send_message_with_only_from_field_unchanged (self , http_client_response ):
1029+ """Test that when only from field is present, it remains unchanged."""
1030+ messages = Messages (http_client_response )
1031+ request_body = {
1032+ "subject" : "Hello from Nylas!" ,
1033+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1034+ "body" : "This is the body of my message." ,
1035+ "from" : [{
"name" :
"Direct Sender" ,
"email" :
"[email protected] " }],
1036+ }
1037+
1038+ messages .send (identifier = "abc-123" , request_body = request_body )
1039+
1040+ # Verify that the from field remains unchanged
1041+ expected_request_body = {
1042+ "subject" : "Hello from Nylas!" ,
1043+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1044+ "body" : "This is the body of my message." ,
1045+ "from" : [{
"name" :
"Direct Sender" ,
"email" :
"[email protected] " }],
1046+ }
1047+
1048+ http_client_response ._execute .assert_called_once_with (
1049+ method = "POST" ,
1050+ path = "/v3/grants/abc-123/messages/send" ,
1051+ request_body = expected_request_body ,
1052+ data = None ,
1053+ overrides = None ,
1054+ )
1055+
1056+ def test_send_message_without_from_fields_unchanged (self , http_client_response ):
1057+ """Test that request body without from or from_ fields remains unchanged."""
1058+ messages = Messages (http_client_response )
1059+ request_body = {
1060+ "subject" : "Hello from Nylas!" ,
1061+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1062+ "body" : "This is the body of my message." ,
1063+ }
1064+
1065+ messages .send (identifier = "abc-123" , request_body = request_body )
1066+
1067+ # Verify that the request body remains unchanged
1068+ expected_request_body = {
1069+ "subject" : "Hello from Nylas!" ,
1070+ "to" : [{
"name" :
"Jon Snow" ,
"email" :
"[email protected] " }],
1071+ "body" : "This is the body of my message." ,
1072+ }
1073+
1074+ http_client_response ._execute .assert_called_once_with (
1075+ method = "POST" ,
1076+ path = "/v3/grants/abc-123/messages/send" ,
1077+ request_body = expected_request_body ,
1078+ data = None ,
1079+ overrides = None ,
1080+ )
0 commit comments