summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorAlexandre Pretto Nunes <anunes@thoughtworks.com>2014-10-08 16:23:09 -0300
committerAlexandre Pretto Nunes <anunes@thoughtworks.com>2014-10-08 16:24:40 -0300
commitd7f522acd0b97da86522841968b05c60ee5de289 (patch)
tree4cb15f642d956bd689d45790d0f88d074b57d02d /service
parentf4b52ce31b9aa56a9aeac7b3198c49fea6c29369 (diff)
#92 Add handling of errors when sending mail
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/user_agent.py17
-rw-r--r--service/test/user_agent_test.py27
2 files changed, 37 insertions, 7 deletions
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 93a3caf3..12b9a97a 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -66,13 +66,16 @@ def features():
@app.route('/mails', methods=['POST'])
def send_mail():
- _mail = InputMail.from_dict(request.json)
- draft_id = request.json.get('ident')
- if draft_id:
- mail_service.send(draft_id, _mail)
- else:
- _mail = mail_service.create_draft(_mail)
- return respond_json(_mail.as_dict())
+ try:
+ _mail = InputMail.from_dict(request.json)
+ draft_id = request.json.get('ident')
+ if draft_id:
+ mail_service.send(draft_id, _mail)
+ else:
+ _mail = mail_service.create_draft(_mail)
+ return respond_json(_mail.as_dict())
+ except Exception as error:
+ return respond_json({'message': '\n'.join(list(error.args))}, status_code=500)
@app.route('/mails', methods=['PUT'])
diff --git a/service/test/user_agent_test.py b/service/test/user_agent_test.py
index 2f1ed01d..d21f1733 100644
--- a/service/test/user_agent_test.py
+++ b/service/test/user_agent_test.py
@@ -58,6 +58,33 @@ class UserAgentTest(unittest.TestCase):
verify(self.mail_service).send(1, self.input_mail)
+ def test_sending_mail_return_sent_mail_data_when_send_succeeds(self):
+ self.input_mail = self.draft()
+ self.input_mail.as_dict = lambda: {'header': {'from': 'a@a.a', 'to': 'b@b.b'},
+ 'ident': 1,
+ 'tags': [],
+ 'status': [],
+ 'security_casing': {},
+ 'body': 'email body'}
+
+ result = self.app.post('/mails', data='{"ident":1}', content_type="application/json")
+
+ self.assertEqual(result.status_code, 200)
+ self.assertEqual(result.data, '{"status": [], "body": "email body", "ident": 1, "tags": [], "header": {"to": "b@b.b", "from": "a@a.a"}, "security_casing": {}}')
+
+ def test_sending_mail_return_error_message_when_send_fails(self):
+ self.input_mail = self.draft()
+
+ def send_that_throws_exception(id, mail):
+ raise Exception('email sending failed', 'more information of error')
+
+ self.mail_service.send = send_that_throws_exception
+
+ result = self.app.post('/mails', data='{"ident":1}', content_type="application/json")
+
+ self.assertEqual(result.status_code, 500)
+ self.assertEqual(result.data, '{"message": "email sending failed\\nmore information of error"}')
+
def test_update_draft(self):
self.input_mail = self.draft()