@@ -393,7 +393,7 @@ def test_process_slack_message_dm_context(
393393
394394
395395@patch ("app.utils.handler_utils.is_latest_message" )
396- def test_process_async_slack_action (
396+ def test_process_async_slack_action_positive (
397397 mock_is_latest_message : Mock ,
398398 mock_get_parameter : Mock ,
399399 mock_env : Mock ,
@@ -435,3 +435,185 @@ def test_process_async_slack_action(
435435 text = "Thank you for your feedback." ,
436436 thread_ts = "1759845114.407989" ,
437437 )
438+
439+
440+ @patch ("app.utils.handler_utils.is_latest_message" )
441+ def test_process_async_slack_action_negative (
442+ mock_is_latest_message : Mock ,
443+ mock_get_parameter : Mock ,
444+ mock_env : Mock ,
445+ ):
446+ """Test successful async action processing"""
447+ # set up mocks
448+ mock_client = Mock ()
449+ mock_is_latest_message .return_value = True
450+
451+ # delete and import module to test
452+ if "app.slack.slack_events" in sys .modules :
453+ del sys .modules ["app.slack.slack_events" ]
454+ from app .slack .slack_events import process_async_slack_action
455+
456+ feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}'
457+
458+ # perform operation
459+ slack_action_data = {
460+ "type" : "block_actions" ,
461+ "user" : {"id" : "U123" },
462+ "channel" : {"id" : "C123" },
463+ "actions" : [{"action_id" : "feedback_no" , "value" : feedback_value }],
464+ }
465+ with patch ("app.slack.slack_events.store_feedback" ) as mock_store_feedback :
466+ process_async_slack_action (body = slack_action_data , client = mock_client )
467+
468+ # assertions
469+ mock_store_feedback .assert_called_once_with (
470+ conversation_key = "thread#C123#123" ,
471+ feedback_type = "negative" ,
472+ user_id = "U123" ,
473+ channel_id = "C123" ,
474+ thread_ts = "1759845114.407989" ,
475+ message_ts = "1759845126.972219" ,
476+ client = mock_client ,
477+ )
478+ mock_client .chat_postMessage .assert_called_once_with (
479+ channel = "C123" ,
480+ text = 'Please let us know how the answer could be improved. Start your message with "feedback:"' ,
481+ thread_ts = "1759845114.407989" ,
482+ )
483+
484+
485+ @patch ("app.utils.handler_utils.is_latest_message" )
486+ def test_process_async_slack_action_not_latest (
487+ mock_is_latest_message : Mock ,
488+ mock_get_parameter : Mock ,
489+ mock_env : Mock ,
490+ ):
491+ """Test successful async action processing"""
492+ # set up mocks
493+ mock_client = Mock ()
494+ mock_is_latest_message .return_value = False
495+
496+ # delete and import module to test
497+ if "app.slack.slack_events" in sys .modules :
498+ del sys .modules ["app.slack.slack_events" ]
499+ from app .slack .slack_events import process_async_slack_action
500+
501+ feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}'
502+
503+ # perform operation
504+ slack_action_data = {
505+ "type" : "block_actions" ,
506+ "user" : {"id" : "U123" },
507+ "channel" : {"id" : "C123" },
508+ "actions" : [{"action_id" : "feedback_no" , "value" : feedback_value }],
509+ }
510+ with patch ("app.slack.slack_events.store_feedback" ) as mock_store_feedback :
511+ process_async_slack_action (body = slack_action_data , client = mock_client )
512+
513+ # assertions
514+ mock_store_feedback .assert_not_called ()
515+ mock_client .chat_postMessage .assert_not_called ()
516+
517+
518+ @patch ("app.utils.handler_utils.is_latest_message" )
519+ def test_process_async_slack_action_unknown_action (
520+ mock_is_latest_message : Mock ,
521+ mock_get_parameter : Mock ,
522+ mock_env : Mock ,
523+ ):
524+ """Test successful async action processing"""
525+ # set up mocks
526+ mock_client = Mock ()
527+ mock_is_latest_message .return_value = True
528+
529+ # delete and import module to test
530+ if "app.slack.slack_events" in sys .modules :
531+ del sys .modules ["app.slack.slack_events" ]
532+ from app .slack .slack_events import process_async_slack_action
533+
534+ feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}'
535+
536+ # perform operation
537+ slack_action_data = {
538+ "type" : "block_actions" ,
539+ "user" : {"id" : "U123" },
540+ "channel" : {"id" : "C123" },
541+ "actions" : [{"action_id" : "I_Do_Not_Know_This_Action" , "value" : feedback_value }],
542+ }
543+ with patch ("app.slack.slack_events.store_feedback" ) as mock_store_feedback :
544+ process_async_slack_action (body = slack_action_data , client = mock_client )
545+
546+ # assertions
547+ mock_store_feedback .assert_not_called ()
548+ mock_client .chat_postMessage .assert_not_called ()
549+
550+
551+ def test_process_feedback_event ():
552+ # set up mocks
553+ mock_client = Mock ()
554+
555+ # delete and import module to test
556+ if "app.slack.slack_events" in sys .modules :
557+ del sys .modules ["app.slack.slack_events" ]
558+ from app .slack .slack_events import process_feedback_event
559+
560+ # perform operation
561+ mock_event = {}
562+ with patch ("app.slack.slack_events.store_feedback" ) as mock_store_feedback :
563+ process_feedback_event (
564+ message_text = "feedback: this is some feedback" ,
565+ conversation_key = "thread#C123#123" ,
566+ user_id = "U123" ,
567+ channel_id = "C123" ,
568+ thread_root = "1759845114.407989" ,
569+ event = mock_event ,
570+ client = mock_client ,
571+ )
572+
573+ # assertions
574+ mock_store_feedback .assert_called_once_with (
575+ conversation_key = "thread#C123#123" ,
576+ feedback_type = "additional" ,
577+ user_id = "U123" ,
578+ channel_id = "C123" ,
579+ thread_ts = "1759845114.407989" ,
580+ message_ts = None ,
581+ feedback_text = "this is some feedback" ,
582+ client = mock_client ,
583+ )
584+ mock_client .chat_postMessage .assert_called_once_with (
585+ channel = "C123" , text = "Thank you for your feedback." , thread_ts = "1759845114.407989"
586+ )
587+
588+
589+ @patch ("app.services.slack.post_error_message" )
590+ def test_process_feedback_event_error (
591+ mock_post_error_message : Mock ,
592+ ):
593+ # set up mocks
594+ mock_client = Mock ()
595+
596+ # delete and import module to test
597+ if "app.slack.slack_events" in sys .modules :
598+ del sys .modules ["app.slack.slack_events" ]
599+ from app .slack .slack_events import process_feedback_event
600+
601+ # perform operation
602+ mock_event = {
603+ "channel" : "C123" ,
604+ "thread_ts" : "123" ,
605+ }
606+ with patch ("app.slack.slack_events.store_feedback" ) as mock_store_feedback :
607+ mock_store_feedback .side_effect = Exception ("There was an error" )
608+ process_feedback_event (
609+ message_text = "feedback: this is some feedback" ,
610+ conversation_key = "thread#C123#123" ,
611+ user_id = "U123" ,
612+ channel_id = "C123" ,
613+ thread_root = "1759845114.407989" ,
614+ event = mock_event ,
615+ client = mock_client ,
616+ )
617+
618+ # assertions
619+ mock_post_error_message .assert_called_once_with (channel = "C123" , thread_ts = "123" , client = mock_client )
0 commit comments