summaryrefslogtreecommitdiff
path: root/service/test
diff options
context:
space:
mode:
Diffstat (limited to 'service/test')
-rw-r--r--service/test/functional/features/environment.py2
-rw-r--r--service/test/integration/drafts_test.py5
-rw-r--r--service/test/integration/mark_as_read_unread_test.py9
-rw-r--r--service/test/integration/search_test.py4
-rw-r--r--service/test/support/integration_helper.py66
-rw-r--r--service/test/unit/adapter/mail_sender_test.py46
-rw-r--r--service/test/unit/controllers/mails_controller_test.py46
-rw-r--r--service/test/unit/controllers/sync_info_controller_test.py4
-rw-r--r--service/test/unit/runserver_test.py23
9 files changed, 99 insertions, 106 deletions
diff --git a/service/test/functional/features/environment.py b/service/test/functional/features/environment.py
index 8cfdcbbc..19e2a6f0 100644
--- a/service/test/functional/features/environment.py
+++ b/service/test/functional/features/environment.py
@@ -33,7 +33,7 @@ def before_all(context):
pixelated.controllers.features_controller.FeaturesController.DISABLED_FEATURES.append('autoRefresh')
logging.disable('INFO')
- worker = lambda app, port: pixelated.runserver.app.run(port=4567, use_reloader=False)
+ worker = lambda app, port: pixelated.runserver.app.run(host='localhost', port=4567)
context._process = multiprocessing.Process(target=worker, args=(context.app, 4567))
context._process.start()
diff --git a/service/test/integration/drafts_test.py b/service/test/integration/drafts_test.py
index e0b49c13..5d2118df 100644
--- a/service/test/integration/drafts_test.py
+++ b/service/test/integration/drafts_test.py
@@ -27,17 +27,22 @@ class DraftsTest(unittest.TestCase, SoledadTestBase):
self.teardown_soledad()
def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self):
+ # creates one draft
first_draft = MailBuilder().with_subject('First draft').build_json()
first_draft_ident = self.put_mail(first_draft)
+ # sends an updated version of the draft
second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json()
self.post_mail(second_draft)
sent_mails = self.get_mails_by_tag('sent')
drafts = self.get_mails_by_tag('drafts')
+ # make sure there is one email in the sent mailbox and it is the second draft
self.assertEquals(1, len(sent_mails))
self.assertEquals('Second draft', sent_mails[0].subject)
+
+ # make sure that there are no drafts in the draft mailbox
self.assertEquals(0, len(drafts))
def test_post_sends_mail_even_when_draft_does_not_exist(self):
diff --git a/service/test/integration/mark_as_read_unread_test.py b/service/test/integration/mark_as_read_unread_test.py
index 3bf56dd5..dc21c7b7 100644
--- a/service/test/integration/mark_as_read_unread_test.py
+++ b/service/test/integration/mark_as_read_unread_test.py
@@ -41,13 +41,12 @@ class MarkAsReadUnreadTest(unittest.TestCase, SoledadTestBase):
def test_mark_single_as_unread(self):
input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail()
-
self.add_mail_to_inbox(input_mail)
self.mark_as_unread(input_mail.ident)
+ mail = self.get_mails_by_tag('inbox')[0]
- mails = self.get_mails_by_tag('inbox')
- self.assertNotIn('read', mails[0].status)
+ self.assertNotIn('read', mail.status)
def test_mark_many_mails_as_unread(self):
input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail()
@@ -76,7 +75,7 @@ class MarkAsReadUnreadTest(unittest.TestCase, SoledadTestBase):
self.assertNotIn('read', mails[1].status)
response = self.mark_many_as_read([input_mail.ident, input_mail2.ident])
- self.assertEquals(200, response.status_code)
+ self.assertEquals(200, response.code)
mails = self.get_mails_by_tag('inbox')
@@ -98,7 +97,7 @@ class MarkAsReadUnreadTest(unittest.TestCase, SoledadTestBase):
self.assertEquals(1, len(read_mails))
response = self.mark_many_as_read([input_mail.ident, input_mail2.ident])
- self.assertEquals(200, response.status_code)
+ self.assertEquals(200, response.code)
mails = self.get_mails_by_tag('inbox')
diff --git a/service/test/integration/search_test.py b/service/test/integration/search_test.py
index 92c2f07e..649f7b96 100644
--- a/service/test/integration/search_test.py
+++ b/service/test/integration/search_test.py
@@ -43,7 +43,7 @@ class SearchTest(unittest.TestCase, SoledadTestBase):
input_mail = MailBuilder().with_tags(['ateu', 'catoa', 'luat', 'zuado']).build_input_mail()
self.add_mail_to_inbox(input_mail)
- all_tags = self.get_tags('?q=at&skipDefaultTags=true')
+ all_tags = self.get_tags(q=["at"], skipDefaultTags=["true"])
all_tag_names = [t['name'] for t in all_tags]
self.assertEqual(3, len(all_tag_names))
@@ -55,7 +55,7 @@ class SearchTest(unittest.TestCase, SoledadTestBase):
input_mail = MailBuilder().with_tags(['sometag']).build_input_mail()
self.add_mail_to_inbox(input_mail)
- all_tags = self.get_tags('?skipDefaultTags=true')
+ all_tags = self.get_tags(skipDefaultTags=["true"])
all_tag_names = [t['name'] for t in all_tags]
self.assertEqual(1, len(all_tag_names))
diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py
index 3abf53da..900b8049 100644
--- a/service/test/support/integration_helper.py
+++ b/service/test/support/integration_helper.py
@@ -14,6 +14,7 @@
# 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 shutil
+from klein.resource import KleinResource
from leap.soledad.client import Soledad
from mockito import mock
@@ -31,6 +32,7 @@ from pixelated.adapter.soledad_querier import SoledadQuerier
from pixelated.controllers import *
import pixelated.config.app_factory as app_factory
from leap.mail.imap.account import SoledadBackedAccount
+from klein.test_resource import requestMock, _render
soledad_test_folder = "soledad-test"
@@ -52,6 +54,7 @@ def initialize_soledad(tempdir):
put_doc = Mock()
lock = Mock(return_value=('atoken', 300))
unlock = Mock(return_value=True)
+ close = Mock()
def __call__(self):
return self
@@ -69,7 +72,7 @@ def initialize_soledad(tempdir):
# from leap.mail.imap.fields import fields
#
# for name, expression in fields.INDEXES.items():
- # _soledad.create_index(name, *expression)
+ # _soledad.create_index(name, *expression)
#
return _soledad
@@ -126,10 +129,6 @@ class SoledadTestBase:
def teardown_soledad(self):
pass
- def _reset_routes(self, app):
- static_files_route = app.view_functions['static']
- app.view_functions = {'static': static_files_route}
-
def setup_soledad(self):
self.soledad = initialize_soledad(tempdir=soledad_test_folder)
self.mail_address = "test@pixelated.org"
@@ -139,9 +138,8 @@ class SoledadTestBase:
SearchEngine.INDEX_FOLDER = soledad_test_folder + '/search_index'
- self.client = pixelated.runserver.app.test_client()
+ self.app = pixelated.runserver.app
- self._reset_routes(self.client.application)
self.soledad_querier = SoledadQuerier(self.soledad)
self.account = SoledadBackedAccount('test', self.soledad, MagicMock())
@@ -164,43 +162,69 @@ class SoledadTestBase:
sync_info_controller = SyncInfoController()
attachments_controller = AttachmentsController(self.soledad_querier)
- app_factory._setup_routes(self.client.application, home_controller, mails_controller, tags_controller,
+ app_factory._setup_routes(self.app, home_controller, mails_controller, tags_controller,
features_controller, sync_info_controller, attachments_controller)
+ self.resource = KleinResource(self.app)
def get_mails_by_tag(self, tag, page=1, window=100):
- response = json.loads(self.client.get("/mails?q=tag:%s&w=%s&p=%s" % (tag, window, page)).data)
+ request = requestMock(path="/mails")
+ request.args = {
+ 'q': ['tag:%s' % tag],
+ 'w': [str(window)],
+ 'p': [str(page)]
+ }
+ _render(self.resource, request)
+ response = json.loads(request.getWrittenData())
return [ResponseMail(m) for m in response['mails']]
def post_mail(self, data):
- response = json.loads(self.client.post('/mails', data=data, content_type="application/json").data)
+ request = requestMock(path='/mails', method="POST", body=data, headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ response = json.loads(request.getWrittenData())
return ResponseMail(response)
def put_mail(self, data):
- response = json.loads(self.client.put('/mails', data=data, content_type="application/json").data)
+ request = requestMock('/mails', method="PUT", body=data, headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ response = json.loads(request.getWrittenData())
return response['ident']
def post_tags(self, mail_ident, tags_json):
- return json.loads(
- self.client.post('/mail/' + mail_ident + '/tags', data=tags_json, content_type="application/json").data)
+ request = requestMock('/mail/' + mail_ident + '/tags', method="POST", body=tags_json,
+ headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ return json.loads(request.getWrittenData())
- def get_tags(self, query_string=""):
- return json.loads(
- self.client.get('/tags' + query_string, content_type="application/json").data)
+ def get_tags(self, **kwargs):
+ request = requestMock('/tags')
+ request.args = kwargs
+ _render(self.resource, request)
+ return json.loads(request.getWrittenData())
def delete_mail(self, mail_ident):
- self.client.delete('/mail/' + mail_ident)
+ request = requestMock(path='/mail/' + mail_ident, method="DELETE")
+ _render(self.resource, request)
+ return request
def mark_as_read(self, mail_ident):
- self.client.post('/mail/' + mail_ident + '/read', content_type="application/json")
+ request = requestMock('/mail/' + mail_ident + '/read', method="POST", headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ return request
def mark_as_unread(self, mail_ident):
- self.client.post('/mail/' + mail_ident + '/unread', content_type="application/json")
+ request = requestMock('/mail/' + mail_ident + '/unread', method="POST", headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ return request
def mark_many_as_unread(self, idents):
- self.client.post('/mails/unread', data={'idents': json.dumps(idents)})
+ request = requestMock('/mails/unread', method="POST", body=json.dumps({'idents': idents}), headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ return request
def mark_many_as_read(self, idents):
- return self.client.post('/mails/read', data={'idents': json.dumps(idents)})
+ request = requestMock('/mails/read', method="POST", body=json.dumps({'idents': idents}), headers={'Content-Type': ['application/json']})
+ _render(self.resource, request)
+ return request
def add_mail_to_inbox(self, input_mail):
mail = self.mailboxes.inbox().add(input_mail)
diff --git a/service/test/unit/adapter/mail_sender_test.py b/service/test/unit/adapter/mail_sender_test.py
deleted file mode 100644
index 721d61b7..00000000
--- a/service/test/unit/adapter/mail_sender_test.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#
-# 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 pixelated.adapter.mail import PixelatedMail
-from pixelated.adapter.mail_sender import MailSender
-from mockito import *
-from test.support import test_helper
-
-
-class MailSenderTest(unittest.TestCase):
- def setUp(self):
- self.mail_address = "pixelated@pixelated.org"
- self.smtp_client = mock()
- self.mail_sender = MailSender(self.mail_address, self.smtp_client)
-
- def test_send_mail_sends_to_To_Cc_and_Bcc(self):
- headers = {
- 'To': ['to@pixelated.org', 'anotherto@pixelated.org'],
- 'Cc': ['cc@pixelated.org', 'anothercc@pixelated.org'],
- 'Bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org']
- }
-
- mail = PixelatedMail.from_soledad(*test_helper.leap_mail(extra_headers=headers))
- mail.to_smtp_format = lambda: "mail as smtp string"
-
- self.mail_sender.sendmail(mail)
-
- expected_recipients = ['to@pixelated.org', 'anotherto@pixelated.org', 'cc@pixelated.org',
- 'anothercc@pixelated.org',
- 'bcc@pixelated.org', 'anotherbcc@pixelated.org']
-
- verify(self.smtp_client).sendmail(self.mail_address, expected_recipients, "mail as smtp string")
diff --git a/service/test/unit/controllers/mails_controller_test.py b/service/test/unit/controllers/mails_controller_test.py
index a64207e9..93748de9 100644
--- a/service/test/unit/controllers/mails_controller_test.py
+++ b/service/test/unit/controllers/mails_controller_test.py
@@ -13,16 +13,21 @@
#
# 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 json
import unittest
+from klein.test_resource import requestMock
+from mock import MagicMock
from mockito import *
from pixelated.controllers.mails_controller import MailsController
class TestMailsController(unittest.TestCase):
+
def setUp(self):
self.mail_service = mock()
self.search_engine = mock()
+ self.dummy_request = MagicMock(spec=['code', 'responseHeaders'])
draft_service = mock()
self.mails_controller = MailsController(mail_service=self.mail_service,
@@ -42,20 +47,22 @@ class TestMailsController(unittest.TestCase):
def test_sending_mail_return_sent_mail_data_when_send_succeeds(self):
self.mail_service.send = self._successfuly_send_mail
+ request = requestMock('', body=json.dumps(self.input_mail.json))
- result = self.mails_controller.send_mail(self.input_mail)
+ result = self.mails_controller.send_mail(request)
- self.assertEqual(result.status_code, 200)
- self.assertEqual(result.data,
+ self.assertEqual(request.code, 200)
+ self.assertEqual(result,
'{"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.mail_service.send = self._send_that_throws_exception
- result = self.mails_controller.send_mail(self.input_mail)
+ request = requestMock('', body=json.dumps(self.input_mail.json))
+ result = self.mails_controller.send_mail(request)
- self.assertEqual(result.status_code, 422)
- self.assertEqual(result.data,
+ self.assertEqual(request.code, 422)
+ self.assertEqual(result,
'{"message": "email sending failed\\nmore information of error\\n123\\nthere was a code before this"}')
def test_fetching_mail_gets_mail_from_mail_service(self):
@@ -63,17 +70,17 @@ class TestMailsController(unittest.TestCase):
mail.as_dict = lambda: {'ident': 1, 'body': 'le mail body'}
when(self.mail_service).mail(1).thenReturn(mail)
- response = self.mails_controller.mail(1)
+ response = self.mails_controller.mail(self.dummy_request, 1)
verify(self.mail_service).mail(1)
- self.assertEqual(response.data, '{"body": "le mail body", "ident": 1}')
+ self.assertEqual(response, '{"body": "le mail body", "ident": 1}')
def test_marking_mail_as_read_set_mail_as_read_on_the_service(self):
mail = mock()
when(self.mail_service).mark_as_read(1).thenReturn(mail)
when(self.search_engine).index_mail(mail).thenReturn(None)
- self.mails_controller.mark_mail_as_read(1)
+ self.mails_controller.mark_mail_as_read(None, 1)
verify(self.mail_service).mark_as_read(1)
verify(self.search_engine).index_mail(mail)
@@ -83,27 +90,28 @@ class TestMailsController(unittest.TestCase):
when(self.mail_service).mark_as_unread(1).thenReturn(mail)
when(self.search_engine).index_mail(mail).thenReturn(None)
- self.mails_controller.mark_mail_as_unread(1)
+ self.mails_controller.mark_mail_as_unread(None, 1)
verify(self.mail_service).mark_as_unread(1)
verify(self.search_engine).index_mail(mail)
- def test_delete_permanently_when_mail_in_trash(self):
+ def test_move_message_to_trash(self):
mail = mock()
- mail.mailbox_name = 'TRASH'
+ mail.mailbox_name = 'INBOX'
when(self.mail_service).mail(1).thenReturn(mail)
- self.mails_controller.delete_mail(1)
+ when(self.mail_service).delete_mail(1).thenReturn(mail)
- verify(self.mail_service).delete_permanent(1)
+ self.mails_controller.delete_mail(self.dummy_request, 1)
- def test_move_message_to_trash(self):
+ verify(self.search_engine).index_mail(mail)
+
+ def test_delete_permanently_when_mail_in_trash(self):
mail = mock()
- mail.mailbox_name = 'INBOX'
+ mail.mailbox_name = 'TRASH'
when(self.mail_service).mail(1).thenReturn(mail)
- when(self.mails_controller).delete_mail(1).thenReturn(mail)
- when(self.search_engine).index_mail(mail)
+ self.mails_controller.delete_mail(self.dummy_request, 1)
- verify(self.search_engine).index_mail(mail)
+ verify(self.mail_service).delete_permanent(1)
def _successfuly_send_mail(self, ident, mail):
sent_mail = mock()
diff --git a/service/test/unit/controllers/sync_info_controller_test.py b/service/test/unit/controllers/sync_info_controller_test.py
index d3bf1190..ce9a0dff 100644
--- a/service/test/unit/controllers/sync_info_controller_test.py
+++ b/service/test/unit/controllers/sync_info_controller_test.py
@@ -14,6 +14,7 @@
# 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 mock import MagicMock
from pixelated.controllers import SyncInfoController
from mockito import *
import json
@@ -22,6 +23,7 @@ import json
class SyncInfoControllerTest(unittest.TestCase):
def setUp(self):
+ self.dummy_request = MagicMock()
self.controller = SyncInfoController()
def _set_count(self, current, total):
@@ -30,7 +32,7 @@ class SyncInfoControllerTest(unittest.TestCase):
self.controller.set_sync_info(soledad_sync_data)
def get_sync_info(self):
- return json.loads(self.controller.sync_info().data)
+ return json.loads(self.controller.sync_info(self.dummy_request))
def test_is_not_syncing_if_total_is_equal_to_current(self):
self._set_count(total=0, current=0)
diff --git a/service/test/unit/runserver_test.py b/service/test/unit/runserver_test.py
index 57e211c9..4a9bca6f 100644
--- a/service/test/unit/runserver_test.py
+++ b/service/test/unit/runserver_test.py
@@ -21,28 +21,29 @@ import thread
import pixelated.runserver
from mockito import *
-import pixelated.config.reactor_manager as reactor_manager
import pixelated.config.app_factory as app_factory
+from leap.common.events import server as events_server
class RunserverTest(unittest.TestCase):
def setUp(self):
- when(reactor_manager).start_reactor().thenReturn(None)
+ events_server.ensure_server = lambda port=None: None
when(app_factory).create_app().thenReturn(None)
def test_that_config_file_can_be_specified_on_command_line(self):
- orig_config = pixelated.runserver.app.config
- try:
- pixelated.runserver.app.config = mock(dict)
- pixelated.runserver.app.config.__setitem__ = mock()
+ self.config_file_loaded = None
- sys.argv = ['/tmp/does_not_exist', '--config', '/tmp/some/config/file']
- pixelated.runserver.setup()
+ def _mock_parse_config_from_file(config_file):
+ self.config_file_loaded = config_file
+ return 1, 2, 3
- verify(pixelated.runserver.app.config).from_pyfile('/tmp/some/config/file')
- finally:
- pixelated.runserver.app.config = orig_config
+ pixelated.runserver.parse_config_from_file = _mock_parse_config_from_file
+ sys.argv = ['pixelated-user-agent', '--config', 'pixelated.cfg']
+
+ pixelated.runserver.setup()
+
+ self.assertEquals('pixelated.cfg', self.config_file_loaded)
def test_that_organization_switch_reads_the_credentials_from_pipe(self):
fifo_path = '/tmp/credentials-pipe'