<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import Permission <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase , override_settings <EOL> from django . utils . dateparse import parse_datetime <EOL> from mock import patch , call <EOL> from model_mommy import mommy <EOL> from open_connect . connectmessages . models import Message <EOL> from open_connect . connectmessages . tests import ConnectMessageTestCase <EOL> from open_connect . notifications import tasks <EOL> from open_connect . notifications . models import Notification <EOL> from open_connect . connect_core . utils . basetests import ConnectTestMixin <EOL> class TestCreateGroupNotifications ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sender = mommy . make ( '<STR_LIT>' ) <EOL> self . group = mommy . make ( '<STR_LIT>' ) <EOL> self . sender . add_to_group ( self . group . pk ) <EOL> self . thread = mommy . make ( '<STR_LIT>' , group = self . group ) <EOL> self . message = mommy . make ( <EOL> '<STR_LIT>' , <EOL> sender = self . sender , <EOL> thread = self . thread <EOL> ) <EOL> Notification . objects . filter ( message_id = self . message . pk ) . delete ( ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_create_group_notification ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> immediate_user = mommy . make ( '<STR_LIT>' ) <EOL> immediate_user . add_to_group ( self . group . pk ) <EOL> tasks . create_group_notifications ( self . message . pk ) <EOL> notification = Notification . objects . get ( <EOL> recipient = immediate_user , message = self . message ) <EOL> mock . delay . assert_called_once_with ( notification . pk ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_no_notification_created_for_none_period ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> none_user = mommy . make ( '<STR_LIT>' ) <EOL> none_user . add_to_group ( self . group . pk , period = '<STR_LIT:none>' ) <EOL> tasks . create_group_notifications ( self . message . pk ) <EOL> self . assertFalse ( <EOL> Notification . objects . filter ( <EOL> recipient = none_user , message = self . message ) . exists ( ) <EOL> ) <EOL> self . assertFalse ( mock . delay . called ) <EOL> def test_unsubscribed_user_not_added ( self ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' , unsubscribed = True ) <EOL> user . add_to_group ( self . group . pk , period = '<STR_LIT>' ) <EOL> user . save ( ) <EOL> tasks . create_group_notifications ( self . message . pk ) <EOL> self . assertFalse ( <EOL> Notification . objects . filter ( message_id = self . message . pk ) . exists ( ) ) <EOL> self . assertFalse ( <EOL> Notification . objects . filter ( <EOL> message_id = self . message . pk , recipient = user ) <EOL> ) <EOL> class TestCreateRecipientNotifications ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . thread = mommy . make ( '<STR_LIT>' ) <EOL> self . message = mommy . make ( '<STR_LIT>' , thread = self . thread ) <EOL> def test_notification_created_for_recipient ( self ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' ) <EOL> mommy . make ( '<STR_LIT>' , thread = self . thread , user = user ) <EOL> tasks . create_recipient_notifications ( self . message . pk ) <EOL> self . assertTrue ( <EOL> Notification . objects . filter ( <EOL> recipient = user , <EOL> message_id = self . message . pk , <EOL> consumed = True , <EOL> message = self . message <EOL> ) . exists ( ) <EOL> ) <EOL> def test_notification_not_created_for_unsubscribed_user ( self ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' , unsubscribed = True ) <EOL> mommy . make ( '<STR_LIT>' , thread = self . thread , user = user ) <EOL> tasks . create_recipient_notifications ( self . message . pk ) <EOL> self . assertFalse ( <EOL> Notification . objects . filter ( <EOL> recipient = user , <EOL> message = self . message <EOL> ) . exists ( ) <EOL> ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_sends_immediate_notification ( self , mock_send ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( <EOL> '<STR_LIT>' , direct_notification_period = '<STR_LIT>' ) <EOL> mommy . make ( '<STR_LIT>' , thread = self . thread , user = user ) <EOL> tasks . create_recipient_notifications ( self . message . pk ) <EOL> notification = Notification . objects . get ( <EOL> recipient = user , message = self . message ) <EOL> mock_send . delay . assert_called_once_with ( notification . pk ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> class TestSendImmediateNotification ( ConnectMessageTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ override_settings ( BRAND_TITLE = '<STR_LIT>' ) <EOL> def test_called ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' ) <EOL> notification = mommy . make ( <EOL> Notification , <EOL> recipient = user , message = self . message1 ) <EOL> self . assertFalse ( notification . consumed ) <EOL> tasks . send_immediate_notification ( notification . pk ) <EOL> self . assertTrue ( <EOL> Notification . objects . get ( pk = notification . pk ) . consumed ) <EOL> mock . assert_called_once ( ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> email = args [ '<STR_LIT:email>' ] <EOL> self . assertIn ( user . email , email ) <EOL> self . assertIn ( user . get_full_name ( ) , email ) <EOL> from_email = args [ '<STR_LIT>' ] <EOL> self . assertIn ( settings . DEFAULT_FROM_ADDRESS , from_email ) <EOL> self . assertIn ( '<STR_LIT>' , from_email ) <EOL> self . assertIn ( self . message1 . sender . get_full_name ( ) , from_email ) <EOL> subject = args [ '<STR_LIT>' ] <EOL> self . assertIn ( str ( self . message1 . thread . group ) , subject ) <EOL> self . assertIn ( self . message1 . thread . subject , subject ) <EOL> html = args [ '<STR_LIT:html>' ] <EOL> plaintext = args [ '<STR_LIT:text>' ] <EOL> self . assertIn ( self . message1 . text , html ) <EOL> self . assertIn ( '<STR_LIT>' , html ) <EOL> self . assertIn ( self . message1 . clean_text , plaintext ) <EOL> @ override_settings ( BRAND_TITLE = '<STR_LIT>' ) <EOL> def test_called_direct_message ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' ) <EOL> notification = mommy . make ( <EOL> Notification , <EOL> recipient = user , message = self . directmessage1 ) <EOL> self . assertFalse ( notification . consumed ) <EOL> tasks . send_immediate_notification ( notification . pk ) <EOL> self . assertIsNotNone ( <EOL> Notification . objects . get ( pk = notification . pk ) . consumed ) <EOL> mock . assert_called_once ( ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> email = args [ '<STR_LIT:email>' ] <EOL> self . assertIn ( user . email , email ) <EOL> self . assertIn ( user . get_full_name ( ) , email ) <EOL> from_email = args [ '<STR_LIT>' ] <EOL> self . assertIn ( settings . DEFAULT_FROM_ADDRESS , from_email ) <EOL> self . assertIn ( '<STR_LIT>' , from_email ) <EOL> self . assertIn ( self . directmessage1 . sender . get_full_name ( ) , from_email ) <EOL> subject = args [ '<STR_LIT>' ] <EOL> self . assertEqual ( self . directmessage1 . thread . subject , subject ) <EOL> html = args [ '<STR_LIT:html>' ] <EOL> plaintext = args [ '<STR_LIT:text>' ] <EOL> self . assertIn ( self . directmessage1 . text , html ) <EOL> self . assertIn ( '<STR_LIT>' , html ) <EOL> self . assertIn ( self . directmessage1 . clean_text , plaintext ) <EOL> def test_includes_unsubscribe_link ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = mommy . make ( '<STR_LIT>' ) <EOL> notification = mommy . make ( <EOL> Notification , <EOL> recipient = user , <EOL> message = self . message1 <EOL> ) <EOL> tasks . send_immediate_notification ( notification . pk ) <EOL> mock . assert_called_once ( ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> self . assertIn ( '<STR_LIT>' , args [ '<STR_LIT:html>' ] . lower ( ) ) <EOL> self . assertIn ( user . unsubscribe_url , args [ '<STR_LIT:html>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , args [ '<STR_LIT:text>' ] . lower ( ) ) <EOL> self . assertIn ( user . unsubscribe_url , args [ '<STR_LIT:text>' ] ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> class TestSendDailyDigestNotification ( ConnectTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_marked_consumed ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> group = self . create_group ( ) <EOL> user . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> thread1 = self . create_thread ( group = group ) <EOL> message1 = thread1 . first_message <EOL> notification1 = user . notification_set . get ( message = message1 ) <EOL> thread2 = self . create_thread ( group = group ) <EOL> message2 = thread2 . first_message <EOL> notification2 = user . notification_set . get ( message = message2 ) <EOL> self . assertFalse ( <EOL> Notification . objects . get ( pk = notification1 . pk ) . consumed ) <EOL> self . assertFalse ( <EOL> Notification . objects . get ( pk = notification2 . pk ) . consumed ) <EOL> tasks . send_daily_digest_notification ( user . pk ) <EOL> mock . assert_called_once ( ) <EOL> self . assertTrue ( <EOL> Notification . objects . get ( pk = notification1 . pk ) . consumed ) <EOL> self . assertTrue ( <EOL> Notification . objects . get ( pk = notification2 . pk ) . consumed ) <EOL> def test_outgoing_email ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> group = self . create_group ( ) <EOL> user . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> thread1 = self . create_thread ( group = group ) <EOL> thread2 = self . create_thread ( group = group ) <EOL> tasks . send_daily_digest_notification ( user . pk ) <EOL> mock . assert_called_once ( ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> email = args [ '<STR_LIT:email>' ] <EOL> self . assertIn ( user . email , email ) <EOL> self . assertIn ( user . get_full_name ( ) , email ) <EOL> from_email = args [ '<STR_LIT>' ] <EOL> self . assertIn ( settings . DEFAULT_FROM_ADDRESS , from_email ) <EOL> self . assertEqual ( settings . DEFAULT_FROM_EMAIL , from_email ) <EOL> subject = args [ '<STR_LIT>' ] <EOL> self . assertIn ( '<STR_LIT>' , subject ) <EOL> days_of_week = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> self . assertTrue ( any ( [ dayname in subject for dayname in days_of_week ] ) ) <EOL> html = args [ '<STR_LIT:html>' ] <EOL> plaintext = args [ '<STR_LIT:text>' ] <EOL> self . assertIn ( thread1 . first_message . text , html ) <EOL> self . assertIn ( thread2 . first_message . text , html ) <EOL> self . assertIn ( '<STR_LIT>' , html ) <EOL> self . assertIn ( thread1 . first_message . clean_text , plaintext ) <EOL> self . assertIn ( thread2 . first_message . clean_text , plaintext ) <EOL> def test_no_pending_messages ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> group = self . create_group ( ) <EOL> user . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> valid_thread = self . create_thread ( group = group ) <EOL> pending_thread = self . create_thread ( group = group ) <EOL> valid_message = valid_thread . first_message <EOL> pending_message = pending_thread . first_message <EOL> pending_message . status = '<STR_LIT>' <EOL> pending_message . save ( ) <EOL> valid_notification = user . notification_set . get ( <EOL> message = valid_message ) <EOL> pending_notification = user . notification_set . get ( <EOL> message = pending_message ) <EOL> tasks . send_daily_digest_notification ( user . pk ) <EOL> mock . assert_called_once ( ) <EOL> self . assertTrue ( <EOL> Notification . objects . get ( pk = valid_notification . pk ) . consumed ) <EOL> self . assertFalse ( <EOL> Notification . objects . get ( pk = pending_notification . pk ) . consumed ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> html = args [ '<STR_LIT:html>' ] <EOL> self . assertIn ( valid_message . text , html ) <EOL> self . assertNotIn ( pending_message . text , html ) <EOL> pending_message . status = '<STR_LIT>' <EOL> pending_message . save ( ) <EOL> tasks . send_daily_digest_notification ( user . pk ) <EOL> self . assertTrue ( <EOL> Notification . objects . get ( pk = pending_notification . pk ) . consumed ) <EOL> def test_includes_unsubscribe_link ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> thread = self . create_thread ( ) <EOL> mommy . make ( <EOL> Notification , <EOL> recipient = user , <EOL> message = thread . first_message , <EOL> subscription__period = '<STR_LIT>' <EOL> ) <EOL> tasks . send_daily_digest_notification ( user . pk ) <EOL> mock . assert_called_once ( ) <EOL> args = mock . call_args [ <NUM_LIT:1> ] <EOL> self . assertIn ( '<STR_LIT>' , args [ '<STR_LIT:html>' ] . lower ( ) ) <EOL> self . assertIn ( user . unsubscribe_url , args [ '<STR_LIT:html>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , args [ '<STR_LIT:html>' ] . lower ( ) ) <EOL> self . assertIn ( user . unsubscribe_url , args [ '<STR_LIT:html>' ] ) <EOL> def test_no_notifications ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> response = tasks . send_daily_digest_notification ( user . pk ) <EOL> self . assertIsNone ( response ) <EOL> self . assertEqual ( mock . call_count , <NUM_LIT:0> ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> class SendDailyEmailNotifications ( ConnectTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_called ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> Notification . objects . update ( consumed = True ) <EOL> group = self . create_group ( ) <EOL> sender = self . create_superuser ( ) <EOL> user1 = self . create_user ( ) <EOL> user2 = self . create_user ( ) <EOL> user3 = self . create_user ( ) <EOL> sender . add_to_group ( group . pk ) <EOL> user1 . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> user2 . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> user3 . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> self . create_thread ( sender = sender , group = group ) <EOL> self . assertEqual ( mock . delay . call_count , <NUM_LIT:0> ) <EOL> tasks . send_daily_email_notifications ( ) <EOL> self . assertEqual ( mock . delay . call_count , <NUM_LIT:2> ) <EOL> self . assertIn ( call ( user1 . pk ) , mock . delay . call_args_list ) <EOL> self . assertIn ( call ( user2 . pk ) , mock . delay . call_args_list ) <EOL> def test_unapproved_not_sent ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> Notification . objects . update ( consumed = True ) <EOL> group = self . create_group ( ) <EOL> user1 = self . create_user ( ) <EOL> user1 . add_to_group ( group . pk , period = '<STR_LIT>' ) <EOL> thread = self . create_thread ( group = group ) <EOL> message = thread . first_message <EOL> message . status = '<STR_LIT>' <EOL> message . save ( ) <EOL> tasks . send_daily_email_notifications ( ) <EOL> self . assertEqual ( mock . delay . call_count , <NUM_LIT:0> ) <EOL> message . status = '<STR_LIT>' <EOL> message . save ( ) <EOL> tasks . send_daily_email_notifications ( ) <EOL> self . assertEqual ( mock . delay . call_count , <NUM_LIT:1> ) <EOL> mock . delay . assert_called_once_with ( user1 . pk ) <EOL> class ModerationNotificationsTest ( ConnectTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . one_hour_ago = parse_datetime ( '<STR_LIT>' ) <EOL> self . two_hours_ago = parse_datetime ( '<STR_LIT>' ) <EOL> self . one_day_ago = parse_datetime ( '<STR_LIT>' ) <EOL> self . two_days_ago = parse_datetime ( '<STR_LIT>' ) <EOL> def generate_pending_message ( <EOL> self , group , last_modified_datetime , status = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> with patch . object ( tasks , '<STR_LIT>' ) : <EOL> thread = self . create_thread ( group = group ) <EOL> Message . objects . filter ( pk = thread . first_message . pk ) . update ( <EOL> status = status , modified_at = last_modified_datetime ) <EOL> return thread . first_message <EOL> class TestSendModerationNotification ( ModerationNotificationsTest ) : <EOL> """<STR_LIT>""" <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_no_valid_messages ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> self . generate_pending_message ( group , self . two_hours_ago ) <EOL> tasks . send_moderation_notification ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> self . assertFalse ( mock . called ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_valid_message ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . one_hour_ago ) <EOL> tasks . send_moderation_notification ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> call_kwargs = mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( mock . called ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT:email>' ] , moderator . email ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:text>' ] ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:html>' ] ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_valid_message_flagged ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( <EOL> group , self . one_hour_ago , status = '<STR_LIT>' ) <EOL> tasks . send_moderation_notification ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> call_kwargs = mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( mock . called ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT:email>' ] , moderator . email ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:text>' ] ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:html>' ] ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_valid_messages ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message1 = self . generate_pending_message ( group , self . one_hour_ago ) <EOL> message2 = self . generate_pending_message ( group , self . one_hour_ago ) <EOL> tasks . send_moderation_notification ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> call_kwargs = mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertIn ( message1 , moderator . messages_to_moderate ) <EOL> self . assertIn ( message2 , moderator . messages_to_moderate ) <EOL> mock . assert_called_once ( ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT:email>' ] , moderator . email ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:text>' ] ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:html>' ] ) <EOL> @ patch . object ( tasks , '<STR_LIT>' ) <EOL> def test_valid_messages_one_old ( self , mock ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message1 = self . generate_pending_message ( group , self . one_hour_ago ) <EOL> message2 = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notification ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> call_kwargs = mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> self . assertIn ( message1 , moderator . messages_to_moderate ) <EOL> self . assertIn ( message2 , moderator . messages_to_moderate ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT:email>' ] , moderator . email ) <EOL> self . assertEqual ( call_kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( <EOL> '<STR_LIT>' , call_kwargs [ '<STR_LIT:html>' ] ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:text>' ] ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , call_kwargs [ '<STR_LIT:html>' ] ) <EOL> class TestSendModerationNotifications ( ModerationNotificationsTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( TestSendModerationNotifications , self ) . setUp ( ) <EOL> now_patcher = patch ( '<STR_LIT>' ) <EOL> self . mock_now = now_patcher . start ( ) <EOL> self . mock_now . return_value = parse_datetime ( <EOL> '<STR_LIT>' ) <EOL> self . addCleanup ( now_patcher . stop ) <EOL> mod_patcher = patch ( <EOL> '<STR_LIT>' ) <EOL> self . mock_modmessage = mod_patcher . start ( ) <EOL> self . addCleanup ( mod_patcher . stop ) <EOL> def test_no_recent_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . two_days_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertFalse ( self . mock_modmessage . delay . called ) <EOL> def test_one_recent_message ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( self . mock_modmessage . delay . called ) <EOL> self . mock_modmessage . delay . assert_called_once_with ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> def test_one_recent_message_flagged ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( <EOL> group , self . one_day_ago , status = '<STR_LIT>' ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( self . mock_modmessage . delay . called ) <EOL> self . mock_modmessage . delay . assert_called_once_with ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> def test_recent_message_non_notification_period ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:6> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertFalse ( self . mock_modmessage . delay . called ) <EOL> def test_multiple_moderators ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator1 = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> moderator2 = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> group . owners . add ( moderator1 ) <EOL> group . owners . add ( moderator2 ) <EOL> message = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator1 . messages_to_moderate ) <EOL> self . assertIn ( message , moderator2 . messages_to_moderate ) <EOL> self . assertTrue ( self . mock_modmessage . delay . called ) <EOL> self . assertEqual ( self . mock_modmessage . delay . call_count , <NUM_LIT:2> ) <EOL> def test_moderator_disables_notifications ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:0> ) <EOL> group . owners . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . two_days_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertFalse ( self . mock_modmessage . delay . called ) <EOL> def test_user_has_moderate_all_messages_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> self . add_perm ( <EOL> moderator , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> message = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( self . mock_modmessage . delay . called ) <EOL> self . mock_modmessage . delay . assert_called_once_with ( <EOL> moderator . pk , '<STR_LIT>' ) <EOL> def test_user_in_group_with_moderate_all_messages_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' ) <EOL> moderator = self . create_user ( moderator_notification_period = <NUM_LIT:1> ) <EOL> perm_group = self . create_group ( ) <EOL> permission = Permission . objects . get_by_natural_key ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' ) <EOL> perm_group . group . permissions . add ( permission ) <EOL> perm_group . group . user_set . add ( moderator ) <EOL> message = self . generate_pending_message ( group , self . one_day_ago ) <EOL> tasks . send_moderation_notifications ( ) <EOL> self . assertIn ( message , moderator . messages_to_moderate ) <EOL> self . assertTrue ( self . mock_modmessage . delay . called ) <EOL> self . mock_modmessage . delay . assert_called_once_with ( <EOL> moderator . pk , '<STR_LIT>' ) </s>
