From 160520dbdec17250a74d5f87174cacb3f72eae26 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Wed, 15 Oct 2014 15:46:53 +0200 Subject: Extracting features controller --- service/pixelated/adapter/pixelated_mail.py | 3 +- service/pixelated/controllers/__init__.py | 1 + .../pixelated/controllers/features_controller.py | 32 ++++++++++++++ service/pixelated/user_agent.py | 49 ++++++++-------------- service/test/support/integration_helper.py | 10 +++-- service/test/unit/adapter/pixelated_mail_test.py | 2 +- 6 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 service/pixelated/controllers/features_controller.py diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py index 6be2e5bf..6ebcc089 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -26,6 +26,7 @@ from pycryptopp.hash import sha256 class InputMail: + FROM_EMAIL_ADDRESS = None def __init__(self): self._raw_message = None @@ -141,7 +142,7 @@ class InputMail: def to_smtp_format(self): mime_multipart = self.to_mime_multipart() - mime_multipart['From'] = PixelatedMail.from_email_address + mime_multipart['From'] = InputMail.FROM_EMAIL_ADDRESS return mime_multipart.as_string() @staticmethod diff --git a/service/pixelated/controllers/__init__.py b/service/pixelated/controllers/__init__.py index 84d7bf4d..fd14b01b 100644 --- a/service/pixelated/controllers/__init__.py +++ b/service/pixelated/controllers/__init__.py @@ -27,3 +27,4 @@ from flask import Response from home_controller import HomeController from mails_controller import MailsController from tags_controller import TagsController +from features_controller import FeaturesController diff --git a/service/pixelated/controllers/features_controller.py b/service/pixelated/controllers/features_controller.py new file mode 100644 index 00000000..d3438352 --- /dev/null +++ b/service/pixelated/controllers/features_controller.py @@ -0,0 +1,32 @@ +# +# 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 . + +from pixelated.controllers import respond_json +import os + + +class FeaturesController: + DISABLED_FEATURES = ['draftReply', 'signatureStatus', 'encryptionStatus', 'contacts'] + + def __init__(self): + pass + + def features(self): + try: + disabled_features = {'logout': os.environ['DISPATCHER_LOGOUT_URL']} + except KeyError: + disabled_features = {} + return respond_json({'disabled_features': self.DISABLED_FEATURES, 'dispatcher_features': disabled_features}) \ No newline at end of file diff --git a/service/pixelated/user_agent.py b/service/pixelated/user_agent.py index 6dca31ae..687433d7 100644 --- a/service/pixelated/user_agent.py +++ b/service/pixelated/user_agent.py @@ -28,7 +28,7 @@ from pixelated.bitmask_libraries.config import LeapConfig from pixelated.bitmask_libraries.provider import LeapProvider from pixelated.bitmask_libraries.auth import LeapAuthenticator, LeapCredentials from pixelated.adapter.mail_service import MailService -from pixelated.adapter.pixelated_mail import PixelatedMail +from pixelated.adapter.pixelated_mail import InputMail from pixelated.adapter.soledad_querier import SoledadQuerier from pixelated.adapter.search import SearchEngine from pixelated.adapter.draft_service import DraftService @@ -38,34 +38,16 @@ from pixelated.adapter.tag_service import TagService static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "web-ui", "app")) - # this is a workaround for packaging if not os.path.exists(static_folder): static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "web-ui", "app")) if not os.path.exists(static_folder): static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent') -app = Flask(__name__, static_url_path='', static_folder=static_folder) -DISABLED_FEATURES = ['draftReply', 'signatureStatus', 'encryptionStatus', 'contacts'] - - -def respond_json(entity, status_code=200): - json_response = json.dumps(entity) - response = Response(response=json_response, mimetype="application/json") - response.status_code = status_code - return response - -@app.route('/features') -def features(): - try: - disabled_features = {'logout': os.environ['DISPATCHER_LOGOUT_URL']} - except KeyError: - disabled_features = {} - return respond_json({'disabled_features': DISABLED_FEATURES, 'dispatcher_features': disabled_features}) +app = Flask(__name__, static_url_path='', static_folder=static_folder) -def register_new_user(username): - server_name = app.config['LEAP_SERVER_NAME'] +def register_new_user(username, server_name): certs_home = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "certificates")) config = LeapConfig(certs_home=certs_home) provider = LeapProvider(server_name, config) @@ -75,7 +57,7 @@ def register_new_user(username): session.nicknym.generate_openpgp_key() -def _setup_routes(app, home_controller, mails_controller, tags_controller): +def _setup_routes(app, home_controller, mails_controller, tags_controller, features_controller): # home app.add_url_rule('/', methods=['GET'], view_func=home_controller.home) # mails @@ -91,32 +73,35 @@ def _setup_routes(app, home_controller, mails_controller, tags_controller): app.add_url_rule('/mails', methods=['PUT'], view_func=mails_controller.update_draft) # tags app.add_url_rule('/tags', methods=['GET'], view_func=tags_controller.tags) + # features + app.add_url_rule('/features', methods=['GET'], view_func=features_controller.features) + +def start_user_agent(debug_enabled, app): -def start_user_agent(debug_enabled): with app.app_context(): leap_session = LeapSession.open(app.config['LEAP_USERNAME'], app.config['LEAP_PASSWORD'], app.config['LEAP_SERVER_NAME']) + tag_service = TagService() soledad_querier = SoledadQuerier(soledad=leap_session.account._soledad) - - PixelatedMail.from_email_address = leap_session.account_email() pixelated_mailboxes = PixelatedMailBoxes(leap_session.account, soledad_querier) pixelated_mail_sender = PixelatedMailSender(leap_session.account_email()) - - tag_service = TagService() mail_service = MailService(pixelated_mailboxes, pixelated_mail_sender, tag_service, soledad_querier) search_engine = SearchEngine() - MailboxListener.SEARCH_ENGINE = search_engine search_engine.index_mails(mail_service.all_mails()) draft_service = DraftService(pixelated_mailboxes) + MailboxListener.SEARCH_ENGINE = search_engine + InputMail.FROM_EMAIL_ADDRESS = leap_session.account_email() + home_controller = HomeController() + features_controller = FeaturesController() mails_controller = MailsController(mail_service=mail_service, draft_service=draft_service, search_engine=search_engine) tags_controller = TagsController(search_engine=search_engine) - _setup_routes(app, home_controller, mails_controller, tags_controller) + _setup_routes(app, home_controller, mails_controller, tags_controller, features_controller) app.run(host=app.config['HOST'], debug=debug_enabled, port=app.config['PORT'], use_reloader=False) @@ -138,12 +123,14 @@ def setup(): reactor_manager.start_reactor(logging=debug_enabled) crochet.setup() + app.config.from_pyfile(args.config) if args.register: - register_new_user(args.register) + server_name = app.config['LEAP_SERVER_NAME'] + register_new_user(args.register, server_name) else: - start_user_agent(debug_enabled) + start_user_agent(debug_enabled, app) finally: reactor_manager.stop_reactor_on_exit() diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py index 3c617438..1b5b1135 100644 --- a/service/test/support/integration_helper.py +++ b/service/test/support/integration_helper.py @@ -136,8 +136,7 @@ class SoledadTestBase: 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} + app.view_functions = {'static': static_files_route} def setup_soledad(self): self.soledad = initialize_soledad(tempdir=soledad_test_folder) @@ -145,7 +144,7 @@ class SoledadTestBase: # setup app PixelatedMail.from_email_address = self.mail_address - pixelated.user_agent.DISABLED_FEATURES.append('autoReload') + SearchEngine.INDEX_FOLDER = soledad_test_folder + '/search_index' self.client = pixelated.user_agent.app.test_client() @@ -162,13 +161,16 @@ class SoledadTestBase: self.search_engine = SearchEngine() self.search_engine.index_mails(self.mail_service.all_mails()) + features_controller = FeaturesController() + features_controller.DISABLED_FEATURES.append('autoReload') 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) + pixelated.user_agent._setup_routes(self.client.application, home_controller, mails_controller, tags_controller, + features_controller) def get_mails_by_tag(self, tag): response = json.loads(self.client.get("/mails?q=tag:" + tag).data) diff --git a/service/test/unit/adapter/pixelated_mail_test.py b/service/test/unit/adapter/pixelated_mail_test.py index eb66f0fb..63995e0e 100644 --- a/service/test/unit/adapter/pixelated_mail_test.py +++ b/service/test/unit/adapter/pixelated_mail_test.py @@ -111,7 +111,7 @@ class InputMailTest(unittest.TestCase): self.assertRegexpMatches(mime_multipart.as_string(), "\nEste \xe9 o corpo") def test_smtp_format(self): - PixelatedMail.from_email_address = 'pixelated@org' + InputMail.FROM_EMAIL_ADDRESS = 'pixelated@org' smtp_format = InputMail.from_dict(self.mail_dict()).to_smtp_format() -- cgit v1.2.3