<s> import logging <EOL> import datetime <EOL> from modularodm import Q <EOL> from framework . email . tasks import send_email <EOL> from website import mails <EOL> from website import models <EOL> from website import settings <EOL> from website . app import init_app <EOL> from scripts import utils as script_utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> FROM_ADDR = '<STR_LIT>' <EOL> MESSAGE_NAME = '<STR_LIT>' <EOL> SECURITY_MESSAGE = mails . Mail ( <EOL> '<STR_LIT>' , <EOL> subject = '<STR_LIT>' , <EOL> ) <EOL> def send_security_message ( user , label , mail ) : <EOL> if label in user . security_messages : <EOL> return <EOL> mails . send_mail ( <EOL> user . username , <EOL> mail , <EOL> from_addr = FROM_ADDR , <EOL> mailer = send_email , <EOL> user = user , <EOL> username = settings . MANDRILL_USERNAME , <EOL> password = settings . MANDRILL_PASSWORD , <EOL> mail_server = settings . MANDRILL_MAIL_SERVER , <EOL> ) <EOL> user . security_messages [ label ] = datetime . datetime . utcnow ( ) <EOL> user . save ( ) <EOL> def get_targets ( ) : <EOL> query = ( <EOL> Q ( '<STR_LIT>' . format ( MESSAGE_NAME ) , '<STR_LIT>' , False ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) & <EOL> Q ( '<STR_LIT:password>' , '<STR_LIT>' , None ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> ) <EOL> return models . User . find ( query ) <EOL> def main ( dry_run ) : <EOL> users = get_targets ( ) <EOL> for user in users : <EOL> logger . info ( '<STR_LIT>' . format ( user ) ) <EOL> if not dry_run : <EOL> send_security_message ( user , MESSAGE_NAME , SECURITY_MESSAGE ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> script_utils . add_file_logger ( logger , __file__ ) <EOL> dry_run = '<STR_LIT>' in sys . argv <EOL> init_app ( set_backends = True , routes = False ) <EOL> main ( dry_run = dry_run ) <EOL> import mock <EOL> from nose . tools import * <EOL> from tests . base import OsfTestCase <EOL> from tests . factories import UserFactory <EOL> class TestSendSecurityMessage ( OsfTestCase ) : <EOL> def tearDown ( self ) : <EOL> super ( TestSendSecurityMessage , self ) . tearDown ( ) <EOL> models . User . remove ( ) <EOL> def test_get_targets ( self ) : <EOL> users = [ UserFactory ( ) for _ in range ( <NUM_LIT:3> ) ] <EOL> users [ <NUM_LIT:0> ] . security_messages [ MESSAGE_NAME ] = datetime . datetime . utcnow ( ) <EOL> users [ <NUM_LIT:0> ] . save ( ) <EOL> targets = get_targets ( ) <EOL> assert_equal ( set ( targets ) , set ( users [ <NUM_LIT:1> : ] ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_send_mail ( self , mock_send_mail ) : <EOL> user = UserFactory ( ) <EOL> send_security_message ( user , MESSAGE_NAME , SECURITY_MESSAGE ) <EOL> user . reload ( ) <EOL> assert_in ( MESSAGE_NAME , user . security_messages ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_main ( self , mock_send_mail ) : <EOL> [ UserFactory ( ) for _ in range ( <NUM_LIT:3> ) ] <EOL> assert_equal ( len ( get_targets ( ) ) , <NUM_LIT:3> ) <EOL> main ( dry_run = False ) <EOL> assert_true ( mock_send_mail . called ) <EOL> assert_equal ( len ( get_targets ( ) ) , <NUM_LIT:0> ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_main_dry ( self , mock_send_mail ) : <EOL> [ UserFactory ( ) for _ in range ( <NUM_LIT:3> ) ] <EOL> assert_equal ( len ( get_targets ( ) ) , <NUM_LIT:3> ) <EOL> main ( dry_run = True ) <EOL> assert_false ( mock_send_mail . called ) <EOL> assert_equal ( len ( get_targets ( ) ) , <NUM_LIT:3> ) </s>
