Skip to content

Commit

Permalink
text cleanup, checking post response, and cleaner emailer
Browse files Browse the repository at this point in the history
  • Loading branch information
jparker165 committed Jun 16, 2014
1 parent d8fa546 commit a8175cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions backend/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def Subscribe(self, **kwargs):
logging.info('Subscribing %s', kwargs)


class MailSender(handlers.MailSender):
class MailSender(object):
def __init__(self, defer=True):
# this can
# this can
self.defer = defer

def Send(self, to, subject, text_body, html_body, reply_to=None):
Expand Down
24 changes: 10 additions & 14 deletions backend/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ def Subscribe(self, email, first_name, last_name, amount_cents, ip_addr, time,
raise NotImplementedError()


class MailSender(object):
"""Interface which sends mail."""
def Send(self, to, subject, text_body, html_body):
raise NotImplementedError()


_STR = dict(type='string')
class PledgeHandler(webapp2.RequestHandler):
"""RESTful handler for pledge objects."""
Expand Down Expand Up @@ -376,24 +370,25 @@ def post(self):
env = self.app.config['env']
util.EnableCors(self)

for field in ['team', 'team_leader_email', 'reply_to', 'subject', 'message_body', 'new_members']:
for field in ['team', 'reply_to', 'subject', 'message_body', 'new_members']:
if not field in self.request.POST:
msg = "Bad Request: required field %s missing." % field
logging.warning(msg)
self.error(400)
self.response.write(msg)
return self.response

# get the pldedges for this team, excluding the team owner
# get the pldedges for this team, excluding the reply_to
pledges = model.Pledge.all().filter(
'team =',self.request.POST['team']).filter(
'email !=', self.request.POST['team_leader_email'])
'email !=', self.request.POST['reply_to'])

for pledge in pledges:
# if only sending to new members, skip those that have a thank_you_sent_at
if self.request.POST['new_members'] and pledge.thank_you_sent_at:
continue
# if only sending to new members, filter out those that have already received emails

if self.request.POST['new_members'] == 'True':
pledges = pledges.filter('thank_you_sent_at =', None)

for pledge in pledges:
env.mail_sender.Send(to=pledge.email,
subject=self.request.POST['subject'],
text_body=self.request.POST['message_body'],
Expand All @@ -405,7 +400,8 @@ def post(self):
pledge.thank_you_sent_at = datetime.datetime.now()
pledge.put()

# FIXME: respond back with the number of messages sent
self.response.write(pledges.count())


def options(self):
util.EnableCors(self)
Expand Down
15 changes: 10 additions & 5 deletions unittests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,35 +350,40 @@ def testTeamTotal(self):
def testThankTeam(self):
self.makeDefaultRequest()

post_data = {'team': 'rocket', 'team_leader_email': self.pledge["email"],
'reply_to': 'the reply to', 'subject': 'the email subject',
post_data = {'team': 'rocket',
'reply_to': 'another@email.com', 'subject': 'the email subject',
'message_body': 'the message body', 'new_members': False}

# fails with a 400 error if the post request is missing any keys
with self.assertRaises(Exception):
resp = self.app.post('/r/thank', {})

# pledge doesn't get the email if they are the team leader
# pledge doesn't get the email if are the reply_to
post_data['reply_to'] = self.pledge["email"]
resp = self.app.post('/r/thank', post_data)
messages = self.mail_stub.get_sent_messages(to=self.pledge["email"])
# 1 email sent is the created pledge
self.assertEquals(len(messages), 1)
# post response should be zero sent thank you emails
self.assertEquals(resp.text, '0')

# this is the happy path
post_data['reply_to'] = 'another@email.com'
self.assertEquals(model.Pledge.all()[0].thank_you_sent_at, None)
post_data['team_leader_email'] = 'bob.loblaw@gmail.com'
resp = self.app.post('/r/thank', post_data)
messages = self.mail_stub.get_sent_messages(to=self.pledge["email"])
self.assertEquals(len(messages), 2)
self.assertEquals(messages[1].reply_to, post_data["reply_to"])
self.assertEquals(messages[1].subject, post_data["subject"])
self.assertEquals(type(model.Pledge.all()[0].thank_you_sent_at), datetime.datetime)
self.assertEquals(resp.text, '1')

# make sure it isn't sent a message again when new_member is set to true
post_data['new_members'] = True
resp = self.app.post('/r/thank', post_data)
messages = self.mail_stub.get_sent_messages(to=self.pledge["email"])
self.assertEquals(len(messages), 2)

self.assertEquals(resp.text, '0')

def testUserInfoNotFound(self):
resp = self.app.get('/user-info/nouserhere', status=404)
Expand Down

0 comments on commit a8175cb

Please sign in to comment.