summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorPatrick Maia <patrickjourdanmaia@gmail.com>2014-10-13 21:15:01 +0000
committerPatrick Maia <patrickjourdanmaia@gmail.com>2014-10-13 21:33:14 +0000
commit69dccf3fd6979d797d49c9a158725cb654c4ec2d (patch)
tree3f629bcfb94137560620a3ef56cfd45675587068 /service
parent21eee8ccf485b8aca8d5677ba0f0de97fc0afc22 (diff)
#89 - separates manage draft and send mail responsibilities
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/mail_service.py3
-rw-r--r--service/pixelated/user_agent.py18
-rw-r--r--service/test/integration/drafts_test.py36
3 files changed, 34 insertions, 23 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index a6241821..d26c7c8f 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -45,7 +45,8 @@ class MailService:
def send(self, last_draft_ident, mail):
self.mail_sender.sendmail(mail)
- self.mailboxes.drafts().remove(last_draft_ident)
+ if last_draft_ident:
+ self.mailboxes.drafts().remove(last_draft_ident)
return self.mailboxes.sent().add(mail)
def thread(self, thread_id):
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 04be0b90..da8b69a0 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -72,12 +72,10 @@ def send_mail():
_mail = InputMail.from_dict(request.json)
draft_id = request.json.get('ident')
if draft_id:
- _mail = mail_service.send(draft_id, _mail)
- search_engine.index_mail(_mail)
search_engine.remove_from_index(draft_id)
- else:
- _mail = draft_service.create_draft(_mail)
- search_engine.index_mail(mail_service.mail(_mail.ident))
+ _mail = mail_service.send(draft_id, _mail)
+ search_engine.index_mail(_mail)
+
return respond_json(_mail.as_dict())
except Exception as error:
return respond_json({'message': '\n'.join(list(error.args))}, status_code=500)
@@ -86,10 +84,14 @@ def send_mail():
@app.route('/mails', methods=['PUT'])
def update_draft():
_mail = InputMail.from_dict(request.json)
- new_revision = draft_service.update_draft(request.json['ident'], _mail)
- ident = new_revision.ident
+ draft_id = request.json.get('ident')
+ if draft_id:
+ ident = draft_service.update_draft(draft_id, _mail).ident
+ search_engine.remove_from_index(draft_id)
+ else:
+ ident = draft_service.create_draft(_mail).ident
+
search_engine.index_mail(mail_service.mail(ident))
- search_engine.remove_from_index(request.json['ident'])
return respond_json({'ident': ident})
diff --git a/service/test/integration/drafts_test.py b/service/test/integration/drafts_test.py
index b70af3f5..64826fde 100644
--- a/service/test/integration/drafts_test.py
+++ b/service/test/integration/drafts_test.py
@@ -25,18 +25,9 @@ class DraftsTest(unittest.TestCase, SoledadTestBase):
def tearDown(self):
self.teardown_soledad()
- def test_post_creates_a_draft_when_data_has_no_ident(self):
- mail = MailBuilder().with_subject('A new draft').build_json()
-
- self.post_mail(mail)
- mails = self.get_mails_by_tag('drafts')
-
- self.assertEquals('A new draft', mails[0].subject)
-
- def test_post_sends_mail_and_deletes_previous_draft_when_data_has_ident(self):
+ def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self):
first_draft = MailBuilder().with_subject('First draft').build_json()
- first_draft_response = self.post_mail(first_draft)
- first_draft_ident = first_draft_response.ident
+ first_draft_ident = self.put_mail(first_draft)
second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json()
self.post_mail(second_draft)
@@ -48,10 +39,27 @@ class DraftsTest(unittest.TestCase, SoledadTestBase):
self.assertEquals('Second draft', sent_mails[0].subject)
self.assertEquals(0, len(drafts))
- def test_update_draft(self):
+ def test_post_sends_mail_even_when_draft_does_not_exist(self):
+ first_draft = MailBuilder().with_subject('First draft').build_json()
+ self.post_mail(first_draft)
+
+ sent_mails = self.get_mails_by_tag('sent')
+ drafts = self.get_mails_by_tag('drafts')
+
+ self.assertEquals(1, len(sent_mails))
+ self.assertEquals('First draft', sent_mails[0].subject)
+ self.assertEquals(0, len(drafts))
+
+ def test_put_creates_a_draft_if_it_does_not_exist(self):
+ mail = MailBuilder().with_subject('A new draft').build_json()
+ self.put_mail(mail)
+ mails = self.get_mails_by_tag('drafts')
+
+ self.assertEquals('A new draft', mails[0].subject)
+
+ def test_put_updates_draft_if_it_already_exists(self):
draft = MailBuilder().with_subject('First draft').build_json()
- create_draft_response = self.post_mail(draft)
- draft_ident = create_draft_response.ident
+ draft_ident = self.put_mail(draft)
updated_draft = MailBuilder().with_subject('First draft edited').with_ident(draft_ident).build_json()
self.put_mail(updated_draft)