summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-09-29 18:00:52 -0300
committerDuda Dornelles <ddornell@thoughtworks.com>2014-09-29 18:00:52 -0300
commit7fa4661018272edaf11109efdb9ff834d365f3ab (patch)
tree158f090a915d99c6314b412035a42c1d810f6d29 /service
parent99d540becb364a46553e0b3e8a6a8933947bd699 (diff)
Testing drafts in integration, fixing a bug where the sent wasnt leaving the drafts mailbox
Diffstat (limited to 'service')
-rw-r--r--service/integration/__init__.py50
-rw-r--r--service/integration/drafts_test.py53
-rw-r--r--service/integration/mail_fetch_test.py25
-rw-r--r--service/pixelated/adapter/pixelated_mail.py8
-rw-r--r--service/pixelated/adapter/pixelated_mailboxes.py2
-rw-r--r--service/pixelated/adapter/soledad_querier.py2
-rw-r--r--service/pixelated/user_agent.py11
7 files changed, 102 insertions, 49 deletions
diff --git a/service/integration/__init__.py b/service/integration/__init__.py
index 717fa3e9..aff5cec0 100644
--- a/service/integration/__init__.py
+++ b/service/integration/__init__.py
@@ -21,6 +21,8 @@ import os
from mock import Mock
import shutil
from pixelated.adapter.mail_service import MailService
+from pixelated.adapter.tag_index import TagIndex
+from pixelated.adapter.tag_service import TagService
import pixelated.user_agent
from pixelated.adapter.pixelated_mail import PixelatedMail
from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes
@@ -89,34 +91,60 @@ class JSONMailBuilder:
self.mail['header']['subject'] = subject
return self
+ def with_ident(self, ident):
+ self.mail['ident'] = ident
+ return self
+
def build(self):
return json.dumps(self.mail)
class SoledadTestBase:
- def __init__(self):
- pass
-
def teardown_soledad(self):
self.soledad.close()
shutil.rmtree(soledad_test_folder)
def setup_soledad(self):
- self.app = pixelated.user_agent.app.test_client()
- self.account = FakeAccount()
- self.mail_sender = mock()
- self.mail_address = "test@pixelated.org"
self.soledad = initialize_soledad(tempdir=soledad_test_folder)
+ self.mail_address = "test@pixelated.org"
SoledadQuerier.instance = None
SoledadQuerier.get_instance(soledad=self.soledad)
PixelatedMail.from_email_address = self.mail_address
- pixelated_mailboxes = PixelatedMailBoxes(self.account)
- pixelated.user_agent.mail_service = MailService(pixelated_mailboxes, self.mail_sender)
+
+ self.app = pixelated.user_agent.app.test_client()
+ self.account = FakeAccount()
+ self.mail_sender = mock()
+ self.tag_index = TagIndex(os.path.join(soledad_test_folder, 'tag_index'))
+ self.tag_service = TagService(self.tag_index)
+ self.pixelated_mailboxes = PixelatedMailBoxes(self.account)
+ self.mail_service = MailService(self.pixelated_mailboxes, self.mail_sender, self.tag_service)
+
+ pixelated.user_agent.mail_service = self.mail_service
def get_mails_by_tag(self, tag):
- return json.loads(self.app.get("/mails?q=tag" + tag).data)
+ response = json.loads(self.app.get("/mails?q=tag:" + tag).data)
+ return [ResponseMail(m) for m in response['mails']]
def post_mail(self, data):
- self.app.post('/mails', data=data, content_type="application/json") \ No newline at end of file
+ response = json.loads(self.app.post('/mails', data=data, content_type="application/json").data)
+ return ResponseMail(response)
+
+
+class ResponseMail:
+
+ def __init__(self, mail_dict):
+ self.mail_dict = mail_dict
+
+ @property
+ def subject(self):
+ return self.headers['subject']
+
+ @property
+ def headers(self):
+ return self.mail_dict['header']
+
+ @property
+ def ident(self):
+ return self.mail_dict['ident'] \ No newline at end of file
diff --git a/service/integration/drafts_test.py b/service/integration/drafts_test.py
new file mode 100644
index 00000000..1f1712a8
--- /dev/null
+++ b/service/integration/drafts_test.py
@@ -0,0 +1,53 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+import unittest
+from integration import JSONMailBuilder, SoledadTestBase
+
+
+class DraftsTest(unittest.TestCase, SoledadTestBase):
+
+ def setUp(self):
+ self.setup_soledad()
+
+ def tearDown(self):
+ self.teardown_soledad()
+
+ def test_post_creates_a_draft_when_data_has_no_ident(self):
+ mail = JSONMailBuilder().with_subject('A new draft').build()
+
+ 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):
+ first_draft = JSONMailBuilder().with_subject('First draft').build()
+ first_draft_response = self.post_mail(first_draft)
+ first_draft_ident = first_draft_response.ident
+
+ second_draft = JSONMailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build()
+ self.post_mail(second_draft)
+
+ sent_mails = self.get_mails_by_tag('sent')
+ drafts = self.get_mails_by_tag('drafts')
+
+ import pdb;pdb.set_trace()
+
+ self.assertEquals(1, len(sent_mails))
+ self.assertEquals('Second draft', sent_mails[0].subject)
+ self.assertEquals(0, len(drafts))
+
+
diff --git a/service/integration/mail_fetch_test.py b/service/integration/mail_fetch_test.py
deleted file mode 100644
index 882e9d6f..00000000
--- a/service/integration/mail_fetch_test.py
+++ /dev/null
@@ -1,25 +0,0 @@
-import unittest
-
-from integration import JSONMailBuilder, SoledadTestBase
-
-
-class MailFetchTest(unittest.TestCase, SoledadTestBase):
-
- def setUp(self):
- self.setup_soledad()
-
- def tearDown(self):
- self.teardown_soledad()
-
- def test_get_mails(self):
- mail_one = JSONMailBuilder().with_subject("Mail One").build()
- mail_two = JSONMailBuilder().with_subject("Mail Two").build()
-
- self.post_mail(mail_one)
- self.post_mail(mail_two)
-
- response = self.get_mails_by_tag("drafts")
-
- # ordered by creation date
- self.assertEquals(u'Mail Two', response['mails'][0]['header']['subject'])
- self.assertEquals(u'Mail One', response['mails'][1]['header']['subject'])
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index 89320b75..b7323af9 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -77,17 +77,17 @@ class InputMail:
self._chash = sha256.SHA256(self._raw()).hexdigest()
return self._chash
- def _get_for_save(self, next_uid):
- docs = [self._fdoc(next_uid), self._hdoc()]
+ def _get_for_save(self, next_uid, mailbox):
+ docs = [self._fdoc(next_uid, mailbox), self._hdoc()]
docs.extend([m for m in self._cdocs()])
return docs
- def _fdoc(self, next_uid):
+ def _fdoc(self, next_uid, mailbox):
if self._fd:
return self._fd
fd = {}
- fd[fields.MBOX_KEY] = 'DRAFTS'
+ fd[fields.MBOX_KEY] = mailbox
fd[fields.UID_KEY] = next_uid
fd[fields.CONTENT_HASH_KEY] = self._get_chash()
fd[fields.SIZE_KEY] = len(self._raw())
diff --git a/service/pixelated/adapter/pixelated_mailboxes.py b/service/pixelated/adapter/pixelated_mailboxes.py
index 9180dd8d..acfdf8fd 100644
--- a/service/pixelated/adapter/pixelated_mailboxes.py
+++ b/service/pixelated/adapter/pixelated_mailboxes.py
@@ -34,7 +34,7 @@ class PixelatedMailBoxes():
return self._create_or_get('TRASH')
def sent(self):
- return self._create_or_get('TRASH')
+ return self._create_or_get('SENT')
@property
def mailboxes(self):
diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py
index 7f617767..7ce7f26d 100644
--- a/service/pixelated/adapter/soledad_querier.py
+++ b/service/pixelated/adapter/soledad_querier.py
@@ -60,7 +60,7 @@ class SoledadQuerier:
def create_mail(self, mail, mailbox_name):
uid = self._next_uid_for_mailbox(mailbox_name)
- new_docs = [self.soledad.create_doc(doc) for doc in mail._get_for_save(next_uid=uid)]
+ new_docs = [self.soledad.create_doc(doc) for doc in mail._get_for_save(next_uid=uid, mailbox=mailbox_name)]
self._update_index(new_docs)
def mail(self, ident):
diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py
index 8651a822..2dc71bf0 100644
--- a/service/pixelated/user_agent.py
+++ b/service/pixelated/user_agent.py
@@ -60,14 +60,11 @@ def disabled_features():
@app.route('/mails', methods=['POST'])
def send_mail():
_mail = InputMail.from_dict(request.json)
- if 'saveDraft' in DISABLED_FEATURES:
- mail_service.send(_mail)
+ draft_id = request.json.get('ident')
+ if draft_id:
+ mail_service.send(draft_id, _mail)
else:
- draft_id = request.json.get('ident')
- if draft_id:
- mail_service.send(draft_id, _mail)
- else:
- _mail = mail_service.create_draft(_mail)
+ _mail = mail_service.create_draft(_mail)
return respond_json(_mail.as_dict())