From 9ab17e2bbf61062ce8399ef1c51d2069a0cced31 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Fri, 31 Oct 2014 12:17:26 +0100 Subject: moving to twisted --- service/pixelated/adapter/mail_sender.py | 19 ++-- service/pixelated/config/app_factory.py | 106 +++++++++++---------- service/pixelated/config/reactor_manager.py | 59 ------------ service/pixelated/controllers/__init__.py | 9 +- .../controllers/attachments_controller.py | 4 +- .../pixelated/controllers/features_controller.py | 4 +- service/pixelated/controllers/home_controller.py | 24 ++++- service/pixelated/controllers/mails_controller.py | 63 ++++++------ .../pixelated/controllers/sync_info_controller.py | 4 +- service/pixelated/controllers/tags_controller.py | 6 +- service/pixelated/runserver.py | 91 +++++++++++------- service/setup.py | 1 + service/test/functional/features/environment.py | 2 +- service/test/integration/drafts_test.py | 5 + .../test/integration/mark_as_read_unread_test.py | 9 +- service/test/integration/search_test.py | 4 +- service/test/support/integration_helper.py | 66 +++++++++---- service/test/unit/adapter/mail_sender_test.py | 46 --------- .../test/unit/controllers/mails_controller_test.py | 46 +++++---- .../unit/controllers/sync_info_controller_test.py | 4 +- service/test/unit/runserver_test.py | 23 ++--- web-ui/app/locales/en-us/translation.json | 71 ++++++++++++++ 22 files changed, 361 insertions(+), 305 deletions(-) delete mode 100644 service/pixelated/config/reactor_manager.py delete mode 100644 service/test/unit/adapter/mail_sender_test.py create mode 100644 web-ui/app/locales/en-us/translation.json diff --git a/service/pixelated/adapter/mail_sender.py b/service/pixelated/adapter/mail_sender.py index 1802a9d5..6fad2b6f 100644 --- a/service/pixelated/adapter/mail_sender.py +++ b/service/pixelated/adapter/mail_sender.py @@ -13,21 +13,26 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . -import smtplib +from StringIO import StringIO +from twisted.internet.defer import Deferred +from twisted.mail.smtp import SMTPSenderFactory +from twisted.internet import reactor from pixelated.support.functional import flatten class MailSender(): def __init__(self, account_email_address, smtp_client=None): self.account_email_address = account_email_address - self.smtp_client = smtp_client or smtplib.SMTP('localhost', 4650) def sendmail(self, mail): recipients = flatten([mail.to, mail.cc, mail.bcc]) - self.smtp_client.sendmail( - self.account_email_address, - recipients, - mail.to_smtp_format() - ) + resultDeferred = Deferred() + senderFactory = SMTPSenderFactory( + fromEmail=self.account_email_address, + toEmail=recipients, + file=StringIO(mail.to_smtp_format()), + deferred=resultDeferred) + + return reactor.connectTCP('localhost', 4650, senderFactory) diff --git a/service/pixelated/config/app_factory.py b/service/pixelated/config/app_factory.py index 02e5781b..bbc29527 100644 --- a/service/pixelated/config/app_factory.py +++ b/service/pixelated/config/app_factory.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . - +from twisted.internet import reactor from pixelated.adapter.mail_service import MailService from pixelated.adapter.mail import InputMail from pixelated.adapter.mail_sender import MailSender @@ -33,6 +33,7 @@ from leap.common.events import ( register, events_pb2 as proto ) +from twisted.web.server import Site def init_index_and_remove_dupes(querier, search_engine, mail_service): @@ -53,28 +54,28 @@ def update_info_sync_and_index_partial(sync_info_controller, search_engine, mail def _setup_routes(app, home_controller, mails_controller, tags_controller, features_controller, sync_info_controller, attachments_controller): - # home - app.add_url_rule('/', methods=['GET'], view_func=home_controller.home) # mails - app.add_url_rule('/mails', methods=['GET'], view_func=mails_controller.mails) - app.add_url_rule('/mail//read', methods=['POST'], view_func=mails_controller.mark_mail_as_read) - app.add_url_rule('/mail//unread', methods=['POST'], view_func=mails_controller.mark_mail_as_unread) - app.add_url_rule('/mails/unread', methods=['POST'], view_func=mails_controller.mark_many_mail_unread) - app.add_url_rule('/mails/read', methods=['POST'], view_func=mails_controller.mark_many_mail_read) - app.add_url_rule('/mail/', methods=['GET'], view_func=mails_controller.mail) - app.add_url_rule('/mail/', methods=['DELETE'], view_func=mails_controller.delete_mail) - app.add_url_rule('/mails', methods=['DELETE'], view_func=mails_controller.delete_mails) - app.add_url_rule('/mails', methods=['POST'], view_func=mails_controller.send_mail) - app.add_url_rule('/mail//tags', methods=['POST'], view_func=mails_controller.mail_tags) - app.add_url_rule('/mails', methods=['PUT'], view_func=mails_controller.update_draft) + app.route('/mails', methods=['GET'])(mails_controller.mails) + app.route('/mail//read', methods=['POST'])(mails_controller.mark_mail_as_read) + app.route('/mail//unread', methods=['POST'])(mails_controller.mark_mail_as_unread) + app.route('/mails/unread', methods=['POST'])(mails_controller.mark_many_mail_unread) + app.route('/mails/read', methods=['POST'])(mails_controller.mark_many_mail_read) + app.route('/mail/', methods=['GET'])(mails_controller.mail) + app.route('/mail/', methods=['DELETE'])(mails_controller.delete_mail) + app.route('/mails', methods=['DELETE'])(mails_controller.delete_mails) + app.route('/mails', methods=['POST'])(mails_controller.send_mail) + app.route('/mail//tags', methods=['POST'])(mails_controller.mail_tags) + app.route('/mails', methods=['PUT'])(mails_controller.update_draft) # tags - app.add_url_rule('/tags', methods=['GET'], view_func=tags_controller.tags) + app.route('/tags', methods=['GET'])(tags_controller.tags) # features - app.add_url_rule('/features', methods=['GET'], view_func=features_controller.features) + app.route('/features', methods=['GET'])(features_controller.features) # sync info - app.add_url_rule('/sync_info', methods=['GET'], view_func=sync_info_controller.sync_info) + app.route('/sync_info', methods=['GET'])(sync_info_controller.sync_info) # attachments - app.add_url_rule('/attachment/', methods=['GET'], view_func=attachments_controller.attachment) + app.route('/attachment/', methods=['GET'])(attachments_controller.attachment) + # static + app.route('/', methods=['GET'], branch=True)(home_controller.home) def init_leap_session(app): @@ -91,46 +92,51 @@ def init_leap_session(app): return leap_session -def create_app(app, debug_enabled): - with app.app_context(): - leap_session = init_leap_session(app) +def init_app(app): + leap_session = init_leap_session(app) + + tag_service = TagService() + search_engine = SearchEngine() + pixelated_mail_sender = MailSender(leap_session.account_email()) - tag_service = TagService() - search_engine = SearchEngine() - pixelated_mail_sender = MailSender(leap_session.account_email()) + soledad_querier = SoledadQuerier(soledad=leap_session.account._soledad) + pixelated_mailboxes = Mailboxes(leap_session.account, soledad_querier) + draft_service = DraftService(pixelated_mailboxes) + mail_service = MailService(pixelated_mailboxes, pixelated_mail_sender, tag_service, soledad_querier) - soledad_querier = SoledadQuerier(soledad=leap_session.account._soledad) - pixelated_mailboxes = Mailboxes(leap_session.account, soledad_querier) + MailboxIndexerListener.SEARCH_ENGINE = search_engine + InputMail.FROM_EMAIL_ADDRESS = leap_session.account_email() - draft_service = DraftService(pixelated_mailboxes) - mail_service = MailService(pixelated_mailboxes, pixelated_mail_sender, tag_service, soledad_querier) + 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) + sync_info_controller = SyncInfoController() + attachments_controller = AttachmentsController(soledad_querier) - MailboxIndexerListener.SEARCH_ENGINE = search_engine - InputMail.FROM_EMAIL_ADDRESS = leap_session.account_email() + register(signal=proto.SOLEDAD_SYNC_RECEIVE_STATUS, + callback=update_info_sync_and_index_partial(sync_info_controller=sync_info_controller, + search_engine=search_engine, + mail_service=mail_service)) + register(signal=proto.SOLEDAD_DONE_DATA_SYNC, + callback=init_index_and_remove_dupes(querier=soledad_querier, + search_engine=search_engine, + mail_service=mail_service)) - 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) - sync_info_controller = SyncInfoController() - attachments_controller = AttachmentsController(soledad_querier) + _setup_routes(app, home_controller, mails_controller, tags_controller, features_controller, + sync_info_controller, attachments_controller) - register(signal=proto.SOLEDAD_SYNC_RECEIVE_STATUS, - callback=update_info_sync_and_index_partial(sync_info_controller=sync_info_controller, - search_engine=search_engine, - mail_service=mail_service)) - register(signal=proto.SOLEDAD_DONE_DATA_SYNC, - callback=init_index_and_remove_dupes(querier=soledad_querier, - search_engine=search_engine, - mail_service=mail_service)) - _setup_routes(app, home_controller, mails_controller, tags_controller, features_controller, - sync_info_controller, attachments_controller) +def create_app(app): + from twisted.python import log + import sys + log.startLogging(sys.stdout) - app.run(host=app.config['HOST'], debug=debug_enabled, - port=app.config['PORT'], use_reloader=False) + reactor.listenTCP(3333, Site(app.resource()), interface='localhost') + reactor.callWhenRunning(lambda: init_app(app)) + reactor.run() def get_static_folder(): diff --git a/service/pixelated/config/reactor_manager.py b/service/pixelated/config/reactor_manager.py deleted file mode 100644 index af140c5b..00000000 --- a/service/pixelated/config/reactor_manager.py +++ /dev/null @@ -1,59 +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 . - -import signal -import sys -from threading import Thread -import logging - -from twisted.internet import reactor - - -def start_reactor(logging=False): - if logging: - enable_logging() - - def start_reactor_run(): - reactor.run(False) - - global REACTOR_THREAD - REACTOR_THREAD = Thread(target=start_reactor_run) - daemon = True - REACTOR_THREAD.start() - - -def stop_reactor_on_exit(): - reactor.callFromThread(reactor.stop) - global REACTOR_THREAD - REACTOR_THREAD = None - - -def enable_logging(): - logging.basicConfig(level=logging.DEBUG, - format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', - datefmt='%m-%d %H:%M', - filename='/tmp/leap.log', - filemode='w') - - # define a Handler which writes INFO messages or higher to the sys.stderr - console = logging.StreamHandler() - console.setLevel(logging.DEBUG) - # set a format which is simpler for console use - formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') - # tell the handler to use this format - console.setFormatter(formatter) - # add the handler to the root logger - logging.getLogger('').addHandler(console) diff --git a/service/pixelated/controllers/__init__.py b/service/pixelated/controllers/__init__.py index e0c05afd..9e447d4d 100644 --- a/service/pixelated/controllers/__init__.py +++ b/service/pixelated/controllers/__init__.py @@ -15,16 +15,15 @@ # along with Pixelated. If not, see . -def respond_json(entity, status_code=200): +def respond_json(entity, request, status_code=200): json_response = json.dumps(entity) - response = Response(response=json_response, mimetype="application/json") - response.status_code = status_code - return response + request.responseHeaders.addRawHeader(b"content-type", b"application/json") + request.code = status_code + return json_response import json -from flask import Response from home_controller import HomeController from mails_controller import MailsController from tags_controller import TagsController diff --git a/service/pixelated/controllers/attachments_controller.py b/service/pixelated/controllers/attachments_controller.py index 7435ce33..68e73bd6 100644 --- a/service/pixelated/controllers/attachments_controller.py +++ b/service/pixelated/controllers/attachments_controller.py @@ -26,8 +26,8 @@ class AttachmentsController: def __init__(self, querier): self.querier = querier - def attachment(self, attachment_id): - encoding = request.args.get('encoding', '') + def attachment(self, request, attachment_id): + encoding = request.args.get('encoding', [''])[0] attachment = self.querier.attachment(attachment_id, encoding) response = send_file(io.BytesIO(attachment['content']), mimetype=self._extract_mimetype(attachment['content-type'])) diff --git a/service/pixelated/controllers/features_controller.py b/service/pixelated/controllers/features_controller.py index 49fc875f..4d375683 100644 --- a/service/pixelated/controllers/features_controller.py +++ b/service/pixelated/controllers/features_controller.py @@ -24,9 +24,9 @@ class FeaturesController: def __init__(self): pass - def features(self): + def features(self, request): 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}) + return respond_json({'disabled_features': self.DISABLED_FEATURES, 'dispatcher_features': disabled_features}, request) diff --git a/service/pixelated/controllers/home_controller.py b/service/pixelated/controllers/home_controller.py index 69ecd52f..ccdad197 100644 --- a/service/pixelated/controllers/home_controller.py +++ b/service/pixelated/controllers/home_controller.py @@ -13,14 +13,30 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +import os -from flask import current_app +from twisted.web.static import File class HomeController: - def __init__(self): + self.static_folder = self._get_static_folder() pass - def home(self): - return current_app.send_static_file('index.html') + def _get_static_folder(self): + + 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') + return static_folder + + def home(self, request): + request_type = request.requestHeaders.getRawHeaders('accept')[0].split(',')[0] + response_type = request_type if request_type else "text/html" + + request.setHeader('Content-Type', response_type) + return File('%s/' % self.static_folder, defaultType=response_type) diff --git a/service/pixelated/controllers/mails_controller.py b/service/pixelated/controllers/mails_controller.py index ebef4af1..3a2e0d3b 100644 --- a/service/pixelated/controllers/mails_controller.py +++ b/service/pixelated/controllers/mails_controller.py @@ -17,7 +17,6 @@ import json from pixelated.adapter.mail import InputMail from pixelated.controllers import respond_json -from flask import request class MailsController: @@ -27,8 +26,8 @@ class MailsController: self._draft_service = draft_service self._search_engine = search_engine - def mails(self, _request=request): - mail_ids, total = self._search_engine.search(_request.args.get('q'), _request.args.get('w'), _request.args.get('p')) + def mails(self, request): + mail_ids, total = self._search_engine.search(request.args.get('q')[0], request.args.get('w')[0], request.args.get('p')[0]) mails = self._mail_service.mails(mail_ids) response = { @@ -38,77 +37,83 @@ class MailsController: "mails": [mail.as_dict() for mail in mails] } - return respond_json(response) + return json.dumps(response) - def mail(self, mail_id): + def mail(self, request, mail_id): mail = self._mail_service.mail(mail_id) - return respond_json(mail.as_dict()) + return respond_json(mail.as_dict(), request) - def mark_mail_as_read(self, mail_id): + def mark_mail_as_read(self, request, mail_id): mail = self._mail_service.mark_as_read(mail_id) self._search_engine.index_mail(mail) return "" - def mark_mail_as_unread(self, mail_id): + def mark_mail_as_unread(self, request, mail_id): mail = self._mail_service.mark_as_unread(mail_id) self._search_engine.index_mail(mail) return "" - def mark_many_mail_unread(self): - idents = json.loads(request.form['idents']) + def mark_many_mail_unread(self, request): + content_dict = json.load(request.content) + idents = content_dict.get('idents') for ident in idents: mail = self._mail_service.mark_as_unread(ident) self._search_engine.index_mail(mail) return "" - def mark_many_mail_read(self): - idents = json.loads(request.form['idents']) + def mark_many_mail_read(self, request): + content_dict = json.load(request.content) + idents = content_dict.get('idents') for ident in idents: mail = self._mail_service.mark_as_read(ident) self._search_engine.index_mail(mail) return "" - def delete_mail(self, mail_id): + def delete_mail(self, request, mail_id): mail = self._mail_service.mail(mail_id) if mail.mailbox_name == 'TRASH': self._mail_service.delete_permanent(mail_id) else: trashed_mail = self._mail_service.delete_mail(mail_id) self._search_engine.index_mail(trashed_mail) - return respond_json(None) + return respond_json(None, request) - def delete_mails(self): + def delete_mails(self, request): idents = json.loads(request.form['idents']) for ident in idents: self.delete_mail(ident) - return respond_json(None) + return respond_json(None, request) - def send_mail(self, _request=request): + def send_mail(self, request): try: - _mail = InputMail.from_dict(_request.json) - draft_id = _request.json.get('ident') + content_dict = json.loads(request.content.read()) + _mail = InputMail.from_dict(content_dict) + draft_id = content_dict.get('ident') if draft_id: self._search_engine.remove_from_index(draft_id) _mail = self._mail_service.send(draft_id, _mail) self._search_engine.index_mail(_mail) - return respond_json(_mail.as_dict()) + return respond_json(_mail.as_dict(), request) except Exception as error: - return respond_json({'message': self._format_exception(error)}, status_code=422) + return respond_json({'message': self._format_exception(error)}, request, status_code=422) - def mail_tags(self, mail_id): - new_tags = map(lambda tag: tag.lower(), request.get_json()['newtags']) + def mail_tags(self, request, mail_id): + content_dict = json.loads(request.content.read()) + new_tags = map(lambda tag: tag.lower(), content_dict['newtags']) try: self._mail_service.update_tags(mail_id, new_tags) mail = self._mail_service.mail(mail_id) self._search_engine.index_mail(mail) except ValueError as ve: - return respond_json(ve.message, 403) - return respond_json(mail.as_dict()) + return respond_json(ve.message, request, 403) + return respond_json(mail.as_dict(), request) + + def update_draft(self, request): + content_dict = json.loads(request.content.read()) - def update_draft(self): - _mail = InputMail.from_dict(request.json) - draft_id = request.json.get('ident') + _mail = InputMail.from_dict(content_dict) + draft_id = content_dict.get('ident') if draft_id: ident = self._draft_service.update_draft(draft_id, _mail).ident self._search_engine.remove_from_index(draft_id) @@ -116,7 +121,7 @@ class MailsController: ident = self._draft_service.create_draft(_mail).ident self._search_engine.index_mail(self._mail_service.mail(ident)) - return respond_json({'ident': ident}) + return respond_json({'ident': ident}, request) def _format_exception(self, exception): exception_info = map(str, list(exception.args)) diff --git a/service/pixelated/controllers/sync_info_controller.py b/service/pixelated/controllers/sync_info_controller.py index 3a8e1a16..50e53852 100644 --- a/service/pixelated/controllers/sync_info_controller.py +++ b/service/pixelated/controllers/sync_info_controller.py @@ -29,7 +29,7 @@ class SyncInfoController: def set_sync_info(self, soledad_sync_status): self.current, self.total = map(int, soledad_sync_status.content.split('/')) - def sync_info(self): + def sync_info(self, request): _sync_info = { 'is_syncing': self.current != self.total, 'count': { @@ -38,4 +38,4 @@ class SyncInfoController: 'progress': self._get_progress() } } - return respond_json(_sync_info) + return respond_json(_sync_info, request) diff --git a/service/pixelated/controllers/tags_controller.py b/service/pixelated/controllers/tags_controller.py index 52ed762a..0b9a94ac 100644 --- a/service/pixelated/controllers/tags_controller.py +++ b/service/pixelated/controllers/tags_controller.py @@ -23,8 +23,8 @@ class TagsController: def __init__(self, search_engine): self._search_engine = search_engine - def tags(self): - query = request.args.get('q') + def tags(self, request): + query = request.args.get('q', [''])[0] skip_default_tags = request.args.get('skipDefaultTags') tags = self._search_engine.tags(query=query, skip_default_tags=skip_default_tags) - return respond_json(tags) + return respond_json(tags, request) diff --git a/service/pixelated/runserver.py b/service/pixelated/runserver.py index 314a5d71..51c8d40a 100644 --- a/service/pixelated/runserver.py +++ b/service/pixelated/runserver.py @@ -17,42 +17,42 @@ import os import sys import logging -from flask import Flask + +from klein import Klein + +klein_app = Klein() + +import ConfigParser from leap.common.events import server as events_server from pixelated.config import app_factory import pixelated.config.args as input_args import pixelated.bitmask_libraries.register as leap_register from pixelated.bitmask_libraries.leap_srp import LeapAuthException import pixelated.config.credentials_prompt as credentials_prompt -import pixelated.config.reactor_manager as reactor_manager import pixelated.support.ext_protobuf # monkey patch for protobuf in OSX import pixelated.support.ext_sqlcipher # monkey patch for sqlcipher in debian -app = Flask(__name__, static_url_path='', static_folder=app_factory.get_static_folder()) +app = Klein() +app.config = {} credentials_pipe = os.path.join('/', 'data', 'credentials-fifo') def setup(): - try: - args = input_args.parse() - app.config.update({'HOST': args.host, 'PORT': args.port}) - - debugger = setup_debugger(args.debug) + args = input_args.parse() + setup_debugger(args.debug) - if args.register: - register(*args.register[::-1]) + if args.register: + register(*args.register[::-1]) + else: + if args.dispatcher: + provider, user, password = fetch_credentials_from_dispatcher() + app.config['LEAP_SERVER_NAME'] = provider + app.config['LEAP_USERNAME'] = user + app.config['LEAP_PASSWORD'] = password else: - if args.dispatcher: - provider, user, password = fetch_credentials_from_dispatcher() - app.config['LEAP_SERVER_NAME'] = provider - app.config['LEAP_USERNAME'] = user - app.config['LEAP_PASSWORD'] = password - else: - configuration_setup(args.config) - start_services(debugger) - finally: - reactor_manager.stop_reactor_on_exit() + configuration_setup(args.config) + start_services() def register(username, server_name): @@ -75,28 +75,47 @@ def fetch_credentials_from_dispatcher(): def setup_debugger(enabled): debug_enabled = enabled or os.environ.get('DEBUG', False) - if not debug_enabled: - logging.basicConfig() - logger = logging.getLogger('werkzeug') - logger.setLevel(logging.INFO) + logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', + datefmt='%m-%d %H:%M', + filename='/tmp/leap.log', + filemode='w') # define a Handler which writes INFO messages or higher to the sys.stderr + console = logging.StreamHandler() + console.setLevel(logging.DEBUG) + # set a format which is simpler for console use + formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') + # tell the handler to use this format + console.setFormatter(formatter) + # add the handler to the root logger + logging.getLogger('').addHandler(console) + return debug_enabled -def configuration_setup(config): - if config is not None: - config_file = os.path.abspath(os.path.expanduser(config)) - app.config.from_pyfile(config_file) - else: - provider, user, password = credentials_prompt.run() - app.config['LEAP_SERVER_NAME'] = provider - app.config['LEAP_USERNAME'] = user - app.config['LEAP_PASSWORD'] = password +def parse_config_from_file(config_file): + config_parser = ConfigParser.ConfigParser() + config_file = os.path.abspath(os.path.expanduser(config_file)) + config_parser.read(config_file) + provider, user, password = \ + config_parser.get('pixelated', 'leap_server_name'), \ + config_parser.get('pixelated', 'leap_username'), \ + config_parser.get('pixelated', 'leap_password') + + # TODO: add error messages in case one of the parameters are empty + return provider, user, password + + +def configuration_setup(config_file): + provider, user, password = parse_config_from_file(config_file) if config_file else credentials_prompt.run() + + app.config['LEAP_SERVER_NAME'] = provider + app.config['LEAP_USERNAME'] = user + app.config['LEAP_PASSWORD'] = password -def start_services(debug): - reactor_manager.start_reactor(logging=debug) +def start_services(): events_server.ensure_server(port=8090) - app_factory.create_app(app, debug) + app_factory.create_app(app) if __name__ == '__main__': diff --git a/service/setup.py b/service/setup.py index 9b7786e0..4d8cf0a9 100644 --- a/service/setup.py +++ b/service/setup.py @@ -78,6 +78,7 @@ setup(name='pixelated-user-agent', 'Twisted==14.0.2', 'service-identity==14.0.0', 'flask==0.10.1', + 'klein==0.2.3', 'requests==2.4.3', 'srp==1.0.5', 'dirspec==13.10', 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 . 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 . -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 . +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 . 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' diff --git a/web-ui/app/locales/en-us/translation.json b/web-ui/app/locales/en-us/translation.json new file mode 100644 index 00000000..b84f39bb --- /dev/null +++ b/web-ui/app/locales/en-us/translation.json @@ -0,0 +1,71 @@ +{ + "compose": "Compose", + "re": "Re: ", + "Fwd: ": "Fwd: ", + "Your message was moved to trash!": "Your message was moved to trash!", + "Your message was archive it!": "Your message was archived!", + "Your message was permanently deleted!": "Your message was permanently deleted!", + "Saved as draft.": "Saved as draft.", + "One or more of the recipients are not valid emails": "One or more of the recipients are not valid emails", + "Could not update mail tags": "Could not update mail tags", + "Invalid tag name": "Invalid tag name", + "Could not delete email": "Could not delete email", + "Could not fetch messages": "Could not fetch messages", + "TO": "TO", + "To": "To", + "CC": "CC", + "BCC": "BCC", + "Body": "Body", + "Subject": "Subject", + "Don't worry about recipients right now, you'll be able to add them just before sending.": "Don't worry about recipients right now, you'll be able to add them just before sending.", + "Send": "Send", + "Cancel": "Cancel", + "Save Draft": "Save Draft", + "Reply": "Reply", + "Reply to All": "Reply to All", + "Mark as read": "Mark as read", + "Delete": "Delete", + "Archive": "Archive", + "Close": "Close", + "Trash this message": "Trash this message", + "NOTHING SELECTED": "NOTHING SELECTED", + "Press Enter to create": "Press Enter to create", + "You are trying to delete the last tag on this message.": "You are trying to delete the last tag on this message.", + "What would you like to do?": "What would you like to do?", + "Trash message": "Trash message", + "Archive it": "Archive it", + "Trash:": "Trash:", + "Archive:": "Archive:", + "we will keep this message for 30 days, then delete it forever.": "we will keep this message for 30 days, then delete it forever.", + "we will remove all the tags, but keep it in your account in case you need it.": "we will remove all the tags, but keep it in your account in case you need it.", + "to:": "to:", + "no_subject": "", + "no_recipient": "", + "you": "you", + "encrypted": "Encrypted", + "encrypted encryption-failure": "You are not authorized to see this message.", + "encrypted encryption-valid": "Message was transmitted securely.", + "not-encrypted": "Message was readable during transmission.", + "signed": "Certified sender.", + "signed signature-revoked": "Sender could not be securely identified.", + "signed signature-expired": "Sender could not be securely identified.", + "signed signature-not-trusted": "Sender and/or message cannot be trusted.", + "signed signature-unknown": "Sender and/or message cannot be trusted.", + "not-signed": "Sender could not be securely identified.", + "send-button": "Send", + "sending-mail": "Sending...", + "draft-button": "Save Draft", + "trash-button": "Trash it", + "Search..." : "Search...", + "Search results for:": "Search results for:", + "Tags": "Tags", + "Forward": "Forward", + + "tags": { + "inbox": "Inbox", + "sent": "Sent", + "drafts": "Drafts", + "trash": "Trash", + "all": "All" + } +} -- cgit v1.2.3