From ac3ea5d3838b98b07d182d71f2f3d9f270b5d274 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Wed, 15 Oct 2014 13:57:13 +0200 Subject: Extracting controller classes --- service/test/support/integration_helper.py | 59 +++++++++++++++++------------- service/test/support/test_helper.py | 6 +++ service/test/unit/user_agent_test.py | 28 -------------- 3 files changed, 40 insertions(+), 53 deletions(-) (limited to 'service/test') diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py index 2316774a..3c617438 100644 --- a/service/test/support/integration_helper.py +++ b/service/test/support/integration_helper.py @@ -13,11 +13,10 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import json import shutil from leap.soledad.client import Soledad -from mockito import mock, unstub +from mockito import mock import os from mock import Mock from pixelated.adapter.mail_service import MailService @@ -29,6 +28,7 @@ import pixelated.user_agent from pixelated.adapter.pixelated_mail import PixelatedMail, InputMail from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes from pixelated.adapter.soledad_querier import SoledadQuerier +from pixelated.controllers import * soledad_test_folder = "soledad-test" @@ -50,6 +50,9 @@ class FakeAccount: def initialize_soledad(tempdir): + if os.path.isdir(soledad_test_folder): + shutil.rmtree(soledad_test_folder) + uuid = "foobar-uuid" passphrase = u"verysecretpassphrase" secret_path = os.path.join(tempdir, "secret.gpg") @@ -125,17 +128,18 @@ class MailBuilder: class SoledadTestBase: - def teardown_soledad(self): - self.soledad.close() - shutil.rmtree(soledad_test_folder) + def __init__(self): + pass - def setup_soledad(self): - unstub() # making sure all mocks from other tests are reset + def teardown_soledad(self): + pass - # making sure soledad test folder is not there - if (os.path.isdir(soledad_test_folder)): - shutil.rmtree(soledad_test_folder) + def _reset_routes(self, app): + static_files_route = app.view_functions['static'] + disabled_features_route = app.view_functions['features'] + app.view_functions = {'static': static_files_route, 'features': disabled_features_route} + def setup_soledad(self): self.soledad = initialize_soledad(tempdir=soledad_test_folder) self.mail_address = "test@pixelated.org" @@ -144,54 +148,59 @@ class SoledadTestBase: pixelated.user_agent.DISABLED_FEATURES.append('autoReload') SearchEngine.INDEX_FOLDER = soledad_test_folder + '/search_index' - self.app = pixelated.user_agent.app.test_client() + self.client = pixelated.user_agent.app.test_client() + self._reset_routes(self.client.application) self.soledad_querier = SoledadQuerier(self.soledad) self.account = FakeAccount() self.pixelated_mailboxes = PixelatedMailBoxes(self.account, self.soledad_querier) self.mail_sender = mock() self.tag_service = TagService() self.draft_service = DraftService(self.pixelated_mailboxes) - self.mail_service = MailService(self.pixelated_mailboxes, self.mail_sender, self.tag_service, self.soledad_querier) + self.mail_service = MailService(self.pixelated_mailboxes, self.mail_sender, self.tag_service, + self.soledad_querier) self.search_engine = SearchEngine() self.search_engine.index_mails(self.mail_service.all_mails()) - pixelated.user_agent.mail_service = self.mail_service - pixelated.user_agent.draft_service = self.draft_service - pixelated.user_agent.tag_service = self.tag_service - pixelated.user_agent.search_engine = self.search_engine + home_controller = HomeController() + mails_controller = MailsController(mail_service=self.mail_service, + draft_service=self.draft_service, + search_engine=self.search_engine) + tags_controller = TagsController(search_engine=self.search_engine) + + pixelated.user_agent._setup_routes(self.client.application, home_controller, mails_controller, tags_controller) def get_mails_by_tag(self, tag): - response = json.loads(self.app.get("/mails?q=tag:" + tag).data) + response = json.loads(self.client.get("/mails?q=tag:" + tag).data) return [ResponseMail(m) for m in response['mails']] def post_mail(self, data): - response = json.loads(self.app.post('/mails', data=data, content_type="application/json").data) + response = json.loads(self.client.post('/mails', data=data, content_type="application/json").data) return ResponseMail(response) def put_mail(self, data): - response = json.loads(self.app.put('/mails', data=data, content_type="application/json").data) + response = json.loads(self.client.put('/mails', data=data, content_type="application/json").data) return response['ident'] def post_tags(self, mail_ident, tags_json): return json.loads( - self.app.post('/mail/' + mail_ident + '/tags', data=tags_json, content_type="application/json").data) + self.client.post('/mail/' + mail_ident + '/tags', data=tags_json, content_type="application/json").data) def get_tags(self, query_string=""): return json.loads( - self.app.get('/tags' + query_string, content_type="application/json").data) + self.client.get('/tags' + query_string, content_type="application/json").data) def delete_mail(self, mail_ident): - self.app.delete('/mail/' + mail_ident) + self.client.delete('/mail/' + mail_ident) def mark_as_read(self, mail_ident): - self.app.post('/mail/' + mail_ident + '/read', content_type="application/json") + self.client.post('/mail/' + mail_ident + '/read', content_type="application/json") def mark_as_unread(self, mail_ident): - self.app.post('/mail/' + mail_ident + '/unread', content_type="application/json") + self.client.post('/mail/' + mail_ident + '/unread', content_type="application/json") def mark_many_as_unread(self, idents): - self.app.post('/mails/unread', data={'idents': json.dumps(idents)}) + self.client.post('/mails/unread', data={'idents': json.dumps(idents)}) def add_mail_to_inbox(self, input_mail): mail = self.pixelated_mailboxes.inbox().add(input_mail) diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index 66ed9a10..4ea4afb5 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -70,3 +70,9 @@ def input_mail(): mail._chash = "123" mail.as_dict = lambda: None return mail + + +class TestRequest: + + def __init__(self, json): + self.json = json diff --git a/service/test/unit/user_agent_test.py b/service/test/unit/user_agent_test.py index 0ea83a5d..49a70dd6 100644 --- a/service/test/unit/user_agent_test.py +++ b/service/test/unit/user_agent_test.py @@ -46,34 +46,6 @@ class UserAgentTest(unittest.TestCase): def tearDown(self): unstub() - def test_sending_mail_return_sent_mail_data_when_send_succeeds(self): - self.input_mail = test_helper.input_mail() - when(self.mail_service).send(1, self.input_mail).thenReturn(self.input_mail) - 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 = test_helper.input_mail() - - 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_that_default_config_file_is_home_dot_pixelated(self): orig_config = pixelated.user_agent.app.config try: -- cgit v1.2.3