class SecureTeaAwsSES(): """Initilize AWS SES.""" modulename = "AWS_SES" enabled = True def __init__(self, cred, debug): """Init logger params. Args: modulename (str): Script module name cred (dict): AWS user_email, access_key, secret_key """ self.logger = logger.SecureTeaLogger( self.modulename, debug ) self.enabled = common.check_config(cred) if not self.enabled: self.logger.log( "Credentials not present, please set AWS SES config at ~/.securetea/securetea.conf ", logtype="error" ) self.user_email = cred['aws_email'] self.access_key = cred['aws_access_key'] self.secret_key = cred['aws_secret_key'] self.email_obj = Email(self.user_email, "secureTea Security Alert!", self.access_key, self.secret_key) def notify(self, msg): """Docstring. Init: "Welcome to SecureTea..!! Initializing System" Intrusion detector: "(Count) Someone has access your laptop" Args: msg (TYPE): Description """ message = (str(msg) + " at " + common.getdatetime() + " " + common.get_current_location() + common.get_platform()) html_str = "<html><head></head><body><h1>Security Alert</h1><p>"+message+"</p></body></html>" self.email_obj.html(html_str) typ, typ_desc = self.email_obj.send() if typ == "Ok": self.logger.log( "Notification sent, Message Id: "+ str(typ_desc) ) else: self.logger.log( "Aws SES notification not sent, error is: " + str(typ_desc), logtype="error" ) return
class TestSecureTeaHelperEmail(unittest.TestCase): """ Test class for SecureTea AWS SES Helper Email. """ def setUp(self): """ Setup class for SecureTeaGmail. """ # Setup Helper email object self.email_obj = Email("*****@*****.**", "secureTea Security Alert!", "random-key", "random-secret") self.email_obj.html("HTML Text") self.email_obj.text("Random Text") def test_get_details(self): """ Test get_details. """ details_dict = { "Destination": { 'ToAddresses': [ "*****@*****.**", ], }, "Message": { 'Body': { 'Html': { 'Charset': "UTF-8", 'Data': "HTML Text", }, 'Text': { 'Charset': "UTF-8", 'Data': "Random Text", }, }, 'Subject': { 'Charset': "UTF-8", 'Data': "secureTea Security Alert!", }, }, "Source": "*****@*****.**" } self.assertEqual(details_dict, self.email_obj.get_details()) @patch('securetea.lib.notifs.aws.helper_email.boto3') def test_send(self, boto3): """ Test send. """ boto3.client.return_value.send_email.return_value = { "MessageId": "Success" } self.assertEqual(self.email_obj.send(), ("Ok", "Success")) def test_html(self): """ Test html. """ self.email_obj.html("<html></html>") self.assertEqual("<html></html>", self.email_obj._html) def test_text(self): """ Test text. """ self.email_obj.text("Random") self.assertEqual("Random", self.email_obj._text)