Skip to content

Commit

Permalink
Merge pull request #219 from olinlibrary/origin/seamless/new-emails
Browse files Browse the repository at this point in the history
Origin/seamless/new emails
  • Loading branch information
osteele committed May 5, 2018
2 parents 158217d + 21d6071 commit 2f5355f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 54 deletions.
52 changes: 24 additions & 28 deletions abe/helper_functions/email_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import icalendar as ic
from mongoengine import ValidationError
from datetime import datetime as dt
from email.message import EmailMessage

from abe import database as db
from abe.helper_functions.converting_helpers import mongo_to_dict
Expand All @@ -17,6 +19,7 @@
ABE_EMAIL_PASSWORD = os.environ.get('ABE_EMAIL_PASSWORD', None)
ABE_EMAIL_HOST = os.environ.get('ABE_EMAIL_HOST', 'mail.privateemail.com')
ABE_EMAIL_PORT = int(os.environ.get('ABE_EMAIL_PORT', 465))
APP_URL = os.environ.get('APP_URL', 'events.olin.build')


def get_msg_list(pop_items, pop_conn):
Expand Down Expand Up @@ -159,45 +162,38 @@ def error_reply(to, error):
""" Given the error, sends an email with the errors that
occured to the original sender. """
server, sent_from = smtp_connect()
subject = 'Event Failed to Add'
msg = EmailMessage()
body = "ABE didn't manage to add the event, sorry. Here's what went wrong: \n"
for err in error.errors:
body = body + str(err) + '\n'
body = body + "Final error message: " + error.message

email_text = """
From: {}
To: {}
Subject: {}
{}
""".format(sent_from, to, subject, body)

send_email(server, email_text, sent_from, to)
msg['Subject'] = 'Event Failed to Add'
msg['From'] = sent_from
msg['To'] = [to]
msg.set_content(body)
server.send_message(msg)
server.close()


def reply_email(to, event_dict):
""" Responds after a successful posting with
the tags under which the event was saved. """
server, sent_from = smtp_connect()
subject = '{} added to ABE!'.format(event_dict['title'])
tags = ', '.join(event_dict['labels']).strip()
body = "Your event was added to ABE! Here's the details: "

email_text = """
From: {}
To: {}
Subject: {}
{}
Description: {}
Tags: {}
""".format(sent_from, to, subject, body, event_dict['description'], tags)
send_email(server, email_text, sent_from, to)


def send_email(server, email_text, sent_from, sent_to):
server.sendmail(sent_from, sent_to, email_text.encode('utf-8'))
start = dt.strptime(event_dict['start'][:16], '%Y-%m-%d %H:%M').strftime('%I:%M %m/%d')
end = dt.strptime(event_dict['end'][:16], '%Y-%m-%d %H:%M').strftime('%I:%M %m/%d')
body = f"""Your event was added to ABE! Here's the details:
Time: {start} to {end}
Description: {event_dict['description']}
Tags: {tags}
Something wrong? Edit this event at {APP_URL}/edit/{event_dict['id']}
"""
msg = EmailMessage()
msg['Subject'] = f"{event_dict['title']} added to ABE!"
msg['From'] = sent_from
msg['To'] = [to]
msg.set_content(body)
server.send_message(msg)
server.close()


Expand Down
49 changes: 23 additions & 26 deletions tests/test_email_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import email
from unittest.mock import ANY, Mock, patch
from unittest.mock import Mock, patch

from icalendar import Calendar

Expand All @@ -15,6 +15,10 @@
with open('./tests/cal_script.txt', 'r') as cal_file:
cal = Calendar.from_ical(cal_file.read())

serv = Mock()
serv.send_message = Mock()
serv.close = Mock()


class EmailHelpersTestCase(abe_unittest.TestCase):

Expand Down Expand Up @@ -70,40 +74,33 @@ def test_smtp_connect(self):
server.ehlo.assert_called()
server.login.assert_called()

@patch('abe.helper_functions.email_helpers.send_email', return_value=None)
@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=('server', 'from_addr'))
def test_error_reply(self, smtp, send):
@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=(serv, 'from_addr'))
def test_error_reply(self, smtp):
error = Mock()
error.errors = [12]
error.message = "This is an error message"
to = "to_addr"
email_helpers.error_reply(to, error)
smtp.assert_called()
send.assert_called_with('server', ANY, 'from_addr', to)

@patch('abe.helper_functions.email_helpers.send_email', return_value=None)
@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=('server', 'from_addr'))
def test_reply_email(self, smtp, send):
event_dict = {'title': 'Test', 'labels': ['test'], 'description': 'empty test'}
serv.send_message.assert_called()
serv.close.assert_called()

@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=(serv, 'from_addr'))
def test_reply_email(self, smtp):
event_dict = {'title': 'Test',
'start': '2018-04-30 14:51:24',
'end': '2018-04-30 14:51:24',
'labels': ['test'],
'description': 'empty test',
'id': 'id_string'}
to = "to_addr"
email_helpers.reply_email(to, event_dict)
smtp.assert_called()
send.assert_called_with('server', ANY, 'from_addr', to)

def test_send_email(self):
server = Mock()
server.sendmail = Mock()
server.close = Mock()
email_text = 'email text'
sent_from = 'from address'
to = 'to address'
email_helpers.send_email(server, email_text, sent_from, to)
server.sendmail.assert_called_once_with(sent_from, to, email_text.encode('utf-8'))
server.close.assert_called_once()

@patch('abe.helper_functions.email_helpers.send_email', return_value=None)
@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=('server', 'from_addr'))
serv.send_message.assert_called()
serv.close.assert_called()

@patch('abe.helper_functions.email_helpers.smtp_connect', return_value=(serv, 'from_addr'))
@patch('abe.helper_functions.email_helpers.get_messages_from_email', return_value=[message])
def test_scrape(self, msgs_from_email, smtp, send):
def test_scrape(self, msgs_from_email, smtp):
completed = email_helpers.scrape()
self.assertEqual(len(completed), 1)

0 comments on commit 2f5355f

Please sign in to comment.