From 9fc2b6f422170a9c5f404aa2500a409812c0b56d Mon Sep 17 00:00:00 2001 From: KrunoKnego Date: Thu, 30 Jun 2016 00:26:56 +0200 Subject: [PATCH 01/15] Download raw email --- .../adapter/mailstore/leap_mailstore.py | 10 ++++++++++ .../pixelated/adapter/services/mail_service.py | 3 +++ service/pixelated/resources/mail_resource.py | 17 +++++++++++++++++ web-ui/app/js/mail_view/ui/mail_actions.js | 6 ++++++ web-ui/app/locales/de_DE/translation.json | 1 + web-ui/app/locales/en_US/translation.json | 1 + web-ui/app/locales/es_ES/translation.json | 1 + web-ui/app/locales/pt_BR/translation.json | 1 + web-ui/app/locales/sv_SE/translation.json | 1 + web-ui/app/templates/mails/mail_actions.hbs | 1 + 10 files changed, 42 insertions(+) diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 97c95a909..45484e326 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -204,6 +204,16 @@ def get_mail(self, mail_id, include_body=False): defer.returnValue(leap_mail) + @defer.inlineCallbacks + def get_mail_raw(self, mail_id, include_body=False): + message = yield self._fetch_msg_from_soledad(mail_id) + if not _is_empty_message(message): + leap_mail = yield self._leap_message_to_leap_mail(mail_id, message, include_body).raw + else: + leap_mail = None + + defer.returnValue(leap_mail) + @defer.inlineCallbacks def get_mails(self, mail_ids, gracefully_ignore_errors=False, include_body=False): deferreds = [] diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 1dce51fef..68b0e6046 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -83,6 +83,9 @@ def _use_current_casing(new_tag_lower): def mail(self, mail_id): return self.mail_store.get_mail(mail_id, include_body=True) + def mail_raw(self, mail_id): + return self.mail_store.get_mail_raw(mail_id, include_body=True) + def attachment(self, attachment_id): return self.attachment_store.get_mail_attachment(attachment_id) diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py index 58bc77568..7163c8b43 100644 --- a/service/pixelated/resources/mail_resource.py +++ b/service/pixelated/resources/mail_resource.py @@ -55,6 +55,23 @@ def populate_reply(mail): return NOT_DONE_YET + def render_raw_GET(self, request): + def populate_reply(mail): + mail_dict = mail.as_dict() + current_user = self._mail_service.account_email + sender = mail.headers.get('Reply-to', mail.headers.get('From')) + to = mail.headers.get('To', []) + ccs = mail.headers.get('Cc', []) + mail_dict['replying'] = replier.generate_recipients(sender, to, ccs, current_user) + return mail_dict + + d = self._mail_service.mail_raw(self._mail_id) + d.addCallback(lambda mail: populate_reply(mail)) + d.addCallback(lambda mail_dict: respond_json_deferred(mail_dict, request)) + d.addErrback(handle_error_deferred, request) + + return NOT_DONE_YET + def render_DELETE(self, request): def response_failed(failure): err(failure, 'something failed') diff --git a/web-ui/app/js/mail_view/ui/mail_actions.js b/web-ui/app/js/mail_view/ui/mail_actions.js index 65cd0aaac..6502e8557 100644 --- a/web-ui/app/js/mail_view/ui/mail_actions.js +++ b/web-ui/app/js/mail_view/ui/mail_actions.js @@ -33,6 +33,7 @@ define( replyButtonTop: '#reply-button-top', viewMoreActions: '#view-more-actions', replyAllButtonTop: '#reply-all-button-top', + getMailRaw: '#get-mail-raw', deleteButtonTop: '#delete-button-top', moreActions: '#more-actions' }); @@ -53,6 +54,11 @@ define( this.select('moreActions').hide(); }.bind(this)); + this.on(this.select('getMailRaw'), 'click', function () { + this.trigger(document, events.ui.replyBox.showReplyAll); + this.select('moreActions').hide(); + }.bind(this)); + this.on(this.select('deleteButtonTop'), 'click', function () { this.trigger(document, events.ui.mail.delete, {mail: this.attr.mail}); this.select('moreActions').hide(); diff --git a/web-ui/app/locales/de_DE/translation.json b/web-ui/app/locales/de_DE/translation.json index 6ab27a1b9..13624332b 100644 --- a/web-ui/app/locales/de_DE/translation.json +++ b/web-ui/app/locales/de_DE/translation.json @@ -21,6 +21,7 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", + "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", diff --git a/web-ui/app/locales/en_US/translation.json b/web-ui/app/locales/en_US/translation.json index cd54ddf7d..3be227023 100644 --- a/web-ui/app/locales/en_US/translation.json +++ b/web-ui/app/locales/en_US/translation.json @@ -25,6 +25,7 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", + "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", diff --git a/web-ui/app/locales/es_ES/translation.json b/web-ui/app/locales/es_ES/translation.json index 6ab27a1b9..13624332b 100644 --- a/web-ui/app/locales/es_ES/translation.json +++ b/web-ui/app/locales/es_ES/translation.json @@ -21,6 +21,7 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", + "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", diff --git a/web-ui/app/locales/pt_BR/translation.json b/web-ui/app/locales/pt_BR/translation.json index 4c75ff0fa..cfda01240 100644 --- a/web-ui/app/locales/pt_BR/translation.json +++ b/web-ui/app/locales/pt_BR/translation.json @@ -23,6 +23,7 @@ "reply": "Responder", "reply-to-all": "Responder para todos", "delete-this-message": "Deletar essa mensagem", + "get-mail-raw": "Get mail raw", "mark-as-read": "Marcar como lida", "mark-as-unread": "Marcar como não lida", "delete": "Deletar", diff --git a/web-ui/app/locales/sv_SE/translation.json b/web-ui/app/locales/sv_SE/translation.json index 6ab27a1b9..13624332b 100644 --- a/web-ui/app/locales/sv_SE/translation.json +++ b/web-ui/app/locales/sv_SE/translation.json @@ -21,6 +21,7 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", + "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", diff --git a/web-ui/app/templates/mails/mail_actions.hbs b/web-ui/app/templates/mails/mail_actions.hbs index 0adfe853f..7c6b5a211 100644 --- a/web-ui/app/templates/mails/mail_actions.hbs +++ b/web-ui/app/templates/mails/mail_actions.hbs @@ -2,5 +2,6 @@ From 818b91f9573640e3ee13f3f03817b56dbdb35aa1 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Fri, 1 Jul 2016 22:08:26 +0200 Subject: [PATCH 02/15] Downloading raw mail --- .../adapter/mailstore/leap_mailstore.py | 2 +- .../pixelated/adapter/mailstore/mailstore.py | 3 +++ service/pixelated/resources/mail_resource.py | 13 ++++++++++-- web-ui/app/js/mail_list/ui/mail_list.js | 6 ++++++ web-ui/app/js/mail_view/ui/mail_actions.js | 5 +++-- web-ui/app/js/page/events.js | 7 ++++--- web-ui/app/js/services/mail_service.js | 15 ++++++++++++++ web-ui/app/scss/views/_compose-view.scss | 2 +- web-ui/app/scss/views/css | 20 +++++++++++++++++++ 9 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 web-ui/app/scss/views/css diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 45484e326..b4fb76308 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -208,7 +208,7 @@ def get_mail(self, mail_id, include_body=False): def get_mail_raw(self, mail_id, include_body=False): message = yield self._fetch_msg_from_soledad(mail_id) if not _is_empty_message(message): - leap_mail = yield self._leap_message_to_leap_mail(mail_id, message, include_body).raw + leap_mail = yield self._leap_message_to_leap_mail(mail_id, message, include_body) else: leap_mail = None diff --git a/service/pixelated/adapter/mailstore/mailstore.py b/service/pixelated/adapter/mailstore/mailstore.py index fbd7fc9ec..8f5f32290 100644 --- a/service/pixelated/adapter/mailstore/mailstore.py +++ b/service/pixelated/adapter/mailstore/mailstore.py @@ -19,6 +19,9 @@ class MailStore(object): def get_mail(self, mail_id): pass + def get_mail_raw(self, mail_id): + pass + def get_mail_attachment(self, attachment_id): pass diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py index 7163c8b43..6f2840df3 100644 --- a/service/pixelated/resources/mail_resource.py +++ b/service/pixelated/resources/mail_resource.py @@ -4,7 +4,7 @@ from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET -from pixelated.resources import respond_json_deferred, BaseResource, handle_error_deferred +from pixelated.resources import respond_json_deferred, respond_json, BaseResource, handle_error_deferred from pixelated.support import replier @@ -55,7 +55,16 @@ def populate_reply(mail): return NOT_DONE_YET - def render_raw_GET(self, request): + def render_POST(self, request): + # return 'HELLO' + + # d = self._mail_service.mail_raw(self._mail_id) + # print("*" * 1000) + # print(d) + # print("*" * 1000) + # + # return d + def populate_reply(mail): mail_dict = mail.as_dict() current_user = self._mail_service.account_email diff --git a/web-ui/app/js/mail_list/ui/mail_list.js b/web-ui/app/js/mail_list/ui/mail_list.js index af4821a86..7ebb748a9 100644 --- a/web-ui/app/js/mail_list/ui/mail_list.js +++ b/web-ui/app/js/mail_list/ui/mail_list.js @@ -149,6 +149,11 @@ define( } }; + this.downloadRaw = function () { + this.trigger(document, events.ui.mail.downloadRaw); + console.log("downloade jetzt diesen scheiss"); + }; + this.refreshAfterMailSent = function () { if (this.attr.currentTag === 'drafts' || this.attr.currentTag === 'sent') { this.refreshWithScroll(); @@ -163,6 +168,7 @@ define( this.on(document, events.mails.availableForRefresh, this.refreshMailList); this.on(document, events.mail.draftSaved, this.refreshAfterSaveDraft); + this.on(document, events.mail.downloadRaw, this.downloadRaw); this.on(document, events.mail.sent, this.refreshAfterMailSent); this.on(document, events.ui.mail.updateSelected, this.updateSelected); diff --git a/web-ui/app/js/mail_view/ui/mail_actions.js b/web-ui/app/js/mail_view/ui/mail_actions.js index 6502e8557..ce39331b9 100644 --- a/web-ui/app/js/mail_view/ui/mail_actions.js +++ b/web-ui/app/js/mail_view/ui/mail_actions.js @@ -55,7 +55,7 @@ define( }.bind(this)); this.on(this.select('getMailRaw'), 'click', function () { - this.trigger(document, events.ui.replyBox.showReplyAll); + this.trigger(document, events.ui.mail.downloadRaw, {mail: this.attr.mail}); this.select('moreActions').hide(); }.bind(this)); @@ -70,9 +70,10 @@ define( this.on(this.select('viewMoreActions'), 'blur', function (event) { var replyButtonTopHover = this.select('replyAllButtonTop').is(':hover'); + var downloadRaw = this.select('getMailRaw').is(':hover'); var deleteButtonTopHover = this.select('deleteButtonTop').is(':hover'); - if (replyButtonTopHover || deleteButtonTopHover) { + if (replyButtonTopHover || deleteButtonTopHover || downloadRaw) { event.preventDefault(); } else { this.select('moreActions').hide(); diff --git a/web-ui/app/js/page/events.js b/web-ui/app/js/page/events.js index 68a6aad1e..d082694c4 100644 --- a/web-ui/app/js/page/events.js +++ b/web-ui/app/js/page/events.js @@ -50,7 +50,7 @@ define(function () { uncheckAll: 'ui:mails:uncheckAll', hasMailsChecked: 'ui:mails:hasMailsChecked' }, - mail: { + mail: { open: 'ui:mail:open', updateSelected: 'ui:mail:updateSelected', delete: 'ui:mail:delete', @@ -64,7 +64,8 @@ define(function () { unchecked: 'ui:mail:unchecked', changedSinceLastSave: 'ui:mail:changedSinceLastSave', send: 'ui:mail:send', - recipientsUpdated: 'ui:mail:recipientsUpdated' + recipientsUpdated: 'ui:mail:recipientsUpdated', + downloadRaw: 'ui:mail:downloadRaw', }, page: { previous: 'ui:page:previous', @@ -82,7 +83,7 @@ define(function () { replyBox: { showReply: 'ui:replyBox:showReply', showReplyAll: 'ui:replyBox:showReplyAll', - showReplyContainer: 'ui:replyBox:showReplyContainer', + showReplyContainer: 'ui:replyBox:showReplyContainer' }, recipients: { entered: 'ui:recipients:entered', diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index 5e4bd4f39..4c58d1aff 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -15,6 +15,8 @@ * along with Pixelated. If not, see . */ + + define( [ 'flight/lib/component', @@ -275,6 +277,18 @@ define( } }; + this.downloadRaw = function (event, data) { + var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},v=function(t,u,s){if(!s){t=p(t)}var v=this,w=t.type,m=w===d,y,h=function(){l(v,"writestart progress write writeend".split(" "))},S=function(){if((f||m&&a)&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=f?r.result:r.result.replace(/^data:[^;]*;/,"data:attachment/file;");var n=e.open(t,"_blank");if(!n)e.location.href=t;t=undefined;v.readyState=v.DONE;h()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(!y){y=n().createObjectURL(t)}if(m){e.location.href=y}else{var o=e.open(y,"_blank");if(!o){e.location.href=y}}v.readyState=v.DONE;h();c(y)};v.readyState=v.INIT;if(o){y=n().createObjectURL(t);setTimeout(function(){r.href=y;r.download=u;i(r);h();c(y);v.readyState=v.DONE});return}S()},w=v.prototype,m=function(e,t,n){return new v(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=p(e)}return navigator.msSaveOrOpenBlob(e,t)}}w.abort=function(){};w.readyState=w.INIT=0;w.WRITING=1;w.DONE=2;w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})} + monitoredAjax(this, '/mail/' + data.mail.ident, + {type: 'POST'}) + .done(function (data) { + var blob = new Blob([data.textPlainBody], {type: "text/plain;charset=utf-8"}); + saveAs(blob, 'raw.txt'); + return; + }) + .fail(this.errorMessage('Failed to download')); + } + this.nextPage = function () { if (this.attr.currentPage < (this.attr.numPages)) { this.updateCurrentPageNumber(this.attr.currentPage + 1); @@ -327,6 +341,7 @@ define( this.on(document, events.ui.mails.refresh, this.refreshMails); this.on(document, events.ui.page.previous, this.previousPage); this.on(document, events.ui.page.next, this.nextPage); + this.on(document, events.ui.mail.downloadRaw, this.downloadRaw); this.fetchByTag(null, {tag: urlParams.getTag()}); }); diff --git a/web-ui/app/scss/views/_compose-view.scss b/web-ui/app/scss/views/_compose-view.scss index 813a60986..b642897c4 100644 --- a/web-ui/app/scss/views/_compose-view.scss +++ b/web-ui/app/scss/views/_compose-view.scss @@ -380,7 +380,7 @@ padding-left: 30px; .reply-container { - margin: 10px 0; + margin: 22px 0; padding: 10px; border: 1px dashed darken($contrast, 10%); diff --git a/web-ui/app/scss/views/css b/web-ui/app/scss/views/css new file mode 100644 index 000000000..02e03dce3 --- /dev/null +++ b/web-ui/app/scss/views/css @@ -0,0 +1,20 @@ +/* +Errno::ENOENT: No such file or directory @ rb_sysopen - css + +Backtrace: +/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:481:in `read' +/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:481:in `update_stylesheet' +/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:215:in `block in update_stylesheets' +/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:209:in `each' +/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:209:in `update_stylesheets' +/usr/lib/ruby/vendor_ruby/sass/plugin.rb:82:in `update_stylesheets' +/usr/lib/ruby/vendor_ruby/sass/exec/sass_scss.rb:346:in `watch_or_update' +/usr/lib/ruby/vendor_ruby/sass/exec/sass_scss.rb:50:in `process_result' +/usr/lib/ruby/vendor_ruby/sass/exec/base.rb:52:in `parse' +/usr/lib/ruby/vendor_ruby/sass/exec/base.rb:19:in `parse!' +/usr/bin/sass:8:in `
' +*/ +body:before { + white-space: pre; + font-family: monospace; + content: "Errno::ENOENT: No such file or directory @ rb_sysopen - css"; } From 5aaac446ff184b0d19f8764c76bd20e12af54f5d Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 18:46:23 +0200 Subject: [PATCH 03/15] clean up --- web-ui/app/js/mail_list/ui/mail_list.js | 6 ------ web-ui/app/js/services/mail_service.js | 2 +- web-ui/bower.json | 3 ++- web-ui/package.json | 3 ++- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/web-ui/app/js/mail_list/ui/mail_list.js b/web-ui/app/js/mail_list/ui/mail_list.js index 7ebb748a9..af4821a86 100644 --- a/web-ui/app/js/mail_list/ui/mail_list.js +++ b/web-ui/app/js/mail_list/ui/mail_list.js @@ -149,11 +149,6 @@ define( } }; - this.downloadRaw = function () { - this.trigger(document, events.ui.mail.downloadRaw); - console.log("downloade jetzt diesen scheiss"); - }; - this.refreshAfterMailSent = function () { if (this.attr.currentTag === 'drafts' || this.attr.currentTag === 'sent') { this.refreshWithScroll(); @@ -168,7 +163,6 @@ define( this.on(document, events.mails.availableForRefresh, this.refreshMailList); this.on(document, events.mail.draftSaved, this.refreshAfterSaveDraft); - this.on(document, events.mail.downloadRaw, this.downloadRaw); this.on(document, events.mail.sent, this.refreshAfterMailSent); this.on(document, events.ui.mail.updateSelected, this.updateSelected); diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index 4c58d1aff..2624a0669 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -278,7 +278,7 @@ define( }; this.downloadRaw = function (event, data) { - var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},v=function(t,u,s){if(!s){t=p(t)}var v=this,w=t.type,m=w===d,y,h=function(){l(v,"writestart progress write writeend".split(" "))},S=function(){if((f||m&&a)&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=f?r.result:r.result.replace(/^data:[^;]*;/,"data:attachment/file;");var n=e.open(t,"_blank");if(!n)e.location.href=t;t=undefined;v.readyState=v.DONE;h()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(!y){y=n().createObjectURL(t)}if(m){e.location.href=y}else{var o=e.open(y,"_blank");if(!o){e.location.href=y}}v.readyState=v.DONE;h();c(y)};v.readyState=v.INIT;if(o){y=n().createObjectURL(t);setTimeout(function(){r.href=y;r.download=u;i(r);h();c(y);v.readyState=v.DONE});return}S()},w=v.prototype,m=function(e,t,n){return new v(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=p(e)}return navigator.msSaveOrOpenBlob(e,t)}}w.abort=function(){};w.readyState=w.INIT=0;w.WRITING=1;w.DONE=2;w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})} + var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},v=function(t,u,s){if(!s){t=p(t)}var v=this,w=t.type,m=w===d,y,h=function(){l(v,"writestart progress write writeend".split(" "))},S=function(){if((f||m&&a)&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=f?r.result:r.result.replace(/^data:[^;]*;/,"data:attachment/file;");var n=e.open(t,"_blank");if(!n)e.location.href=t;t=undefined;v.readyState=v.DONE;h()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(!y){y=n().createObjectURL(t)}if(m){e.location.href=y}else{var o=e.open(y,"_blank");if(!o){e.location.href=y}}v.readyState=v.DONE;h();c(y)};v.readyState=v.INIT;if(o){y=n().createObjectURL(t);setTimeout(function(){r.href=y;r.download=u;i(r);h();c(y);v.readyState=v.DONE});return}S()},w=v.prototype,m=function(e,t,n){return new v(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=p(e)}return navigator.msSaveOrOpenBlob(e,t)}}w.abort=function(){};w.readyState=w.INIT=0;w.WRITING=1;w.DONE=2;w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})} monitoredAjax(this, '/mail/' + data.mail.ident, {type: 'POST'}) .done(function (data) { diff --git a/web-ui/bower.json b/web-ui/bower.json index 4b7bdfb95..c68b856fd 100644 --- a/web-ui/bower.json +++ b/web-ui/bower.json @@ -20,7 +20,8 @@ "jquery-ui": "~1.11.4", "DOMPurify": "~0.7.4", "he": "~0.5.0", - "iframe-resizer": "~3.5.3" + "iframe-resizer": "~3.5.3", + "file-saver": "1.3.2" }, "devDependencies": { "handlebars": "4.0.5", diff --git a/web-ui/package.json b/web-ui/package.json index e1c478eed..786a0c89b 100644 --- a/web-ui/package.json +++ b/web-ui/package.json @@ -20,7 +20,8 @@ "karma-coverage": "0.2.7", "minify": "2.0.9", "requirejs": "2.2.0", - "watch": "0.19.1" + "watch": "0.19.1", + "file-saver": "1.3.2" }, "scripts": { "test": "npm run jshint --silent && npm run build --silent && node_modules/karma/bin/karma start --single-run $GRUNT_OPTS", From 36e813fa1de7d9d95e52bfc554ea8917b5629c20 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 19:26:28 +0200 Subject: [PATCH 04/15] clean up and changed download raw function --- web-ui/app/js/services/mail_service.js | 12 +++++++----- web-ui/package.json | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index 2624a0669..df0324ead 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -26,7 +26,7 @@ define( 'page/events', 'features', 'mixins/with_auto_refresh', - 'page/router/url_params' + 'page/router/url_params', ], function (defineComponent, i18n, Mail, monitoredAjax, events, features, withAutoRefresh, urlParams) { 'use strict'; @@ -278,13 +278,15 @@ define( }; this.downloadRaw = function (event, data) { - var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},v=function(t,u,s){if(!s){t=p(t)}var v=this,w=t.type,m=w===d,y,h=function(){l(v,"writestart progress write writeend".split(" "))},S=function(){if((f||m&&a)&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=f?r.result:r.result.replace(/^data:[^;]*;/,"data:attachment/file;");var n=e.open(t,"_blank");if(!n)e.location.href=t;t=undefined;v.readyState=v.DONE;h()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(!y){y=n().createObjectURL(t)}if(m){e.location.href=y}else{var o=e.open(y,"_blank");if(!o){e.location.href=y}}v.readyState=v.DONE;h();c(y)};v.readyState=v.INIT;if(o){y=n().createObjectURL(t);setTimeout(function(){r.href=y;r.download=u;i(r);h();c(y);v.readyState=v.DONE});return}S()},w=v.prototype,m=function(e,t,n){return new v(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=p(e)}return navigator.msSaveOrOpenBlob(e,t)}}w.abort=function(){};w.readyState=w.INIT=0;w.WRITING=1;w.DONE=2;w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})} monitoredAjax(this, '/mail/' + data.mail.ident, {type: 'POST'}) .done(function (data) { - var blob = new Blob([data.textPlainBody], {type: "text/plain;charset=utf-8"}); - saveAs(blob, 'raw.txt'); - return; + var a=document.createElement('a'); + a.href='data:text/plain;base64,'+btoa(data.textPlainBody); + a.textContent='download'; + a.download='raw_mail_content.txt'; + a.click(); + return; }) .fail(this.errorMessage('Failed to download')); } diff --git a/web-ui/package.json b/web-ui/package.json index 786a0c89b..e1c478eed 100644 --- a/web-ui/package.json +++ b/web-ui/package.json @@ -20,8 +20,7 @@ "karma-coverage": "0.2.7", "minify": "2.0.9", "requirejs": "2.2.0", - "watch": "0.19.1", - "file-saver": "1.3.2" + "watch": "0.19.1" }, "scripts": { "test": "npm run jshint --silent && npm run build --silent && node_modules/karma/bin/karma start --single-run $GRUNT_OPTS", From dd24883fa0f9194a8c86e0ed694153f10248d244 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 19:28:27 +0200 Subject: [PATCH 05/15] delete comma --- web-ui/app/js/services/mail_service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index df0324ead..1fc9a047a 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -26,7 +26,7 @@ define( 'page/events', 'features', 'mixins/with_auto_refresh', - 'page/router/url_params', + 'page/router/url_params' ], function (defineComponent, i18n, Mail, monitoredAjax, events, features, withAutoRefresh, urlParams) { 'use strict'; From 3f77fa725268f19b44d9cc373745fcf374b3c0dd Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 19:33:52 +0200 Subject: [PATCH 06/15] clean up --- web-ui/bower.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web-ui/bower.json b/web-ui/bower.json index c68b856fd..4b7bdfb95 100644 --- a/web-ui/bower.json +++ b/web-ui/bower.json @@ -20,8 +20,7 @@ "jquery-ui": "~1.11.4", "DOMPurify": "~0.7.4", "he": "~0.5.0", - "iframe-resizer": "~3.5.3", - "file-saver": "1.3.2" + "iframe-resizer": "~3.5.3" }, "devDependencies": { "handlebars": "4.0.5", From c5e9f8c886bde7af8d4ff56ac5e8a60a23d922d2 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 19:45:29 +0200 Subject: [PATCH 07/15] pr fix --- web-ui/app/scss/views/_compose-view.scss | 2 +- web-ui/app/scss/views/css | 20 -------------------- 2 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 web-ui/app/scss/views/css diff --git a/web-ui/app/scss/views/_compose-view.scss b/web-ui/app/scss/views/_compose-view.scss index b642897c4..bb8120ddf 100644 --- a/web-ui/app/scss/views/_compose-view.scss +++ b/web-ui/app/scss/views/_compose-view.scss @@ -380,7 +380,7 @@ padding-left: 30px; .reply-container { - margin: 22px 0; + margin: 20px 0; padding: 10px; border: 1px dashed darken($contrast, 10%); diff --git a/web-ui/app/scss/views/css b/web-ui/app/scss/views/css deleted file mode 100644 index 02e03dce3..000000000 --- a/web-ui/app/scss/views/css +++ /dev/null @@ -1,20 +0,0 @@ -/* -Errno::ENOENT: No such file or directory @ rb_sysopen - css - -Backtrace: -/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:481:in `read' -/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:481:in `update_stylesheet' -/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:215:in `block in update_stylesheets' -/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:209:in `each' -/usr/lib/ruby/vendor_ruby/sass/plugin/compiler.rb:209:in `update_stylesheets' -/usr/lib/ruby/vendor_ruby/sass/plugin.rb:82:in `update_stylesheets' -/usr/lib/ruby/vendor_ruby/sass/exec/sass_scss.rb:346:in `watch_or_update' -/usr/lib/ruby/vendor_ruby/sass/exec/sass_scss.rb:50:in `process_result' -/usr/lib/ruby/vendor_ruby/sass/exec/base.rb:52:in `parse' -/usr/lib/ruby/vendor_ruby/sass/exec/base.rb:19:in `parse!' -/usr/bin/sass:8:in `
' -*/ -body:before { - white-space: pre; - font-family: monospace; - content: "Errno::ENOENT: No such file or directory @ rb_sysopen - css"; } From d6faf47cb1706684e3b8c6016e911cfb64a060c3 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 19:46:35 +0200 Subject: [PATCH 08/15] pr fix --- web-ui/app/scss/views/_compose-view.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web-ui/app/scss/views/_compose-view.scss b/web-ui/app/scss/views/_compose-view.scss index bb8120ddf..813a60986 100644 --- a/web-ui/app/scss/views/_compose-view.scss +++ b/web-ui/app/scss/views/_compose-view.scss @@ -380,7 +380,7 @@ padding-left: 30px; .reply-container { - margin: 20px 0; + margin: 10px 0; padding: 10px; border: 1px dashed darken($contrast, 10%); From 8a38f14213fab69b841395befd468ef358cdd775 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 20:27:09 +0200 Subject: [PATCH 09/15] pr fix --- web-ui/app/locales/es_ES/translation.json | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/web-ui/app/locales/es_ES/translation.json b/web-ui/app/locales/es_ES/translation.json index 13624332b..3be227023 100644 --- a/web-ui/app/locales/es_ES/translation.json +++ b/web-ui/app/locales/es_ES/translation.json @@ -7,11 +7,15 @@ "your-message-was-archived": "Your message was archived", "delete-single": "Your message was permanently deleted!", "delete-bulk": "Your messages were permanently deleted!", + "draft-saving": "Saving to Drafts...", + "draft-saved": "Draft saved", "recipients-not-valid": "One or more of the recipients are not valid emails", - "failed-change-tags": "Could not update mail tags", + "failed-change-tags": "Could not change mail tags", "invalid-tag-name": "Invalid tag name", "failed-delete-single": "Could not delete email", + "failed-delete-bulk": "Could not delete emails", "failed-fetch-messages": "Could not fetch messages", + "failed-archive": "Could not archive emails", "to": "to", "cc": "CC", "bcc": "BCC", @@ -49,10 +53,14 @@ "delete-permanently": "Delete Permanently", "move-to-inbox": "Move to Inbox", "reply-author-line": "On {{date}}, <{{from}}> wrote:\n", + "refresh": "refresh", + "click-to-remove": "Click to remove", + "no-results-for": "No results for", + "no-emails-in": "No emails in", "error": { - "timeout": "a timeout occurred", - "general": "problems talking to server", - "parse": "got invalid response from server" + "timeout": "A timeout occurred", + "general": "Problems talking to server", + "parse": "Got invalid response from server" }, "tags": { "inbox": "Inbox", From 3a460eaf2dbb0ffb70dabd369bea7b25d6edbc71 Mon Sep 17 00:00:00 2001 From: Eike Niebuhr Date: Sun, 3 Jul 2016 20:49:03 +0200 Subject: [PATCH 10/15] pr fix translation --- web-ui/app/locales/de_DE/translation.json | 18 +++-- web-ui/app/locales/en_US/translation.json | 2 +- web-ui/app/locales/es_ES/translation.json | 2 +- web-ui/app/locales/fr_FR/translation.json | 73 ++++++++++++++++++ web-ui/app/locales/pt_BR/translation.json | 6 +- web-ui/app/locales/sv_SE/translation.json | 94 +++++++++-------------- 6 files changed, 129 insertions(+), 66 deletions(-) create mode 100644 web-ui/app/locales/fr_FR/translation.json diff --git a/web-ui/app/locales/de_DE/translation.json b/web-ui/app/locales/de_DE/translation.json index 13624332b..5d06c82f7 100644 --- a/web-ui/app/locales/de_DE/translation.json +++ b/web-ui/app/locales/de_DE/translation.json @@ -7,11 +7,15 @@ "your-message-was-archived": "Your message was archived", "delete-single": "Your message was permanently deleted!", "delete-bulk": "Your messages were permanently deleted!", + "draft-saving": "Saving to Drafts...", + "draft-saved": "Draft saved", "recipients-not-valid": "One or more of the recipients are not valid emails", - "failed-change-tags": "Could not update mail tags", + "failed-change-tags": "Could not change mail tags", "invalid-tag-name": "Invalid tag name", "failed-delete-single": "Could not delete email", + "failed-delete-bulk": "Could not delete emails", "failed-fetch-messages": "Could not fetch messages", + "failed-archive": "Could not archive emails", "to": "to", "cc": "CC", "bcc": "BCC", @@ -21,10 +25,10 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", - "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", + "get-mail-raw": "Get mail raw", "archive": "Archive", "nothing-selected": "Nothing selected", "add-tag-placeholder": "Press Enter to add tag", @@ -49,10 +53,14 @@ "delete-permanently": "Delete Permanently", "move-to-inbox": "Move to Inbox", "reply-author-line": "On {{date}}, <{{from}}> wrote:\n", + "refresh": "refresh", + "click-to-remove": "Click to remove", + "no-results-for": "No results for", + "no-emails-in": "No emails in", "error": { - "timeout": "a timeout occurred", - "general": "problems talking to server", - "parse": "got invalid response from server" + "timeout": "A timeout occurred", + "general": "Problems talking to server", + "parse": "Got invalid response from server" }, "tags": { "inbox": "Inbox", diff --git a/web-ui/app/locales/en_US/translation.json b/web-ui/app/locales/en_US/translation.json index 3be227023..5d06c82f7 100644 --- a/web-ui/app/locales/en_US/translation.json +++ b/web-ui/app/locales/en_US/translation.json @@ -25,10 +25,10 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", - "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", + "get-mail-raw": "Get mail raw", "archive": "Archive", "nothing-selected": "Nothing selected", "add-tag-placeholder": "Press Enter to add tag", diff --git a/web-ui/app/locales/es_ES/translation.json b/web-ui/app/locales/es_ES/translation.json index 3be227023..5d06c82f7 100644 --- a/web-ui/app/locales/es_ES/translation.json +++ b/web-ui/app/locales/es_ES/translation.json @@ -25,10 +25,10 @@ "reply": "Reply", "reply-to-all": "Reply to all", "delete-this-message": "Delete this message", - "get-mail-raw": "Get mail raw", "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", + "get-mail-raw": "Get mail raw", "archive": "Archive", "nothing-selected": "Nothing selected", "add-tag-placeholder": "Press Enter to add tag", diff --git a/web-ui/app/locales/fr_FR/translation.json b/web-ui/app/locales/fr_FR/translation.json new file mode 100644 index 000000000..5d06c82f7 --- /dev/null +++ b/web-ui/app/locales/fr_FR/translation.json @@ -0,0 +1,73 @@ +{ + "compose": "Compose", + "re": "Re", + "fwd": "Fwd", + "trash-single": "Your message was moved to trash!", + "trash-bulk": "Your messages were moved to trash!", + "your-message-was-archived": "Your message was archived", + "delete-single": "Your message was permanently deleted!", + "delete-bulk": "Your messages were permanently deleted!", + "draft-saving": "Saving to Drafts...", + "draft-saved": "Draft saved", + "recipients-not-valid": "One or more of the recipients are not valid emails", + "failed-change-tags": "Could not change mail tags", + "invalid-tag-name": "Invalid tag name", + "failed-delete-single": "Could not delete email", + "failed-delete-bulk": "Could not delete emails", + "failed-fetch-messages": "Could not fetch messages", + "failed-archive": "Could not archive emails", + "to": "to", + "cc": "CC", + "bcc": "BCC", + "body": "Body", + "subject": "Subject", + "send": "Send", + "reply": "Reply", + "reply-to-all": "Reply to all", + "delete-this-message": "Delete this message", + "mark-as-read": "Mark as read", + "mark-as-unread": "Mark as unread", + "delete": "Delete", + "get-mail-raw": "Get mail raw", + "archive": "Archive", + "nothing-selected": "Nothing selected", + "add-tag-placeholder": "Press Enter to add tag", + "no-subject": "", + "no-recipient": "", + "you": "you", + "encrypted": "Encrypted", + "not-encrypted": "Not encrypted", + "signed": "Verified sender", + "not-signed": "Not signed", + "sending-mail": "Sending...", + "trash-button": "Delete it", + "search-placeholder" : "Search...", + "search-results-for": "Search results for", + "forward": "Forward", + "feedback-placeholder": "Tell us what you liked, didn't like, what is missing and generally what you think about Pixelated.", + "user-settings": "User Settings", + "email-address": "Email address", + "public-key-fingerprint": "Public key fingerprint", + "version": "version", + "logout": "Logout", + "delete-permanently": "Delete Permanently", + "move-to-inbox": "Move to Inbox", + "reply-author-line": "On {{date}}, <{{from}}> wrote:\n", + "refresh": "refresh", + "click-to-remove": "Click to remove", + "no-results-for": "No results for", + "no-emails-in": "No emails in", + "error": { + "timeout": "A timeout occurred", + "general": "Problems talking to server", + "parse": "Got invalid response from server" + }, + "tags": { + "inbox": "Inbox", + "sent": "Sent", + "drafts": "Drafts", + "trash": "Trash", + "all": "All", + "tags": "Tags" + } +} diff --git a/web-ui/app/locales/pt_BR/translation.json b/web-ui/app/locales/pt_BR/translation.json index cfda01240..19c0e2b40 100644 --- a/web-ui/app/locales/pt_BR/translation.json +++ b/web-ui/app/locales/pt_BR/translation.json @@ -13,7 +13,9 @@ "failed-change-tags": "Não pode atualizar as tags do email", "invalid-tag-name": "Nome inválido para tag", "failed-delete-single": "Não pode deletar o email", + "failed-delete-bulk": "Não foi possível remover os emails", "failed-fetch-messages": "Não pode receber as mensagens", + "failed-archive": "Não foi possível arquivar os emails", "to": "para", "cc": "CC", "bcc": "CCO", @@ -23,10 +25,10 @@ "reply": "Responder", "reply-to-all": "Responder para todos", "delete-this-message": "Deletar essa mensagem", - "get-mail-raw": "Get mail raw", "mark-as-read": "Marcar como lida", "mark-as-unread": "Marcar como não lida", "delete": "Deletar", + "get-mail-raw": "Get mail raw", "archive": "Arquivar", "nothing-selected": "Nada selecionado", "add-tag-placeholder": "Aperte enter para adicionar a tag", @@ -53,6 +55,8 @@ "reply-author-line": "Em {{date}}, <{{from}}> escreveu:\n", "refresh": "atualizar", "click-to-remove": "Pressione para remover", + "no-results-for": "Sem resultados para", + "no-emails-in": "Nenhum email em", "error": { "timeout": "A operação excedeu o limite de tempo", "general": "Problemas ao se comunicar com o servidor", diff --git a/web-ui/app/locales/sv_SE/translation.json b/web-ui/app/locales/sv_SE/translation.json index 13624332b..39e3731fb 100644 --- a/web-ui/app/locales/sv_SE/translation.json +++ b/web-ui/app/locales/sv_SE/translation.json @@ -1,65 +1,43 @@ { - "compose": "Compose", - "re": "Re", - "fwd": "Fwd", - "trash-single": "Your message was moved to trash!", - "trash-bulk": "Your messages were moved to trash!", - "your-message-was-archived": "Your message was archived", - "delete-single": "Your message was permanently deleted!", - "delete-bulk": "Your messages were permanently deleted!", - "recipients-not-valid": "One or more of the recipients are not valid emails", - "failed-change-tags": "Could not update mail tags", - "invalid-tag-name": "Invalid tag name", - "failed-delete-single": "Could not delete email", - "failed-fetch-messages": "Could not fetch messages", - "to": "to", + "compose": "Skriv nytt", + "re": "Sv", + "fwd": "VB", + "trash-single": "Ditt meddelande har flyttats till papperskorgen!", + "trash-bulk": "Ditt meddelande har arkiverats!", + "recipients-not-valid": "En eller flera mottagare är inte giltiga epost-adresser", + "failed-change-tags": "Kan inte ändra taggar", + "invalid-tag-name": "Ogiltigt taggnamn", + "failed-delete-single": "Kan inte ta bort meddelande", + "failed-fetch-messages": "Kan inte hämta meddelanden", + "to": "till", "cc": "CC", "bcc": "BCC", - "body": "Body", - "subject": "Subject", - "send": "Send", - "reply": "Reply", - "reply-to-all": "Reply to all", - "delete-this-message": "Delete this message", + "body": "Innehåll", + "subject": "Titel", + "send": "Skicka", + "reply": "Svara", + "reply-to-all": "Svara Alla", + "mark-as-read": "Markera som läst", + "delete": "Ta bort", "get-mail-raw": "Get mail raw", - "mark-as-read": "Mark as read", - "mark-as-unread": "Mark as unread", - "delete": "Delete", - "archive": "Archive", - "nothing-selected": "Nothing selected", - "add-tag-placeholder": "Press Enter to add tag", - "no-subject": "", - "no-recipient": "", - "you": "you", - "encrypted": "Encrypted", - "not-encrypted": "Not encrypted", - "signed": "Verified sender", - "not-signed": "Not signed", - "sending-mail": "Sending...", - "trash-button": "Delete it", - "search-placeholder" : "Search...", - "search-results-for": "Search results for", - "forward": "Forward", - "feedback-placeholder": "Tell us what you liked, didn't like, what is missing and generally what you think about Pixelated.", - "user-settings": "User Settings", - "email-address": "Email address", - "public-key-fingerprint": "Public key fingerprint", - "version": "version", - "logout": "Logout", - "delete-permanently": "Delete Permanently", - "move-to-inbox": "Move to Inbox", - "reply-author-line": "On {{date}}, <{{from}}> wrote:\n", - "error": { - "timeout": "a timeout occurred", - "general": "problems talking to server", - "parse": "got invalid response from server" - }, + "archive": "Arkivera", + "nothing-selected": "INGET VALT", + "add-tag-placeholder": "Tryck retur för att skapa", + "no-subject": "", + "no-recipient": "", + "you": "du", + "encrypted": "krypterad", + "not-encrypted": "Meddelandet var läsbart medans det var på väg.", + "signed": "Certifierad avsändare.", + "not-signed": "Avsändaren kunde inte säkert identifieras.", + "search-placeholder" : "Sök...", + "search-results-for": "Sökresultat för", + "forward": "Vidarebefodra", "tags": { - "inbox": "Inbox", - "sent": "Sent", - "drafts": "Drafts", - "trash": "Trash", - "all": "All", - "tags": "Tags" + "inbox": "Inlåda", + "sent": "Skickat", + "drafts": "Utkast", + "trash": "Skräp", + "all": "Alla" } } From 768bc6e9ebee44a1bef83a413162d78f6983e4b8 Mon Sep 17 00:00:00 2001 From: Kruno Date: Mon, 4 Jul 2016 13:10:34 +0200 Subject: [PATCH 11/15] Update mail_service.js --- web-ui/app/js/services/mail_service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index 1fc9a047a..d8ceb8eb5 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -289,7 +289,7 @@ define( return; }) .fail(this.errorMessage('Failed to download')); - } + }; this.nextPage = function () { if (this.attr.currentPage < (this.attr.numPages)) { From 77210bec2ff454696fc91e7796fbb5e0aef9d696 Mon Sep 17 00:00:00 2001 From: KrunoKnego Date: Fri, 8 Jul 2016 00:52:37 +0200 Subject: [PATCH 12/15] Renamed to get raw mail --- service/pixelated/adapter/mailstore/leap_mailstore.py | 2 +- service/pixelated/adapter/mailstore/mailstore.py | 2 +- service/pixelated/adapter/services/mail_service.py | 4 ++-- service/pixelated/resources/mail_resource.py | 11 +---------- web-ui/app/js/mail_view/ui/mail_actions.js | 4 ++-- web-ui/app/locales/en_US/translation.json | 2 +- web-ui/app/locales/pt_BR/translation.json | 2 +- web-ui/app/templates/mails/mail_actions.hbs | 2 +- 8 files changed, 10 insertions(+), 19 deletions(-) diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index b4fb76308..b9c764a1f 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -205,7 +205,7 @@ def get_mail(self, mail_id, include_body=False): defer.returnValue(leap_mail) @defer.inlineCallbacks - def get_mail_raw(self, mail_id, include_body=False): + def get_raw_mail(self, mail_id, include_body=False): message = yield self._fetch_msg_from_soledad(mail_id) if not _is_empty_message(message): leap_mail = yield self._leap_message_to_leap_mail(mail_id, message, include_body) diff --git a/service/pixelated/adapter/mailstore/mailstore.py b/service/pixelated/adapter/mailstore/mailstore.py index 8f5f32290..92b30ba15 100644 --- a/service/pixelated/adapter/mailstore/mailstore.py +++ b/service/pixelated/adapter/mailstore/mailstore.py @@ -19,7 +19,7 @@ class MailStore(object): def get_mail(self, mail_id): pass - def get_mail_raw(self, mail_id): + def get_raw_mail(self, mail_id): pass def get_mail_attachment(self, attachment_id): diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 68b0e6046..06a2c85eb 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -83,8 +83,8 @@ def _use_current_casing(new_tag_lower): def mail(self, mail_id): return self.mail_store.get_mail(mail_id, include_body=True) - def mail_raw(self, mail_id): - return self.mail_store.get_mail_raw(mail_id, include_body=True) + def raw_mail(self, mail_id): + return self.mail_store.get_raw_mail(mail_id, include_body=True) def attachment(self, attachment_id): return self.attachment_store.get_mail_attachment(attachment_id) diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py index 6f2840df3..61a38798e 100644 --- a/service/pixelated/resources/mail_resource.py +++ b/service/pixelated/resources/mail_resource.py @@ -56,15 +56,6 @@ def populate_reply(mail): return NOT_DONE_YET def render_POST(self, request): - # return 'HELLO' - - # d = self._mail_service.mail_raw(self._mail_id) - # print("*" * 1000) - # print(d) - # print("*" * 1000) - # - # return d - def populate_reply(mail): mail_dict = mail.as_dict() current_user = self._mail_service.account_email @@ -74,7 +65,7 @@ def populate_reply(mail): mail_dict['replying'] = replier.generate_recipients(sender, to, ccs, current_user) return mail_dict - d = self._mail_service.mail_raw(self._mail_id) + d = self._mail_service.raw_mail(self._mail_id) d.addCallback(lambda mail: populate_reply(mail)) d.addCallback(lambda mail_dict: respond_json_deferred(mail_dict, request)) d.addErrback(handle_error_deferred, request) diff --git a/web-ui/app/js/mail_view/ui/mail_actions.js b/web-ui/app/js/mail_view/ui/mail_actions.js index ce39331b9..a661e0dc0 100644 --- a/web-ui/app/js/mail_view/ui/mail_actions.js +++ b/web-ui/app/js/mail_view/ui/mail_actions.js @@ -33,7 +33,7 @@ define( replyButtonTop: '#reply-button-top', viewMoreActions: '#view-more-actions', replyAllButtonTop: '#reply-all-button-top', - getMailRaw: '#get-mail-raw', + getMailRaw: '#get-raw-mail', deleteButtonTop: '#delete-button-top', moreActions: '#more-actions' }); @@ -54,7 +54,7 @@ define( this.select('moreActions').hide(); }.bind(this)); - this.on(this.select('getMailRaw'), 'click', function () { + this.on(this.select('getRawMail'), 'click', function () { this.trigger(document, events.ui.mail.downloadRaw, {mail: this.attr.mail}); this.select('moreActions').hide(); }.bind(this)); diff --git a/web-ui/app/locales/en_US/translation.json b/web-ui/app/locales/en_US/translation.json index dd865d175..c61219ca4 100644 --- a/web-ui/app/locales/en_US/translation.json +++ b/web-ui/app/locales/en_US/translation.json @@ -28,7 +28,7 @@ "mark-as-read": "Mark as read", "mark-as-unread": "Mark as unread", "delete": "Delete", - "get-mail-raw": "Get mail raw", + "get-raw-mail": "Get raw mail", "archive": "Archive", "nothing-selected": "Nothing selected", "add-tag-placeholder": "Press Enter to add tag", diff --git a/web-ui/app/locales/pt_BR/translation.json b/web-ui/app/locales/pt_BR/translation.json index 9b8ae4d23..a9489d8ea 100644 --- a/web-ui/app/locales/pt_BR/translation.json +++ b/web-ui/app/locales/pt_BR/translation.json @@ -28,7 +28,7 @@ "mark-as-read": "Marcar como lida", "mark-as-unread": "Marcar como não lida", "delete": "Deletar", - "get-mail-raw": "Get mail raw", + "get-raw-mail": "Get raw mail", "archive": "Arquivar", "nothing-selected": "Nada selecionado", "add-tag-placeholder": "Aperte enter para adicionar a tag", diff --git a/web-ui/app/templates/mails/mail_actions.hbs b/web-ui/app/templates/mails/mail_actions.hbs index 7c6b5a211..0245ac363 100644 --- a/web-ui/app/templates/mails/mail_actions.hbs +++ b/web-ui/app/templates/mails/mail_actions.hbs @@ -2,6 +2,6 @@
  • {{t 'reply-to-all'}}
  • -
  • {{t 'get-mail-raw'}}
  • +
  • {{t 'get-raw-mail'}}
  • {{t 'delete-this-message'}}
From 69a1387cd7d4d1ccc7fe2739fdd6bf84e56b61e0 Mon Sep 17 00:00:00 2001 From: KrunoKnego Date: Fri, 8 Jul 2016 00:53:59 +0200 Subject: [PATCH 13/15] Renamed to get raw mail --- web-ui/app/js/mail_view/ui/mail_actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web-ui/app/js/mail_view/ui/mail_actions.js b/web-ui/app/js/mail_view/ui/mail_actions.js index a661e0dc0..3e9f71c07 100644 --- a/web-ui/app/js/mail_view/ui/mail_actions.js +++ b/web-ui/app/js/mail_view/ui/mail_actions.js @@ -33,7 +33,7 @@ define( replyButtonTop: '#reply-button-top', viewMoreActions: '#view-more-actions', replyAllButtonTop: '#reply-all-button-top', - getMailRaw: '#get-raw-mail', + getRawMail: '#get-raw-mail', deleteButtonTop: '#delete-button-top', moreActions: '#more-actions' }); @@ -70,7 +70,7 @@ define( this.on(this.select('viewMoreActions'), 'blur', function (event) { var replyButtonTopHover = this.select('replyAllButtonTop').is(':hover'); - var downloadRaw = this.select('getMailRaw').is(':hover'); + var downloadRaw = this.select('getRawMail').is(':hover'); var deleteButtonTopHover = this.select('deleteButtonTop').is(':hover'); if (replyButtonTopHover || deleteButtonTopHover || downloadRaw) { From 821c2717f8a3934f62d83d2f0f2430c51350009d Mon Sep 17 00:00:00 2001 From: KrunoKnego Date: Fri, 13 Jan 2017 00:55:26 +0100 Subject: [PATCH 14/15] Ran autopep8 --- service/README.md | 2 +- .../listeners/mailbox_indexer_listener.py | 6 +- .../adapter/mailstore/body_parser.py | 3 +- .../mailstore/leap_attachment_store.py | 6 +- .../adapter/mailstore/leap_mailstore.py | 50 +++-- .../pixelated/adapter/mailstore/mailstore.py | 1 + .../adapter/mailstore/maintenance/__init__.py | 10 +- .../adapter/mailstore/searchable_mailstore.py | 3 +- service/pixelated/adapter/model/mail.py | 23 +- service/pixelated/adapter/model/status.py | 3 +- service/pixelated/adapter/search/__init__.py | 27 ++- .../pixelated/adapter/services/mail_sender.py | 7 +- .../adapter/services/mail_service.py | 12 +- .../pixelated/adapter/services/tag_service.py | 3 +- service/pixelated/application.py | 28 ++- service/pixelated/authentication.py | 11 +- .../pixelated/bitmask_libraries/keymanager.py | 3 +- .../pixelated/bitmask_libraries/provider.py | 22 +- service/pixelated/config/arguments.py | 66 ++++-- service/pixelated/config/credentials.py | 3 +- service/pixelated/config/leap.py | 9 +- service/pixelated/config/logger.py | 9 +- service/pixelated/config/services.py | 13 +- service/pixelated/config/sessions.py | 27 ++- service/pixelated/config/site.py | 3 +- .../extensions/esmtp_sender_factory.py | 3 +- .../pixelated/extensions/requests_urllib3.py | 6 +- service/pixelated/maintenance.py | 6 +- service/pixelated/register.py | 3 +- service/pixelated/resources/__init__.py | 8 +- .../resources/attachments_resource.py | 12 +- service/pixelated/resources/auth.py | 1 + .../pixelated/resources/features_resource.py | 6 +- service/pixelated/resources/login_resource.py | 24 +- service/pixelated/resources/mail_resource.py | 15 +- service/pixelated/resources/mails_resource.py | 43 ++-- service/pixelated/resources/root_resource.py | 44 ++-- service/pixelated/resources/tags_resource.py | 3 +- .../resources/user_settings_resource.py | 6 +- service/pixelated/support/__init__.py | 15 +- service/pixelated/support/clock.py | 3 +- .../support/encrypted_file_storage.py | 12 +- service/pixelated/support/loglinegenerator.py | 1 + service/pixelated/support/mail_generator.py | 9 +- service/pixelated/support/replier.py | 9 +- .../test/functional/features/environment.py | 3 +- .../functional/features/steps/attachments.py | 24 +- .../test/functional/features/steps/common.py | 19 +- .../test/functional/features/steps/compose.py | 6 +- .../functional/features/steps/data_setup.py | 6 +- .../functional/features/steps/mail_list.py | 9 +- .../functional/features/steps/mail_view.py | 15 +- .../test/functional/features/steps/search.py | 3 +- .../functional/features/steps/tag_list.py | 3 +- service/test/integration/test_contacts.py | 6 +- .../test/integration/test_draft_service.py | 3 +- service/test/integration/test_drafts.py | 15 +- .../test/integration/test_feedback_service.py | 6 +- .../test/integration/test_leap_mailstore.py | 42 ++-- .../integration/test_mark_as_read_unread.py | 6 +- .../test/integration/test_multi_user_login.py | 9 +- .../integration/test_retrieve_attachment.py | 22 +- service/test/integration/test_tags.py | 6 +- service/test/integration/test_users_count.py | 3 +- service/test/perf/contacts/test_Contacts.py | 9 +- service/test/perf/mails/test_Mails.py | 3 +- service/test/perf/search/test_Search.py | 15 +- service/test/perf/tags/test_Tags.py | 3 +- service/test/reactor/create_users.py | 10 +- .../support/integration/app_test_client.py | 43 ++-- service/test/support/integration/model.py | 2 + .../support/integration/multi_user_client.py | 30 ++- service/test/support/integration/util.py | 3 +- service/test/support/mockito/__init__.py | 1 + service/test/support/test_helper.py | 8 +- .../maintenance/test_soledad_maintenance.py | 36 ++- .../adapter/mailstore/test_body_parser.py | 24 +- .../mailstore/test_leap_attachment_store.py | 40 ++-- .../unit/adapter/mailstore/test_leap_mail.py | 43 ++-- .../adapter/mailstore/test_leap_mailstore.py | 208 ++++++++++++------ .../mailstore/test_searchable_mailstore.py | 24 +- .../adapter/search/test_index_storage_key.py | 6 +- .../test/unit/adapter/search/test_search.py | 11 +- .../unit/adapter/services/test_mail_sender.py | 39 ++-- service/test/unit/adapter/test_contacts.py | 6 +- .../test/unit/adapter/test_draft_service.py | 9 +- service/test/unit/adapter/test_mail.py | 56 +++-- .../test/unit/adapter/test_mail_service.py | 52 +++-- .../adapter/test_mailbox_indexer_listener.py | 28 ++- service/test/unit/adapter/test_status.py | 3 +- .../bitmask_libraries/test_abstract_leap.py | 3 +- .../unit/bitmask_libraries/test_keymanager.py | 39 ++-- .../unit/bitmask_libraries/test_provider.py | 25 ++- .../test_smtp_client_certificate.py | 15 +- service/test/unit/config/test_leap.py | 27 ++- service/test/unit/config/test_register.py | 15 +- service/test/unit/config/test_sessions.py | 6 +- service/test/unit/config/test_site.py | 29 ++- .../test/unit/maintenance/test_commands.py | 21 +- service/test/unit/resources/__init__.py | 1 + .../unit/resources/test_archive_resource.py | 7 +- .../resources/test_attachments_resource.py | 21 +- .../unit/resources/test_feedback_resource.py | 7 +- .../unit/resources/test_keys_resources.py | 12 +- .../unit/resources/test_login_resource.py | 24 +- .../unit/resources/test_logout_resources.py | 4 +- .../unit/resources/test_mails_resource.py | 22 +- .../test/unit/resources/test_root_resource.py | 9 +- .../unit/resources/test_sandbox_resource.py | 10 +- .../test/unit/resources/test_tags_resource.py | 1 + .../resources/test_user_settings_resource.py | 9 +- .../unit/resources/test_users_resource.py | 6 +- .../test/unit/support/mail_generator_test.py | 3 +- service/test/unit/support/test_markov.py | 12 +- service/test/unit/support/test_replier.py | 13 +- service/test/unit/test_application.py | 58 +++-- service/test/unit/test_authentication.py | 22 +- 117 files changed, 1294 insertions(+), 624 deletions(-) diff --git a/service/README.md b/service/README.md index da1b334f1..110024acd 100644 --- a/service/README.md +++ b/service/README.md @@ -1,4 +1,4 @@ -Pixelated User Agent Service +ixelated User Agent Service ============================ This is the service for the Pixelated User Agent. The primary purpose of this is to provide an interface for the user agent to communicate with the Pixelated Provider. diff --git a/service/pixelated/adapter/listeners/mailbox_indexer_listener.py b/service/pixelated/adapter/listeners/mailbox_indexer_listener.py index 9cc522cfd..a660062b3 100644 --- a/service/pixelated/adapter/listeners/mailbox_indexer_listener.py +++ b/service/pixelated/adapter/listeners/mailbox_indexer_listener.py @@ -25,7 +25,8 @@ class MailboxIndexerListener(object): @classmethod @defer.inlineCallbacks def listen(cls, account, mailbox_name, mail_store, search_engine): - listener = MailboxIndexerListener(mailbox_name, mail_store, search_engine) + listener = MailboxIndexerListener( + mailbox_name, mail_store, search_engine) mail_collection = yield account.get_collection_by_mailbox(mailbox_name) mail_collection.addListener(listener) @@ -39,7 +40,8 @@ def __init__(self, mailbox_name, mail_store, search_engine): @defer.inlineCallbacks def notify_new(self): try: - indexed_idents = set(self.search_engine.search('tag:' + self.mailbox_name.lower(), all_mails=True)) + indexed_idents = set(self.search_engine.search( + 'tag:' + self.mailbox_name.lower(), all_mails=True)) soledad_idents = yield self.mail_store.get_mailbox_mail_ids(self.mailbox_name) soledad_idents = set(soledad_idents) diff --git a/service/pixelated/adapter/mailstore/body_parser.py b/service/pixelated/adapter/mailstore/body_parser.py index 8cac75cf1..c40a744f8 100644 --- a/service/pixelated/adapter/mailstore/body_parser.py +++ b/service/pixelated/adapter/mailstore/body_parser.py @@ -63,7 +63,8 @@ def _serialize_for_parser(self, charset): try: return encoded_text + self._content.encode(charset) except UnicodeError, e: - logger.warn('Failed to encode content for charset %s. Ignoring invalid chars: %s' % (charset, e)) + logger.warn( + 'Failed to encode content for charset %s. Ignoring invalid chars: %s' % (charset, e)) return encoded_text + self._content.encode(charset, 'ignore') else: return encoded_text + self._content diff --git a/service/pixelated/adapter/mailstore/leap_attachment_store.py b/service/pixelated/adapter/mailstore/leap_attachment_store.py index c95b776a2..7f9025b16 100644 --- a/service/pixelated/adapter/mailstore/leap_attachment_store.py +++ b/service/pixelated/adapter/mailstore/leap_attachment_store.py @@ -50,12 +50,14 @@ def _attachment_to_cdoc(self, content, content_type, encoder=encoders.encode_bas attachment = MIMENonMultipart(major, sub) attachment.set_payload(content) encoder(attachment) - attachment.add_header('Content-Disposition', 'attachment', filename='does_not_matter.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='does_not_matter.txt') pseudo_mail = MIMEMultipart() pseudo_mail.attach(attachment) - tmp_mail = SoledadMailAdaptor().get_msg_from_string(MessageClass=Message, raw_msg=pseudo_mail.as_string()) + tmp_mail = SoledadMailAdaptor().get_msg_from_string( + MessageClass=Message, raw_msg=pseudo_mail.as_string()) cdoc = tmp_mail.get_wrapper().cdocs[1] return cdoc diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 97cfdb04f..1a4887198 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -30,6 +30,7 @@ class AttachmentInfo(object): + def __init__(self, ident, name, encoding=None, ctype='application/octet-stream', size=0): self.ident = ident self.name = name @@ -62,7 +63,8 @@ def __init__(self, mail_id, mailbox_name, headers=None, tags=set(), flags=set(), def headers(self): cpy = dict(self._headers) for name in set(self._headers.keys()).intersection(['To', 'Cc', 'Bcc']): - cpy[name] = [address.strip() for address in (self._headers[name].split(',') if self._headers[name] else [])] + cpy[name] = [address.strip() for address in ( + self._headers[name].split(',') if self._headers[name] else [])] return cpy @@ -133,8 +135,11 @@ def encode_chunk(content, encoding): return unicode(content.strip(), encoding=encoding or 'ascii', errors='ignore') try: - encoded_chunks = [encode_chunk(content, encoding) for content, encoding in decode_header(header_value)] - return ' '.join(encoded_chunks) # decode_header strips whitespaces on all chunks, joining over ' ' is only a workaround, not a proper fix + encoded_chunks = [encode_chunk( + content, encoding) for content, encoding in decode_header(header_value)] + # decode_header strips whitespaces on all chunks, joining over + # ' ' is only a workaround, not a proper fix + return ' '.join(encoded_chunks) except UnicodeEncodeError: return unicode(header_value.encode('ascii', errors='ignore')) @@ -154,19 +159,22 @@ def as_dict(self): @staticmethod def from_dict(mail_dict): # TODO: implement this method and also write tests for it - headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} + headers = {key.capitalize(): value for key, + value in mail_dict.get('header', {}).items()} headers['Date'] = date.mail_date_now() body = mail_dict.get('body', '') tags = set(mail_dict.get('tags', [])) status = set(mail_dict.get('status', [])) attachments = [] - # mail_id, mailbox_name, headers=None, tags=set(), flags=set(), body=None, attachments=[] + # mail_id, mailbox_name, headers=None, tags=set(), flags=set(), + # body=None, attachments=[] return LeapMail(None, None, headers, tags, set(), body, attachments) def _extract_filename(headers, default_filename='UNNAMED'): - content_disposition = headers.get('Content-Disposition') or headers.get('content-disposition', '') + content_disposition = headers.get( + 'Content-Disposition') or headers.get('content-disposition', '') filename = _extract_filename_from_name_header_part(content_disposition) if not filename: filename = headers.get('Content-Description', '') @@ -222,7 +230,8 @@ def get_mails(self, mail_ids, gracefully_ignore_errors=False, include_body=False if gracefully_ignore_errors: results = yield DeferredList(deferreds, consumeErrors=True) - defer.returnValue([mail for ok, mail in results if ok and mail is not None]) + defer.returnValue( + [mail for ok, mail in results if ok and mail is not None]) else: result = yield defer.gatherResults(deferreds, consumeErrors=True) defer.returnValue(result) @@ -232,7 +241,8 @@ def update_mail(self, mail): message = yield self._fetch_msg_from_soledad(mail.mail_id) message.get_wrapper().set_tags(tuple(mail.tags)) message.get_wrapper().set_flags(tuple(mail.flags)) - yield self._update_mail(message) # TODO assert this is yielded (otherwise asynchronous) + # TODO assert this is yielded (otherwise asynchronous) + yield self._update_mail(message) @defer.inlineCallbacks def all_mails(self, gracefully_ignore_errors=False): @@ -259,7 +269,8 @@ def _mailbox_uuid_to_name_map(self): map = {} mbox_docs = yield self.soledad.get_from_index('by-type', 'mbox') for doc in mbox_docs: - map[underscore_uuid(doc.content.get('uuid'))] = doc.content.get('mbox') + map[underscore_uuid(doc.content.get('uuid')) + ] = doc.content.get('mbox') defer.returnValue(map) @@ -272,7 +283,8 @@ def add_mail(self, mailbox_name, raw_msg): yield SoledadMailAdaptor().create_msg(self.soledad, message) # add behavious from insert_mdoc_id from mail.py - mail = yield self._leap_message_to_leap_mail(message.get_wrapper().mdoc.doc_id, message, include_body=True) # TODO test that asserts include_body + # TODO test that asserts include_body + mail = yield self._leap_message_to_leap_mail(message.get_wrapper().mdoc.doc_id, message, include_body=True) defer.returnValue(mail) @defer.inlineCallbacks @@ -330,15 +342,18 @@ def _leap_message_to_leap_mail(self, mail_id, message, include_body): mbox_uuid = message.get_wrapper().fdoc.mbox_uuid mbox_name = yield self._mailbox_name_from_uuid(mbox_uuid) attachments = self._extract_attachment_info_from(message) - mail = LeapMail(mail_id, mbox_name, message.get_wrapper().hdoc.headers, set(message.get_tags()), set(message.get_flags()), body=body, attachments=attachments) # TODO assert flags are passed on + mail = LeapMail(mail_id, mbox_name, message.get_wrapper().hdoc.headers, set(message.get_tags()), set( + message.get_flags()), body=body, attachments=attachments) # TODO assert flags are passed on defer.returnValue(mail) @defer.inlineCallbacks def _raw_message_body(self, message): content_doc = (yield message.get_wrapper().get_body(self.soledad)) - parser = BodyParser('', content_type='text/plain', content_transfer_encoding='UTF-8') - # It fix the problem when leap doesn'r found body_phash and returns empty string + parser = BodyParser('', content_type='text/plain', + content_transfer_encoding='UTF-8') + # It fix the problem when leap doesn'r found body_phash and returns + # empty string if not isinstance(content_doc, str): parser = BodyParser(content_doc.raw, content_type=content_doc.content_type, content_transfer_encoding=content_doc.content_transfer_encoding, charset=content_doc.charset) @@ -374,7 +389,8 @@ def _extract_attachment_info_from(self, message): return self._extract_part_map(part_maps) def _is_attachment(self, part_map, headers): - disposition = headers.get('Content-Disposition') or headers.get('content-disposition') + disposition = headers.get( + 'Content-Disposition') or headers.get('content-disposition') content_type = part_map['ctype'] if 'multipart' in content_type: @@ -399,9 +415,11 @@ def _extract_part_map(self, part_maps): for nr, part_map in part_maps.items(): if 'headers' in part_map and 'phash' in part_map: - headers = {header[0]: header[1] for header in part_map['headers']} + headers = {header[0]: header[1] + for header in part_map['headers']} if self._is_attachment(part_map, headers): - result.append(self._create_attachment_info_from(part_map, headers)) + result.append( + self._create_attachment_info_from(part_map, headers)) if 'part_map' in part_map: result += self._extract_part_map(part_map['part_map']) diff --git a/service/pixelated/adapter/mailstore/mailstore.py b/service/pixelated/adapter/mailstore/mailstore.py index 92b30ba15..ff66755bf 100644 --- a/service/pixelated/adapter/mailstore/mailstore.py +++ b/service/pixelated/adapter/mailstore/mailstore.py @@ -16,6 +16,7 @@ class MailStore(object): + def get_mail(self, mail_id): pass diff --git a/service/pixelated/adapter/mailstore/maintenance/__init__.py b/service/pixelated/adapter/mailstore/maintenance/__init__.py index 9a1007cc6..9316f7968 100644 --- a/service/pixelated/adapter/mailstore/maintenance/__init__.py +++ b/service/pixelated/adapter/mailstore/maintenance/__init__.py @@ -53,6 +53,7 @@ def _address(doc): class SoledadMaintenance(object): + def __init__(self, soledad): self._soledad = soledad @@ -60,11 +61,13 @@ def __init__(self, soledad): def repair(self): _, docs = yield self._soledad.get_all_docs() - private_key_fingerprints = self._key_fingerprints_with_private_key(docs) + private_key_fingerprints = self._key_fingerprints_with_private_key( + docs) for doc in docs: if _is_key_doc(doc) and _key_fingerprint(doc) not in private_key_fingerprints: - logger.warn('Deleting doc %s for key %s of <%s>' % (doc.doc_id, _key_fingerprint(doc), _address(doc))) + logger.warn('Deleting doc %s for key %s of <%s>' % + (doc.doc_id, _key_fingerprint(doc), _address(doc))) yield self._soledad.delete_doc(doc) yield self._repair_missing_active_docs(docs, private_key_fingerprints) @@ -75,7 +78,8 @@ def _repair_missing_active_docs(self, docs, private_key_fingerprints): for fingerprint in missing: emails = self._emails_for_key_fingerprint(docs, fingerprint) for email in emails: - logger.warn('Re-creating active doc for key %s, email %s' % (fingerprint, email)) + logger.warn('Re-creating active doc for key %s, email %s' % + (fingerprint, email)) yield self._soledad.create_doc_from_json(OpenPGPKey(email, fingerprint=fingerprint, private=False).get_active_json()) def _key_fingerprints_with_private_key(self, docs): diff --git a/service/pixelated/adapter/mailstore/searchable_mailstore.py b/service/pixelated/adapter/mailstore/searchable_mailstore.py index 07e99ba7a..e578e6a70 100644 --- a/service/pixelated/adapter/mailstore/searchable_mailstore.py +++ b/service/pixelated/adapter/mailstore/searchable_mailstore.py @@ -72,7 +72,8 @@ def __getattr__(self, name): :param name: attribute name :return: method or attribute """ - methods = ([key for key, value in MailStore.__dict__.items() if type(value) == FunctionType]) + methods = ([key for key, value in MailStore.__dict__.items() + if type(value) == FunctionType]) if name in methods: SearchableMailStore._create_delegator(name) diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index b6a8beb02..78ac0f2c7 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -33,6 +33,7 @@ class Mail(object): + @property def from_sender(self): return self.headers['From'] @@ -71,7 +72,8 @@ def mailbox_name(self): return self.fdoc.content.get('mbox', 'INBOX') def _encode_header_value_list(self, header_value_list): - encoded_header_list = [self._encode_header_value(v) for v in header_value_list] + encoded_header_list = [self._encode_header_value( + v) for v in header_value_list] return ', '.join(encoded_header_list) def _encode_header_value(self, header_value): @@ -83,12 +85,15 @@ def _add_message_content(self, mime_multipart, body_to_use=None): body_to_use = body_to_use or self.body if isinstance(body_to_use, list): for part in body_to_use: - mime_multipart.attach(MIMEText(part['raw'], part['content-type'])) + mime_multipart.attach( + MIMEText(part['raw'], part['content-type'])) else: - mime_multipart.attach(MIMEText(body_to_use, 'plain', self._charset())) + mime_multipart.attach( + MIMEText(body_to_use, 'plain', self._charset())) def _add_body(self, mime): - body_to_use = getattr(self, 'body', None) or getattr(self, 'text_plain_body', None) + body_to_use = getattr(self, 'body', None) or getattr( + self, 'text_plain_body', None) self._add_message_content(mime, body_to_use) self._add_attachments(mime) @@ -116,7 +121,8 @@ def _add_attachments(self, mime): attachment_mime = MIMENonMultipart(major, sub) base64_attachment_file = binascii.b2a_base64(attachment['raw']) attachment_mime.set_payload(base64_attachment_file) - attachment_mime['Content-Disposition'] = 'attachment; filename="%s"' % attachment['name'] + attachment_mime[ + 'Content-Disposition'] = 'attachment; filename="%s"' % attachment['name'] attachment_mime['Content-Transfer-Encoding'] = 'base64' mime.attach(attachment_mime) @@ -141,6 +147,7 @@ def _get_chash(self): class InputMail(Mail): + def __init__(self): self._raw_message = None self._fd = None @@ -193,7 +200,8 @@ def delivery_error_template(delivery_address): @staticmethod def from_dict(mail_dict, from_address): input_mail = InputMail() - input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} + input_mail.headers = { + key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} input_mail.headers['Date'] = date.mail_date_now() input_mail.headers['From'] = from_address @@ -207,7 +215,8 @@ def from_dict(mail_dict, from_address): @staticmethod def from_python_mail(mail): input_mail = InputMail() - input_mail.headers = {unicode(key.capitalize()): unicode(value) for key, value in mail.items()} + input_mail.headers = {unicode(key.capitalize()): unicode( + value) for key, value in mail.items()} input_mail.headers[u'Date'] = unicode(date.mail_date_now()) input_mail.headers[u'To'] = [u''] diff --git a/service/pixelated/adapter/model/status.py b/service/pixelated/adapter/model/status.py index 5a11ee7b9..7101f4c01 100644 --- a/service/pixelated/adapter/model/status.py +++ b/service/pixelated/adapter/model/status.py @@ -38,5 +38,6 @@ def from_flags(flags): @staticmethod def to_flags(statuses): - statuses_to_flags = dict(zip(Status.FLAGS_TO_STATUSES.values(), Status.FLAGS_TO_STATUSES.keys())) + statuses_to_flags = dict( + zip(Status.FLAGS_TO_STATUSES.values(), Status.FLAGS_TO_STATUSES.keys())) return [statuses_to_flags[status] for status in statuses] diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py index e7a4e8c6b..79f0b2812 100644 --- a/service/pixelated/adapter/search/__init__.py +++ b/service/pixelated/adapter/search/__init__.py @@ -44,7 +44,8 @@ def __init__(self, key, user_home=DEFAULT_INDEX_HOME): self._index = self._create_index() def _add_to_tags(self, tags, group, skip_default_tags, count_type, query=None): - query_matcher = re.compile(".*%s.*" % query.lower()) if query else re.compile(".*") + query_matcher = re.compile( + ".*%s.*" % query.lower()) if query else re.compile(".*") for tag, count in group.iteritems(): @@ -59,12 +60,15 @@ def _add_to_tags(self, tags, group, skip_default_tags, count_type, query=None): def _search_tag_groups(self, is_filtering_tags): seen = None query_parser = QueryParser('tag', self._index.schema) - options = {'limit': None, 'groupedby': sorting.FieldFacet('tag', allow_overlap=True), 'maptype': sorting.Count} + options = {'limit': None, 'groupedby': sorting.FieldFacet( + 'tag', allow_overlap=True), 'maptype': sorting.Count} with self._index.searcher() as searcher: - total = searcher.search(query_parser.parse('*'), **options).groups() + total = searcher.search( + query_parser.parse('*'), **options).groups() if not is_filtering_tags: - seen = searcher.search(query_parser.parse("* AND flags:%s" % Status.SEEN), **options).groups() + seen = searcher.search(query_parser.parse( + "* AND flags:%s" % Status.SEEN), **options).groups() return seen, total def _init_tags_defaults(self): @@ -86,14 +90,16 @@ def _build_tags(self, seen, total, skip_default_tags, query): tags = {} if not skip_default_tags: tags = self._init_tags_defaults() - self._add_to_tags(tags, total, skip_default_tags, count_type='total', query=query) + self._add_to_tags(tags, total, skip_default_tags, + count_type='total', query=query) if seen: self._add_to_tags(tags, seen, skip_default_tags, count_type='read') return tags.values() def tags(self, query, skip_default_tags): is_filtering_tags = True if query else False - seen, total = self._search_tag_groups(is_filtering_tags=is_filtering_tags) + seen, total = self._search_tag_groups( + is_filtering_tags=is_filtering_tags) return self._build_tags(seen, total, skip_default_tags, query) def _mail_schema(self): @@ -179,7 +185,8 @@ def search(self, query, window=25, page=1, all_mails=False): def _search_all_mails(self, query): with self._index.searcher() as searcher: sorting_facet = sorting.FieldFacet('date', reverse=True) - results = searcher.search(query, sortedby=sorting_facet, reverse=True, limit=None) + results = searcher.search( + query, sortedby=sorting_facet, reverse=True, limit=None) return unique([mail['ident'] for mail in results]) def _paginated_search_mails(self, query, window, page): @@ -187,9 +194,11 @@ def _paginated_search_mails(self, query, window, page): window = int(window) if window is not None else 25 with self._index.searcher() as searcher: - tags_facet = sorting.FieldFacet('tag', allow_overlap=True, maptype=sorting.Count) + tags_facet = sorting.FieldFacet( + 'tag', allow_overlap=True, maptype=sorting.Count) sorting_facet = sorting.FieldFacet('date', reverse=True) - results = searcher.search_page(query, page, pagelen=window, groupedby=tags_facet, sortedby=sorting_facet) + results = searcher.search_page( + query, page, pagelen=window, groupedby=tags_facet, sortedby=sorting_facet) return unique([mail['ident'] for mail in results]), sum(results.results.groups().values()) def prepare_query(self, query): diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 9e506c345..26fdb0f1d 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -26,6 +26,7 @@ class SMTPDownException(Exception): + def __init__(self): Exception.__init__(self, "Couldn't send mail now, try again later.") @@ -58,7 +59,8 @@ def sendmail(self, mail): if not all_succeeded: error_map = self._build_error_map(recipients, results) - raise MailSenderException('Failed to send mail to all recipients', error_map) + raise MailSenderException( + 'Failed to send mail to all recipients', error_map) defer.returnValue(all_succeeded) @@ -70,7 +72,8 @@ def _send_mail_to_all_recipients(self, mail, recipients): for recipient in recipients: self._define_bcc_field(mail, recipient, bccs) smtp_recipient = self._create_twisted_smtp_recipient(recipient) - deferreds.append(outgoing_mail.send_message(mail.to_smtp_format(), smtp_recipient)) + deferreds.append(outgoing_mail.send_message( + mail.to_smtp_format(), smtp_recipient)) return defer.DeferredList(deferreds, fireOnOneErrback=False, consumeErrors=True) diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 851825009..2ca7c702c 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -60,7 +60,8 @@ def update_tags(self, mail_id, new_tags): new_tags = self._filter_white_space_tags(new_tags) reserved_words = extract_reserved_tags(new_tags) if len(reserved_words): - raise ValueError('None of the following words can be used as tags: ' + ' '.join(reserved_words)) + raise ValueError( + 'None of the following words can be used as tags: ' + ' '.join(reserved_words)) new_tags = self._favor_existing_tags_casing(new_tags) mail = yield self.mail(mail_id) mail.tags = set(new_tags) @@ -72,7 +73,8 @@ def _filter_white_space_tags(self, tags): return [tag.strip() for tag in tags if not tag.isspace()] def _favor_existing_tags_casing(self, new_tags): - current_tags = [tag['name'] for tag in self.search_engine.tags(query='', skip_default_tags=True)] + current_tags = [tag['name'] for tag in self.search_engine.tags( + query='', skip_default_tags=True)] current_tags_lower = [tag.lower() for tag in current_tags] def _use_current_casing(new_tag_lower): @@ -117,8 +119,10 @@ def _remove_canonical(self, mail): mail.headers['Bcc'] = map(self._remove_canonical_recipient, mail.bcc) def _remove_duplicates_form_cc_and_to(self, mail): - mail.headers['To'] = list(set(self._remove_duplicates(mail.to)).difference(set(mail.bcc))) - mail.headers['Cc'] = list((set(self._remove_duplicates(mail.cc)).difference(set(mail.bcc)).difference(set(mail.to)))) + mail.headers['To'] = list( + set(self._remove_duplicates(mail.to)).difference(set(mail.bcc))) + mail.headers['Cc'] = list((set(self._remove_duplicates( + mail.cc)).difference(set(mail.bcc)).difference(set(mail.to)))) mail.headers['Bcc'] = self._remove_duplicates(mail.bcc) def _remove_duplicates(self, recipient): diff --git a/service/pixelated/adapter/services/tag_service.py b/service/pixelated/adapter/services/tag_service.py index c51da6250..ad94170b4 100644 --- a/service/pixelated/adapter/services/tag_service.py +++ b/service/pixelated/adapter/services/tag_service.py @@ -15,7 +15,8 @@ # along with Pixelated. If not, see . from pixelated.adapter.model.tag import Tag -SPECIAL_TAGS = {Tag('inbox', True), Tag('sent', True), Tag('drafts', True), Tag('trash', True), Tag('ALL', True)} +SPECIAL_TAGS = {Tag('inbox', True), Tag('sent', True), Tag( + 'drafts', True), Tag('trash', True), Tag('ALL', True)} def extract_reserved_tags(tags): diff --git a/service/pixelated/application.py b/service/pixelated/application.py index 46e5ba854..667c34a80 100644 --- a/service/pixelated/application.py +++ b/service/pixelated/application.py @@ -44,13 +44,15 @@ class UserAgentMode(object): + def __init__(self, is_single_user): self.is_single_user = is_single_user @defer.inlineCallbacks def start_user_agent_in_single_user_mode(root_resource, services_factory, leap_home, leap_session): - log.info('Bootstrap done, loading services for user %s' % leap_session.user_auth.username) + log.info('Bootstrap done, loading services for user %s' % + leap_session.user_auth.username) _services = services.Services(leap_session) yield _services.setup() @@ -113,7 +115,8 @@ def _quit_on_error(failure): def _log_user_out(event, user_data): log.info('Invalid soledad token, logging out %s' % user_data) - user_data = {'user_id': user_data['uuid']} if 'uuid' in user_data else {'user_id': user_data, 'using_email': True} + user_data = {'user_id': user_data['uuid']} if 'uuid' in user_data else { + 'user_id': user_data, 'using_email': True} services_factory.destroy_session(**user_data) def _log_user_out_on_token_expire(leap_session): @@ -134,7 +137,8 @@ def _start_mode(args, resource, services_factory): def _start_in_multi_user_mode(args, root_resource, services_factory): try: - protected_resources = _setup_multi_user(args, root_resource, services_factory) + protected_resources = _setup_multi_user( + args, root_resource, services_factory) start_site(args, protected_resources) reactor.getThreadPool().adjustPoolsize(5, 15) return defer.succeed(None) @@ -147,8 +151,10 @@ def _setup_multi_user(args, root_resource, services_factory): raise ValueError('Multi-user mode: provider name is required') init_monkeypatches() events_server.ensure_server() - provider = initialize_leap_provider(args.provider, args.leap_provider_cert, args.leap_provider_cert_fingerprint, args.leap_home) - protected_resource = set_up_protected_resources(root_resource, provider, services_factory, banner=args.banner) + provider = initialize_leap_provider( + args.provider, args.leap_provider_cert, args.leap_provider_cert_fingerprint, args.leap_home) + protected_resource = set_up_protected_resources( + root_resource, provider, services_factory, banner=args.banner) return protected_resource @@ -158,9 +164,12 @@ def set_up_protected_resources(root_resource, provider, services_factory, banner realm = PixelatedRealm() _portal = portal.Portal(realm, [session_checker, AllowAnonymousAccess()]) - anonymous_resource = LoginResource(services_factory, provider, disclaimer_banner=banner, authenticator=authenticator) - protected_resource = PixelatedAuthSessionWrapper(_portal, root_resource, anonymous_resource, []) - root_resource.initialize(provider, disclaimer_banner=banner, authenticator=authenticator) + anonymous_resource = LoginResource( + services_factory, provider, disclaimer_banner=banner, authenticator=authenticator) + protected_resource = PixelatedAuthSessionWrapper( + _portal, root_resource, anonymous_resource, []) + root_resource.initialize( + provider, disclaimer_banner=banner, authenticator=authenticator) return protected_resource @@ -173,7 +182,8 @@ def _start_in_single_user_mode(args, resource, services_factory): def _handle_error(exception): if(exception.type is InvalidAuthTokenError): - log.critical('Got an invalid soledad token, the user agent can\'t synchronize data, exiting') + log.critical( + 'Got an invalid soledad token, the user agent can\'t synchronize data, exiting') os._exit(1) else: exception.raiseException() diff --git a/service/pixelated/authentication.py b/service/pixelated/authentication.py index 983086ce6..4e74ad2db 100644 --- a/service/pixelated/authentication.py +++ b/service/pixelated/authentication.py @@ -27,6 +27,7 @@ class Authenticator(object): + def __init__(self, leap_provider): self._leap_provider = leap_provider self.domain = leap_provider.server_name @@ -42,16 +43,19 @@ def _srp_auth(self, username, password): try: auth = yield self._bonafide_auth(username, password) except SRPAuthError: - raise UnauthorizedLogin("User typed wrong password/username combination.") + raise UnauthorizedLogin( + "User typed wrong password/username combination.") returnValue(auth) @inlineCallbacks def _bonafide_auth(self, user, password): srp_provider = Api(self._leap_provider.api_uri) credentials = Credentials(user, password) - srp_auth = Session(credentials, srp_provider, self._leap_provider.local_ca_crt) + srp_auth = Session(credentials, srp_provider, + self._leap_provider.local_ca_crt) yield srp_auth.authenticate() - returnValue(Authentication(user, srp_auth.token, srp_auth.uuid, 'session_id', {'is_admin': False})) + returnValue(Authentication(user, srp_auth.token, + srp_auth.uuid, 'session_id', {'is_admin': False})) def clean_username(self, username): if '@' not in username: @@ -69,6 +73,7 @@ def username_with_domain(self, username): class Authentication(object): + def __init__(self, username, token, uuid, session_id, user_attributes): self.username = username self.token = token diff --git a/service/pixelated/bitmask_libraries/keymanager.py b/service/pixelated/bitmask_libraries/keymanager.py index 9a1b730ed..62ec5325c 100644 --- a/service/pixelated/bitmask_libraries/keymanager.py +++ b/service/pixelated/bitmask_libraries/keymanager.py @@ -53,7 +53,8 @@ def generate_openpgp_key(self): current_key = yield self._regenerate_key_and_send_to_leap() self._synchronize_remote_key(current_key) - logger.debug("Current key for {}: {}".format(self._email, current_key.fingerprint)) + logger.debug("Current key for {}: {}".format( + self._email, current_key.fingerprint)) @defer.inlineCallbacks def _synchronize_remote_key(self, current_key): diff --git a/service/pixelated/bitmask_libraries/provider.py b/service/pixelated/bitmask_libraries/provider.py index 96935fbcd..22b1652c0 100644 --- a/service/pixelated/bitmask_libraries/provider.py +++ b/service/pixelated/bitmask_libraries/provider.py @@ -29,6 +29,7 @@ class LeapProvider(object): + def __init__(self, server_name): self.server_name = server_name self.local_ca_crt = '%s/ca.crt' % leap_config.leap_home @@ -124,7 +125,8 @@ def validate_certificate(self, cert_data=None): digest = get_digest(cert_data, method) if fingerprint.strip() != digest: - raise Exception('Certificate fingerprints don\'t match! Expected [%s] but got [%s]' % (fingerprint.strip(), digest)) + raise Exception('Certificate fingerprints don\'t match! Expected [%s] but got [%s]' % ( + fingerprint.strip(), digest)) def smtp_info(self): hosts = self.smtp_json['hosts'] @@ -135,8 +137,10 @@ def smtp_info(self): def _validated_get(self, url): session = requests.session() try: - session.mount('https://', EnforceTLSv1Adapter(assert_fingerprint=LeapCertificate.LEAP_FINGERPRINT)) - response = session.get(url, verify=LeapCertificate(self).provider_web_cert, timeout=REQUESTS_TIMEOUT) + session.mount( + 'https://', EnforceTLSv1Adapter(assert_fingerprint=LeapCertificate.LEAP_FINGERPRINT)) + response = session.get(url, verify=LeapCertificate( + self).provider_web_cert, timeout=REQUESTS_TIMEOUT) response.raise_for_status() return response finally: @@ -151,14 +155,16 @@ def fetch_provider_json(self): def fetch_soledad_json(self): service_url = "%s/%s/config/soledad-service.json" % ( self.api_uri, self.api_version) - response = requests.get(service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) + response = requests.get( + service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) response.raise_for_status() return json.loads(response.content) def fetch_smtp_json(self): service_url = '%s/%s/config/smtp-service.json' % ( self.api_uri, self.api_version) - response = requests.get(service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) + response = requests.get( + service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) response.raise_for_status() return json.loads(response.content) @@ -187,13 +193,15 @@ def create_combined_bundle_file(self): return leap_ca_bundle with open(self.combined_cerfificates_path, 'w') as fout: - fin = fileinput.input(files=(leap_ca_bundle, self.provider_api_cert)) + fin = fileinput.input( + files=(leap_ca_bundle, self.provider_api_cert)) for line in fin: fout.write(line) fin.close() def setup_ca_bundle(self): - path = os.path.join(leap_config.leap_home, 'providers', self.server_name, 'keys', 'client') + path = os.path.join(leap_config.leap_home, 'providers', + self.server_name, 'keys', 'client') if not os.path.isdir(path): os.makedirs(path, 0700) self._download_cert(self.provider_api_cert) diff --git a/service/pixelated/config/arguments.py b/service/pixelated/config/arguments.py index 01152a348..9ce255f36 100644 --- a/service/pixelated/config/arguments.py +++ b/service/pixelated/config/arguments.py @@ -23,14 +23,21 @@ def parse_user_agent_args(): parser_add_default_arguments(parser) - parser.add_argument('--host', default='127.0.0.1', help='the host to run the user agent on') - parser.add_argument('--port', type=int, default=3333, help='the port to run the user agent on') - parser.add_argument('-sk', '--sslkey', metavar='', default=None, help='use specified file as web server\'s SSL key (when using the user-agent in server-mode)') - parser.add_argument('-sc', '--sslcert', metavar='', default=None, help='use specified file as web server\'s SSL certificate (when using the user-agent in server-mode)') - parser.add_argument('--multi-user', help='Run user agent in multi user mode', action='store_false', default=True, dest='single_user') - parser.add_argument('-p', '--provider', help='specify a provider for mutli-user mode', metavar='', default=None, dest='provider') + parser.add_argument('--host', default='127.0.0.1', + help='the host to run the user agent on') + parser.add_argument('--port', type=int, default=3333, + help='the port to run the user agent on') + parser.add_argument('-sk', '--sslkey', metavar='', default=None, + help='use specified file as web server\'s SSL key (when using the user-agent in server-mode)') + parser.add_argument('-sc', '--sslcert', metavar='', default=None, + help='use specified file as web server\'s SSL certificate (when using the user-agent in server-mode)') + parser.add_argument('--multi-user', help='Run user agent in multi user mode', + action='store_false', default=True, dest='single_user') + parser.add_argument('-p', '--provider', help='specify a provider for mutli-user mode', + metavar='', default=None, dest='provider') parser.add_argument('--banner', help='banner file to show on login screen') - parser.add_argument('--manhole', help='Run an interactive Python shell on port 8008', action='store_true', default=False, dest='manhole') + parser.add_argument('--manhole', help='Run an interactive Python shell on port 8008', + action='store_true', default=False, dest='manhole') args = parser.parse_args() @@ -42,18 +49,24 @@ def parse_maintenance_args(): parser_add_default_arguments(parser) subparsers = parser.add_subparsers(help='commands', dest='command') subparsers.add_parser('reset', help='reset account command') - mails_parser = subparsers.add_parser('load-mails', help='load mails into account') + mails_parser = subparsers.add_parser( + 'load-mails', help='load mails into account') mails_parser.add_argument('file', nargs='+', help='file(s) with mail data') - markov_mails_parser = subparsers.add_parser('markov-generate', help='generate mails using markov chains') - markov_mails_parser.add_argument('--seed', default=None, help='Specify a seed to always generate the same output') - markov_mails_parser.add_argument('-l', '--limit', metavar='count', default='5', help='limit number of generated mails', dest='limit') - markov_mails_parser.add_argument('file', nargs='+', help='file(s) with mail data') + markov_mails_parser = subparsers.add_parser( + 'markov-generate', help='generate mails using markov chains') + markov_mails_parser.add_argument( + '--seed', default=None, help='Specify a seed to always generate the same output') + markov_mails_parser.add_argument('-l', '--limit', metavar='count', + default='5', help='limit number of generated mails', dest='limit') + markov_mails_parser.add_argument( + 'file', nargs='+', help='file(s) with mail data') subparsers.add_parser('dump-soledad', help='dump the soledad database') subparsers.add_parser('sync', help='sync the soledad database') subparsers.add_parser('repair', help='repair database if possible') - subparsers.add_parser('integrity-check', help='run integrity check on database') + subparsers.add_parser( + 'integrity-check', help='run integrity check on database') return parser.parse_args() @@ -62,17 +75,26 @@ def parse_register_args(): parser = argparse.ArgumentParser(description='Pixelated register') parser.add_argument('provider', metavar='provider', action='store') parser.add_argument('username', metavar='username', action='store') - parser.add_argument('-p', '--password', metavar='password', action='store', default=None, help='used just to register account automatically by scripts') - parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') - parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') - parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) - parser.add_argument('--invite-code', help='invite code to register a user, if required', dest='invite_code', default=None) + parser.add_argument('-p', '--password', metavar='password', action='store', + default=None, help='used just to register account automatically by scripts') + parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, + help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') + parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, + help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') + parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', + dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) + parser.add_argument( + '--invite-code', help='invite code to register a user, if required', dest='invite_code', default=None) return parser.parse_args() def parser_add_default_arguments(parser): parser.add_argument('--debug', action='store_true', help='DEBUG mode.') - parser.add_argument('-c', '--config', dest='credentials_file', metavar='', default=None, help='use specified file for credentials (for test purposes only)') - parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) - parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') - parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') + parser.add_argument('-c', '--config', dest='credentials_file', metavar='', + default=None, help='use specified file for credentials (for test purposes only)') + parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', + dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) + parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, + help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') + parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, + help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') diff --git a/service/pixelated/config/credentials.py b/service/pixelated/config/credentials.py index 89901b3f3..9d663ee1a 100644 --- a/service/pixelated/config/credentials.py +++ b/service/pixelated/config/credentials.py @@ -36,7 +36,8 @@ def prompt_for_credentials(): def read_from_file(credentials_file): config_parser = ConfigParser.ConfigParser() - credentials_file_path = os.path.abspath(os.path.expanduser(credentials_file)) + credentials_file_path = os.path.abspath( + os.path.expanduser(credentials_file)) config_parser.read(credentials_file_path) provider, user, password = \ config_parser.get('pixelated', 'leap_server_name'), \ diff --git a/service/pixelated/config/leap.py b/service/pixelated/config/leap.py index 4832b6d21..b1fabe48f 100644 --- a/service/pixelated/config/leap.py +++ b/service/pixelated/config/leap.py @@ -31,7 +31,8 @@ def initialize_leap_multi_user(provider_hostname, credentials_file, leap_home): - config, provider = initialize_leap_provider(provider_hostname, leap_provider_cert, leap_provider_cert_fingerprint, leap_home) + config, provider = initialize_leap_provider( + provider_hostname, leap_provider_cert, leap_provider_cert_fingerprint, leap_home) defer.returnValue((config, provider)) @@ -53,7 +54,8 @@ def initialize_leap_single_user(leap_provider_cert, provider, username, password = credentials.read(credentials_file) - provider = initialize_leap_provider(provider, leap_provider_cert, leap_provider_cert_fingerprint, leap_home) + provider = initialize_leap_provider( + provider, leap_provider_cert, leap_provider_cert_fingerprint, leap_home) auth = yield Authenticator(provider).authenticate(username, password) @@ -83,7 +85,8 @@ def _setup_user_services(self, leap_session): user_id = leap_session.user_auth.uuid if not self._services_factory.has_session(user_id): yield self._services_factory.create_services_from(leap_session) - self._services_factory.map_email(leap_session.user_auth.username, user_id) + self._services_factory.map_email( + leap_session.user_auth.username, user_id) @defer.inlineCallbacks def _add_welcome_email(self, leap_session, language): diff --git a/service/pixelated/config/logger.py b/service/pixelated/config/logger.py index a1e825a65..47eb6dcb2 100644 --- a/service/pixelated/config/logger.py +++ b/service/pixelated/config/logger.py @@ -25,7 +25,8 @@ class PrivateKeyFilter(logging.Filter): def filter(self, record): if '-----BEGIN PGP PRIVATE KEY BLOCK-----' in record.msg: - record.msg = '*** private key removed by %s.%s ***' % (type(self).__module__, type(self).__name__) + record.msg = '*** private key removed by %s.%s ***' % ( + type(self).__module__, type(self).__name__) return True @@ -42,9 +43,11 @@ def init(debug=False): logging.getLogger('gnupg').addFilter(PrivateKeyFilter()) def formatter(event): - event['log_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(event['log_time'])) + event['log_time'] = time.strftime( + '%Y-%m-%d %H:%M:%S', time.localtime(event['log_time'])) event['log_level'] = event['log_level'].name.upper() - logstring = u'{log_time} [{log_namespace}] {log_level} ' + event['log_format'] + '\n' + logstring = u'{log_time} [{log_namespace}] {log_level} ' + \ + event['log_format'] + '\n' return logstring.format(**event) observers = [FileLogObserver(sys.stdout, formatter)] diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py index 9e4de84e7..7e976024e 100644 --- a/service/pixelated/config/services.py +++ b/service/pixelated/config/services.py @@ -27,7 +27,8 @@ def __init__(self, leap_session): @defer.inlineCallbacks def setup(self): - search_index_storage_key = self._setup_search_index_storage_key(self._leap_session.soledad) + search_index_storage_key = self._setup_search_index_storage_key( + self._leap_session.soledad) yield self._setup_search_engine(self._leap_session.user_auth.uuid, search_index_storage_key) self._wrap_mail_store_with_indexing_mail_store(self._leap_session) @@ -37,7 +38,8 @@ def setup(self): self.mail_service = self._setup_mail_service(self.search_engine) self.keymanager = self._leap_session.keymanager - self.draft_service = self._setup_draft_service(self._leap_session.mail_store) + self.draft_service = self._setup_draft_service( + self._leap_session.mail_store) self.feedback_service = self._setup_feedback_service() yield self._index_all_mails() @@ -45,7 +47,8 @@ def close(self): self._leap_session.close() def _wrap_mail_store_with_indexing_mail_store(self, leap_session): - leap_session.mail_store = SearchableMailStore(leap_session.mail_store, self.search_engine) + leap_session.mail_store = SearchableMailStore( + leap_session.mail_store, self.search_engine) @defer.inlineCallbacks def _index_all_mails(self): @@ -63,7 +66,8 @@ def _setup_search_engine(self, namespace, search_index_storage_key): self.search_engine = search_engine def _setup_mail_service(self, search_engine): - pixelated_mail_sender = MailSender(self._leap_session.smtp_config, self._leap_session.keymanager.keymanager) + pixelated_mail_sender = MailSender( + self._leap_session.smtp_config, self._leap_session.keymanager.keymanager) return MailService( pixelated_mail_sender, @@ -122,6 +126,7 @@ def create_services_from(self, leap_session): class SingleUserServicesFactory(object): + def __init__(self, mode): self._services = None self.mode = mode diff --git a/service/pixelated/config/sessions.py b/service/pixelated/config/sessions.py index ec5e1c0ee..1d6d4782b 100644 --- a/service/pixelated/config/sessions.py +++ b/service/pixelated/config/sessions.py @@ -29,6 +29,7 @@ class LeapSessionFactory(object): + def __init__(self, provider): self._provider = provider @@ -58,9 +59,11 @@ def _create_new_session(self, username, password, auth): smtp_client_cert = self._download_smtp_cert(auth) smtp_host, smtp_port = self._provider.smtp_info() - smtp_config = LeapSMTPConfig(account_email, smtp_client_cert, smtp_host, smtp_port) + smtp_config = LeapSMTPConfig( + account_email, smtp_client_cert, smtp_host, smtp_port) - leap_session = LeapSession(self._provider, auth, mail_store, soledad, keymanager, smtp_config) + leap_session = LeapSession( + self._provider, auth, mail_store, soledad, keymanager, smtp_config) defer.returnValue(leap_session) @@ -99,7 +102,8 @@ def setup_keymanager(self, provider, soledad, account_email, token, uuid): defer.returnValue(keymanager) def _download_smtp_cert(self, auth): - cert = SmtpClientCertificate(self._provider, auth, self._user_path(auth.uuid)) + cert = SmtpClientCertificate( + self._provider, auth, self._user_path(auth.uuid)) return cert.cert_path() def _create_dir(self, path): @@ -147,7 +151,8 @@ def __init__(self, provider, user_auth, mail_store, soledad, keymanager, smtp_co self.account = None self._has_been_initially_synced = False self._is_closed = False - register(events.KEYMANAGER_FINISHED_KEY_GENERATION, self._set_fresh_account, uid=self.account_email()) + register(events.KEYMANAGER_FINISHED_KEY_GENERATION, + self._set_fresh_account, uid=self.account_email()) @defer.inlineCallbacks def first_required_sync(self): @@ -180,7 +185,8 @@ def account_email(self): def close(self): self.stop_background_jobs() - unregister(events.KEYMANAGER_FINISHED_KEY_GENERATION, uid=self.account_email()) + unregister(events.KEYMANAGER_FINISHED_KEY_GENERATION, + uid=self.account_email()) self.soledad.close() self._close_account() self.remove_from_cache() @@ -202,9 +208,9 @@ def remove_from_cache(self): def _create_incoming_mail_fetcher(self, keymanager, soledad, account, user_mail): inbox = yield account.callWhenReady(lambda _: account.get_collection_by_mailbox('INBOX')) defer.returnValue(IncomingMail(keymanager.keymanager, - soledad, - inbox, - user_mail)) + soledad, + inbox, + user_mail)) def stop_background_jobs(self): if self.incoming_mail_fetcher: @@ -246,6 +252,7 @@ def session_key(provider, username): class SmtpClientCertificate(object): + def __init__(self, provider, auth, user_path): self._provider = provider self._auth = auth @@ -279,7 +286,8 @@ def _smtp_client_cert_path(self): "keys", "client", "smtp.pem") def download(self): - cert_url = '%s/%s/smtp_cert' % (self._provider.api_uri, self._provider.api_version) + cert_url = '%s/%s/smtp_cert' % (self._provider.api_uri, + self._provider.api_version) headers = {} headers["Authorization"] = 'Token token="{0}"'.format(self._auth.token) params = {'address': self._auth.username} @@ -304,5 +312,6 @@ def download_to(self, target_file): class SoledadWrongPassphraseException(Exception): + def __init__(self, *args, **kwargs): super(SoledadWrongPassphraseException, self).__init__(*args, **kwargs) diff --git a/service/pixelated/config/site.py b/service/pixelated/config/site.py index 7163b52b7..1fb884b0a 100644 --- a/service/pixelated/config/site.py +++ b/service/pixelated/config/site.py @@ -13,7 +13,8 @@ def process(self): self.setHeader('X-Content-Type-Options', 'nosniff') if self.isSecure(): - self.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains') + self.setHeader('Strict-Transport-Security', + 'max-age=31536000; includeSubDomains') Request.process(self) diff --git a/service/pixelated/extensions/esmtp_sender_factory.py b/service/pixelated/extensions/esmtp_sender_factory.py index 59aa90c85..9288fc4d1 100644 --- a/service/pixelated/extensions/esmtp_sender_factory.py +++ b/service/pixelated/extensions/esmtp_sender_factory.py @@ -24,4 +24,5 @@ def wrapper(*args, **kwargs): return wrapper -smtp.ESMTPSenderFactory = no_require_transport_security(smtp.ESMTPSenderFactory) +smtp.ESMTPSenderFactory = no_require_transport_security( + smtp.ESMTPSenderFactory) diff --git a/service/pixelated/extensions/requests_urllib3.py b/service/pixelated/extensions/requests_urllib3.py index c4ec24389..5b0a6644e 100644 --- a/service/pixelated/extensions/requests_urllib3.py +++ b/service/pixelated/extensions/requests_urllib3.py @@ -49,9 +49,11 @@ def patched_connect(self): # Add certificate verification try: - sock = socket.create_connection(address=(self.host, self.port), timeout=self.timeout) + sock = socket.create_connection( + address=(self.host, self.port), timeout=self.timeout) except SocketTimeout: - raise ConnectTimeoutError(self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) + raise ConnectTimeoutError( + self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) resolved_ssl_version = resolve_ssl_version(self.ssl_version) diff --git a/service/pixelated/maintenance.py b/service/pixelated/maintenance.py index a2286034a..3eb55ac7b 100644 --- a/service/pixelated/maintenance.py +++ b/service/pixelated/maintenance.py @@ -221,7 +221,8 @@ def markov_generate(args, mail_paths, limit, seed): username = leap_session.user_auth.username server_name = leap_session.provider.server_name - markov_mails = _generate_mails(limit, mail_paths, seed, server_name, username) + markov_mails = _generate_mails( + limit, mail_paths, seed, server_name, username) deferreds = [] yield add_mail_folder(store, markov_mails, 'INBOX', deferreds) yield defer.gatherResults(deferreds, consumeErrors=True) @@ -234,7 +235,8 @@ def _generate_mails(limit, mail_paths, seed, server_name, username): for path in mail_paths: mbox_mails = mbox(path, factory=None) mails.extend(mbox_mails) - gen = MailGenerator(username, server_name, mails, random=random.Random(seed)) + gen = MailGenerator(username, server_name, mails, + random=random.Random(seed)) markov_mails = [gen.generate_mail() for _ in range(limit)] return markov_mails diff --git a/service/pixelated/register.py b/service/pixelated/register.py index b6faf454a..30eba86dd 100644 --- a/service/pixelated/register.py +++ b/service/pixelated/register.py @@ -42,7 +42,8 @@ def _set_provider(provider_cert, provider_cert_fingerprint, server_name, leap_ho if leap_home: leap_config.set_leap_home(leap_home) - LeapCertificate.set_cert_and_fingerprint(provider_cert, provider_cert_fingerprint) + LeapCertificate.set_cert_and_fingerprint( + provider_cert, provider_cert_fingerprint) provider = LeapProvider(server_name) provider.setup_ca() provider.download_settings() diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index 11611f0bc..b1006bb17 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -28,6 +28,7 @@ class SetEncoder(json.JSONEncoder): + def default(self, obj): if isinstance(obj, set): return list(obj) @@ -36,14 +37,16 @@ def default(self, obj): def respond_json(entity, request, status_code=200): json_response = json.dumps(entity, cls=SetEncoder) - request.responseHeaders.setRawHeaders(b"content-type", [b"application/json"]) + request.responseHeaders.setRawHeaders( + b"content-type", [b"application/json"]) request.code = status_code return json_response def respond_json_deferred(entity, request, status_code=200): json_response = json.dumps(entity, cls=SetEncoder) - request.responseHeaders.setRawHeaders(b"content-type", [b"application/json"]) + request.responseHeaders.setRawHeaders( + b"content-type", [b"application/json"]) request.code = status_code request.write(json_response) request.finish() @@ -119,6 +122,7 @@ def render_POST(self, request): class UnavailableResource(Resource): + def __init__(self): Resource.__init__(self) diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 1081b4b8e..2c765a29d 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -47,9 +47,11 @@ def error_handler(failure): encoding = request.args.get('encoding', [None])[0] filename = request.args.get('filename', [self.attachment_id])[0] - content_type = request.args.get('content_type', ['application/octet-stream'])[0] + content_type = request.args.get( + 'content_type', ['application/octet-stream'])[0] request.setHeader(b'Content-Type', content_type) - request.setHeader(b'Content-Disposition', bytes('attachment; filename="' + filename + '"')) + request.setHeader(b'Content-Disposition', + bytes('attachment; filename="' + filename + '"')) d = self._send_attachment(encoding, filename, request) d.addErrback(error_handler) @@ -92,7 +94,8 @@ def render_POST(self, request): deferred = _mail_service.save_attachment(_file.value, _file.type) def send_location(attachment_id): - request.setHeader('Location', '/%s/%s' % (self.BASE_URL, attachment_id)) + request.setHeader('Location', '/%s/%s' % + (self.BASE_URL, attachment_id)) response_json = {"ident": attachment_id, "content-type": _file.type, "encoding": "base64", # hard coded for now -- not really used @@ -102,7 +105,8 @@ def send_location(attachment_id): def error_handler(error): logger.error(error) - respond_json_deferred({"message": "Something went wrong. Attachment not saved."}, request, status_code=500) + respond_json_deferred( + {"message": "Something went wrong. Attachment not saved."}, request, status_code=500) deferred.addCallback(send_location) deferred.addErrback(error_handler) diff --git a/service/pixelated/resources/auth.py b/service/pixelated/resources/auth.py index adac985ff..c5f0271eb 100644 --- a/service/pixelated/resources/auth.py +++ b/service/pixelated/resources/auth.py @@ -40,6 +40,7 @@ class ISessionCredential(ICredentials): @implementer(ISessionCredential) class SessionCredential(object): + def __init__(self, request): self.request = request diff --git a/service/pixelated/resources/features_resource.py b/service/pixelated/resources/features_resource.py index c1b61f121..75d944172 100644 --- a/service/pixelated/resources/features_resource.py +++ b/service/pixelated/resources/features_resource.py @@ -36,11 +36,13 @@ def render_GET(self, request): return respond_json(features, request) def _disabled_features(self): - disabled_features = [default_disabled_feature for default_disabled_feature in self.DISABLED_FEATURES] + disabled_features = [ + default_disabled_feature for default_disabled_feature in self.DISABLED_FEATURES] if not os.environ.get('FEEDBACK_URL'): disabled_features.append('feedback') return disabled_features def _add_multi_user_to(self, features): if self._multi_user: - features.update({'multi_user': {'logout': LogoutResource.BASE_URL}}) + features.update( + {'multi_user': {'logout': LogoutResource.BASE_URL}}) diff --git a/service/pixelated/resources/login_resource.py b/service/pixelated/resources/login_resource.py index 2a11b73eb..60802ef25 100644 --- a/service/pixelated/resources/login_resource.py +++ b/service/pixelated/resources/login_resource.py @@ -40,13 +40,15 @@ def _get_startup_folder(): def _get_static_folder(): - static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "web-ui", "dist")) + static_folder = os.path.abspath(os.path.join( + os.path.abspath(__file__), "..", "..", "..", "web-ui", "dist")) # 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", "dist")) if not os.path.exists(static_folder): - static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent') + static_folder = os.path.join( + '/', 'usr', 'share', 'pixelated-user-agent') return static_folder @@ -60,7 +62,8 @@ def parse_accept_language(all_headers): class DisclaimerElement(Element): - loader = XMLFile(FilePath(os.path.join(_get_startup_folder(), '_login_disclaimer_banner.html'))) + loader = XMLFile(FilePath(os.path.join( + _get_startup_folder(), '_login_disclaimer_banner.html'))) def __init__(self, banner): super(DisclaimerElement, self).__init__() @@ -70,7 +73,8 @@ def __init__(self, banner): def _set_loader(self, banner): if banner: current_path = os.path.dirname(os.path.abspath(__file__)) - banner_file_path = os.path.join(current_path, "..", "..", "..", banner) + banner_file_path = os.path.join( + current_path, "..", "..", "..", banner) self.loader = XMLFile(FilePath(banner_file_path)) def render(self, request): @@ -83,7 +87,8 @@ def render(self, request): class LoginWebSite(Element): - loader = XMLFile(FilePath(os.path.join(_get_startup_folder(), 'login.html'))) + loader = XMLFile(FilePath(os.path.join( + _get_startup_folder(), 'login.html'))) def __init__(self, error_msg=None, disclaimer_banner_file=None): super(LoginWebSite, self).__init__() @@ -111,7 +116,8 @@ def __init__(self, services_factory, provider=None, disclaimer_banner=None, auth self._disclaimer_banner = disclaimer_banner self._provider = provider self._authenticator = authenticator or Authenticator(provider) - self._bootstrap_user_services = BootstrapUserServices(services_factory, provider) + self._bootstrap_user_services = BootstrapUserServices( + services_factory, provider) self.putChild('startup-assets', File(self._startup_folder)) with open(os.path.join(self._startup_folder, 'Interstitial.html')) as f: @@ -131,7 +137,8 @@ def render_GET(self, request): return self._render_template(request) def _render_template(self, request, error_msg=None): - site = LoginWebSite(error_msg=error_msg, disclaimer_banner_file=self._disclaimer_banner) + site = LoginWebSite(error_msg=error_msg, + disclaimer_banner_file=self._disclaimer_banner) return renderElement(request, site) def render_POST(self, request): @@ -165,7 +172,8 @@ def _handle_login(self, request): def _complete_bootstrap(self, user_auth, request): def log_error(error): - log.error('Login error during %s services setup: %s \n %s' % (user_auth.username, error.getErrorMessage(), error.getTraceback())) + log.error('Login error during %s services setup: %s \n %s' % ( + user_auth.username, error.getErrorMessage(), error.getTraceback())) def set_session_cookies(_): session = IPixelatedSession(request.getSession()) diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py index 61a38798e..ec5a441ce 100644 --- a/service/pixelated/resources/mail_resource.py +++ b/service/pixelated/resources/mail_resource.py @@ -21,7 +21,8 @@ def render_POST(self, request): new_tags = json.loads(request.content.read()).get('newtags') d = self._mail_service.update_tags(self._mail_id, new_tags) - d.addCallback(lambda mail: respond_json_deferred(mail.as_dict(), request)) + d.addCallback(lambda mail: respond_json_deferred( + mail.as_dict(), request)) def handle403(failure): failure.trap(ValueError) @@ -45,12 +46,14 @@ def populate_reply(mail): sender = mail.headers.get('Reply-to', mail.headers.get('From')) to = mail.headers.get('To', []) ccs = mail.headers.get('Cc', []) - mail_dict['replying'] = replier.generate_recipients(sender, to, ccs, current_user) + mail_dict['replying'] = replier.generate_recipients( + sender, to, ccs, current_user) return mail_dict d = self._mail_service.mail(self._mail_id) d.addCallback(lambda mail: populate_reply(mail)) - d.addCallback(lambda mail_dict: respond_json_deferred(mail_dict, request)) + d.addCallback( + lambda mail_dict: respond_json_deferred(mail_dict, request)) d.addErrback(handle_error_deferred, request) return NOT_DONE_YET @@ -62,12 +65,14 @@ def populate_reply(mail): sender = mail.headers.get('Reply-to', mail.headers.get('From')) to = mail.headers.get('To', []) ccs = mail.headers.get('Cc', []) - mail_dict['replying'] = replier.generate_recipients(sender, to, ccs, current_user) + mail_dict['replying'] = replier.generate_recipients( + sender, to, ccs, current_user) return mail_dict d = self._mail_service.raw_mail(self._mail_id) d.addCallback(lambda mail: populate_reply(mail)) - d.addCallback(lambda mail_dict: respond_json_deferred(mail_dict, request)) + d.addCallback( + lambda mail_dict: respond_json_deferred(mail_dict, request)) d.addErrback(handle_error_deferred, request) return NOT_DONE_YET diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index 93070dd6f..9150e7207 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -33,7 +33,8 @@ def render_POST(self, request): d = defer.gatherResults(deferreds, consumeErrors=True) d.addCallback(lambda _: respond_json_deferred(None, request)) - d.addErrback(lambda _: respond_json_deferred(None, request, status_code=500)) + d.addErrback(lambda _: respond_json_deferred( + None, request, status_code=500)) return NOT_DONE_YET @@ -53,7 +54,8 @@ def render_POST(self, request): d = defer.gatherResults(deferreds, consumeErrors=True) d.addCallback(lambda _: respond_json_deferred(None, request)) - d.addErrback(lambda _: respond_json_deferred(None, request, status_code=500)) + d.addErrback(lambda _: respond_json_deferred( + None, request, status_code=500)) return NOT_DONE_YET @@ -95,7 +97,8 @@ def render_POST(self, request): deferreds.append(self._mail_service.recover_mail(ident)) d = defer.gatherResults(deferreds, consumeErrors=True) d.addCallback(lambda _: respond_json_deferred(None, request)) - d.addErrback(lambda _: respond_json_deferred(None, request, status_code=500)) + d.addErrback(lambda _: respond_json_deferred( + None, request, status_code=500)) return NOT_DONE_YET @@ -112,8 +115,10 @@ def render_POST(self, request): for ident in idents: deferreds.append(self._mail_service.archive_mail(ident)) d = defer.gatherResults(deferreds, consumeErrors=True) - d.addCallback(lambda _: respond_json_deferred({'successMessage': 'your-message-was-archived'}, request)) - d.addErrback(lambda _: respond_json_deferred(None, request, status_code=500)) + d.addCallback(lambda _: respond_json_deferred( + {'successMessage': 'your-message-was-archived'}, request)) + d.addErrback(lambda _: respond_json_deferred( + None, request, status_code=500)) return NOT_DONE_YET @@ -122,10 +127,12 @@ class MailsResource(BaseResource): def _register_smtp_error_handler(self): def on_error(event, content): - delivery_error_mail = InputMail.delivery_error_template(delivery_address=event.content) + delivery_error_mail = InputMail.delivery_error_template( + delivery_address=event.content) self._mail_service.mailboxes.inbox.add(delivery_error_mail) - events.register(events.catalog.SMTP_SEND_MESSAGE_ERROR, callback=on_error) + events.register(events.catalog.SMTP_SEND_MESSAGE_ERROR, + callback=on_error) def __init__(self, services_factory): BaseResource.__init__(self, services_factory) @@ -156,7 +163,8 @@ def _build_mails_response(self, (mails, total)): def render_GET(self, request): _mail_service = self.mail_service(request) - query, window_size, page = request.args.get('q')[0], request.args.get('w')[0], request.args.get('p')[0] + query, window_size, page = request.args.get('q')[0], request.args.get('w')[ + 0], request.args.get('p')[0] unicode_query = to_unicode(query) d = _mail_service.mails(unicode_query, window_size, page) @@ -173,10 +181,13 @@ def error_handler(error): def render_POST(self, request): def onError(error): if isinstance(error.value, SMTPDownException): - respond_json_deferred({'message': str(error.value)}, request, status_code=503) + respond_json_deferred( + {'message': str(error.value)}, request, status_code=503) else: - log.error('error occurred while sending: %s' % error.getErrorMessage()) - respond_json_deferred({'message': 'an error occurred while sending'}, request, status_code=422) + log.error('error occurred while sending: %s' % + error.getErrorMessage()) + respond_json_deferred( + {'message': 'an error occurred while sending'}, request, status_code=422) deferred = self._handle_post(request) deferred.addErrback(onError) @@ -185,8 +196,8 @@ def onError(error): def render_PUT(self, request): def onError(error): - log.error('error saving draft: %s' % error.getErrorMessage()) - respond_json_deferred("", request, status_code=422) + log.error('error saving draft: %s' % error.getErrorMessage()) + respond_json_deferred("", request, status_code=422) deferred = self._handle_put(request) deferred.addErrback(onError) @@ -195,7 +206,8 @@ def onError(error): @defer.inlineCallbacks def _fetch_attachment_contents(self, content_dict, _mail_service): - attachments = content_dict.get('attachments', []) if content_dict else [] + attachments = content_dict.get( + 'attachments', []) if content_dict else [] for attachment in attachments: retrieved_attachment = yield _mail_service.attachment(attachment['ident']) attachment['raw'] = retrieved_attachment['content'] @@ -218,7 +230,8 @@ def _handle_put(self, request): content_dict = json.loads(request.content.read()) with_attachment_content = yield self._fetch_attachment_contents(content_dict, _mail_service) - _mail = InputMail.from_dict(with_attachment_content, from_address=_mail_service.account_email) + _mail = InputMail.from_dict( + with_attachment_content, from_address=_mail_service.account_email) draft_id = content_dict.get('ident') pixelated_mail = yield _draft_service.process_draft(draft_id, _mail) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index b4eef4ec0..243d3a800 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -48,11 +48,13 @@ class RootResource(BaseResource): + def __init__(self, services_factory): BaseResource.__init__(self, services_factory) self._startup_assets_folder = self._get_startup_folder() self._static_folder = self._get_static_folder() - self._html_template = open(os.path.join(self._static_folder, 'index.html')).read() + self._html_template = open(os.path.join( + self._static_folder, 'index.html')).read() self._services_factory = services_factory self._child_resources = ChildResourcesMap() with open(os.path.join(self._startup_assets_folder, 'Interstitial.html')) as f: @@ -79,30 +81,40 @@ def _is_xsrf_valid(self, request): xsrf_token = request.getCookie('XSRF-TOKEN') - ajax_request = (request.getHeader('x-requested-with') == 'XMLHttpRequest') + ajax_request = (request.getHeader( + 'x-requested-with') == 'XMLHttpRequest') if ajax_request: xsrf_header = request.getHeader('x-xsrf-token') return xsrf_header and xsrf_header == xsrf_token - csrf_input = request.args.get('csrftoken', [None])[0] or json.loads(request.content.read()).get('csrftoken', [None])[0] + csrf_input = request.args.get('csrftoken', [None])[0] or json.loads( + request.content.read()).get('csrftoken', [None])[0] return csrf_input and csrf_input == xsrf_token def initialize(self, provider=None, disclaimer_banner=None, authenticator=None): - self._child_resources.add('sandbox', SandboxResource(self._static_folder)) + self._child_resources.add( + 'sandbox', SandboxResource(self._static_folder)) self._child_resources.add('assets', File(self._static_folder)) self._child_resources.add('keys', KeysResource(self._services_factory)) - self._child_resources.add(AttachmentsResource.BASE_URL, AttachmentsResource(self._services_factory)) - self._child_resources.add('contacts', ContactsResource(self._services_factory)) + self._child_resources.add( + AttachmentsResource.BASE_URL, AttachmentsResource(self._services_factory)) + self._child_resources.add( + 'contacts', ContactsResource(self._services_factory)) self._child_resources.add('features', FeaturesResource(provider)) self._child_resources.add('tags', TagsResource(self._services_factory)) - self._child_resources.add('mails', MailsResource(self._services_factory)) + self._child_resources.add( + 'mails', MailsResource(self._services_factory)) self._child_resources.add('mail', MailResource(self._services_factory)) - self._child_resources.add('feedback', FeedbackResource(self._services_factory)) - self._child_resources.add('user-settings', UserSettingsResource(self._services_factory)) - self._child_resources.add('users', UsersResource(self._services_factory)) + self._child_resources.add( + 'feedback', FeedbackResource(self._services_factory)) + self._child_resources.add( + 'user-settings', UserSettingsResource(self._services_factory)) + self._child_resources.add( + 'users', UsersResource(self._services_factory)) self._child_resources.add(LoginResource.BASE_URL, LoginResource(self._services_factory, provider, disclaimer_banner=disclaimer_banner, authenticator=authenticator)) - self._child_resources.add(LogoutResource.BASE_URL, LogoutResource(self._services_factory)) + self._child_resources.add( + LogoutResource.BASE_URL, LogoutResource(self._services_factory)) self._mode = MODE_RUNNING @@ -111,9 +123,11 @@ def _get_startup_folder(self): return os.path.join(path, '..', 'assets') def _get_static_folder(self): - static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "..", "web-ui", "dist")) + static_folder = os.path.abspath(os.path.join(os.path.abspath( + __file__), "..", "..", "..", "..", "web-ui", "dist")) if not os.path.exists(static_folder): - static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent') + static_folder = os.path.join( + '/', 'usr', 'share', 'pixelated-user-agent') return static_folder def _is_starting(self): @@ -129,11 +143,13 @@ def render_GET(self, request): return self.interstitial else: account_email = self.mail_service(request).account_email - response = Template(self._html_template).safe_substitute(account_email=account_email) + response = Template(self._html_template).safe_substitute( + account_email=account_email) return str(response) class ChildResourcesMap(object): + def __init__(self): self._registry = {} diff --git a/service/pixelated/resources/tags_resource.py b/service/pixelated/resources/tags_resource.py index 4cea4ca79..453ad772f 100644 --- a/service/pixelated/resources/tags_resource.py +++ b/service/pixelated/resources/tags_resource.py @@ -31,7 +31,8 @@ def render_GET(self, request): query = request.args.get('q', [''])[0] skip_default_tags = request.args.get('skipDefaultTags', [False])[0] - d = deferToThread(lambda: _search_engine.tags(query=query, skip_default_tags=skip_default_tags)) + d = deferToThread(lambda: _search_engine.tags( + query=query, skip_default_tags=skip_default_tags)) d.addCallback(lambda tags: respond_json_deferred(tags, request)) d.addErrback(handle_error_deferred, request) diff --git a/service/pixelated/resources/user_settings_resource.py b/service/pixelated/resources/user_settings_resource.py index 04b434bdc..b59306e7f 100644 --- a/service/pixelated/resources/user_settings_resource.py +++ b/service/pixelated/resources/user_settings_resource.py @@ -31,10 +31,12 @@ def render_GET(self, request): def finish_request(key): _fingerprint = key.fingerprint - respond_json_deferred({'account_email': _account_email, 'fingerprint': _fingerprint}, request) + respond_json_deferred( + {'account_email': _account_email, 'fingerprint': _fingerprint}, request) def key_not_found(_): - respond_json_deferred({'account_email': _account_email, 'fingerprint': FINGERPRINT_NOT_FOUND}, request) + respond_json_deferred( + {'account_email': _account_email, 'fingerprint': FINGERPRINT_NOT_FOUND}, request) d = self.keymanager(request).get_key(_account_email) d.addCallback(finish_request) diff --git a/service/pixelated/support/__init__.py b/service/pixelated/support/__init__.py index 0685f48dd..7806b0ecc 100644 --- a/service/pixelated/support/__init__.py +++ b/service/pixelated/support/__init__.py @@ -35,7 +35,8 @@ def _stop_stopwatch(start): if time_duration < 0.00000001: # avoid division by zero time_duration = 0.00000001 - estimate_percent_io = ((time_duration - clock_duration) / time_duration) * 100.0 + estimate_percent_io = ( + (time_duration - clock_duration) / time_duration) * 100.0 return time_duration, clock_duration, estimate_percent_io @@ -48,8 +49,10 @@ def wrapper(*args, **kwds): result = f(*args, **kwds) - time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) - log.info('Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch( + start) + log.info('Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % ( + time_duration, clock_duration, estimate_percent_io, f)) return result @@ -59,8 +62,10 @@ def wrapper(*args, **kwds): def log_time_deferred(f): def log_time(result, start): - time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) - log.info('after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch( + start) + log.info('after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % ( + time_duration, clock_duration, estimate_percent_io, f)) return result @wraps(f) diff --git a/service/pixelated/support/clock.py b/service/pixelated/support/clock.py index f47c20c60..2d947bb22 100644 --- a/service/pixelated/support/clock.py +++ b/service/pixelated/support/clock.py @@ -13,4 +13,5 @@ def stop(self, fresh=False, user=None): end = datetime.now() with open(expanduser('~/MetricsTime'), 'a') as f: flag = ' fresh-account' if fresh else '' - f.write('{} {:.5f} {} {}\n'.format((self.user or user or 'Unknown'), (end - self.start).total_seconds(), self.label, flag)) + f.write('{} {:.5f} {} {}\n'.format((self.user or user or 'Unknown'), + (end - self.start).total_seconds(), self.label, flag)) diff --git a/service/pixelated/support/encrypted_file_storage.py b/service/pixelated/support/encrypted_file_storage.py index a1dbffa82..d4e06e0bf 100644 --- a/service/pixelated/support/encrypted_file_storage.py +++ b/service/pixelated/support/encrypted_file_storage.py @@ -29,6 +29,7 @@ class DelayedCloseBytesIO(io.BytesIO): + def __init__(self, name): super(DelayedCloseBytesIO, self).__init__() self._name = name @@ -43,6 +44,7 @@ def explicit_close(self): class DelayedCloseStructFile(StructFile): + def __init__(self, fileobj, name=None, onclose=None): super(DelayedCloseStructFile, self).__init__(fileobj, name, onclose) @@ -60,6 +62,7 @@ def close(self): class EncryptedFileStorage(FileStorage): + def __init__(self, path, masterkey=None): FileStorage.__init__(self, path, supports_mmap=False) self.masterkey = masterkey[:32] @@ -72,7 +75,8 @@ def open_file(self, name, **kwargs): return self._open_encrypted_file(name) def create_file(self, name, excl=False, mode="w+b", **kwargs): - f = DelayedCloseStructFile(DelayedCloseBytesIO(name), name=name, onclose=self._encrypt_index_on_close(name)) + f = DelayedCloseStructFile(DelayedCloseBytesIO( + name), name=name, onclose=self._encrypt_index_on_close(name)) f.is_real = False self._open_files[name] = f return f @@ -100,10 +104,12 @@ def encrypt(self, content): return ''.join((mac, iv, ciphertext)) def decrypt(self, payload): - payload_mac, iv, ciphertext = payload[:32], payload[32:57], payload[57:] + payload_mac, iv, ciphertext = payload[ + :32], payload[32:57], payload[57:] generated_mac = self.gen_mac(iv, ciphertext) if sha256(payload_mac).digest() != sha256(generated_mac).digest(): - raise Exception("EncryptedFileStorage - Error opening file. Wrong MAC") + raise Exception( + "EncryptedFileStorage - Error opening file. Wrong MAC") return decrypt_sym(ciphertext, self.masterkey, iv) def _encrypt_index_on_close(self, name): diff --git a/service/pixelated/support/loglinegenerator.py b/service/pixelated/support/loglinegenerator.py index 3b7820fb4..06fdfcd85 100644 --- a/service/pixelated/support/loglinegenerator.py +++ b/service/pixelated/support/loglinegenerator.py @@ -2,6 +2,7 @@ class ILogLineGenerator(Interface): + def getLogLine(self): """ Return a string that will be logged, or None. This method will be called every second. """ diff --git a/service/pixelated/support/mail_generator.py b/service/pixelated/support/mail_generator.py index e52323703..e01274f38 100644 --- a/service/pixelated/support/mail_generator.py +++ b/service/pixelated/support/mail_generator.py @@ -109,8 +109,10 @@ def __init__(self, receiver, domain_name, sample_mail_list, random=None): self._subjects, self._bodies = load_all_mails(sample_mail_list) self._potential_tags = search_for_tags(' '.join(self._bodies)) - self._subject_markov = MarkovGenerator(self._subjects, random=self._random) - self._body_markov = MarkovGenerator(self._bodies, random=self._random, add_paragraph_on_empty_chain=True) + self._subject_markov = MarkovGenerator( + self._subjects, random=self._random) + self._body_markov = MarkovGenerator( + self._bodies, random=self._random, add_paragraph_on_empty_chain=True) def generate_mail(self): body = self._body_markov.generate(150) @@ -140,7 +142,8 @@ def _random_signature_state(self): return self._random.choice(['could not verify', 'valid']) def _random_from(self): - name = self._random.choice(filter(lambda name: name != self._receiver, MailGenerator.NAMES)) + name = self._random.choice( + filter(lambda name: name != self._receiver, MailGenerator.NAMES)) return '%s@%s' % (name, self._domain_name) diff --git a/service/pixelated/support/replier.py b/service/pixelated/support/replier.py index 0bcb1a27b..83324eae7 100644 --- a/service/pixelated/support/replier.py +++ b/service/pixelated/support/replier.py @@ -8,9 +8,12 @@ def generate_recipients(sender, to, ccs, current_user): to = remove_duplicates(to) ccs = remove_duplicates(ccs) - result['single'] = swap_recipient_if_needed(sender, remove_address(to, current_user), current_user) - result['all']['to-field'] = remove_address(to, current_user) if len(to) > 1 else to - result['all']['cc-field'] = remove_address(ccs, current_user) if len(ccs) > 1 else ccs + result['single'] = swap_recipient_if_needed( + sender, remove_address(to, current_user), current_user) + result['all'][ + 'to-field'] = remove_address(to, current_user) if len(to) > 1 else to + result['all'][ + 'cc-field'] = remove_address(ccs, current_user) if len(ccs) > 1 else ccs return result diff --git a/service/test/functional/features/environment.py b/service/test/functional/features/environment.py index d49016b63..5bf198998 100644 --- a/service/test/functional/features/environment.py +++ b/service/test/functional/features/environment.py @@ -73,7 +73,8 @@ def _setup_webdriver(context): try: context.browser = supported_webdrivers[browser]() except KeyError: - raise UnsuportedWebDriverError('{} is not a supported webdriver'.format(browser)) + raise UnsuportedWebDriverError( + '{} is not a supported webdriver'.format(browser)) context.browser.set_window_size(1280, 1024) context.browser.implicitly_wait(DEFAULT_IMPLICIT_WAIT_TIMEOUT_IN_S) diff --git a/service/test/functional/features/steps/attachments.py b/service/test/functional/features/steps/attachments.py index 8852b7873..1df2dcd29 100644 --- a/service/test/functional/features/steps/attachments.py +++ b/service/test/functional/features/steps/attachments.py @@ -41,7 +41,8 @@ def build_mail_with_attachment(subject): mail['Subject'] = subject mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') mail.attach(attachment) return mail @@ -54,13 +55,15 @@ def load_mail_into_soledad(context, mail): @then(u'I see the mail has an attachment') def step_impl(context): - attachments_list = find_elements_by_css_selector(context, '.mail-read-view__attachments-item') + attachments_list = find_elements_by_css_selector( + context, '.mail-read-view__attachments-item') assert len(attachments_list) == 1 @when(u'I find an attachment icon') def find_icon(context): - assert find_element_by_css_selector(context, '#attachment-button .fa.fa-paperclip') + assert find_element_by_css_selector( + context, '#attachment-button .fa.fa-paperclip') @when(u'I try to upload a file bigger than 5MB') @@ -74,7 +77,8 @@ def upload_big_file(context): @then(u'I see an upload error message') def show_upload_error_message(context): - upload_error_message = find_elements_by_css_selector(context, '#upload-error-message') + upload_error_message = find_elements_by_css_selector( + context, '#upload-error-message') error_messages = [e.text for e in upload_error_message] assert "Upload failed. This file exceeds the 1MB limit." in error_messages @@ -89,7 +93,8 @@ def dismiss_error_message(context): @then(u'It should not show the error message anymore') def should_not_show_upload_error_message(context): - upload_error_message_is_present = page_has_css(context, '#upload-error-message') + upload_error_message_is_present = page_has_css( + context, '#upload-error-message') assert not upload_error_message_is_present @@ -98,17 +103,20 @@ def upload_attachment(context): base_dir = "test/functional/features/files/" fname = "5mb.data" fill_by_css_selector(context, '#fileupload', base_dir + fname) - attachment_list_item = find_element_by_css_selector(context, '#attachment-list-item li a') + attachment_list_item = find_element_by_css_selector( + context, '#attachment-list-item li a') assert attachment_list_item.text == "%s (5.00 Mb)" % fname @when(u'remove the file') def click_remove_icon(context): - remove_icon = find_element_by_css_selector(context, '#attachment-list-item i.remove-icon') + remove_icon = find_element_by_css_selector( + context, '#attachment-list-item i.remove-icon') remove_icon.click() @then(u'I should not see it attached') def assert_attachment_removed(context): - attachments_list_li = context.browser.find_elements_by_css_selector('#attachment-list-item li a') + attachments_list_li = context.browser.find_elements_by_css_selector( + '#attachment-list-item li a') assert len(attachments_list_li) == 0 diff --git a/service/test/functional/features/steps/common.py b/service/test/functional/features/steps/common.py index edfe2a50e..eebcd0ac7 100644 --- a/service/test/functional/features/steps/common.py +++ b/service/test/functional/features/steps/common.py @@ -29,6 +29,7 @@ class ImplicitWait(object): + def __init__(self, context, timeout=5.0): self._context = context self._timeout = timeout @@ -37,7 +38,8 @@ def __enter__(self): self._context.browser.implicitly_wait(self._timeout) def __exit__(self, exc_type, exc_val, exc_tb): - self._context.browser.implicitly_wait(DEFAULT_IMPLICIT_WAIT_TIMEOUT_IN_S) + self._context.browser.implicitly_wait( + DEFAULT_IMPLICIT_WAIT_TIMEOUT_IN_S) def wait_until_element_is_invisible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): @@ -46,11 +48,13 @@ def wait_until_element_is_invisible_by_locator(context, locator_tuple, timeout=T def wait_for_loading_to_finish(context, timeout=TIMEOUT_IN_S): - wait_until_element_is_invisible_by_locator(context, (By.ID, 'loading'), timeout) + wait_until_element_is_invisible_by_locator( + context, (By.ID, 'loading'), timeout) def wait_for_user_alert_to_disapear(context, timeout=TIMEOUT_IN_S): - wait_until_element_is_invisible_by_locator(context, (By.ID, 'user-alerts'), timeout) + wait_until_element_is_invisible_by_locator( + context, (By.ID, 'user-alerts'), timeout) def _wait_until_elements_are_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): @@ -66,7 +70,8 @@ def _wait_until_element_is_visible_by_locator(context, locator_tuple, timeout=TI def wait_for_condition(context, predicate_func, timeout=TIMEOUT_IN_S, poll_frequency=0.1): - wait = WebDriverWait(context.browser, timeout, poll_frequency=poll_frequency) + wait = WebDriverWait(context.browser, timeout, + poll_frequency=poll_frequency) wait.until(predicate_func) @@ -76,7 +81,8 @@ def fill_by_xpath(context, xpath, text): def fill_by_css_selector(context, css_selector, text, timeout=TIMEOUT_IN_S): - field = find_element_by_css_selector(context, css_selector, timeout=timeout) + field = find_element_by_css_selector( + context, css_selector, timeout=timeout) field.send_keys(text) @@ -138,7 +144,8 @@ def execute_ignoring_staleness(func, timeout=TIMEOUT_IN_S): return func() except StaleElementReferenceException: pass - raise TimeoutException('did not solve stale state until timeout %f' % timeout) + raise TimeoutException( + 'did not solve stale state until timeout %f' % timeout) def click_button(context, title, element='button'): diff --git a/service/test/functional/features/steps/compose.py b/service/test/functional/features/steps/compose.py index 1dab1b6d9..15175a0e7 100644 --- a/service/test/functional/features/steps/compose.py +++ b/service/test/functional/features/steps/compose.py @@ -52,11 +52,13 @@ def send_impl(context): @when(u'I toggle the cc and bcc fields') def collapse_cc_bcc_fields(context): - cc_and_bcc_chevron = find_element_by_css_selector(context, '#cc-bcc-collapse') + cc_and_bcc_chevron = find_element_by_css_selector( + context, '#cc-bcc-collapse') cc_and_bcc_chevron.click() def _enter_recipient(context, recipients_field, to_type): recipients_field = recipients_field.lower() - field = find_element_by_css_selector(context, '#recipients-%s-area .tt-input' % recipients_field) + field = find_element_by_css_selector( + context, '#recipients-%s-area .tt-input' % recipients_field) field.send_keys(to_type) diff --git a/service/test/functional/features/steps/data_setup.py b/service/test/functional/features/steps/data_setup.py index 3a10345ce..88b3069dd 100644 --- a/service/test/functional/features/steps/data_setup.py +++ b/service/test/functional/features/steps/data_setup.py @@ -27,7 +27,8 @@ def add_mail_impl(context): input_mail = MailBuilder().with_subject(subject).build_input_mail() load_mail_into_soledad(context, input_mail) - wait_for_condition(context, lambda _: context.single_user_client.search_engine.search(subject)[1] > 0, poll_frequency=0.1) + wait_for_condition(context, lambda _: context.single_user_client.search_engine.search( + subject)[1] > 0, poll_frequency=0.1) context.last_subject = subject @@ -39,7 +40,8 @@ def add_mail_to_user_inbox(context, username): input_mail = MailBuilder().with_subject(subject).build_input_mail() load_mail_into_user_account(context, input_mail, username) - wait_for_condition(context, lambda _: context.multi_user_client.account_for(username).search_engine.search(subject)[1] > 0, poll_frequency=0.1) + wait_for_condition(context, lambda _: context.multi_user_client.account_for( + username).search_engine.search(subject)[1] > 0, poll_frequency=0.1) context.last_subject = subject diff --git a/service/test/functional/features/steps/mail_list.py b/service/test/functional/features/steps/mail_list.py index 227aa9ed5..1c44ec166 100644 --- a/service/test/functional/features/steps/mail_list.py +++ b/service/test/functional/features/steps/mail_list.py @@ -60,7 +60,8 @@ def impl(context): @when('I open the first mail in the mail list') def impl(context): # it seems page is often still loading so staleness exceptions happen often - context.current_mail_id = 'mail-' + execute_ignoring_staleness(lambda: get_first_email(context).get_attribute('href').split('/')[-1]) + context.current_mail_id = 'mail-' + execute_ignoring_staleness( + lambda: get_first_email(context).get_attribute('href').split('/')[-1]) execute_ignoring_staleness(lambda: get_first_email(context).click()) @@ -89,7 +90,8 @@ def impl(context): @given('I have mails') @then(u'I have mails') def impl(context): - emails = find_elements_by_css_selector(context, '.mail-list-entry', timeout=40) + emails = find_elements_by_css_selector( + context, '.mail-list-entry', timeout=40) assert len(emails) > 0 @@ -106,7 +108,8 @@ def impl(context): @when('I delete the email') def impl(context): - mail_id = find_element_by_css_selector(context, '.mail-list-entry').get_attribute('id') + mail_id = find_element_by_css_selector( + context, '.mail-list-entry').get_attribute('id') find_element_by_css_selector(context, '#%s input' % mail_id).click() find_element_by_id(context, 'delete-selected').click() diff --git a/service/test/functional/features/steps/mail_view.py b/service/test/functional/features/steps/mail_view.py index 65959b700..1914c52da 100644 --- a/service/test/functional/features/steps/mail_view.py +++ b/service/test/functional/features/steps/mail_view.py @@ -27,7 +27,8 @@ @then('I see that the subject reads \'{subject}\'') def impl(context, subject): - e = find_element_by_css_selector(context, '#mail-view .mail-read-view__header-subject') + e = find_element_by_css_selector( + context, '#mail-view .mail-read-view__header-subject') assert e.text == subject @@ -43,7 +44,8 @@ def impl(context, expected_body): @then('that email has the \'{tag}\' tag') def impl(context, tag): find_element_by_css_selector(context, '#mail-view .tagsArea .tag') - elements = find_elements_by_css_selector(context, '#mail-view .tagsArea .tag') + elements = find_elements_by_css_selector( + context, '#mail-view .tagsArea .tag') tags = [e.text for e in elements] assert tag in tags @@ -69,7 +71,8 @@ def impl(context): @when('I try to delete the first mail') def impl(context): context.execute_steps(u"When I open the first mail in the mail list") - find_element_by_css_selector(context, '#mail-view #view-more-actions').click() + find_element_by_css_selector( + context, '#mail-view #view-more-actions').click() context.browser.execute_script("$('#delete-button-top').click();") e = find_element_by_css_selector(context, '.message-panel__growl--success') @@ -105,8 +108,10 @@ def impl(context): @then('I see the mail has a cc and a bcc recipient') def impl(context): - cc = find_element_by_css_selector(context, '.mail-read-view__header-recipients .cc') - bcc = find_element_by_css_selector(context, '.mail-read-view__header-recipients .bcc') + cc = find_element_by_css_selector( + context, '.mail-read-view__header-recipients .cc') + bcc = find_element_by_css_selector( + context, '.mail-read-view__header-recipients .bcc') assert cc is not None assert bcc is not None diff --git a/service/test/functional/features/steps/search.py b/service/test/functional/features/steps/search.py index 5a6d2d1ca..dacb72e2e 100644 --- a/service/test/functional/features/steps/search.py +++ b/service/test/functional/features/steps/search.py @@ -23,7 +23,8 @@ @when('I search for a mail with the words "{search_term}"') def impl(context, search_term): - search_field = find_element_by_css_selector(context, '#search-trigger input[type="search"]') + search_field = find_element_by_css_selector( + context, '#search-trigger input[type="search"]') ActionChains(context.browser)\ .send_keys_to_element(search_field, search_term)\ .send_keys_to_element(search_field, Keys.ENTER)\ diff --git a/service/test/functional/features/steps/tag_list.py b/service/test/functional/features/steps/tag_list.py index daea416dd..8d6941675 100644 --- a/service/test/functional/features/steps/tag_list.py +++ b/service/test/functional/features/steps/tag_list.py @@ -56,7 +56,8 @@ def impl(context, tag): e = find_element_by_id(context, 'tag-%s' % tag) e.click() - find_element_by_css_selector(context, ".mail-list-entry__item[href*='%s']" % tag) + find_element_by_css_selector( + context, ".mail-list-entry__item[href*='%s']" % tag) success = True except TimeoutException: pass diff --git a/service/test/integration/test_contacts.py b/service/test/integration/test_contacts.py index 946818fd1..3e3f740a1 100644 --- a/service/test/integration/test_contacts.py +++ b/service/test/integration/test_contacts.py @@ -34,7 +34,8 @@ def test_TO_CC_and_BCC_fields_are_being_searched(self): @defer.inlineCallbacks def test_FROM_address_is_being_searched(self): - input_mail = MailBuilder().with_tags(['important']).with_from('Formatted Sender ').build_input_mail() + input_mail = MailBuilder().with_tags(['important']).with_from( + 'Formatted Sender ').build_input_mail() yield self.app_test_client.add_mail_to_inbox(input_mail) contacts = yield self.app_test_client.get_contacts(query='Sender') @@ -57,7 +58,8 @@ def test_trash_and_drafts_mailboxes_are_being_ignored(self): @defer.inlineCallbacks def test_deduplication_on_same_mail_address_using_largest(self): - input_mail = MailBuilder().with_tags(['important']).with_from('sender@from.com').build_input_mail() + input_mail = MailBuilder().with_tags(['important']).with_from( + 'sender@from.com').build_input_mail() formatted_input_mail = MailBuilder().with_tags(['important']) formatted_input_mail.with_to('Recipient Principal ') diff --git a/service/test/integration/test_draft_service.py b/service/test/integration/test_draft_service.py index 159de605f..be8b1010f 100644 --- a/service/test/integration/test_draft_service.py +++ b/service/test/integration/test_draft_service.py @@ -36,7 +36,8 @@ def test_update_draft(self): input_mail = MailBuilder().with_body('some test text').build_input_mail() saved_mail = yield self.app_test_client.mail_store.add_mail('DRAFTS', input_mail.raw) draft_id = saved_mail.ident - new_email = MailBuilder().with_body('other test text').with_ident(draft_id).build_input_mail() + new_email = MailBuilder().with_body( + 'other test text').with_ident(draft_id).build_input_mail() stored_draft = yield self.app_test_client.draft_service.process_draft(draft_id, new_email) diff --git a/service/test/integration/test_drafts.py b/service/test/integration/test_drafts.py index 657cfab16..2950983d6 100644 --- a/service/test/integration/test_drafts.py +++ b/service/test/integration/test_drafts.py @@ -28,14 +28,16 @@ def tearDown(self): def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self): # act as if sending the mail by SMTP succeeded sendmail_deferred = defer.Deferred() - when(self.app_test_client.mail_sender).sendmail(any()).thenReturn(sendmail_deferred) + when(self.app_test_client.mail_sender).sendmail( + any()).thenReturn(sendmail_deferred) # creates one draft first_draft = MailBuilder().with_subject('First draft').build_json() first_draft_ident = (yield self.app_test_client.put_mail(first_draft)[0])['ident'] # sends an updated version of the draft - second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json() + second_draft = MailBuilder().with_subject( + 'Second draft').with_ident(first_draft_ident).build_json() deferred_res = self.post_mail(second_draft) sendmail_deferred.callback(None) # SMTP succeeded @@ -45,7 +47,8 @@ def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self): sent_mails = yield self.app_test_client.get_mails_by_tag('sent') drafts = yield self.app_test_client.get_mails_by_tag('drafts') - # make sure there is one email in the sent mailbox and it is the second draft + # 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) @@ -56,7 +59,8 @@ def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self): def test_post_sends_mail_even_when_draft_does_not_exist(self): # act as if sending the mail by SMTP succeeded sendmail_deferred = defer.Deferred() - when(self.app_test_client.mail_sender).sendmail(any()).thenReturn(sendmail_deferred) + when(self.app_test_client.mail_sender).sendmail( + any()).thenReturn(sendmail_deferred) first_draft = MailBuilder().with_subject('First draft').build_json() res = self.post_mail(first_draft) @@ -87,7 +91,8 @@ def test_put_updates_draft_if_it_already_exists(self): draft = MailBuilder().with_subject('First draft').build_json() draft_ident = (yield self.app_test_client.put_mail(draft)[0])['ident'] - updated_draft = MailBuilder().with_subject('First draft edited').with_ident(draft_ident).build_json() + updated_draft = MailBuilder().with_subject( + 'First draft edited').with_ident(draft_ident).build_json() yield self.app_test_client.put_mail(updated_draft)[0] drafts = yield self.app_test_client.get_mails_by_tag('drafts') diff --git a/service/test/integration/test_feedback_service.py b/service/test/integration/test_feedback_service.py index ff6593965..30f5b609c 100644 --- a/service/test/integration/test_feedback_service.py +++ b/service/test/integration/test_feedback_service.py @@ -17,7 +17,9 @@ def test_open_ticket(self): with HTTMock(google_mock): yield self.start_client() self.feedback_service.FEEDBACK_URL = "https://some.leap-provider.tld/tickets" - when(self.leap_session).account_email().thenReturn("text@pixelated-project.org") - response = self.feedback_service.open_ticket("Pixelated is awesome!") + when(self.leap_session).account_email().thenReturn( + "text@pixelated-project.org") + response = self.feedback_service.open_ticket( + "Pixelated is awesome!") self.assertEquals(response.status_code, 200) diff --git a/service/test/integration/test_leap_mailstore.py b/service/test/integration/test_leap_mailstore.py index d331c22a5..8d1bc23e9 100644 --- a/service/test/integration/test_leap_mailstore.py +++ b/service/test/integration/test_leap_mailstore.py @@ -30,7 +30,8 @@ def measure(): end_time = time.time() end_clock = time.clock() # print 'time: %10d - %10d = %10d' % (start_time, end_time, start_time - end_time) - # print 'clock: %10d - %10d = %10d' % (start_clock, end_clock, start_clock - end_clock) + # print 'clock: %10d - %10d = %10d' % (start_clock, end_clock, start_clock + # - end_clock) class LeapMailStoreTest(SoledadTestBase): @@ -44,7 +45,8 @@ def test_get_mail_for_measuring(self): self.maxDiff = None mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set([]), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set( + []), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} with measure(): result = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) @@ -56,7 +58,8 @@ def test_get_mail_with_body(self): self.maxDiff = None mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set([]), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set( + []), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} result = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) self.assertIsNotNone(result) @@ -67,7 +70,8 @@ def test_get_mail_with_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') input_mail.attach(attachment) mail = yield self.app_test_client.mail_store.add_mail('INBOX', input_mail.as_string()) @@ -79,12 +83,14 @@ def test_attachment_name(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') input_mail.attach(attachment) mail = yield self.app_test_client.mail_store.add_mail('INBOX', input_mail.as_string()) fetched_mail = yield self.app_test_client.mail_store.get_mail(mail.ident, include_body=True) - fetched_attachment_name = fetched_mail.as_dict()['attachments'][0]['name'] + fetched_attachment_name = fetched_mail.as_dict()['attachments'][ + 0]['name'] self.assertEqual(fetched_attachment_name, 'filename.txt') @defer.inlineCallbacks @@ -92,31 +98,37 @@ def test_attachment_name_with_lowercase_header(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('content-disposition', 'attachment', filename='filename.txt') + attachment.add_header('content-disposition', + 'attachment', filename='filename.txt') input_mail.attach(attachment) mail = yield self.app_test_client.mail_store.add_mail('INBOX', input_mail.as_string()) fetched_mail = yield self.app_test_client.mail_store.get_mail(mail.ident, include_body=True) - fetched_attachment_name = fetched_mail.as_dict()['attachments'][0]['name'] + fetched_attachment_name = fetched_mail.as_dict()['attachments'][ + 0]['name'] self.assertEqual(fetched_attachment_name, 'filename.txt') @defer.inlineCallbacks def test_round_trip_through_soledad_does_not_modify_content(self): mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set([])} + expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': { + u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set([])} mail = yield self.app_test_client.mail_store.add_mail('INBOX', mail.as_string()) fetched_mail = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) - self.assertEqual(expected_mail_dict['header'], mail.as_dict()['header']) - self.assertEqual(expected_mail_dict['header'], fetched_mail.as_dict()['header']) + self.assertEqual(expected_mail_dict[ + 'header'], mail.as_dict()['header']) + self.assertEqual(expected_mail_dict[ + 'header'], fetched_mail.as_dict()['header']) @defer.inlineCallbacks def test_round_trip_through_soledad_keeps_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') input_mail.attach(attachment) mail = yield self.app_test_client.mail_store.add_mail('INBOX', input_mail.as_string()) @@ -131,7 +143,8 @@ def test_all_mails(self): mails = yield self.app_test_client.mail_store.all_mails() self.assertEqual(1, len(mails)) - self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + self.assertEqual( + 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) @defer.inlineCallbacks def test_add_and_remove_mail(self): @@ -151,7 +164,8 @@ def test_add_and_remove_mail(self): def test_add_add_mail_twice(self): yield self.adaptor.initialize_store(self.app_test_client.soledad) mail = load_mail_from_file('mbox00000000', enforceUniqueMessageId=True) - mail2 = load_mail_from_file('mbox00000000', enforceUniqueMessageId=True) + mail2 = load_mail_from_file( + 'mbox00000000', enforceUniqueMessageId=True) yield self.app_test_client.mail_store.add_mailbox('INBOX') msg1 = yield self.app_test_client.mail_store.add_mail('INBOX', mail.as_string()) diff --git a/service/test/integration/test_mark_as_read_unread.py b/service/test/integration/test_mark_as_read_unread.py index 18c3ddc2d..dcbcd0d34 100644 --- a/service/test/integration/test_mark_as_read_unread.py +++ b/service/test/integration/test_mark_as_read_unread.py @@ -48,8 +48,10 @@ def test_mark_single_as_unread(self): @defer.inlineCallbacks def test_mark_many_mails_as_unread(self): - input_mail = MailBuilder().with_status([Status.SEEN]).build_input_mail() - input_mail2 = MailBuilder().with_status([Status.SEEN]).build_input_mail() + input_mail = MailBuilder().with_status( + [Status.SEEN]).build_input_mail() + input_mail2 = MailBuilder().with_status( + [Status.SEEN]).build_input_mail() mail1 = yield self.app_test_client.add_mail_to_inbox(input_mail) mail2 = yield self.app_test_client.add_mail_to_inbox(input_mail2) diff --git a/service/test/integration/test_multi_user_login.py b/service/test/integration/test_multi_user_login.py index fe4565834..51913dff3 100644 --- a/service/test/integration/test_multi_user_login.py +++ b/service/test/integration/test_multi_user_login.py @@ -38,8 +38,10 @@ def test_logged_in_users_sees_resources(self): mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': [], 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} - response, request = self.app_test_client.get("/mail/%s" % mail_id, from_request=login_request) + expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [ + ], 'tags': [], 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + response, request = self.app_test_client.get( + "/mail/%s" % mail_id, from_request=login_request) response = yield response self.assertEqual(200, request.code) @@ -48,7 +50,8 @@ def test_logged_in_users_sees_resources(self): @defer.inlineCallbacks def test_wrong_credentials_cannot_access_resources(self): - response, login_request = self.app_test_client.login('username', 'wrong_password') + response, login_request = self.app_test_client.login( + 'username', 'wrong_password') response_str = yield response self.assertEqual(401, login_request.responseCode) self.assertIn('Invalid username or password', login_request.written) diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py index b46d40d5d..8b0970324 100644 --- a/service/test/integration/test_retrieve_attachment.py +++ b/service/test/integration/test_retrieve_attachment.py @@ -25,6 +25,7 @@ class RetrieveAttachmentTest(SoledadTestBase): + @defer.inlineCallbacks def test_attachment_content_is_retrieved(self): attachment_id, input_mail = self._create_mail_with_attachment() @@ -38,8 +39,10 @@ def test_attachment_content_is_retrieved(self): self.assertEqual(200, req.code) self.assertEquals('pretend to be binary attachment data', attachment) - self.assertEquals(expected_content_disposition, req.responseHeaders.getRawHeaders('content-disposition')[0]) - self.assertEquals(expected_content_type, req.responseHeaders.getRawHeaders('content-type')[0]) + self.assertEquals(expected_content_disposition, + req.responseHeaders.getRawHeaders('content-disposition')[0]) + self.assertEquals(expected_content_type, + req.responseHeaders.getRawHeaders('content-type')[0]) @defer.inlineCallbacks def test_should_retrieve_attachment_even_if_xsrf_token_not_passed(self): @@ -57,14 +60,17 @@ def test_should_retrieve_attachment_even_if_xsrf_token_not_passed(self): self.assertEqual(200, req.code) self.assertEquals('pretend to be binary attachment data', attachment) - self.assertEquals(expected_content_disposition, req.responseHeaders.getRawHeaders('content-disposition')[0]) - self.assertEquals(expected_content_type, req.responseHeaders.getRawHeaders('content-type')[0]) + self.assertEquals(expected_content_disposition, + req.responseHeaders.getRawHeaders('content-disposition')[0]) + self.assertEquals(expected_content_type, + req.responseHeaders.getRawHeaders('content-type')[0]) def _create_mail_with_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='file name.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='file name.txt') attachment.add_header('Content-Type', 'text/plain') input_mail.attach(attachment) attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A' @@ -82,14 +88,16 @@ def test_post_new_attachment(self): content_type = 'text/plain' filename = 'filename.txt' data = 'pretend to be binary attachment data' - file = MultipartParam('attachment', value=data, filename=filename, filetype=content_type) + file = MultipartParam('attachment', value=data, + filename=filename, filetype=content_type) datagen, headers = multipart_encode([file]) post_data = "".join(datagen) _, req = yield self.app_test_client.post_attachment(post_data, headers) self.assertEqual(201, req.code) - self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', req.responseHeaders.getRawHeaders('location')[0]) + self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', + req.responseHeaders.getRawHeaders('location')[0]) response_json = {'ident': 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', 'content-type': content_type, 'name': filename, diff --git a/service/test/integration/test_tags.py b/service/test/integration/test_tags.py index 555a7382a..ef4fc2864 100644 --- a/service/test/integration/test_tags.py +++ b/service/test/integration/test_tags.py @@ -47,7 +47,8 @@ def test_use_old_casing_when_same_tag_with_different_casing_is_posted(self): mails = yield self.app_test_client.get_mails_by_tag('ImPoRtAnT') self.assertEquals({'ImPoRtAnT'}, set(mails[0].tags)) - another_input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() + another_input_mail = MailBuilder().with_subject( + 'Mail with tags').build_input_mail() another_mail = yield self.app_test_client.add_mail_to_inbox(another_input_mail) yield self.app_test_client.post_tags(another_mail.ident, self._tags_json(['IMPORTANT'])) mails = yield self.app_test_client.get_mails_by_tag('IMPORTANT') @@ -91,7 +92,8 @@ def test_addition_of_reserved_tags_is_not_allowed(self): for tag in SPECIAL_TAGS: response = yield self.app_test_client.post_tags(mail.ident, self._tags_json([tag.name.upper()])) - self.assertEquals("None of the following words can be used as tags: %s" % tag.name, response) + self.assertEquals( + "None of the following words can be used as tags: %s" % tag.name, response) mail = yield self.app_test_client.mail_store.get_mail(mail.ident) self.assertNotIn('drafts', mail.tags) diff --git a/service/test/integration/test_users_count.py b/service/test/integration/test_users_count.py index a03adacf1..515dd35f8 100644 --- a/service/test/integration/test_users_count.py +++ b/service/test/integration/test_users_count.py @@ -45,5 +45,6 @@ def test_online_users_count_uses_leap_auth_privileges(self): yield response self.assertEqual(200, request.code) # redirected - self.assertEqual('{"count": 1}', request.getWrittenData()) # redirected + self.assertEqual( + '{"count": 1}', request.getWrittenData()) # redirected verify(self.app_test_client.user_auth).is_admin() diff --git a/service/test/perf/contacts/test_Contacts.py b/service/test/perf/contacts/test_Contacts.py index ade975918..12f4e586a 100644 --- a/service/test/perf/contacts/test_Contacts.py +++ b/service/test/perf/contacts/test_Contacts.py @@ -38,9 +38,12 @@ def setUpBench(self): proxy = Proxy(proxy_port='8889', app_port='4567') # setup data - client.add_multiple_to_mailbox(10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com') - client.add_multiple_to_mailbox(10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com') - client.add_multiple_to_mailbox(10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com') + client.add_multiple_to_mailbox( + 10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com') + client.add_multiple_to_mailbox( + 10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com') + client.add_multiple_to_mailbox( + 10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com') self.call_to_terminate = proxy.run_on_a_thread() diff --git a/service/test/perf/mails/test_Mails.py b/service/test/perf/mails/test_Mails.py index a7a918ade..3ad0397a3 100644 --- a/service/test/perf/mails/test_Mails.py +++ b/service/test/perf/mails/test_Mails.py @@ -24,7 +24,8 @@ class Mails(FunkLoadTestCase): def setUpBench(self): client = AppTestClient() client.add_multiple_to_mailbox(100, 'INBOX') - self.call_to_terminate = client.run_on_a_thread(logfile='results/app.log') + self.call_to_terminate = client.run_on_a_thread( + logfile='results/app.log') def tearDownBench(self): self.call_to_terminate() diff --git a/service/test/perf/search/test_Search.py b/service/test/perf/search/test_Search.py index 5e646edd6..3f58e5aa3 100644 --- a/service/test/perf/search/test_Search.py +++ b/service/test/perf/search/test_Search.py @@ -28,11 +28,15 @@ class Search(FunkLoadTestCase): def setUpBench(self): # setup data - CLIENT.add_multiple_to_mailbox(10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com', tags=['inbox']) - CLIENT.add_multiple_to_mailbox(10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com', tags=['trash']) - CLIENT.add_multiple_to_mailbox(10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com', tags=['drafts']) + CLIENT.add_multiple_to_mailbox( + 10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com', tags=['inbox']) + CLIENT.add_multiple_to_mailbox( + 10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com', tags=['trash']) + CLIENT.add_multiple_to_mailbox( + 10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com', tags=['drafts']) - self.call_to_terminate = CLIENT.run_on_a_thread(logfile='results/app.log') + self.call_to_terminate = CLIENT.run_on_a_thread( + logfile='results/app.log') def tearDownBench(self): self.call_to_terminate() @@ -57,7 +61,8 @@ def test_search(self): self.get(contacts_url, description='Query for contacts') idents = self.idents_by_tag(tags[i % 3]) tag_data = Data('application/json', '{"newtags":["newtag%s"]}' % i) - self.post(mail_tags_url % idents[i % 3], tag_data, description='Change tags on a mail') + self.post(mail_tags_url % idents[ + i % 3], tag_data, description='Change tags on a mail') self.idents_by_tag(tags[i % 3]) self.get(all_tags_url, description='Query for all tags listing') diff --git a/service/test/perf/tags/test_Tags.py b/service/test/perf/tags/test_Tags.py index 8f62b4c9a..ddde29d2d 100644 --- a/service/test/perf/tags/test_Tags.py +++ b/service/test/perf/tags/test_Tags.py @@ -30,7 +30,8 @@ def setUpBench(self): client.add_multiple_to_mailbox(10, 'INBOX', tags=['tag3']) client.add_multiple_to_mailbox(10, 'INBOX', tags=['tag4']) - self.call_to_terminate = client.run_on_a_thread(logfile='results/app.log') + self.call_to_terminate = client.run_on_a_thread( + logfile='results/app.log') def tearDownBench(self): self.call_to_terminate() diff --git a/service/test/reactor/create_users.py b/service/test/reactor/create_users.py index 18dc9dcf6..57ce80085 100755 --- a/service/test/reactor/create_users.py +++ b/service/test/reactor/create_users.py @@ -21,6 +21,7 @@ class User(object): + def __init__(self, number, provider, certificate): self._username = 'loadtest%d' % number self._password = 'password_%d' % number @@ -67,9 +68,12 @@ def failed(err, username): def _parse_args(): parser = argparse.ArgumentParser() - parser.add_argument('--number', '-n', default=1, type=int, help='the number of user to be registered') - parser.add_argument('--invite-code', '-i', default=None, help='invite code') - parser.add_argument('--provider', '-p', default=None, help='leap provider e.g. unstable.pix.org') + parser.add_argument('--number', '-n', default=1, type=int, + help='the number of user to be registered') + parser.add_argument('--invite-code', '-i', + default=None, help='invite code') + parser.add_argument('--provider', '-p', default=None, + help='leap provider e.g. unstable.pix.org') return parser.parse_args() diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index d52c85c05..dd51d8a24 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -67,15 +67,18 @@ def __init__(self, user_id, leap_home): self._mail_address = '%s@pixelated.org' % user_id self._soledad = None self._services = None - self.soledad_test_folder = os.path.join(self._pixelated_home, self._uuid) + self.soledad_test_folder = os.path.join( + self._pixelated_home, self._uuid) @defer.inlineCallbacks def start(self): self.soledad = yield initialize_soledad(tempdir=self.soledad_test_folder, uuid=self._uuid) - self.search_engine = SearchEngine(self.INDEX_KEY, user_home=self.soledad_test_folder) + self.search_engine = SearchEngine( + self.INDEX_KEY, user_home=self.soledad_test_folder) self.keymanager = mock() self.mail_sender = self._create_mail_sender() - self.mail_store = SearchableMailStore(LeapMailStore(self.soledad), self.search_engine) + self.mail_store = SearchableMailStore( + LeapMailStore(self.soledad), self.search_engine) self.attachment_store = LeapAttachmentStore(self.soledad) yield self._initialize_account() @@ -84,7 +87,8 @@ def start(self): self.leap_session = mock() self.feedback_service = FeedbackService(self.leap_session) - self.mail_service = self._create_mail_service(self.mail_sender, self.mail_store, self.search_engine, self.attachment_store) + self.mail_service = self._create_mail_service( + self.mail_sender, self.mail_store, self.search_engine, self.attachment_store) mails = yield self.mail_service.all_mails() if len(mails) > 0: @@ -138,13 +142,15 @@ def add_user(self, username, password): def requestAvatarId(self, credentials): if(self._credentials[credentials.username] == credentials.password): - leap_auth = Authentication(credentials.username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {}) + leap_auth = Authentication( + credentials.username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {}) return defer.succeed(LeapSession(self._leap_provider, leap_auth, None, None, None, None)) else: return defer.fail() class StubAuthenticator(object): + def __init__(self, provider, credentials={}): self._leap_provider = provider self._credentials = credentials.copy() @@ -154,11 +160,13 @@ def add_user(self, username, password): def _set_leap_session_cache(self, auth): key = SessionCache.session_key(self._leap_provider, 'username') - SessionCache.remember_session(key, LeapSession(self._leap_provider, auth, None, None, None, None)) + SessionCache.remember_session(key, LeapSession( + self._leap_provider, auth, None, None, None, None)) def authenticate(self, username, password): if self._credentials[username] == password: - leap_auth = Authentication(username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {}) + leap_auth = Authentication( + username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {}) self._set_leap_session_cache(leap_auth) return defer.succeed(leap_auth) else: @@ -226,7 +234,8 @@ def start_client(self, mode=UserAgentMode(is_single_user=True)): bonafide_checker = StubAuthenticator(provider) bonafide_checker.add_user('username', 'password') - self.resource = set_up_protected_resources(RootResource(self.service_factory), provider, self.service_factory, authenticator=bonafide_checker) + self.resource = set_up_protected_resources(RootResource( + self.service_factory), provider, self.service_factory, authenticator=bonafide_checker) @defer.inlineCallbacks def create_user(self, account_name): @@ -280,15 +289,18 @@ def get(self, path, get_args='', as_json=True, ajax=True, csrf='token'): def post(self, path, body='', headers=None, ajax=True, csrf='token'): headers = headers or {'Content-Type': 'application/json'} - request = request_mock(path=path, method="POST", body=body, headers=headers, ajax=ajax, csrf=csrf) + request = request_mock(path=path, method="POST", + body=body, headers=headers, ajax=ajax, csrf=csrf) return self._render(request) def put(self, path, body, ajax=True, csrf='token'): - request = request_mock(path=path, method="PUT", body=body, headers={'Content-Type': ['application/json']}, ajax=ajax, csrf=csrf) + request = request_mock(path=path, method="PUT", body=body, headers={ + 'Content-Type': ['application/json']}, ajax=ajax, csrf=csrf) return self._render(request) def delete(self, path, body="", ajax=True, csrf='token'): - request = request_mock(path=path, body=body, headers={'Content-Type': ['application/json']}, method="DELETE", ajax=ajax, csrf=csrf) + request = request_mock(path=path, body=body, headers={ + 'Content-Type': ['application/json']}, method="DELETE", ajax=ajax, csrf=csrf) return self._render(request) @defer.inlineCallbacks @@ -308,7 +320,8 @@ def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[], to='recipi mails = [] yield self.mail_store.add_mailbox(mailbox) for _ in range(num): - builder = MailBuilder().with_status(flags).with_tags(tags).with_to(to).with_cc(cc).with_bcc(bcc) + builder = MailBuilder().with_status(flags).with_tags( + tags).with_to(to).with_cc(cc).with_bcc(bcc) builder.with_body(str(random.random())) input_mail = builder.build_input_mail() mail = yield self.mail_store.add_mail(mailbox, input_mail.raw) @@ -360,13 +373,15 @@ def get_attachment(self, ident, encoding, filename=None, content_type=None, ajax params['filename'] = [filename] if content_type: params['content_type'] = [content_type] - deferred_result, req = self.get("/attachment/%s" % ident, params, as_json=False, ajax=ajax, csrf=csrf) + deferred_result, req = self.get( + "/attachment/%s" % ident, params, as_json=False, ajax=ajax, csrf=csrf) res = yield deferred_result defer.returnValue((res, req)) @defer.inlineCallbacks def post_attachment(self, data, headers): - deferred_result, req = self.post('/attachment', body=data, headers=headers) + deferred_result, req = self.post( + '/attachment', body=data, headers=headers) res = yield deferred_result defer.returnValue((res, req)) diff --git a/service/test/support/integration/model.py b/service/test/support/integration/model.py index 87a18af62..714fe5964 100644 --- a/service/test/support/integration/model.py +++ b/service/test/support/integration/model.py @@ -21,6 +21,7 @@ class MailBuilder: + def __init__(self): self.mail = { 'header': { @@ -86,6 +87,7 @@ def build_leap_mail(self): class ResponseMail: + def __init__(self, mail_dict): self.mail_dict = mail_dict diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 82acb2109..1709226b6 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -43,22 +43,27 @@ def start_client(self, mode=UserAgentMode(is_single_user=True)): self.cleanup = lambda: self._test_account.cleanup() self.soledad = self._test_account.soledad - self.service_factory = ServicesFactory(UserAgentMode(is_single_user=False)) + self.service_factory = ServicesFactory( + UserAgentMode(is_single_user=False)) root_resource = RootResource(self.service_factory) leap_provider = mock() self.credentials_checker = StubSRPChecker(leap_provider) - self.resource = set_up_protected_resources(root_resource, leap_provider, self.service_factory) + self.resource = set_up_protected_resources( + root_resource, leap_provider, self.service_factory) def _mock_bonafide_auth(self, username, password): if username == 'username' and password == 'password': self.credentials_checker.add_user(username, password) - when(Authenticator)._bonafide_auth(username, password).thenReturn(self.user_auth) + when(Authenticator)._bonafide_auth( + username, password).thenReturn(self.user_auth) else: - when(Authenticator)._bonafide_auth(username, password).thenRaise(SRPAuthError) + when(Authenticator)._bonafide_auth( + username, password).thenRaise(SRPAuthError) def login(self, username='username', password='password'): - session = Authentication(username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) + session = Authentication( + username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) leap_session = self._test_account.leap_session leap_session.user_auth = session config = mock() @@ -71,12 +76,16 @@ def login(self, username='username', password='password'): self._mock_bonafide_auth(username, password) - when(LeapSessionFactory).create(username, password, session).thenReturn(leap_session) + when(LeapSessionFactory).create( + username, password, session).thenReturn(leap_session) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(leap_session).initial_sync().thenAnswer(lambda: defer.succeed(None)) - when(pixelated.config.services).Services(ANY()).thenReturn(self.services) + when(leap_session).initial_sync().thenAnswer( + lambda: defer.succeed(None)) + when(pixelated.config.services).Services( + ANY()).thenReturn(self.services) - request = request_mock(path='/login', method="POST", body={'username': username, 'password': password}) + request = request_mock(path='/login', method="POST", + body={'username': username, 'password': password}) return self._render(request, as_json=False) def get(self, path, get_args='', as_json=True, from_request=None): @@ -89,7 +98,8 @@ def get(self, path, get_args='', as_json=True, from_request=None): def post(self, path, body='', headers=None, ajax=True, csrf='token', as_json=True, from_request=None): headers = headers or {'Content-Type': 'application/json'} - request = request_mock(path=path, method="POST", body=body, headers=headers, ajax=ajax, csrf=csrf) + request = request_mock(path=path, method="POST", + body=body, headers=headers, ajax=ajax, csrf=csrf) if from_request: session = from_request.getSession() diff --git a/service/test/support/integration/util.py b/service/test/support/integration/util.py index 302edeaa1..28c723cee 100644 --- a/service/test/support/integration/util.py +++ b/service/test/support/integration/util.py @@ -20,7 +20,8 @@ def load_mail_from_file(mail_file, enforceUniqueMessageId=False): - mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') + mailset_dir = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mailset') mail_file = os.path.join(mailset_dir, 'new', mail_file) with open(mail_file) as f: mail = Parser().parse(f) diff --git a/service/test/support/mockito/__init__.py b/service/test/support/mockito/__init__.py index 8589fac02..cb7e4543a 100644 --- a/service/test/support/mockito/__init__.py +++ b/service/test/support/mockito/__init__.py @@ -20,6 +20,7 @@ class FunctionReturn(object): """ Instead of returning a constant value a function is called """ + def __init__(self, function_answer): self.function_answer = function_answer diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index fbff58198..ba0340c7c 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -60,6 +60,7 @@ def duplicates_in_fields_mail_dict(): class TestDoc(object): + def __init__(self, content): self.content = content @@ -69,7 +70,8 @@ def __getitem__(self, key): def leap_mail(uid=0, flags=LEAP_FLAGS, headers=None, extra_headers={}, mbox_uuid='INBOX', body='body', chash='chash'): - fdoc = TestDoc({'flags': flags, 'mbox_uuid': mbox_uuid, 'type': 'flags', 'uid': uid, 'chash': chash}) + fdoc = TestDoc({'flags': flags, 'mbox_uuid': mbox_uuid, + 'type': 'flags', 'uid': uid, 'chash': chash}) if headers is None: headers = {} @@ -98,6 +100,7 @@ def __init__(self, json): class PixRequestMock(DummyRequest): + def __init__(self, path): DummyRequest.__init__(self, path) self.path = '/' + "/".join(path) @@ -131,7 +134,8 @@ def request_mock(path='', method='GET', body='', headers={}, ajax=True, csrf='to for key, val in body.items(): dummy.addArg(key, val) if ajax: - dummy.requestHeaders.setRawHeaders('x-requested-with', ['XMLHttpRequest']) + dummy.requestHeaders.setRawHeaders( + 'x-requested-with', ['XMLHttpRequest']) dummy.requestHeaders.setRawHeaders('x-xsrf-token', [csrf]) dummy.addCookie('XSRF-TOKEN', csrf) diff --git a/service/test/unit/adapter/mailstore/maintenance/test_soledad_maintenance.py b/service/test/unit/adapter/mailstore/maintenance/test_soledad_maintenance.py index f0e1e8c91..d39fe4de4 100644 --- a/service/test/unit/adapter/mailstore/maintenance/test_soledad_maintenance.py +++ b/service/test/unit/adapter/mailstore/maintenance/test_soledad_maintenance.py @@ -40,8 +40,10 @@ def test_repair_is_deferred(self): def test_repair_delete_public_key_active_docs(self): soledad = mock() key = self._public_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) - active_doc = SoledadDocument(doc_id='some_doc', json=key.get_active_json()) - when(soledad).get_all_docs().thenReturn(defer.succeed((1, [active_doc]))) + active_doc = SoledadDocument( + doc_id='some_doc', json=key.get_active_json()) + when(soledad).get_all_docs().thenReturn( + defer.succeed((1, [active_doc]))) yield SoledadMaintenance(soledad).repair() @@ -51,9 +53,11 @@ def test_repair_delete_public_key_active_docs(self): def test_repair_delete_public_key_docs(self): soledad = mock() key = self._public_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) - active_doc = SoledadDocument(doc_id='some_doc', json=key.get_active_json()) + active_doc = SoledadDocument( + doc_id='some_doc', json=key.get_active_json()) key_doc = SoledadDocument(doc_id='some_doc', json=key.get_json()) - when(soledad).get_all_docs().thenReturn(defer.succeed((1, [key_doc, active_doc]))) + when(soledad).get_all_docs().thenReturn( + defer.succeed((1, [key_doc, active_doc]))) yield SoledadMaintenance(soledad).repair() @@ -65,10 +69,13 @@ def test_repair_keeps_active_and_key_doc_if_private_key_exists(self): soledad = mock() key = self._public_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) private_key = self._private_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) - active_doc = SoledadDocument(doc_id='some_doc', json=key.get_active_json()) + active_doc = SoledadDocument( + doc_id='some_doc', json=key.get_active_json()) key_doc = SoledadDocument(doc_id='some_doc', json=key.get_json()) - private_key_doc = SoledadDocument(doc_id='some_doc', json=private_key.get_json()) - when(soledad).get_all_docs().thenReturn(defer.succeed((1, [key_doc, active_doc, private_key_doc]))) + private_key_doc = SoledadDocument( + doc_id='some_doc', json=private_key.get_json()) + when(soledad).get_all_docs().thenReturn( + defer.succeed((1, [key_doc, active_doc, private_key_doc]))) yield SoledadMaintenance(soledad).repair() @@ -80,9 +87,11 @@ def test_repair_keeps_active_and_key_doc_if_private_key_exists(self): def test_repair_only_deletes_key_docs(self): soledad = mock() key = self._public_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) - key_doc = SoledadDocument(doc_id='some_doc', json=key.get_active_json()) + key_doc = SoledadDocument( + doc_id='some_doc', json=key.get_active_json()) other_doc = SoledadDocument(doc_id='something', json='{}') - when(soledad).get_all_docs().thenReturn(defer.succeed((1, [key_doc, other_doc]))) + when(soledad).get_all_docs().thenReturn( + defer.succeed((1, [key_doc, other_doc]))) yield SoledadMaintenance(soledad).repair() @@ -93,12 +102,15 @@ def test_repair_recreates_public_key_active_doc_if_necessary(self): soledad = mock() private_key = self._private_key(SOME_EMAIL_ADDRESS, SOME_FINGERPRINT) - private_key_doc = SoledadDocument(doc_id='some_doc', json=private_key.get_active_json()) - when(soledad).get_all_docs().thenReturn(defer.succeed((1, [private_key_doc]))) + private_key_doc = SoledadDocument( + doc_id='some_doc', json=private_key.get_active_json()) + when(soledad).get_all_docs().thenReturn( + defer.succeed((1, [private_key_doc]))) yield SoledadMaintenance(soledad).repair() - verify(soledad).create_doc_from_json('{"encr_used": false, "sign_used": false, "validation": "Weak_Chain", "version": 1, "address": "foo@example.tld", "last_audited_at": 0, "fingerprint": "4914254E384E264C", "type": "OpenPGPKey-active", "private": false, "tags": ["keymanager-active"]}') + verify(soledad).create_doc_from_json( + '{"encr_used": false, "sign_used": false, "validation": "Weak_Chain", "version": 1, "address": "foo@example.tld", "last_audited_at": 0, "fingerprint": "4914254E384E264C", "type": "OpenPGPKey-active", "private": false, "tags": ["keymanager-active"]}') def _public_key(self, address, fingerprint): return self._gpgkey(address, fingerprint, private=False) diff --git a/service/test/unit/adapter/mailstore/test_body_parser.py b/service/test/unit/adapter/mailstore/test_body_parser.py index cff0b09ed..fe70396cb 100644 --- a/service/test/unit/adapter/mailstore/test_body_parser.py +++ b/service/test/unit/adapter/mailstore/test_body_parser.py @@ -27,34 +27,42 @@ def test_simple_text(self): self.assertEqual('simple text', parser.parsed_content()) def test_base64_text(self): - parser = BodyParser('dGVzdCB0ZXh0\n', content_type='text/plain; charset="utf-8"', content_transfer_encoding='base64') + parser = BodyParser( + 'dGVzdCB0ZXh0\n', content_type='text/plain; charset="utf-8"', content_transfer_encoding='base64') self.assertEqual('test text', parser.parsed_content()) def test_8bit_transfer_encoding_with_iso_8859_1_str_input(self): data = 'Hmm, here are \xdcml\xe4\xfcts again!' - parser = BodyParser(data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + parser = BodyParser( + data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') - self.assertEqual(u'Hmm, here are Ümläüts again!', parser.parsed_content()) + self.assertEqual(u'Hmm, here are Ümläüts again!', + parser.parsed_content()) def test_8bit_transfer_encoding_with_iso_8859_1_unicode_input(self): data = u'Hmm, here are \xdcml\xe4\xfcts again!' - parser = BodyParser(data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + parser = BodyParser( + data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') - self.assertEqual(u'Hmm, here are Ümläüts again!', parser.parsed_content()) + self.assertEqual(u'Hmm, here are Ümläüts again!', + parser.parsed_content()) def test_base64_with_default_us_ascii_encoding(self): - parser = BodyParser('dGVzdCB0ZXh0\n', content_type='text/plain', content_transfer_encoding='base64') + parser = BodyParser( + 'dGVzdCB0ZXh0\n', content_type='text/plain', content_transfer_encoding='base64') self.assertEqual('test text', parser.parsed_content()) @patch('pixelated.adapter.mailstore.body_parser.logger') def test_body_parser_logs_problems_and_then_ignores_invalid_chars(self, logger_mock): data = u'unkown char: \ufffd' - parser = BodyParser(data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + parser = BodyParser( + data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') self.assertEqual(u'unkown char: ', parser.parsed_content()) - logger_mock.warn.assert_called_with(u'Failed to encode content for charset iso-8859-1. Ignoring invalid chars: \'latin-1\' codec can\'t encode character u\'\\ufffd\' in position 13: ordinal not in range(256)') + logger_mock.warn.assert_called_with( + u'Failed to encode content for charset iso-8859-1. Ignoring invalid chars: \'latin-1\' codec can\'t encode character u\'\\ufffd\' in position 13: ordinal not in range(256)') def test_charset_can_be_explicitely_set_and_take_precedence_over_content_type_if_set(self): parser = BodyParser('YmzDoQ==\n', content_type='text/plain; us-ascii', content_transfer_encoding='base64', diff --git a/service/test/unit/adapter/mailstore/test_leap_attachment_store.py b/service/test/unit/adapter/mailstore/test_leap_attachment_store.py index 1cde2ce2e..a3f096835 100644 --- a/service/test/unit/adapter/mailstore/test_leap_attachment_store.py +++ b/service/test/unit/adapter/mailstore/test_leap_attachment_store.py @@ -31,6 +31,7 @@ class TestLeapAttachmentStore(TestCase): + def setUp(self): self.soledad = mock() self.mbox_uuid = str(uuid4()) @@ -39,19 +40,23 @@ def setUp(self): self.mbox_soledad_docs = [] with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_from_index('by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) + when(self.soledad).get_from_index( + 'by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) self._mock_get_mailbox('INBOX') @defer.inlineCallbacks def test_get_mail_attachment(self): attachment_id = 'AAAA9AAD9E153D24265395203C53884506ABA276394B9FEC02B214BF9E77E48E' - doc = SoledadDocument(json=json.dumps({'content_type': 'foo/bar', 'raw': 'asdf'})) - when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + doc = SoledadDocument(json=json.dumps( + {'content_type': 'foo/bar', 'raw': 'asdf'})) + when(self.soledad).get_from_index('by-type-and-payloadhash', + 'cnt', attachment_id).thenReturn(defer.succeed([doc])) store = LeapAttachmentStore(self.soledad) attachment = yield store.get_mail_attachment(attachment_id) - self.assertEqual({'content-type': 'foo/bar', 'content': bytearray('asdf')}, attachment) + self.assertEqual({'content-type': 'foo/bar', + 'content': bytearray('asdf')}, attachment) @defer.inlineCallbacks def test_store_attachment(self): @@ -66,7 +71,8 @@ def test_store_attachment(self): attachment_id = yield store.add_attachment(content, content_type) - self.assertEqual('9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', attachment_id) + self.assertEqual( + '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', attachment_id) verify(self.soledad).create_doc(cdoc_serialized, doc_id=attachment_id) @@ -79,12 +85,15 @@ def test_store_attachment_twice_does_not_cause_exception(self): 'ctype': '', 'raw': 'dGhpcyBpcyBzb21lIGF0dGFjaG1lbnQgY29udGVudA==', 'phash': '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', 'content_type': 'text/plain', 'type': 'cnt'} - doc = SoledadDocument(json=json.dumps({'content_type': content_type, 'raw': content})) - when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + doc = SoledadDocument(json=json.dumps( + {'content_type': content_type, 'raw': content})) + when(self.soledad).get_from_index('by-type-and-payloadhash', + 'cnt', attachment_id).thenReturn(defer.succeed([doc])) store = LeapAttachmentStore(self.soledad) - when(self.soledad).create_doc(cdoc_serialized, doc_id=attachment_id).thenRaise(l2db.errors.RevisionConflict()) + when(self.soledad).create_doc(cdoc_serialized, + doc_id=attachment_id).thenRaise(l2db.errors.RevisionConflict()) actual_attachment_id = yield store.add_attachment(content, content_type) @@ -100,7 +109,8 @@ def test_get_mail_attachment_different_content_encodings(self): for transfer_encoding, data, encoded_data in encoding_examples: doc = SoledadDocument(json=json.dumps({'content_type': 'foo/bar', 'raw': encoded_data, 'content_transfer_encoding': transfer_encoding})) - when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + when(self.soledad).get_from_index('by-type-and-payloadhash', + 'cnt', attachment_id).thenReturn(defer.succeed([doc])) store = LeapAttachmentStore(self.soledad) attachment = yield store.get_mail_attachment(attachment_id) @@ -110,7 +120,8 @@ def test_get_mail_attachment_different_content_encodings(self): @defer.inlineCallbacks def test_get_mail_attachment_throws_exception_if_attachment_does_not_exist(self): attachment_id = '1B0A9AAD9E153D24265395203C53884506ABA276394B9FEC02B214BF9E77E48E' - when(self.soledad).get_from_index('by-type-and-payloadhash', 'cnt', attachment_id).thenReturn(defer.succeed([])) + when(self.soledad).get_from_index('by-type-and-payloadhash', + 'cnt', attachment_id).thenReturn(defer.succeed([])) store = LeapAttachmentStore(self.soledad) try: yield store.get_mail_attachment(attachment_id) @@ -124,8 +135,10 @@ def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): defer.succeed(MAIL_INDEXES)) doc_id = str(uuid4()) mbox = MailboxWrapper(doc_id=doc_id, mbox=mailbox_name, uuid=mbox_uuid) - soledad_doc = SoledadDocument(doc_id, json=json.dumps(mbox.serialize())) - when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', mailbox_name).thenReturn(defer.succeed([soledad_doc])) + soledad_doc = SoledadDocument( + doc_id, json=json.dumps(mbox.serialize())) + when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', + mailbox_name).thenReturn(defer.succeed([soledad_doc])) self._mock_get_soledad_doc(doc_id, mbox) self.mbox_uuid_by_name[mailbox_name] = mbox_uuid @@ -137,6 +150,7 @@ def _mock_get_soledad_doc(self, doc_id, doc): soledad_doc = SoledadDocument(doc_id, json=json.dumps(doc.serialize())) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(doc_id).thenAnswer(lambda: defer.succeed(soledad_doc)) + when(self.soledad).get_doc(doc_id).thenAnswer( + lambda: defer.succeed(soledad_doc)) self.doc_by_id[doc_id] = soledad_doc diff --git a/service/test/unit/adapter/mailstore/test_leap_mail.py b/service/test/unit/adapter/mailstore/test_leap_mail.py index cd8852adf..85158714e 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mail.py +++ b/service/test/unit/adapter/mailstore/test_leap_mail.py @@ -21,27 +21,35 @@ class TestLeapMail(TestCase): + def test_leap_mail(self): - mail = LeapMail('', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}) + mail = LeapMail('', 'INBOX', { + 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}) self.assertEqual('test@example.test', mail.from_sender) self.assertEqual(['receiver@example.test'], mail.to) self.assertEqual('A test Mail', mail.subject) def test_email_addresses_in_to_are_split_into_a_list(self): - mail = LeapMail('', 'INBOX', {'To': 'first@example.test,second@example.test'}) + mail = LeapMail( + '', 'INBOX', {'To': 'first@example.test,second@example.test'}) - self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['To']) + self.assertEqual( + ['first@example.test', 'second@example.test'], mail.headers['To']) def test_email_addresses_in_cc_are_split_into_a_list(self): - mail = LeapMail('', 'INBOX', {'Cc': 'first@example.test,second@example.test'}) + mail = LeapMail( + '', 'INBOX', {'Cc': 'first@example.test,second@example.test'}) - self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['Cc']) + self.assertEqual( + ['first@example.test', 'second@example.test'], mail.headers['Cc']) def test_email_addresses_in_bcc_are_split_into_a_list(self): - mail = LeapMail('', 'INBOX', {'Bcc': 'first@example.test,second@example.test'}) + mail = LeapMail( + '', 'INBOX', {'Bcc': 'first@example.test,second@example.test'}) - self.assertEqual(['first@example.test', 'second@example.test'], mail.headers['Bcc']) + self.assertEqual( + ['first@example.test', 'second@example.test'], mail.headers['Bcc']) def test_email_addresses_might_be_empty_array(self): mail = LeapMail('', 'INBOX', {'Cc': None}) @@ -49,7 +57,8 @@ def test_email_addresses_might_be_empty_array(self): self.assertEqual([], mail.headers['Cc']) def test_as_dict(self): - mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test,receiver2@other.test'}, ('foo', 'bar')) + mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', + 'To': 'receiver@example.test,receiver2@other.test'}, ('foo', 'bar')) self.maxDiff = None expected = { 'header': { @@ -75,12 +84,14 @@ def test_as_dict(self): def test_as_dict_with_body(self): body = 'some body content' - mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) + mail = LeapMail('doc id', 'INBOX', { + 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) self.assertEqual(body, mail.as_dict()['body']) def test_as_dict_with_attachments(self): - attachment_info = AttachmentInfo('id', 'name', 'encoding', ctype='text/plain', size=2) + attachment_info = AttachmentInfo( + 'id', 'name', 'encoding', ctype='text/plain', size=2) mail = LeapMail('doc id', 'INBOX', attachments=[attachment_info]) self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding', 'content-type': 'text/plain', 'size': 2}], @@ -105,11 +116,13 @@ def test_as_dict_with_mixed_encodings(self): mail = LeapMail('', 'INBOX', {'Subject': subject}) - self.assertEqual(u'Another test with Ümläüts', mail.as_dict()['header']['subject']) + self.assertEqual(u'Another test with Ümläüts', + mail.as_dict()['header']['subject']) def test_raw_constructed_by_headers_and_body(self): body = 'some body content' - mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) + mail = LeapMail('doc id', 'INBOX', { + 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) result = mail.raw @@ -126,7 +139,8 @@ def test_headers_none_recipients_are_converted_to_empty_array(self): def test_security_casing(self): # No Encryption, no Signature mail = LeapMail('id', 'INBOX', {}) - self.assertEqual({'locks': [], 'imprints': [{'state': 'no_signature_information'}]}, mail.security_casing) + self.assertEqual({'locks': [], 'imprints': [ + {'state': 'no_signature_information'}]}, mail.security_casing) # Encryption mail = LeapMail('id', 'INBOX', {'X-Leap-Encryption': 'decrypted'}) @@ -137,7 +151,8 @@ def test_security_casing(self): # Signature mail = LeapMail('id', 'INBOX', {'X-Leap-Signature': 'valid'}) - self.assertEqual([{'seal': {'validity': 'valid'}, 'state': 'valid'}], mail.security_casing['imprints']) + self.assertEqual([{'seal': {'validity': 'valid'}, + 'state': 'valid'}], mail.security_casing['imprints']) mail = LeapMail('id', 'INBOX', {'X-Leap-Signature': 'invalid'}) self.assertEqual([], mail.security_casing['imprints']) diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py index 5e897dd74..969f631e8 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py @@ -42,6 +42,7 @@ class TestLeapMailStore(TestCase): + def setUp(self): self.soledad = mock() self.mbox_uuid = str(uuid4()) @@ -50,13 +51,15 @@ def setUp(self): self.mbox_soledad_docs = [] with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_from_index('by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) + when(self.soledad).get_from_index( + 'by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) self._mock_get_mailbox('INBOX') @defer.inlineCallbacks def test_get_mail_not_exist(self): with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(ANY()).thenAnswer(lambda: defer.succeed(None)) + when(self.soledad).get_doc(ANY()).thenAnswer( + lambda: defer.succeed(None)) store = LeapMailStore(self.soledad) mail = yield store.get_mail(_format_mdoc_id(uuid4(), 1)) @@ -65,7 +68,8 @@ def test_get_mail_not_exist(self): @defer.inlineCallbacks def test_get_mail(self): - mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') store = LeapMailStore(self.soledad) @@ -74,14 +78,16 @@ def test_get_mail(self): self.assertIsInstance(mail, LeapMail) self.assertEqual('darby.senger@zemlak.biz', mail.from_sender) self.assertEqual(['carmel@murazikortiz.name'], mail.to) - self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mail.subject) + self.assertEqual( + 'Itaque consequatur repellendus provident sunt quia.', mail.subject) self.assertIsNone(mail.body) self.assertEqual('INBOX', mail.mailbox_name) @defer.inlineCallbacks def test_get_mail_from_mailbox(self): other, _ = self._mock_get_mailbox('OTHER', create_new_uuid=True) - mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000', other.uuid) + mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000', other.uuid) store = LeapMailStore(self.soledad) @@ -91,8 +97,10 @@ def test_get_mail_from_mailbox(self): @defer.inlineCallbacks def test_get_two_different_mails(self): - first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000') - second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000001') + first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') + second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000001') store = LeapMailStore(self.soledad) @@ -100,21 +108,27 @@ def test_get_two_different_mails(self): mail2 = yield store.get_mail(second_mdoc_id) self.assertNotEqual(mail1, mail2) - self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mail1.subject) - self.assertEqual('Error illum dignissimos autem eos aspernatur.', mail2.subject) + self.assertEqual( + 'Itaque consequatur repellendus provident sunt quia.', mail1.subject) + self.assertEqual( + 'Error illum dignissimos autem eos aspernatur.', mail2.subject) @defer.inlineCallbacks def test_get_mails(self): - first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000') - second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000001') + first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') + second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000001') store = LeapMailStore(self.soledad) mails = yield store.get_mails([first_mdoc_id, second_mdoc_id]) self.assertEqual(2, len(mails)) - self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mails[0].subject) - self.assertEqual('Error illum dignissimos autem eos aspernatur.', mails[1].subject) + self.assertEqual( + 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + self.assertEqual( + 'Error illum dignissimos autem eos aspernatur.', mails[1].subject) @defer.inlineCallbacks def test_get_mails_fails_for_invalid_mail_id(self): @@ -129,7 +143,8 @@ def test_get_mails_fails_for_invalid_mail_id(self): @defer.inlineCallbacks def test_get_mail_with_body(self): expeted_body = 'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n' - mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') store = LeapMailStore(self.soledad) @@ -139,9 +154,11 @@ def test_get_mail_with_body(self): @defer.inlineCallbacks def test_update_mail(self): - mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') soledad_fdoc = self.doc_by_id[fdoc_id] - when(self.soledad).put_doc(soledad_fdoc).thenReturn(defer.succeed(None)) + when(self.soledad).put_doc( + soledad_fdoc).thenReturn(defer.succeed(None)) store = LeapMailStore(self.soledad) @@ -156,9 +173,12 @@ def test_update_mail(self): @defer.inlineCallbacks def test_all_mails(self): - first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000000') - second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file('mbox00000001') - when(self.soledad).get_from_index('by-type', 'meta').thenReturn(defer.succeed([self.doc_by_id[first_mdoc_id], self.doc_by_id[second_mdoc_id]])) + first_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') + second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000001') + when(self.soledad).get_from_index('by-type', 'meta').thenReturn( + defer.succeed([self.doc_by_id[first_mdoc_id], self.doc_by_id[second_mdoc_id]])) store = LeapMailStore(self.soledad) @@ -166,17 +186,24 @@ def test_all_mails(self): self.assertIsNotNone(mails) self.assertEqual(2, len(mails)) - self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mails[0].subject) - self.assertEqual('Error illum dignissimos autem eos aspernatur.', mails[1].subject) + self.assertEqual( + 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + self.assertEqual( + 'Error illum dignissimos autem eos aspernatur.', mails[1].subject) @defer.inlineCallbacks def test_add_mailbox(self): - when(self.soledad).list_indexes().thenReturn(defer.succeed(MAIL_INDEXES)).thenReturn(defer.succeed(MAIL_INDEXES)) - when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', 'TEST').thenReturn(defer.succeed([])) - self._mock_create_soledad_doc(self.mbox_uuid, MailboxWrapper(mbox='TEST')) + when(self.soledad).list_indexes().thenReturn(defer.succeed( + MAIL_INDEXES)).thenReturn(defer.succeed(MAIL_INDEXES)) + when(self.soledad).get_from_index('by-type-and-mbox', + 'mbox', 'TEST').thenReturn(defer.succeed([])) + self._mock_create_soledad_doc( + self.mbox_uuid, MailboxWrapper(mbox='TEST')) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(self.mbox_uuid).thenAnswer(lambda: defer.succeed(self.doc_by_id[self.mbox_uuid])) - when(self.soledad).put_doc(ANY()).thenAnswer(lambda: defer.succeed(None)) + when(self.soledad).get_doc(self.mbox_uuid).thenAnswer( + lambda: defer.succeed(self.doc_by_id[self.mbox_uuid])) + when(self.soledad).put_doc(ANY()).thenAnswer( + lambda: defer.succeed(None)) store = LeapMailStore(self.soledad) mbox = yield store.add_mailbox('TEST') @@ -211,7 +238,8 @@ def test_handles_unmapped_mailbox_uuid(self): new_uuid = 'UNICORN' # if no mailbox doc is created yet (async hell?) - when(self.soledad).get_from_index('by-type', 'mbox').thenReturn(defer.succeed([])) + when(self.soledad).get_from_index( + 'by-type', 'mbox').thenReturn(defer.succeed([])) # then it should point to empty, which is all mails name = yield store._mailbox_name_from_uuid(new_uuid) @@ -219,7 +247,8 @@ def test_handles_unmapped_mailbox_uuid(self): @defer.inlineCallbacks def test_add_mail(self): - expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file('mbox00000000') + expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file( + 'mbox00000000') mail = self._load_mail_from_file('mbox00000000') self._mock_get_mailbox('INBOX') @@ -235,14 +264,16 @@ def test_add_mail_with_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') input_mail.attach(attachment) mocked_message = self._add_create_mail_mocks_to_soledad(input_mail) store = LeapMailStore(self.soledad) message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [{'ident': self._cdoc_phash_from_message( + mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) @defer.inlineCallbacks @@ -250,14 +281,16 @@ def test_add_mail_with_inline_attachment(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be an inline attachment') - attachment.add_header('Content-Disposition', 'u\'inline;\n\tfilename=super_nice_photo.jpg') + attachment.add_header('Content-Disposition', + 'u\'inline;\n\tfilename=super_nice_photo.jpg') input_mail.attach(attachment) mocked_message = self._add_create_mail_mocks_to_soledad(input_mail) store = LeapMailStore(self.soledad) message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'super_nice_photo.jpg', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [{'ident': self._cdoc_phash_from_message( + mocked_message, 2), 'name': 'super_nice_photo.jpg', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) @defer.inlineCallbacks @@ -265,7 +298,8 @@ def test_add_mail_with_nested_attachments(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) attachment = MIMEApplication('pretend to be binary attachment data') - attachment.add_header('Content-Disposition', 'attachment', filename='filename.txt') + attachment.add_header('Content-Disposition', + 'attachment', filename='filename.txt') nested_attachment = MIMEMultipart() nested_attachment.attach(attachment) input_mail.attach(nested_attachment) @@ -274,19 +308,24 @@ def test_add_mail_with_nested_attachments(self): message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message(mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [{'ident': self._cdoc_phash_from_message( + mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) def test_extract_attachment_filename_with_or_without_quotes(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) - attachment_without_quotes = MIMEApplication('pretend to be an attachment from apple mail') - attachment_without_quotes.add_header('Content-Disposition', 'u\'attachment;\n\tfilename=batatinha.rtf') + attachment_without_quotes = MIMEApplication( + 'pretend to be an attachment from apple mail') + attachment_without_quotes.add_header( + 'Content-Disposition', 'u\'attachment;\n\tfilename=batatinha.rtf') input_mail.attach(attachment_without_quotes) - attachment_with_quotes = MIMEApplication('pretend to be an attachment from thunderbird') - attachment_with_quotes.add_header('Content-Disposition', 'u\'attachment; filename="receipt.pdf"') + attachment_with_quotes = MIMEApplication( + 'pretend to be an attachment from thunderbird') + attachment_with_quotes.add_header( + 'Content-Disposition', 'u\'attachment; filename="receipt.pdf"') input_mail.attach(attachment_with_quotes) message = self._add_create_mail_mocks_to_soledad(input_mail) @@ -300,14 +339,19 @@ def test_extract_attachment_filename_from_other_headers(self): input_mail = MIMEMultipart() input_mail.attach(MIMEText(u'a utf8 message', _charset='utf-8')) - attachment_without_description = MIMEApplication('pretend to be an attachment from apple mail', _subtype='pgp-keys') - attachment_without_description.add_header('Content-Disposition', 'attachment') - attachment_without_description.add_header('Content-Description', 'Some GPG Key') + attachment_without_description = MIMEApplication( + 'pretend to be an attachment from apple mail', _subtype='pgp-keys') + attachment_without_description.add_header( + 'Content-Disposition', 'attachment') + attachment_without_description.add_header( + 'Content-Description', 'Some GPG Key') input_mail.attach(attachment_without_description) - attachment_with_name_in_content_type = MIMEApplication('pretend to be an attachment from thunderbird', _subtype='pgp-signature; name="signature.asc"') - attachment_with_name_in_content_type.add_header('Content-Disposition', 'inline') + attachment_with_name_in_content_type = MIMEApplication( + 'pretend to be an attachment from thunderbird', _subtype='pgp-signature; name="signature.asc"') + attachment_with_name_in_content_type.add_header( + 'Content-Disposition', 'inline') input_mail.attach(attachment_with_name_in_content_type) message = self._add_create_mail_mocks_to_soledad(input_mail) @@ -320,21 +364,24 @@ def test_extract_attachment_filename_from_other_headers(self): @defer.inlineCallbacks def test_add_mail_with_special_chars(self): input_mail = MIMEText(u'a utf8 message', _charset='utf-8') - input_mail['From'] = Header(u'"Älbert Übrö" <äüö@example.mail>', 'iso-8859-1') + input_mail['From'] = Header( + u'"Älbert Übrö" <äüö@example.mail>', 'iso-8859-1') input_mail['Subject'] = Header(u'Hällö Wörld', 'iso-8859-1') self._add_create_mail_mocks_to_soledad(input_mail) store = LeapMailStore(self.soledad) message = yield store.add_mail('INBOX', input_mail.as_string()) - self.assertEqual(u'"\xc4lbert \xdcbr\xf6" <\xe4\xfc\xf6@example.mail>', message.as_dict()['header']['from']) + self.assertEqual(u'"\xc4lbert \xdcbr\xf6" <\xe4\xfc\xf6@example.mail>', + message.as_dict()['header']['from']) def _cdoc_phash_from_message(self, mocked_message, attachment_nr): return mocked_message.get_wrapper().cdocs[attachment_nr].future_doc_id[2:] @defer.inlineCallbacks def test_delete_mail(self): - mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') store = LeapMailStore(self.soledad) @@ -344,8 +391,10 @@ def test_delete_mail(self): @defer.inlineCallbacks def test_get_mailbox_mail_ids(self): - mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') - when(self.soledad).get_from_index('by-type-and-mbox-uuid', 'flags', underscore_uuid(self.mbox_uuid)).thenReturn(defer.succeed([self.doc_by_id[fdoc_id]])) + mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') + when(self.soledad).get_from_index('by-type-and-mbox-uuid', 'flags', + underscore_uuid(self.mbox_uuid)).thenReturn(defer.succeed([self.doc_by_id[fdoc_id]])) self._mock_get_mailbox('INBOX') store = LeapMailStore(self.soledad) @@ -358,42 +407,53 @@ def test_get_mailbox_mail_ids(self): def test_delete_mailbox(self): _, mbox_soledad_doc = self._mock_get_mailbox('INBOX') store = LeapMailStore(self.soledad) - when(self.soledad).delete_doc(mbox_soledad_doc).thenReturn(defer.succeed(None)) + when(self.soledad).delete_doc( + mbox_soledad_doc).thenReturn(defer.succeed(None)) yield store.delete_mailbox('INBOX') - verify(self.soledad).delete_doc(self.doc_by_id[mbox_soledad_doc.doc_id]) + verify(self.soledad).delete_doc( + self.doc_by_id[mbox_soledad_doc.doc_id]) # should also verify index is updated @defer.inlineCallbacks def test_copy_mail_to_mailbox(self): - expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file('mbox00000000') - mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file( + 'mbox00000000') + mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') self._mock_get_mailbox('TRASH') store = LeapMailStore(self.soledad) mail = yield store.copy_mail_to_mailbox(mail_id, 'TRASH') - self._assert_message_docs_created(expected_message, mail, only_mdoc_and_fdoc=True) + self._assert_message_docs_created( + expected_message, mail, only_mdoc_and_fdoc=True) @defer.inlineCallbacks def test_move_to_mailbox(self): - expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file('mbox00000000') - mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') + expected_message = self._add_create_mail_mocks_to_soledad_from_fixture_file( + 'mbox00000000') + mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') self._mock_get_mailbox('TRASH') store = LeapMailStore(self.soledad) mail = yield store.move_mail_to_mailbox(mail_id, 'TRASH') - self._assert_message_docs_created(expected_message, mail, only_mdoc_and_fdoc=True) + self._assert_message_docs_created( + expected_message, mail, only_mdoc_and_fdoc=True) self._assert_mail_got_deleted(fdoc_id, mail_id) @defer.inlineCallbacks def test_all_mail_graceful_error_handling(self): - mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file('mbox00000000') - when(self.soledad).get_from_index('by-type', 'meta').thenReturn(defer.succeed([self.doc_by_id[mail_id]])) + mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( + 'mbox00000000') + when(self.soledad).get_from_index( + 'by-type', 'meta').thenReturn(defer.succeed([self.doc_by_id[mail_id]])) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(self.doc_by_id[mail_id].content['cdocs'][0]).thenAnswer(lambda: defer.fail(Exception('fail loading attachment'))) + when(self.soledad).get_doc(self.doc_by_id[mail_id].content['cdocs'][ + 0]).thenAnswer(lambda: defer.fail(Exception('fail loading attachment'))) store = LeapMailStore(self.soledad) mails = yield store.all_mails(gracefully_ignore_errors=True) @@ -407,12 +467,16 @@ def _assert_mail_got_deleted(self, fdoc_id, mail_id): def _assert_message_docs_created(self, expected_message, actual_message, only_mdoc_and_fdoc=False): wrapper = expected_message.get_wrapper() - verify(self.soledad).create_doc(wrapper.mdoc.serialize(), doc_id=actual_message.mail_id) - verify(self.soledad).create_doc(wrapper.fdoc.serialize(), doc_id=wrapper.fdoc.future_doc_id) + verify(self.soledad).create_doc( + wrapper.mdoc.serialize(), doc_id=actual_message.mail_id) + verify(self.soledad).create_doc( + wrapper.fdoc.serialize(), doc_id=wrapper.fdoc.future_doc_id) if not only_mdoc_and_fdoc: - verify(self.soledad).create_doc(wrapper.hdoc.serialize(), doc_id=wrapper.hdoc.future_doc_id) + verify(self.soledad).create_doc( + wrapper.hdoc.serialize(), doc_id=wrapper.hdoc.future_doc_id) for nr, cdoc in wrapper.cdocs.items(): - verify(self.soledad).create_doc(cdoc.serialize(), doc_id=wrapper.cdocs[nr].future_doc_id) + verify(self.soledad).create_doc(cdoc.serialize(), + doc_id=wrapper.cdocs[nr].future_doc_id) def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): mbox_uuid = self.mbox_uuid if not create_new_uuid else str(uuid4()) @@ -420,8 +484,10 @@ def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): defer.succeed(MAIL_INDEXES)) doc_id = str(uuid4()) mbox = MailboxWrapper(doc_id=doc_id, mbox=mailbox_name, uuid=mbox_uuid) - soledad_doc = SoledadDocument(doc_id, json=json.dumps(mbox.serialize())) - when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', mailbox_name).thenReturn(defer.succeed([soledad_doc])) + soledad_doc = SoledadDocument( + doc_id, json=json.dumps(mbox.serialize())) + when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', + mailbox_name).thenReturn(defer.succeed([soledad_doc])) self._mock_get_soledad_doc(doc_id, mbox) self.mbox_uuid_by_name[mailbox_name] = mbox_uuid @@ -482,20 +548,24 @@ def _mock_get_soledad_doc(self, doc_id, doc): soledad_doc = SoledadDocument(doc_id, json=json.dumps(doc.serialize())) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(doc_id).thenAnswer(lambda: defer.succeed(soledad_doc)) + when(self.soledad).get_doc(doc_id).thenAnswer( + lambda: defer.succeed(soledad_doc)) self.doc_by_id[doc_id] = soledad_doc def _mock_create_soledad_doc(self, doc_id, doc): soledad_doc = SoledadDocument(doc_id, json=json.dumps(doc.serialize())) if doc.future_doc_id: - when(self.soledad).create_doc(doc.serialize(), doc_id=doc_id).thenReturn(defer.succeed(soledad_doc)) + when(self.soledad).create_doc(doc.serialize(), + doc_id=doc_id).thenReturn(defer.succeed(soledad_doc)) else: - when(self.soledad).create_doc(doc.serialize()).thenReturn(defer.succeed(soledad_doc)) + when(self.soledad).create_doc(doc.serialize() + ).thenReturn(defer.succeed(soledad_doc)) self.doc_by_id[doc_id] = soledad_doc def _load_mail_from_file(self, mail_file): - mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') + mailset_dir = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mailset') mail_file = os.path.join(mailset_dir, 'new', mail_file) with open(mail_file) as f: mail = Parser().parse(f) diff --git a/service/test/unit/adapter/mailstore/test_searchable_mailstore.py b/service/test/unit/adapter/mailstore/test_searchable_mailstore.py index 8c5712014..c7b3398d8 100644 --- a/service/test/unit/adapter/mailstore/test_searchable_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_searchable_mailstore.py @@ -34,13 +34,15 @@ def setUp(self): super(TestSearchableMailStore, self).setUp() self.search_index = mock(mocked_obj=SearchEngine) self.delegate_mail_store = mock(mocked_obj=MailStore) - self.store = SearchableMailStore(self.delegate_mail_store, self.search_index) + self.store = SearchableMailStore( + self.delegate_mail_store, self.search_index) @defer.inlineCallbacks def test_add_mail_delegates_to_mail_store_and_updates_index(self): mail = self._load_mail_from_file('mbox00000000') leap_mail = LeapMail('id', ANY_MAILBOX) - when(self.delegate_mail_store).add_mail(ANY_MAILBOX, mail).thenReturn(defer.succeed(leap_mail)) + when(self.delegate_mail_store).add_mail( + ANY_MAILBOX, mail).thenReturn(defer.succeed(leap_mail)) result = yield self.store.add_mail(ANY_MAILBOX, mail) @@ -50,8 +52,10 @@ def test_add_mail_delegates_to_mail_store_and_updates_index(self): @defer.inlineCallbacks def test_delete_mail_delegates_to_mail_store_and_updates_index(self): - when(self.delegate_mail_store).delete_mail('mail id').thenReturn(defer.succeed(None)) - when(self.search_index).remove_from_index('mail id').thenReturn(defer.succeed(None)) + when(self.delegate_mail_store).delete_mail( + 'mail id').thenReturn(defer.succeed(None)) + when(self.search_index).remove_from_index( + 'mail id').thenReturn(defer.succeed(None)) yield self.store.delete_mail('mail id') @@ -70,7 +74,8 @@ def test_update_mail_delegates_to_mail_store_and_updates_index(self): @defer.inlineCallbacks def test_copy_mail_delegates_to_mail_store_and_updates_index(self): copied_mail = LeapMail('new id', ANY_MAILBOX) - when(self.delegate_mail_store).copy_mail_to_mailbox('mail id', ANY_MAILBOX).thenReturn(defer.succeed(copied_mail)) + when(self.delegate_mail_store).copy_mail_to_mailbox( + 'mail id', ANY_MAILBOX).thenReturn(defer.succeed(copied_mail)) result = yield self.store.copy_mail_to_mailbox('mail id', ANY_MAILBOX) @@ -80,7 +85,8 @@ def test_copy_mail_delegates_to_mail_store_and_updates_index(self): @defer.inlineCallbacks def test_move_mail_delegates_to_mail_store_and_updates_index(self): moved_mail = LeapMail('new id', ANY_MAILBOX) - when(self.delegate_mail_store).move_mail_to_mailbox('mail id', ANY_MAILBOX).thenReturn(defer.succeed(moved_mail)) + when(self.delegate_mail_store).move_mail_to_mailbox( + 'mail id', ANY_MAILBOX).thenReturn(defer.succeed(moved_mail)) result = yield self.store.move_mail_to_mailbox('mail id', ANY_MAILBOX) @@ -91,7 +97,8 @@ def test_move_mail_delegates_to_mail_store_and_updates_index(self): @defer.inlineCallbacks def test_other_methods_are_delegated(self): mail = LeapMail('mail id', ANY_MAILBOX) - when(self.delegate_mail_store).get_mail('mail id').thenReturn(defer.succeed(mail), defer.succeed(mail)) + when(self.delegate_mail_store).get_mail('mail id').thenReturn( + defer.succeed(mail), defer.succeed(mail)) result = yield self.store.get_mail('mail id') self.assertEqual(mail, result) @@ -105,7 +112,8 @@ def test_delete_mailbox_is_not_implemented(self): pass def _load_mail_from_file(self, mail_file): - mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') + mailset_dir = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mailset') mail_file = os.path.join(mailset_dir, 'new', mail_file) with open(mail_file) as f: mail = Parser().parse(f) diff --git a/service/test/unit/adapter/search/test_index_storage_key.py b/service/test/unit/adapter/search/test_index_storage_key.py index e60c69efe..1af57fe68 100644 --- a/service/test/unit/adapter/search/test_index_storage_key.py +++ b/service/test/unit/adapter/search/test_index_storage_key.py @@ -30,7 +30,8 @@ def tearDown(self): def test_get_or_create_key_returns_key(self): soledad = mock() - when(soledad).get_from_index('by-type', 'index_key').thenReturn([SoledadDocument(json='{"value": "somekey"}')]) + when(soledad).get_from_index( + 'by-type', 'index_key').thenReturn([SoledadDocument(json='{"value": "somekey"}')]) key = yield SearchIndexStorageKey(soledad).get_or_create_key() @@ -49,4 +50,5 @@ def test_get_or_create_creates_key_if_not_exists(self): self.assertEqual(expected_key, key) - verify(soledad).create_doc(dict(type='index_key', value=base64_encoded_key)) + verify(soledad).create_doc( + dict(type='index_key', value=base64_encoded_key)) diff --git a/service/test/unit/adapter/search/test_search.py b/service/test/unit/adapter/search/test_search.py index 1465961d9..4bc605ce6 100644 --- a/service/test/unit/adapter/search/test_search.py +++ b/service/test/unit/adapter/search/test_search.py @@ -30,6 +30,7 @@ class LockStub(object): + def __init__(self): self.called = False @@ -42,6 +43,7 @@ def __exit__(self, type, value, traceback): class SearchEngineTest(unittest.TestCase): + def setUp(self): self.tempdir = TempDir() self.user_home = self.tempdir.name @@ -62,7 +64,8 @@ def test_headers_encoding(self): } # when - se.index_mail(LeapMail('mailid', 'INBOX', headers=headers)) # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + se.index_mail(LeapMail('mailid', 'INBOX', headers=headers)) result = se.search('folker') @@ -83,7 +86,8 @@ def test_contents_encoding_accents(self): body = "When doing the search, it's not possible to find words with graphical accents, e.g.: 'coração', 'é', 'Fièvre', La Pluie d'été, 'não'." # when - se.index_mail(LeapMail('mailid', 'INBOX', headers=headers, body=body)) # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + se.index_mail(LeapMail('mailid', 'INBOX', headers=headers, body=body)) result = se.search(u"'coração', 'é',") self.assertEqual((['mailid'], 1), result) @@ -109,7 +113,8 @@ def test_contents_encoding_special_characters(self): body = "When doing the search, 您好 أهلا" # when - se.index_mail(LeapMail('mailid', 'INBOX', headers=headers, body=body)) # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + se.index_mail(LeapMail('mailid', 'INBOX', headers=headers, body=body)) result = se.search(u"您好") self.assertEqual((['mailid'], 1), result) diff --git a/service/test/unit/adapter/services/test_mail_sender.py b/service/test/unit/adapter/services/test_mail_sender.py index 239512145..19437fb77 100644 --- a/service/test/unit/adapter/services/test_mail_sender.py +++ b/service/test/unit/adapter/services/test_mail_sender.py @@ -60,7 +60,8 @@ def setUp(self): self._keymanager_mock = mock() self._remote_smtp_host = 'some.host.test' self._remote_smtp_port = 1234 - self._smtp_config = LeapSMTPConfig('someone@somedomain.tld', self._cert_path, self._remote_smtp_host, self._remote_smtp_port) + self._smtp_config = LeapSMTPConfig( + 'someone@somedomain.tld', self._cert_path, self._remote_smtp_host, self._remote_smtp_port) self.sender = MailSender(self._smtp_config, self._keymanager_mock) def tearDown(self): @@ -68,34 +69,42 @@ def tearDown(self): @defer.inlineCallbacks def test_iterates_over_recipients(self): - input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') + input_mail = InputMail.from_dict( + mail_dict(), from_address='pixelated@org') - when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) + when(OutgoingMail).send_message( + any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): - verify(OutgoingMail).send_message(any(), TwistedSmtpUserCapture(recipient)) + verify(OutgoingMail).send_message( + any(), TwistedSmtpUserCapture(recipient)) @defer.inlineCallbacks def test_send_leaves_mail_in_tact(self): input_mail_dict = mail_dict() - input_mail = InputMail.from_dict(input_mail_dict, from_address='pixelated@org') + input_mail = InputMail.from_dict( + input_mail_dict, from_address='pixelated@org') - when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) + when(OutgoingMail).send_message( + any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) self.assertEqual(input_mail.to, input_mail_dict["header"]["to"]) self.assertEqual(input_mail.cc, input_mail_dict["header"]["cc"]) self.assertEqual(input_mail.bcc, input_mail_dict["header"]["bcc"]) - self.assertEqual(input_mail.subject, input_mail_dict["header"]["subject"]) + self.assertEqual(input_mail.subject, input_mail_dict[ + "header"]["subject"]) @defer.inlineCallbacks def test_problem_with_email_raises_exception(self): - input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') + input_mail = InputMail.from_dict( + mail_dict(), from_address='pixelated@org') - when(OutgoingMail).send_message(any(), any()).thenReturn(defer.fail(Exception('pretend something went wrong'))) + when(OutgoingMail).send_message(any(), any()).thenReturn( + defer.fail(Exception('pretend something went wrong'))) try: yield self.sender.sendmail(input_mail) @@ -106,7 +115,8 @@ def test_problem_with_email_raises_exception(self): @defer.inlineCallbacks def test_keymanager_encrypt_problem_raises_exception(self): - input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') + input_mail = InputMail.from_dict( + mail_dict(), from_address='pixelated@org') when(OutgoingMail)._maybe_attach_key(any(), any(), any()).thenReturn( defer.succeed(None)) @@ -125,12 +135,15 @@ def test_keymanager_encrypt_problem_raises_exception(self): @defer.inlineCallbacks def test_iterates_over_recipients_and_send_whitout_bcc_field(self): - input_mail = InputMail.from_dict(mail_dict(), from_address='pixelated@org') + input_mail = InputMail.from_dict( + mail_dict(), from_address='pixelated@org') bccs = input_mail.bcc - when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) + when(OutgoingMail).send_message( + any(), any()).thenReturn(defer.succeed(None)) yield self.sender.sendmail(input_mail) for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): - verify(OutgoingMail).send_message(MailToSmtpFormatCapture(recipient, bccs), TwistedSmtpUserCapture(recipient)) + verify(OutgoingMail).send_message(MailToSmtpFormatCapture( + recipient, bccs), TwistedSmtpUserCapture(recipient)) diff --git a/service/test/unit/adapter/test_contacts.py b/service/test/unit/adapter/test_contacts.py index 833837556..105c5425f 100644 --- a/service/test/unit/adapter/test_contacts.py +++ b/service/test/unit/adapter/test_contacts.py @@ -22,10 +22,12 @@ class TestContacts(unittest.TestCase): def test_contacts_filter_duplication_by_largest(self): - contacts = ['John Large Name ', 'john@name.example.com', 'dont.delete@example.com'] + contacts = ['John Large Name ', + 'john@name.example.com', 'dont.delete@example.com'] contacts_filtered = address_duplication_filter(contacts) self.assertIn('dont.delete@example.com', contacts_filtered) - self.assertIn('John Large Name ', contacts_filtered) + self.assertIn('John Large Name ', + contacts_filtered) self.assertNotIn('john@name.example.com', contacts_filtered) def test_extract_mail_address_from_contact(self): diff --git a/service/test/unit/adapter/test_draft_service.py b/service/test/unit/adapter/test_draft_service.py index e18589eb1..e1283b361 100644 --- a/service/test/unit/adapter/test_draft_service.py +++ b/service/test/unit/adapter/test_draft_service.py @@ -22,9 +22,12 @@ def test_add_draft(self): verify(self.mail_store).add_mail('DRAFTS', mail.raw) def test_update_draft(self): - mail = InputMail.from_dict(test_helper.mail_dict(), from_address='pixelated@org') - when(self.mail_store).delete_mail(mail.ident).thenReturn(defer.succeed(True)) - when(self.mail_store).add_mail('DRAFTS', mail.raw).thenReturn(defer.succeed(LeapMail('id', 'DRAFTS'))) + mail = InputMail.from_dict( + test_helper.mail_dict(), from_address='pixelated@org') + when(self.mail_store).delete_mail( + mail.ident).thenReturn(defer.succeed(True)) + when(self.mail_store).add_mail('DRAFTS', mail.raw).thenReturn( + defer.succeed(LeapMail('id', 'DRAFTS'))) self.draft_service.update_draft(mail.ident, mail) diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 22a56df51..02b348548 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -83,12 +83,14 @@ def test_to_mime_multipart_should_add_blank_fields(self): mail_dict['header']['cc'] = '' mail_dict['header']['subject'] = '' - mime_multipart = InputMail.from_dict(mail_dict, from_address='pixelated@org').to_mime_multipart() + mime_multipart = InputMail.from_dict( + mail_dict, from_address='pixelated@org').to_mime_multipart() self.assertNotRegexpMatches(mime_multipart.as_string(), "\nTo: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nBcc: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nCc: \n") - self.assertNotRegexpMatches(mime_multipart.as_string(), "\nSubject: \n") + self.assertNotRegexpMatches( + mime_multipart.as_string(), "\nSubject: \n") def test_single_recipient(self): mail_single_recipient = { @@ -101,39 +103,51 @@ def test_single_recipient(self): } } - result = InputMail.from_dict(mail_single_recipient, from_address='pixelated@org').raw + result = InputMail.from_dict( + mail_single_recipient, from_address='pixelated@org').raw self.assertRegexpMatches(result, 'To: to@pixelated.org') def test_to_mime_multipart(self): pixelated.support.date.mail_date_now = lambda: 'date now' - mime_multipart = InputMail.from_dict(simple_mail_dict(), from_address='pixelated@org').to_mime_multipart() - - self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: to@pixelated.org, anotherto@pixelated.org\n") - self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n") - self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n") - self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n") + mime_multipart = InputMail.from_dict( + simple_mail_dict(), from_address='pixelated@org').to_mime_multipart() + + self.assertRegexpMatches(mime_multipart.as_string( + ), "\nTo: to@pixelated.org, anotherto@pixelated.org\n") + self.assertRegexpMatches(mime_multipart.as_string( + ), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n") + self.assertRegexpMatches(mime_multipart.as_string( + ), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n") + self.assertRegexpMatches( + mime_multipart.as_string(), "\nDate: date now\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n") - self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(simple_mail_dict()['body'])) + self.assertRegexpMatches(mime_multipart.as_string( + ), base64.b64encode(simple_mail_dict()['body'])) def test_to_mime_multipart_with_special_chars(self): mail_dict = simple_mail_dict() - mail_dict['header']['to'] = u'"Älbert Übrö \xF0\x9F\x92\xA9" <äüö@example.mail>' + mail_dict['header'][ + 'to'] = u'"Älbert Übrö \xF0\x9F\x92\xA9" <äüö@example.mail>' pixelated.support.date.mail_date_now = lambda: 'date now' - mime_multipart = InputMail.from_dict(mail_dict, from_address='pixelated@org').to_mime_multipart() + mime_multipart = InputMail.from_dict( + mail_dict, from_address='pixelated@org').to_mime_multipart() expected_part_of_encoded_to = 'Iiwgw4QsIGwsIGIsIGUsIHIsIHQsICAsIMOcLCBiLCByLCDDtiwgICwgw7As' - self.assertRegexpMatches(mime_multipart.as_string(), expected_part_of_encoded_to) + self.assertRegexpMatches( + mime_multipart.as_string(), expected_part_of_encoded_to) def test_smtp_format(self): - smtp_format = InputMail.from_dict(simple_mail_dict(), from_address='pixelated@org').to_smtp_format() + smtp_format = InputMail.from_dict( + simple_mail_dict(), from_address='pixelated@org').to_smtp_format() self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org") def test_to_mime_multipart_handles_alternative_bodies(self): - mime_multipart = InputMail.from_dict(multipart_mail_dict(), from_address='pixelated@org').to_mime_multipart() + mime_multipart = InputMail.from_dict( + multipart_mail_dict(), from_address='pixelated@org').to_mime_multipart() part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!' part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n

Hello html world!

' @@ -142,9 +156,11 @@ def test_to_mime_multipart_handles_alternative_bodies(self): self.assertRegexpMatches(mime_multipart.as_string(), part_two) def test_raw_with_attachment_data(self): - input_mail = InputMail.from_dict(with_attachment_mail_dict(), from_address='pixelated@org') + input_mail = InputMail.from_dict( + with_attachment_mail_dict(), from_address='pixelated@org') - attachment = MIMENonMultipart('text', 'plain', Content_Disposition='attachment; filename=ayoyo.txt') + attachment = MIMENonMultipart( + 'text', 'plain', Content_Disposition='attachment; filename=ayoyo.txt') attachment.set_payload('Hello World') mail = MIMEMultipart() mail.attach(attachment) @@ -156,7 +172,8 @@ def test_raw_with_attachment_data(self): self.assertRegexpMatches(input_mail.raw, part_two) def test_charset_utf8(self): - mail_file = pkg_resources.resource_filename('test.unit.fixtures', 'mail.utf8') + mail_file = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mail.utf8') with open(mail_file) as utf8_mail: mail = message_from_file(utf8_mail) input_mail = InputMail.from_python_mail(mail) @@ -164,7 +181,8 @@ def test_charset_utf8(self): self.assertEqual(body, input_mail.body) def test_charset_latin1(self): - mail_file = pkg_resources.resource_filename('test.unit.fixtures', 'mail.latin1') + mail_file = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mail.latin1') with open(mail_file) as latin1_mail: mail = message_from_file(latin1_mail) input_mail = InputMail.from_python_mail(mail) diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py index fc1da2365..19e3355a0 100644 --- a/service/test/unit/adapter/test_mail_service.py +++ b/service/test/unit/adapter/test_mail_service.py @@ -25,6 +25,7 @@ class TestMailService(unittest.TestCase): + def setUp(self): self.drafts = mock() self.mail_store = mock() @@ -38,8 +39,10 @@ def setUp(self): self.mail_sender = mock() self.search_engine = mock() - self.mail_service = MailService(self.mail_sender, self.mail_store, self.search_engine, 'acount@email', self.attachment_store) - self.mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + self.mail_service = MailService( + self.mail_sender, self.mail_store, self.search_engine, 'acount@email', self.attachment_store) + self.mail = InputMail.from_dict( + duplicates_in_fields_mail_dict(), from_address='pixelated@org') def tearDown(self): unstub() @@ -81,11 +84,13 @@ def test_send_mail_marks_as_read(self): when(self.mail).raw = 'raw mail' when(InputMail).from_dict(ANY(), ANY()).thenReturn(self.mail) when(self.mail_store).delete_mail('12').thenReturn(defer.succeed(None)) - when(self.mail_sender).sendmail(self.mail).thenReturn(defer.succeed(None)) + when(self.mail_sender).sendmail( + self.mail).thenReturn(defer.succeed(None)) sent_mail = LeapMail('id', 'INBOX') add_mail_deferral = defer.succeed(sent_mail) - when(self.mail_store).add_mail('SENT', ANY()).thenReturn(add_mail_deferral) + when(self.mail_store).add_mail( + 'SENT', ANY()).thenReturn(add_mail_deferral) yield self.mail_service.send_mail({'ident': '12'}) @@ -130,7 +135,8 @@ def test_mark_as_unread(self): @defer.inlineCallbacks def test_delete_mail(self): mail_to_delete = LeapMail(1, 'INBOX') - when(self.mail_store).get_mail(1, include_body=True).thenReturn(defer.succeed(mail_to_delete)) + when(self.mail_store).get_mail(1, include_body=True).thenReturn( + defer.succeed(mail_to_delete)) yield self.mail_service.delete_mail(1) @@ -140,7 +146,8 @@ def test_delete_mail(self): def test_delete_mail_does_not_fail_for_invalid_mail(self): no_mail = None mail_id = 1 - when(self.mail_store).get_mail(mail_id, include_body=True).thenReturn(defer.succeed(no_mail)) + when(self.mail_store).get_mail( + mail_id, include_body=True).thenReturn(defer.succeed(no_mail)) yield self.mail_service.delete_mail(mail_id) @@ -151,7 +158,8 @@ def test_delete_mail_does_not_fail_for_invalid_mail(self): def test_recover_mail(self): mail_to_recover = LeapMail(1, 'TRASH') when(self.mail_service).mail(1).thenReturn(mail_to_recover) - when(self.mail_store).move_mail_to_mailbox(1, 'INBOX').thenReturn(mail_to_recover) + when(self.mail_store).move_mail_to_mailbox( + 1, 'INBOX').thenReturn(mail_to_recover) yield self.mail_service.recover_mail(1) @@ -159,8 +167,10 @@ def test_recover_mail(self): @defer.inlineCallbacks def test_get_attachment(self): - attachment_dict = {'content': bytearray('data'), 'content-type': 'text/plain'} - when(self.attachment_store).get_mail_attachment('some attachment id').thenReturn(defer.succeed(attachment_dict)) + attachment_dict = {'content': bytearray( + 'data'), 'content-type': 'text/plain'} + when(self.attachment_store).get_mail_attachment( + 'some attachment id').thenReturn(defer.succeed(attachment_dict)) attachment = yield self.mail_service.attachment('some attachment id') @@ -170,7 +180,8 @@ def test_get_attachment(self): def test_update_tags_return_a_set_with_the_current_tags(self): mail = LeapMail(1, 'INBOX', tags={'custom_1', 'custom_2'}) when(self.mail_store).get_mail(1, include_body=True).thenReturn(mail) - when(self.search_engine).tags(query='', skip_default_tags=True).thenReturn([]) + when(self.search_engine).tags( + query='', skip_default_tags=True).thenReturn([]) updated_mail = yield self.mail_service.update_tags(1, {'custom_1', 'custom_3'}) @@ -179,7 +190,8 @@ def test_update_tags_return_a_set_with_the_current_tags(self): @defer.inlineCallbacks def test_if_recipient_doubled_in_fields_send_only_in_bcc(self): - mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + mail = InputMail.from_dict( + duplicates_in_fields_mail_dict(), from_address='pixelated@org') yield self.mail_service._deduplicate_recipients(mail) @@ -189,7 +201,8 @@ def test_if_recipient_doubled_in_fields_send_only_in_bcc(self): @defer.inlineCallbacks def test_if_recipient_doubled_in_fields_send_only_in_to(self): - mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + mail = InputMail.from_dict( + duplicates_in_fields_mail_dict(), from_address='pixelated@org') yield self.mail_service._deduplicate_recipients(mail) @@ -200,24 +213,29 @@ def test_if_recipient_doubled_in_fields_send_only_in_to(self): @defer.inlineCallbacks def test_if_deduplicates_when_recipient_repeated_in_field(self): - mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + mail = InputMail.from_dict( + duplicates_in_fields_mail_dict(), from_address='pixelated@org') yield self.mail_service._deduplicate_recipients(mail) - self.assertItemsEqual(['bcc@pixelated.org', 'another@pixelated.org'], mail.bcc) - self.assertItemsEqual(['third@pixelated.org', 'to@pixelated.org'], mail.to) + self.assertItemsEqual( + ['bcc@pixelated.org', 'another@pixelated.org'], mail.bcc) + self.assertItemsEqual( + ['third@pixelated.org', 'to@pixelated.org'], mail.to) self.assertItemsEqual(['cc@pixelated.org'], mail.cc) def test_remove_canonical_recipient_when_it_is_not_canonical(self): recipient = u'user@pixelated.org' - non_canonical = self.mail_service._remove_canonical_recipient(recipient) + non_canonical = self.mail_service._remove_canonical_recipient( + recipient) self.assertEqual(recipient, non_canonical) def test_remove_canonical_recipient_when_it_is_canonical(self): recipient = u'User ' - non_canonical = self.mail_service._remove_canonical_recipient(recipient) + non_canonical = self.mail_service._remove_canonical_recipient( + recipient) self.assertEqual(u'user@pixelated.org', non_canonical) diff --git a/service/test/unit/adapter/test_mailbox_indexer_listener.py b/service/test/unit/adapter/test_mailbox_indexer_listener.py index c101aa1a7..e2acad8e7 100644 --- a/service/test/unit/adapter/test_mailbox_indexer_listener.py +++ b/service/test/unit/adapter/test_mailbox_indexer_listener.py @@ -23,6 +23,7 @@ class MailboxListenerTest(unittest.TestCase): + def setUp(self): self.mail_store = mock() self.account = mock() @@ -31,27 +32,36 @@ def setUp(self): def test_add_itself_to_mailbox_listeners(self): self.account.mailboxes = ['INBOX'] mailbox = mock() - when(self.account).get_collection_by_mailbox('INBOX').thenReturn(mailbox) + when(self.account).get_collection_by_mailbox( + 'INBOX').thenReturn(mailbox) mailbox.listeners = set() when(mailbox).addListener = lambda x: mailbox.listeners.add(x) - self.assertNotIn(MailboxIndexerListener('INBOX', self.mail_store, mock()), mailbox.listeners) + self.assertNotIn(MailboxIndexerListener( + 'INBOX', self.mail_store, mock()), mailbox.listeners) - MailboxIndexerListener.listen(self.account, 'INBOX', self.mail_store, mock()) + MailboxIndexerListener.listen( + self.account, 'INBOX', self.mail_store, mock()) - self.assertIn(MailboxIndexerListener('INBOX', self.mail_store, mock()), mailbox.listeners) + self.assertIn(MailboxIndexerListener( + 'INBOX', self.mail_store, mock()), mailbox.listeners) def test_reindex_missing_idents(self): mail = mock() search_engine = mock() - when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2']) + when(search_engine).search('tag:inbox', + all_mails=True).thenReturn(['ident1', 'ident2']) - listener = MailboxIndexerListener('INBOX', self.mail_store, search_engine) - when(self.mail_store).get_mailbox_mail_ids('INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'}) - when(self.mail_store).get_mails({'missing_ident'}, include_body=True).thenReturn([mail]) + listener = MailboxIndexerListener( + 'INBOX', self.mail_store, search_engine) + when(self.mail_store).get_mailbox_mail_ids( + 'INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'}) + when(self.mail_store).get_mails( + {'missing_ident'}, include_body=True).thenReturn([mail]) listener.notify_new() - verify(self.mail_store, times=1).get_mails({'missing_ident'}, include_body=True) + verify(self.mail_store, times=1).get_mails( + {'missing_ident'}, include_body=True) verify(search_engine).index_mails([mail]) @defer.inlineCallbacks diff --git a/service/test/unit/adapter/test_status.py b/service/test/unit/adapter/test_status.py index 4624dceea..3547bd880 100644 --- a/service/test/unit/adapter/test_status.py +++ b/service/test/unit/adapter/test_status.py @@ -29,5 +29,6 @@ def test_leap_answered_flag_is_translated_to_replied_status(self): self.assertEquals('replied', status) def test_bulk_conversion(self): - statuses = Status.from_flags(['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom']) + statuses = Status.from_flags( + ['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom']) self.assertEquals(set(['read', 'replied', 'recent']), statuses) diff --git a/service/test/unit/bitmask_libraries/test_abstract_leap.py b/service/test/unit/bitmask_libraries/test_abstract_leap.py index 2fed2a4c7..026a41136 100644 --- a/service/test/unit/bitmask_libraries/test_abstract_leap.py +++ b/service/test/unit/bitmask_libraries/test_abstract_leap.py @@ -30,7 +30,8 @@ def setUp(self): self.leap_home = os.path.join(tempfile.mkdtemp(), 'leap') - self.config = Mock(leap_home=self.leap_home, bootstrap_ca_cert_bundle='/some/path/to/ca_cert', ca_cert_bundle='/some/path/to/provider_ca_cert', gpg_binary='/path/to/gpg') + self.config = Mock(leap_home=self.leap_home, bootstrap_ca_cert_bundle='/some/path/to/ca_cert', + ca_cert_bundle='/some/path/to/provider_ca_cert', gpg_binary='/path/to/gpg') self.provider = Mock(config=self.config, server_name='some-server.test', domain='some-server.test', api_uri='https://api.some-server.test:4430', api_version='1') self.soledad = Mock() diff --git a/service/test/unit/bitmask_libraries/test_keymanager.py b/service/test/unit/bitmask_libraries/test_keymanager.py index 1886e10f7..7b6f452cf 100644 --- a/service/test/unit/bitmask_libraries/test_keymanager.py +++ b/service/test/unit/bitmask_libraries/test_keymanager.py @@ -42,7 +42,8 @@ def tearDown(self): @patch('pixelated.bitmask_libraries.keymanager.KeyManager') def test_keymanager_is_created(self, keymanager_mock): - when(self.provider)._discover_nicknym_server().thenReturn('nicknym_server') + when(self.provider)._discover_nicknym_server( + ).thenReturn('nicknym_server') self.provider.provider_api_cert = 'ca_cert_path' self.provider.api_uri = 'api_uri' self.provider.api_version = '1' @@ -69,7 +70,8 @@ def test_keymanager_is_created(self, keymanager_mock): def test_keymanager_generate_openpgp_key_generates_key_correctly(self): self.keymanager._synchronize_remote_key = MagicMock() - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(None) self.leap_keymanager.gen_key = MagicMock() self.leap_keymanager.send_key = MagicMock() @@ -82,7 +84,8 @@ def test_keymanager_generate_openpgp_key_generates_key_correctly(self): def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key(self): mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) self.leap_keymanager.gen_key = MagicMock() @@ -94,7 +97,8 @@ def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self): self.keymanager._synchronize_remote_key = MagicMock() mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) self.leap_keymanager.send_key = MagicMock() @@ -104,10 +108,12 @@ def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self): @defer.inlineCallbacks def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails(self): - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(None) self.keymanager.delete_key_pair = MagicMock() - when(self.leap_keymanager).send_key().thenRaise(Exception('Could not upload key')) + when(self.leap_keymanager).send_key().thenRaise( + Exception('Could not upload key')) with self.assertRaises(UploadKeyError): yield self.keymanager.generate_openpgp_key() @@ -119,7 +125,8 @@ def test_keymanager_regenerate_key_pair_if_current_key_is_about_to_expire(self): self.keymanager._synchronize_remote_key = MagicMock() mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=True) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) yield self.keymanager.generate_openpgp_key() @@ -130,8 +137,10 @@ def test_keymanager_regenerate_key_pair_if_current_key_is_about_to_expire(self): def test_key_regeneration_does_not_delete_key_when_upload_fails(self): mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=True) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) - when(self.leap_keymanager).send_key().thenRaise(Exception('Could not upload key')) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.leap_keymanager).send_key().thenRaise( + Exception('Could not upload key')) self.keymanager.delete_key_pair = MagicMock() @@ -143,20 +152,24 @@ def test_key_regeneration_does_not_delete_key_when_upload_fails(self): @defer.inlineCallbacks def test_key_is_syncronized_with_server(self): - self.keymanager._is_key_synchronized_with_server = MagicMock(return_value=True) + self.keymanager._is_key_synchronized_with_server = MagicMock( + return_value=True) mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) yield self.keymanager.generate_openpgp_key() self.leap_keymanager.send_key.assert_not_called() @defer.inlineCallbacks def test_key_is_not_syncronized_with_server(self): - self.keymanager._is_key_synchronized_with_server = MagicMock(return_value=False) + self.keymanager._is_key_synchronized_with_server = MagicMock( + return_value=False) mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) - when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key) + when(self.keymanager)._key_exists( + 'test_user@some-server.test').thenReturn(mock_open_pgp_key) yield self.keymanager.generate_openpgp_key() self.leap_keymanager.send_key.assert_called_once() diff --git a/service/test/unit/bitmask_libraries/test_provider.py b/service/test/unit/bitmask_libraries/test_provider.py index 708b05b66..84f1a262d 100644 --- a/service/test/unit/bitmask_libraries/test_provider.py +++ b/service/test/unit/bitmask_libraries/test_provider.py @@ -138,6 +138,7 @@ def ca_cert_mock(url, request): class LeapProviderTest(AbstractLeapTest): + def setUp(self): leap_config.leap_home = '/tmp/foobar' LeapCertificate.set_cert_and_fingerprint(PROVIDER_WEB_CERT, None) @@ -151,8 +152,10 @@ def test_provider_fetches_provider_json(self): self.assertEqual("1", provider.api_version) self.assertEqual("some-provider.test", provider.domain) - self.assertEqual("https://api.some-provider.test:4430", provider.api_uri) - self.assertEqual("https://some-provider.test/ca.crt", provider.ca_cert_uri) + self.assertEqual( + "https://api.some-provider.test:4430", provider.api_uri) + self.assertEqual( + "https://some-provider.test/ca.crt", provider.ca_cert_uri) self.assertEqual("SHA256: 06e2300bdbc118c290eda0dc977c24080718f4eeca68c8b0ad431872a2baa22d", provider.ca_cert_fingerprint) self.assertEqual(["mx"], provider.services) @@ -172,7 +175,8 @@ def test_fetch_smtp_json(self): with HTTMock(provider_json_mock, soledad_json_mock, smtp_json_mock, not_found_mock): provider = LeapProvider('some-provider.test') smtp = provider.fetch_smtp_json() - self.assertEqual('mx.some-provider.test', smtp.get('hosts').get('leap-mx').get('hostname')) + self.assertEqual('mx.some-provider.test', + smtp.get('hosts').get('leap-mx').get('hostname')) def test_throw_exception_for_fetch_smtp_status_code(self): with HTTMock(provider_json_mock, soledad_json_mock, not_found_mock): @@ -205,8 +209,10 @@ def test_that_bootstrap_cert_is_used_to_fetch_certificate(self): provider = LeapProvider('some-provider.test') provider.fetch_valid_certificate() - session.get.assert_any_call('https://some-provider.test/ca.crt', verify=PROVIDER_WEB_CERT, timeout=15) - session.get.assert_any_call('https://some-provider.test/provider.json', verify=PROVIDER_WEB_CERT, timeout=15) + session.get.assert_any_call( + 'https://some-provider.test/ca.crt', verify=PROVIDER_WEB_CERT, timeout=15) + session.get.assert_any_call( + 'https://some-provider.test/provider.json', verify=PROVIDER_WEB_CERT, timeout=15) def test_that_provider_cert_is_used_to_fetch_soledad_json(self): get_func = MagicMock(wraps=requests.get) @@ -215,7 +221,8 @@ def test_that_provider_cert_is_used_to_fetch_soledad_json(self): with HTTMock(provider_json_mock, soledad_json_mock, not_found_mock): provider = LeapProvider('some-provider.test') provider.fetch_soledad_json() - get_func.assert_called_with('https://api.some-provider.test:4430/1/config/soledad-service.json', verify='/tmp/foobar/providers/some-provider.test/keys/client/api.pem', timeout=15) + get_func.assert_called_with('https://api.some-provider.test:4430/1/config/soledad-service.json', + verify='/tmp/foobar/providers/some-provider.test/keys/client/api.pem', timeout=15) def test_that_leap_fingerprint_is_validated(self): session = MagicMock(wraps=requests.session()) @@ -227,7 +234,8 @@ def test_that_leap_fingerprint_is_validated(self): provider = LeapProvider('some-provider.test') provider.fetch_valid_certificate() - session.get.assert_any_call('https://some-provider.test/ca.crt', verify=False, timeout=15) + session.get.assert_any_call( + 'https://some-provider.test/ca.crt', verify=False, timeout=15) session.mount.assert_called_with('https://', ANY) def test_provider_api_cert(self): @@ -235,4 +243,5 @@ def test_provider_api_cert(self): provider = LeapProvider('some-provider.test') certs = provider.provider_api_cert - self.assertEqual('/tmp/foobar/providers/some-provider.test/keys/client/api.pem', certs) + self.assertEqual( + '/tmp/foobar/providers/some-provider.test/keys/client/api.pem', certs) diff --git a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py index 1ed086530..332f09826 100644 --- a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py +++ b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py @@ -58,14 +58,16 @@ def setUp(self): self.provider.server_name = 'some.host.tld' self.provider.domain = 'some-provider.tld' self.auth = Authentication(USERNAME, 'token', 'uuid', 'session_id', {}) - self.pem_path = os.path.join(self.tmp_dir.name, 'providers', 'some-provider.tld', 'keys', 'client', 'smtp.pem') + self.pem_path = os.path.join( + self.tmp_dir.name, 'providers', 'some-provider.tld', 'keys', 'client', 'smtp.pem') def tearDown(self): self.tmp_dir.dissolve() unstub() def test_download_certificate(self): - cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + cert = SmtpClientCertificate( + self.provider, self.auth, self.tmp_dir.name) when(cert).download_to(ANY()).thenReturn(None) result = cert.cert_path() @@ -75,7 +77,8 @@ def test_download_certificate_if_redownload_necessary_e_g_certificate_expired(se self.pretend_all_paths_exist() when(certs).should_redownload(self.pem_path).thenReturn(True) - cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + cert = SmtpClientCertificate( + self.provider, self.auth, self.tmp_dir.name) when(cert).download_to(ANY()).thenReturn(None) result = cert.cert_path() @@ -87,13 +90,15 @@ def pretend_all_paths_exist(self): def test_skip_download_if_already_downloaded_and_still_valid(self): when(os.path).exists(self.pem_path).thenReturn(True) when(certs).should_redownload(ANY()).thenReturn(False) - cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + cert = SmtpClientCertificate( + self.provider, self.auth, self.tmp_dir.name) result = cert.cert_path() self.assertEqual(self.pem_path, result) def test_download_to(self): - cert = SmtpClientCertificate(self.provider, self.auth, self.tmp_dir.name) + cert = SmtpClientCertificate( + self.provider, self.auth, self.tmp_dir.name) with NamedTemporaryFile() as tmp_file: with HTTMock(smtp_cert_mock, not_found_mock): diff --git a/service/test/unit/config/test_leap.py b/service/test/unit/config/test_leap.py index 7e073d262..dae4de6ca 100644 --- a/service/test/unit/config/test_leap.py +++ b/service/test/unit/config/test_leap.py @@ -43,12 +43,14 @@ def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_ def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock_event_server, mock_credentials, mock_init_leap_provider): provider_mock = MagicMock() mock_init_leap_provider.return_value = provider_mock - mock_credentials.read.return_value = ('provider_url', 'username', 'password') + mock_credentials.read.return_value = ( + 'provider_url', 'username', 'password') mock_authenticator = MagicMock() with patch('pixelated.config.leap.Authenticator', return_value=mock_authenticator) as mock_instantiate_authenticator: auth_mock = MagicMock() - mock_authenticator.authenticate.return_value = defer.succeed(auth_mock) + mock_authenticator.authenticate.return_value = defer.succeed( + auth_mock) leap_session = MagicMock() deferred_leap_session = defer.succeed(leap_session) with patch.object(LeapSessionFactory, 'create', return_value=deferred_leap_session) as mock_create_leap_session: @@ -56,10 +58,13 @@ def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock mock_event_server.ensure_server.assert_called_once() mock_credentials.read.assert_called_once_with('credentials_file') - mock_init_leap_provider.asser_called_once_with('provider_url', 'leap_provider_cert', 'leap_provider_cert_fingerprint', 'leap_home') + mock_init_leap_provider.asser_called_once_with( + 'provider_url', 'leap_provider_cert', 'leap_provider_cert_fingerprint', 'leap_home') mock_instantiate_authenticator.assert_called_once_with(provider_mock) - mock_authenticator.authenticate.assert_called_once_with('username', 'password') - mock_create_leap_session.assert_called_once_with('username', 'password', auth_mock) + mock_authenticator.authenticate.assert_called_once_with( + 'username', 'password') + mock_create_leap_session.assert_called_once_with( + 'username', 'password', auth_mock) self.assertEqual(leap_session, returned_session) @@ -68,7 +73,8 @@ class TestUserBootstrap(unittest.TestCase): def setUp(self): self._service_factory = Mock() self._provider = Mock() - self._user_bootstrap = BootstrapUserServices(self._service_factory, self._provider) + self._user_bootstrap = BootstrapUserServices( + self._service_factory, self._provider) username = 'ayoyo' password = 'ayoyo_password' @@ -93,7 +99,8 @@ def test_should_create_leap_session(self, mock_create_leap_session): self._user_bootstrap.setup(self.user_auth, self.password) - mock_create_leap_session.called_once_with(self._provider, self.username, self.password, self.user_auth) + mock_create_leap_session.called_once_with( + self._provider, self.username, self.password, self.user_auth) @patch('pixelated.config.leap.create_leap_session') def test_should_setup_user_services_and_map_email(self, mock_create_leap_session): @@ -102,8 +109,10 @@ def test_should_setup_user_services_and_map_email(self, mock_create_leap_session self._user_bootstrap.setup(self.user_auth, self.password) - self._service_factory.create_services_from.assert_called_once_with(self.leap_session) - self._service_factory.map_email.assert_called_once_with(self.username, self.uuid) + self._service_factory.create_services_from.assert_called_once_with( + self.leap_session) + self._service_factory.map_email.assert_called_once_with( + self.username, self.uuid) @patch('pixelated.config.leap.create_leap_session') def test_should_not_user_services_if_there_is_already_a_session(self, mock_create_leap_session): diff --git a/service/test/unit/config/test_register.py b/service/test/unit/config/test_register.py index 1d7918c46..822517d10 100644 --- a/service/test/unit/config/test_register.py +++ b/service/test/unit/config/test_register.py @@ -29,8 +29,10 @@ def test_username_pass_when_valid(self): def test_sets_provider(self): mock_provider = Mock() with patch('pixelated.register.LeapProvider', return_value=mock_provider) as mock_instantiate_provider: - provider = _set_provider('mocked_provider_cert', 'mocked_provider_cert_fingerprint', 'mocked_server_name') - mock_instantiate_provider.assert_called_once_with('mocked_server_name') + provider = _set_provider( + 'mocked_provider_cert', 'mocked_provider_cert_fingerprint', 'mocked_server_name') + mock_instantiate_provider.assert_called_once_with( + 'mocked_server_name') self.assertEqual(provider, mock_provider) self.assertTrue(mock_provider.setup_ca.called) self.assertTrue(mock_provider.download_settings.called) @@ -41,9 +43,12 @@ def test_register_uses_bonafide_auth(self, mock_logger): mock_provider = Mock() mock_provider.api_uri = 'https://pro.vi.der' mock_bonafide_session = Mock() - mock_bonafide_session.signup.return_value = defer.succeed(('created', 'user')) + mock_bonafide_session.signup.return_value = defer.succeed( + ('created', 'user')) with patch('pixelated.register.Session', return_value=mock_bonafide_session) as mock_instantiate_bonafide_session: yield register('username', 'password', mock_provider, 'invite') mock_instantiate_bonafide_session.assert_called_once() - mock_bonafide_session.signup.assert_called_once_with('username', 'password', 'invite') - mock_logger.info.assert_called_with('User username successfully registered') + mock_bonafide_session.signup.assert_called_once_with( + 'username', 'password', 'invite') + mock_logger.info.assert_called_with( + 'User username successfully registered') diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index a7e62c7a5..1af0d483f 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -68,7 +68,8 @@ def test_session_registers_to_generated_keys(self): with patch('pixelated.config.sessions.register') as register_mock: session = self._create_session() - register_mock.assert_called_once_with(KEYMANAGER_FINISHED_KEY_GENERATION, session._set_fresh_account, uid=email) + register_mock.assert_called_once_with( + KEYMANAGER_FINISHED_KEY_GENERATION, session._set_fresh_account, uid=email) @patch('pixelated.config.sessions.register') def test_close_unregisters_from_generate_keys_events(self, _): @@ -79,7 +80,8 @@ def test_close_unregisters_from_generate_keys_events(self, _): with patch('pixelated.config.sessions.unregister') as unregister_mock: session.close() - unregister_mock.assert_called_once_with(KEYMANAGER_FINISHED_KEY_GENERATION, uid=email) + unregister_mock.assert_called_once_with( + KEYMANAGER_FINISHED_KEY_GENERATION, uid=email) @patch('pixelated.config.sessions.register') def test_close_stops_soledad(self, _): diff --git a/service/test/unit/config/test_site.py b/service/test/unit/config/test_site.py index 63ac9f1ad..9721c568e 100644 --- a/service/test/unit/config/test_site.py +++ b/service/test/unit/config/test_site.py @@ -11,13 +11,19 @@ def test_add_security_headers(self): request.process() header_value = "default-src 'self'; style-src 'self' 'unsafe-inline'" - self.assertEquals(header_value, request.responseHeaders.getRawHeaders('X-Content-Security-Policy'.lower())[0]) - self.assertEquals(header_value, request.responseHeaders.getRawHeaders('Content-Security-Policy'.lower())[0]) - self.assertEquals(header_value, request.responseHeaders.getRawHeaders('X-Webkit-CSP'.lower())[0]) - - self.assertEqual('SAMEORIGIN', request.responseHeaders.getRawHeaders('X-Frame-Options'.lower())[0]) - self.assertEqual('1; mode=block', request.responseHeaders.getRawHeaders('X-XSS-Protection'.lower())[0]) - self.assertEqual('nosniff', request.responseHeaders.getRawHeaders('X-Content-Type-Options'.lower())[0]) + self.assertEquals(header_value, request.responseHeaders.getRawHeaders( + 'X-Content-Security-Policy'.lower())[0]) + self.assertEquals(header_value, request.responseHeaders.getRawHeaders( + 'Content-Security-Policy'.lower())[0]) + self.assertEquals(header_value, request.responseHeaders.getRawHeaders( + 'X-Webkit-CSP'.lower())[0]) + + self.assertEqual('SAMEORIGIN', request.responseHeaders.getRawHeaders( + 'X-Frame-Options'.lower())[0]) + self.assertEqual('1; mode=block', request.responseHeaders.getRawHeaders( + 'X-XSS-Protection'.lower())[0]) + self.assertEqual('nosniff', request.responseHeaders.getRawHeaders( + 'X-Content-Type-Options'.lower())[0]) def test_add_strict_transport_security_header_if_secure(self): request = self.create_request() @@ -25,15 +31,18 @@ def test_add_strict_transport_security_header_if_secure(self): request.process() - self.assertTrue(request.responseHeaders.hasHeader('Strict-Transport-Security'.lower())) - self.assertEqual('max-age=31536000; includeSubDomains', request.responseHeaders.getRawHeaders('Strict-Transport-Security'.lower())[0]) + self.assertTrue(request.responseHeaders.hasHeader( + 'Strict-Transport-Security'.lower())) + self.assertEqual('max-age=31536000; includeSubDomains', + request.responseHeaders.getRawHeaders('Strict-Transport-Security'.lower())[0]) def test_does_not_add_strict_transport_security_header_if_plain_http(self): request = self.create_request() request.process() - self.assertFalse(request.responseHeaders.hasHeader('Strict-Transport-Security'.lower())) + self.assertFalse(request.responseHeaders.hasHeader( + 'Strict-Transport-Security'.lower())) def create_request(self): channel = LineReceiver() diff --git a/service/test/unit/maintenance/test_commands.py b/service/test/unit/maintenance/test_commands.py index 0a8ffe536..1d2fdfec7 100644 --- a/service/test/unit/maintenance/test_commands.py +++ b/service/test/unit/maintenance/test_commands.py @@ -54,7 +54,8 @@ def test_delete_all_mails(self): self.soledad.delete_doc.assert_called_once_with(doc) def test_only_mail_documents_are_deleted(self): - docs = self._create_docs_of_type(['head', 'cnt', 'flags', 'mbx', 'foo', None]) + docs = self._create_docs_of_type( + ['head', 'cnt', 'flags', 'mbx', 'foo', None]) self.soledad.get_all_docs.return_value = (1, docs) delete_all_mails(self.args) @@ -79,10 +80,12 @@ def test_load_mails_empty_path_list(self): def test_load_mails_adds_mails(self): # given - mail_root = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') + mail_root = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mailset') firstMailDeferred = defer.succeed(MagicMock()) secondMailDeferred = defer.succeed(MagicMock()) - self.mail_store.add_mail.side_effect = [firstMailDeferred, secondMailDeferred] + self.mail_store.add_mail.side_effect = [ + firstMailDeferred, secondMailDeferred] self.mail_store.add_mailbox.return_value = defer.succeed(None) # when @@ -91,8 +94,10 @@ def test_load_mails_adds_mails(self): # then def assert_mails_added(_): self.assertTrue(self.mail_store.add_mail.called) - self.mail_store.add_mail.assert_any_call('INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000000'))) - self.mail_store.add_mail.assert_any_call('INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000001'))) + self.mail_store.add_mail.assert_any_call( + 'INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000000'))) + self.mail_store.add_mail.assert_any_call( + 'INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000001'))) # TODO Should we check for flags? def error_callack(err): @@ -106,14 +111,16 @@ def error_callack(err): def test_load_mails_supports_mbox(self): # given - mbox_file = pkg_resources.resource_filename('test.unit.fixtures', 'mbox') + mbox_file = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mbox') d = load_mails(self.args, [mbox_file]) # then def assert_mails_added(_): self.assertTrue(self.mail_store.add_mail.called) - self.mail_store.add_mail.assert_any_call('INBOX', self._mail_content(mbox_file)) + self.mail_store.add_mail.assert_any_call( + 'INBOX', self._mail_content(mbox_file)) def error_callack(err): print err diff --git a/service/test/unit/resources/__init__.py b/service/test/unit/resources/__init__.py index 5abd502ba..faeeeaf04 100644 --- a/service/test/unit/resources/__init__.py +++ b/service/test/unit/resources/__init__.py @@ -18,5 +18,6 @@ def resolve_result(request, result): class DummySite(Site): + def get(self, request): return resolve_result(request, self.getResourceFor(request).render(request)) diff --git a/service/test/unit/resources/test_archive_resource.py b/service/test/unit/resources/test_archive_resource.py index 1876f897a..d3c49674d 100644 --- a/service/test/unit/resources/test_archive_resource.py +++ b/service/test/unit/resources/test_archive_resource.py @@ -8,6 +8,7 @@ class TestArchiveResource(unittest.TestCase): + def setUp(self): self.mail_service = mock() self.web = DummySite(MailsArchiveResource(self.mail_service)) @@ -19,8 +20,10 @@ def test_render_POST_should_archive_mails(self): content = mock() when(content).read().thenReturn(json.dumps({'idents': ['1', '2']})) - when(self.mail_service).archive_mail('1').thenReturn(defer.succeed(None)) - when(self.mail_service).archive_mail('2').thenReturn(defer.succeed(None)) + when(self.mail_service).archive_mail( + '1').thenReturn(defer.succeed(None)) + when(self.mail_service).archive_mail( + '2').thenReturn(defer.succeed(None)) request.content = content d = self.web.get(request) diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index 15f38406e..aabaa390c 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -19,7 +19,8 @@ def setUp(self): self.services_factory.mode = UserAgentMode(is_single_user=True) self.services = mock() self.services.mail_service = self.mail_service - self.services_factory._services_by_user = {'someuserid': self.mail_service} + self.services_factory._services_by_user = { + 'someuserid': self.mail_service} when(self.services_factory).services(ANY()).thenReturn(self.services) self.mails_resource = AttachmentsResource(self.services_factory) @@ -37,17 +38,20 @@ def test_post_new_attachment(self, mock_fields): _file.type = 'some mocked type' _file.filename = 'filename.txt' mock_fields.return_value = {'attachment': _file} - when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.succeed(attachment_id)) + when(self.mail_service).save_attachment('some mocked value', + 'some mocked type').thenReturn(defer.succeed(attachment_id)) d = self.web.get(request) def assert_response(_): self.assertEqual(201, request.code) - self.assertEqual('/attachment/%s' % attachment_id, request.responseHeaders.getRawHeaders("location")[0]) + self.assertEqual('/attachment/%s' % attachment_id, + request.responseHeaders.getRawHeaders("location")[0]) response_json = {'ident': attachment_id, 'content-type': 'some mocked type', 'name': 'filename.txt', 'size': 17, 'encoding': 'base64'} self.assertEqual(response_json, json.loads(request.written[0])) - verify(self.mail_service).save_attachment('some mocked value', 'some mocked type') + verify(self.mail_service).save_attachment( + 'some mocked value', 'some mocked type') d.addCallback(assert_response) return d @@ -62,15 +66,18 @@ def test_post_attachment_fails(self, mock_fields): _file.value = 'some mocked value' _file.type = 'some mocked type' mock_fields.return_value = {'attachment': _file} - when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.fail(Exception)) + when(self.mail_service).save_attachment('some mocked value', + 'some mocked type').thenReturn(defer.fail(Exception)) d = self.web.get(request) def assert_response(_): self.assertEqual(500, request.code) - self.assertFalse(request.responseHeaders.hasHeader('Location'.lower())) + self.assertFalse( + request.responseHeaders.hasHeader('Location'.lower())) self.assertIn("message", json.loads(request.written[0])) - verify(self.mail_service).save_attachment('some mocked value', 'some mocked type') + verify(self.mail_service).save_attachment( + 'some mocked value', 'some mocked type') d.addCallback(assert_response) return d diff --git a/service/test/unit/resources/test_feedback_resource.py b/service/test/unit/resources/test_feedback_resource.py index 56433f563..04cb158f5 100644 --- a/service/test/unit/resources/test_feedback_resource.py +++ b/service/test/unit/resources/test_feedback_resource.py @@ -9,13 +9,15 @@ class TestFeedbackResource(unittest.TestCase): + def setUp(self): self.feedback_service = mock() self.services_factory = mock() self.services_factory.mode = UserAgentMode(is_single_user=True) self.services = mock() self.services.feedback_service = self.feedback_service - self.services_factory._services_by_user = {'someuserid': self.feedback_service} + self.services_factory._services_by_user = { + 'someuserid': self.feedback_service} when(self.services_factory).services(ANY()).thenReturn(self.services) self.web = DummySite(FeedbackResource(self.services_factory)) @@ -24,7 +26,8 @@ def test_sends_feedback_to_leap_web(self): request = DummyRequest(['/feedback']) request.method = 'POST' content = mock() - when(content).read().thenReturn(json.dumps({'feedback': 'Pixelated is awesome!'})) + when(content).read().thenReturn(json.dumps( + {'feedback': 'Pixelated is awesome!'})) request.content = content d = self.web.get(request) diff --git a/service/test/unit/resources/test_keys_resources.py b/service/test/unit/resources/test_keys_resources.py index 172972063..26467a4ab 100644 --- a/service/test/unit/resources/test_keys_resources.py +++ b/service/test/unit/resources/test_keys_resources.py @@ -21,14 +21,16 @@ def setUp(self): self.services_factory.mode = UserAgentMode(is_single_user=True) self.services = mock() self.services.keymanager = self.keymanager - self.services_factory._services_by_user = {'someuserid': self.keymanager} + self.services_factory._services_by_user = { + 'someuserid': self.keymanager} when(self.services_factory).services(ANY()).thenReturn(self.services) self.web = DummySite(KeysResource(self.services_factory)) def test_returns_404_if_key_not_found(self): request = DummyRequest(['/keys']) request.addArg('search', 'some@inexistent.key') - when(self.keymanager).get_key('some@inexistent.key').thenReturn(defer.fail(KeyNotFound())) + when(self.keymanager).get_key( + 'some@inexistent.key').thenReturn(defer.fail(KeyNotFound())) d = self.web.get(request) @@ -41,7 +43,8 @@ def assert_404(_): def test_returns_the_key_as_json_if_found(self): request = DummyRequest(['/keys']) request.addArg('search', 'some@key') - when(self.keymanager).get_key('some@key').thenReturn(defer.succeed(OpenPGPKey('some@key'))) + when(self.keymanager).get_key('some@key').thenReturn( + defer.succeed(OpenPGPKey('some@key'))) d = self.web.get(request) @@ -68,7 +71,8 @@ def assert_response(_): def test_returns_unauthorized_if_key_is_private(self): request = DummyRequest(['/keys']) request.addArg('search', 'some@key') - when(self.keymanager).get_key('some@key').thenReturn(defer.succeed(OpenPGPKey('some@key', private=True))) + when(self.keymanager).get_key('some@key').thenReturn( + defer.succeed(OpenPGPKey('some@key', private=True))) d = self.web.get(request) diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index 45036f8d6..f90159879 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -13,6 +13,7 @@ class TestParseAcceptLanguage(unittest.TestCase): + def test_parse_pt_br_simple(self): all_headers = { 'accept-language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3'} @@ -33,6 +34,7 @@ def test_parse_pt_br_as_default(self): class TestLoginResource(unittest.TestCase): + def setUp(self): self.services_factory = mock() self.portal = mock() @@ -131,7 +133,8 @@ def test_non_xml_compliant_banner_will_send_default_invalid_format_banner(self): def assert_default_invalid_banner_disclaimer_rendered(_): self.assertEqual(200, request.responseCode) written_response = ''.join(request.written) - self.assertIn("Invalid XML template format for service/_trial_temp/banner.txt.", written_response) + self.assertIn( + "Invalid XML template format for service/_trial_temp/banner.txt.", written_response) def tear_down(_): os.remove(banner_file_name) @@ -152,13 +155,15 @@ def test_wrong_banner_file_location_will_send_default_invalid_format_banner(self def assert_default_invalid_banner_disclaimer_rendered(_): self.assertEqual(200, request.responseCode) written_response = ''.join(request.written) - self.assertIn("Disclaimer banner file banner.txt could not be read or does not exit.", written_response) + self.assertIn( + "Disclaimer banner file banner.txt could not be read or does not exit.", written_response) d.addCallback(assert_default_invalid_banner_disclaimer_rendered) return d class TestLoginPOST(unittest.TestCase): + def setUp(self): self.services_factory = mock() self.provider = mock() @@ -203,12 +208,14 @@ def test_should_return_form_back_with_error_message_when_login_fails(self, mock_ d = self.web.get(self.request) def assert_error_response_and_user_services_not_setup(_): - mock_authenticate.assert_called_once_with(self.username, self.password) + mock_authenticate.assert_called_once_with( + self.username, self.password) self.assertEqual(401, self.request.responseCode) written_response = ''.join(self.request.written) self.assertIn('Invalid username or password', written_response) self.assertFalse(mock_user_bootstrap_setup.called) - self.assertFalse(self.resource.get_session(self.request).is_logged_in()) + self.assertFalse(self.resource.get_session( + self.request).is_logged_in()) d.addCallback(assert_error_response_and_user_services_not_setup) return d @@ -221,7 +228,8 @@ def test_successful_login_responds_interstitial(self, mock_authenticate, mock_us d = self.web.get(self.request) def assert_interstitial_in_response(_): - mock_authenticate.assert_called_once_with(self.username, self.password) + mock_authenticate.assert_called_once_with( + self.username, self.password) interstitial_js_in_template = '' self.assertIn(interstitial_js_in_template, self.request.written[0]) @@ -236,7 +244,8 @@ def test_successful_login_runs_user_services_bootstrap_when_interstitial_loaded( d = self.web.get(self.request) def assert_login_setup_service_for_user(_): - mock_user_bootstrap_setup.assert_called_once_with(self.user_auth, self.password, 'pt-BR') + mock_user_bootstrap_setup.assert_called_once_with( + self.user_auth, self.password, 'pt-BR') d.addCallback(assert_login_setup_service_for_user) return d @@ -251,7 +260,8 @@ def test_successful_adds_cookies_to_indicat_logged_in_status_when_services_are_l d = self.web.get(self.request) def assert_login_setup_service_for_user(_): - self.assertTrue(self.resource.get_session(self.request).is_logged_in()) + self.assertTrue(self.resource.get_session( + self.request).is_logged_in()) d.addCallback(assert_login_setup_service_for_user) return d diff --git a/service/test/unit/resources/test_logout_resources.py b/service/test/unit/resources/test_logout_resources.py index 33c8537f5..d7b4c8e6a 100644 --- a/service/test/unit/resources/test_logout_resources.py +++ b/service/test/unit/resources/test_logout_resources.py @@ -9,6 +9,7 @@ class TestLogoutResource(unittest.TestCase): + def setUp(self): self.services_factory = MagicMock() self.resource = LogoutResource(self.services_factory) @@ -28,7 +29,8 @@ def test_logout(self, mock_redirect): def expire_session_and_redirect(_): session = self.resource.get_session(request) - self.services_factory.destroy_session.assert_called_once_with(session.user_uuid) + self.services_factory.destroy_session.assert_called_once_with( + session.user_uuid) session.expire.assert_called_once_with() mock_redirect.assert_called_once_with('/login', request) diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py index bdd15657b..f2def52ac 100644 --- a/service/test/unit/resources/test_mails_resource.py +++ b/service/test/unit/resources/test_mails_resource.py @@ -27,6 +27,7 @@ class TestMailsResource(unittest.TestCase): + def setUp(self): self.mail_service = mock() self.services_factory = mock() @@ -34,7 +35,8 @@ def setUp(self): self.services = mock() self.services.mail_service = self.mail_service self.services.draft_service = mock() - self.services_factory._services_by_user = {'someuserid': self.mail_service} + self.services_factory._services_by_user = { + 'someuserid': self.mail_service} when(self.services_factory).services(ANY()).thenReturn(self.services) @patch('leap.common.events.register') @@ -46,7 +48,8 @@ def test_render_GET_should_unicode_mails_search_query(self, mock_register): request.addArg('p', 1) unicodified_search_term = u'coração' - when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.succeed(([], 0))) + when(self.mail_service).mails(unicodified_search_term, + 25, 1).thenReturn(defer.succeed(([], 0))) mails_resource = MailsResource(self.services_factory) web = DummySite(mails_resource) @@ -63,8 +66,10 @@ def test_render_PUT_should_store_draft_with_attachments(self, mock_register): request = DummyRequest([]) request.method = 'PUT' request.content = mock() - when(request.content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') - when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({'content': mock()})) + when(request.content).read().thenReturn( + '{"attachments": [{"ident": "some fake attachment id"}]}') + when(self.mail_service).attachment('some fake attachment id').thenReturn( + defer.succeed({'content': mock()})) mails_resource = MailsResource(self.services_factory) web = DummySite(mails_resource) @@ -81,8 +86,10 @@ def test_render_POST_should_send_email_with_attachments(self, mock_register): request = DummyRequest([]) request.method = 'POST' request.content = mock() - when(request.content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}') - when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"})) + when(request.content).read().thenReturn( + '{"attachments": [{"ident": "some fake attachment id"}]}') + when(self.mail_service).attachment('some fake attachment id').thenReturn( + defer.succeed({"content": "some content"})) as_dictable = mock() when(as_dictable).as_dict().thenReturn({}) when(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})\ @@ -94,7 +101,8 @@ def test_render_POST_should_send_email_with_attachments(self, mock_register): def assert_response(_): verify(self.mail_service).attachment('some fake attachment id') - verify(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]}) + verify(self.mail_service).send_mail( + {"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]}) d.addCallback(assert_response) return d diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index 5b1ed0817..d5787eb5b 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -20,7 +20,8 @@ def setUp(self): self.services_factory.mode = UserAgentMode(is_single_user=True) self.services = mock() self.services.mail_service = self.mail_service - self.services_factory._services_by_user = {'someuserid': self.mail_service} + self.services_factory._services_by_user = { + 'someuserid': self.mail_service} when(self.services_factory).services(ANY()).thenReturn(self.services) self.mail_service.account_email = self.MAIL_ADDRESS @@ -55,7 +56,8 @@ def _test_should_renew_xsrf_cookie(self): d = self.web.get(request) def assert_csrf_cookie(_): - request.addCookie.assert_called_once_with('XSRF-TOKEN', generated_csrf_token) + request.addCookie.assert_called_once_with( + 'XSRF-TOKEN', generated_csrf_token) d.addCallback(assert_csrf_cookie) return d @@ -84,7 +86,8 @@ def assert_unavailable(_): return d def _mock_ajax_csrf(self, request, csrf_token): - request.requestHeaders.setRawHeaders('x-requested-with', ['XMLHttpRequest']) + request.requestHeaders.setRawHeaders( + 'x-requested-with', ['XMLHttpRequest']) request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self): diff --git a/service/test/unit/resources/test_sandbox_resource.py b/service/test/unit/resources/test_sandbox_resource.py index 4f263af12..96e6f5949 100644 --- a/service/test/unit/resources/test_sandbox_resource.py +++ b/service/test/unit/resources/test_sandbox_resource.py @@ -9,6 +9,7 @@ class TestSandBoxResource(unittest.TestCase): + def setUp(self): static_folder = os.path.dirname(os.path.abspath(__file__)) self.resource = SandboxResource(static_folder) @@ -33,6 +34,9 @@ def test_render_GET_should_set_sandbox_csp_header(self): yield self.web.get(request) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders('X-Content-Security-Policy'.lower())[0]) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders('Content-Security-Policy'.lower())[0]) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders('X-Webkit-CSP'.lower())[0]) + self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( + 'X-Content-Security-Policy'.lower())[0]) + self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( + 'Content-Security-Policy'.lower())[0]) + self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( + 'X-Webkit-CSP'.lower())[0]) diff --git a/service/test/unit/resources/test_tags_resource.py b/service/test/unit/resources/test_tags_resource.py index a05da7a7c..4fbb422ce 100644 --- a/service/test/unit/resources/test_tags_resource.py +++ b/service/test/unit/resources/test_tags_resource.py @@ -6,6 +6,7 @@ class TestTagsResource(unittest.TestCase): + def setUp(self): self.services_factory = MagicMock() self.resource = TagsResource(self.services_factory) diff --git a/service/test/unit/resources/test_user_settings_resource.py b/service/test/unit/resources/test_user_settings_resource.py index e9748e72a..16358f91b 100644 --- a/service/test/unit/resources/test_user_settings_resource.py +++ b/service/test/unit/resources/test_user_settings_resource.py @@ -26,7 +26,8 @@ def setUp(self): self.services_factory.mode = UserAgentMode(is_single_user=True) self.services.mail_service = self.mail_service self.services.keymanager = self.keymanager - self.services_factory._services_by_user = {'someuserid': self.keymanager} + self.services_factory._services_by_user = { + 'someuserid': self.keymanager} self.resource = UserSettingsResource(self.services_factory) when(self.services_factory).services(any()).thenReturn(self.services) self.web = DummySite(self.resource) @@ -35,7 +36,8 @@ def test_fingerprint_given(self): key = OpenPGPKey(MAIL_ADDRESS) key.fingerprint = FINGERPRINT request = DummyRequest(['/user-settings']) - when(self.keymanager).get_key(MAIL_ADDRESS).thenReturn(defer.succeed(key)) + when(self.keymanager).get_key( + MAIL_ADDRESS).thenReturn(defer.succeed(key)) d = self.web.get(request) @@ -51,7 +53,8 @@ def test_fingerprint_missing(self): key = OpenPGPKey(MAIL_ADDRESS) key.fingerprint = FINGERPRINT request = DummyRequest(['/user-settings']) - when(self.keymanager).get_key(MAIL_ADDRESS).thenReturn(defer.fail(Failure)) + when(self.keymanager).get_key( + MAIL_ADDRESS).thenReturn(defer.fail(Failure)) d = self.web.get(request) diff --git a/service/test/unit/resources/test_users_resource.py b/service/test/unit/resources/test_users_resource.py index 4ca578ae2..f48d47cd6 100644 --- a/service/test/unit/resources/test_users_resource.py +++ b/service/test/unit/resources/test_users_resource.py @@ -24,7 +24,8 @@ def test_numbers_of_users_online(self): def assert_users_count(_): self.assertEqual(200, request.code) - self.assertEqual('{"count": %d}' % number_of_users_online, request.written[0]) + self.assertEqual('{"count": %d}' % + number_of_users_online, request.written[0]) d.addCallback(assert_users_count) return d @@ -55,7 +56,8 @@ def test_is_admin_is_queried_from_leap_auth(self): request = mock() resource = UsersResource(service_factory) - when(resource)._get_user_id_from_request(request).thenReturn('some_id1') + when(resource)._get_user_id_from_request( + request).thenReturn('some_id1') self.assertTrue(resource.is_admin(request)) verify(auth).is_admin() diff --git a/service/test/unit/support/mail_generator_test.py b/service/test/unit/support/mail_generator_test.py index dd6da5222..709ae300d 100644 --- a/service/test/unit/support/mail_generator_test.py +++ b/service/test/unit/support/mail_generator_test.py @@ -28,7 +28,8 @@ def test_generator(self): mail = """Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Scott\'s Laws with a longer subject Scott\'s Laws\nTo: alice@domain.test\nFrom: bob@domain.test\nDate: Sun, 18 Oct 2015 21:45:13 -0000\nX-Tags: \nX-Leap-Encryption: true\nX-Leap-Signature: valid\n\nFirst Law: No matter what goes wrong, it will probably look right. Scott\'s Second Law: When an error has been detected and corrected, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will be impossible to fit the original quantity back into the \n\n First Law: No matter what goes wrong, it will be impossible to fit the original quantity back into the \n\n Scott\'s Second Law: When an error has been found in error, it will be found to have been wrong in the first place. After the correction has been found in error, it will be impossible to fit the original quantity back into the \n\n Second Law: When an error""" receiver = 'alice' domain_name = 'domain.test' - mbox_file = pkg_resources.resource_filename('test.unit.fixtures', 'mbox') + mbox_file = pkg_resources.resource_filename( + 'test.unit.fixtures', 'mbox') mails = mbox(mbox_file) rnd = random.Random(0) diff --git a/service/test/unit/support/test_markov.py b/service/test/unit/support/test_markov.py index 911cef30e..b258a9a43 100644 --- a/service/test/unit/support/test_markov.py +++ b/service/test/unit/support/test_markov.py @@ -29,7 +29,8 @@ def setUp(self): self.random = random.Random(0) def test_starts_with_capital_case_workd(self): - gen = MarkovGenerator(['lower Upper smaller Capital'], random=self.random) + gen = MarkovGenerator( + ['lower Upper smaller Capital'], random=self.random) result = gen.generate(1) @@ -54,14 +55,16 @@ def test_minimum_three_words(self): self.assertRaises(ValueError, MarkovGenerator, ['1', '2', '3']) def test_add_paragraph_on_empty_chain(self): - gen = MarkovGenerator([SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True) + gen = MarkovGenerator( + [SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True) result = gen.generate(5) self.assertEqual('One two three four \n\n One', result) def test_multiple_inputs(self): - gen = MarkovGenerator([SAMPLE_TEXT, 'Five Six seven eight'], random=self.random) + gen = MarkovGenerator( + [SAMPLE_TEXT, 'Five Six seven eight'], random=self.random) result = gen.generate(3) @@ -76,7 +79,8 @@ def test_add(self): self.assertEqual('One two three', result) def test_multiple_word_occurences(self): - gen = MarkovGenerator(['One Two One Three One Two One Four'], random=self.random) + gen = MarkovGenerator( + ['One Two One Three One Two One Four'], random=self.random) result = gen.generate(2) diff --git a/service/test/unit/support/test_replier.py b/service/test/unit/support/test_replier.py index ef9b321cf..38a38a621 100644 --- a/service/test/unit/support/test_replier.py +++ b/service/test/unit/support/test_replier.py @@ -3,13 +3,15 @@ class TestReplier(unittest.TestCase): + def test_reply_all_dont_exclude_own_address_if_only_recipient(self): current_user = sender = 'me@pixelated.org' to = [sender] cc = [] reply_dict = replier.generate_recipients(sender, to, cc, current_user) - expected = {'single': sender, 'all': {'to-field': [current_user], 'cc-field': []}} + expected = {'single': sender, 'all': { + 'to-field': [current_user], 'cc-field': []}} self.assertEquals(expected, reply_dict) def test_reply_all_does_not_contain_own_address_in_to(self): @@ -19,7 +21,8 @@ def test_reply_all_does_not_contain_own_address_in_to(self): cc = [] reply_dict = replier.generate_recipients(sender, to, cc, current_user) - expected = {'single': sender, 'all': {'to-field': ['test@pixelated.org', sender], 'cc-field': []}} + expected = {'single': sender, 'all': { + 'to-field': ['test@pixelated.org', sender], 'cc-field': []}} self.assertEquals(expected, reply_dict) def test_reply_all_does_not_contain_own_address_in_cc(self): @@ -29,7 +32,8 @@ def test_reply_all_does_not_contain_own_address_in_cc(self): cc = ['test2@pixelated.org', current_user] reply_dict = replier.generate_recipients(sender, to, cc, current_user) - expected = {'single': sender, 'all': {'to-field': ['test@pixelated.org', sender], 'cc-field': ['test2@pixelated.org']}} + expected = {'single': sender, 'all': { + 'to-field': ['test@pixelated.org', sender], 'cc-field': ['test2@pixelated.org']}} self.assertEquals(expected, reply_dict) def test_reply_single_swaps_current_user_and_recipient_if_a_am_the_sender(self): @@ -38,5 +42,6 @@ def test_reply_single_swaps_current_user_and_recipient_if_a_am_the_sender(self): cc = [] reply_dict = replier.generate_recipients(sender, to, cc, current_user) - expected = {'single': 'test@pixelated.org', 'all': {'to-field': ['test@pixelated.org'], 'cc-field': []}} + expected = {'single': 'test@pixelated.org', 'all': { + 'to-field': ['test@pixelated.org'], 'cc-field': []}} self.assertEquals(expected, reply_dict) diff --git a/service/test/unit/test_application.py b/service/test/unit/test_application.py index 67c044c28..f6b1b8989 100644 --- a/service/test/unit/test_application.py +++ b/service/test/unit/test_application.py @@ -8,6 +8,7 @@ class ApplicationTest(unittest.TestCase): class MockConfig: + def __init__(self, port, host, sslkey=None, sslcert=None, manhole=False): self.port = port self.host = host @@ -24,7 +25,8 @@ def test_that_start_site_binds_to_tcp_port_if_no_ssl_options(self, reactor_mock, pixelated.application.start_site(config, app_mock) - reactor_mock.listenTCP.assert_called_once_with(12345, ANY, interface='127.0.0.1') + reactor_mock.listenTCP.assert_called_once_with( + 12345, ANY, interface='127.0.0.1') @patch('leap.common.events.client') @patch('pixelated.application.reactor') @@ -32,11 +34,13 @@ def test_that_start_site_binds_to_ssl_if_ssl_options(self, reactor_mock, _): app_mock = MagicMock() pixelated.application._ssl_options = lambda x, y: 'options' - config = ApplicationTest.MockConfig(12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert") + config = ApplicationTest.MockConfig( + 12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert") pixelated.application.start_site(config, app_mock) - reactor_mock.listenSSL.assert_called_once_with(12345, ANY, 'options', interface='127.0.0.1') + reactor_mock.listenSSL.assert_called_once_with( + 12345, ANY, 'options', interface='127.0.0.1') @patch('leap.common.events.client') @patch('pixelated.application.reactor') @@ -49,7 +53,8 @@ def test_that_start_user_agent_binds_to_tcp_port_if_no_ssl_options(self, service leap_session.fresh_account = False config = ApplicationTest.MockConfig(12345, '127.0.0.1', leap_session) - d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session) + d = pixelated.application.start_user_agent_in_single_user_mode( + app_mock, services_factory_mock, config.home, leap_session) def _assert(_): services_mock.assert_called_once_with(leap_session) @@ -68,9 +73,11 @@ def test_that_start_user_agent_binds_to_ssl_if_ssl_options(self, services_mock, leap_session.fresh_account = False pixelated.application._ssl_options = lambda x, y: 'options' - config = ApplicationTest.MockConfig(12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert") + config = ApplicationTest.MockConfig( + 12345, '127.0.0.1', sslkey="sslkey", sslcert="sslcert") - d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session) + d = pixelated.application.start_user_agent_in_single_user_mode( + app_mock, services_factory_mock, config.home, leap_session) def _assert(_): services_mock.assert_called_once_with(leap_session) @@ -94,25 +101,32 @@ def test_should_log_user_out_if_invalid_soledad_token(self, services_mock, react register_mock.register.return_value = None config = ApplicationTest.MockConfig(12345, '127.0.0.1') - d = pixelated.application.start_user_agent_in_single_user_mode(app_mock, services_factory_mock, config.home, leap_session) + d = pixelated.application.start_user_agent_in_single_user_mode( + app_mock, services_factory_mock, config.home, leap_session) - pixelated.application.add_top_level_system_callbacks(d, services_factory_mock) + pixelated.application.add_top_level_system_callbacks( + d, services_factory_mock) def _assert_user_logged_out_using_uuid(_): used_arguments = register_mock.call_args[0] self.assertIsNotNone(used_arguments) soledad_invalid_auth_event = used_arguments[0] - self.assertEqual(soledad_invalid_auth_event, events.SOLEDAD_INVALID_AUTH_TOKEN) + self.assertEqual(soledad_invalid_auth_event, + events.SOLEDAD_INVALID_AUTH_TOKEN) used_log_out_method = used_arguments[1] - used_log_out_method(events.SOLEDAD_INVALID_AUTH_TOKEN, {'uuid': 'some_uuid'}) - mock_service_log_user_out.assert_called_once_with(user_id='some_uuid') + used_log_out_method(events.SOLEDAD_INVALID_AUTH_TOKEN, { + 'uuid': 'some_uuid'}) + mock_service_log_user_out.assert_called_once_with( + user_id='some_uuid') def _assert_user_logged_out_using_email_id(_): mock_service_log_user_out.reset_mock() used_arguments = register_mock.call_args[0] used_log_out_method = used_arguments[1] - used_log_out_method(events.SOLEDAD_INVALID_AUTH_TOKEN, 'haha@ayo.yo') - mock_service_log_user_out.assert_called_once_with(user_id='haha@ayo.yo', using_email=True) + used_log_out_method( + events.SOLEDAD_INVALID_AUTH_TOKEN, 'haha@ayo.yo') + mock_service_log_user_out.assert_called_once_with( + user_id='haha@ayo.yo', using_email=True) d.addCallback(_assert_user_logged_out_using_uuid) d.addCallback(_assert_user_logged_out_using_email_id) @@ -125,13 +139,16 @@ def test_should_defer_fail_errors_during_multi_user_start_site(self, mock_multi_ root_resources_mock = MagicMock() services_factory_mock = MagicMock() - mock_multi_user_bootstrap.side_effect = Exception('multi-user failed bootstrap for whatever reason') + mock_multi_user_bootstrap.side_effect = Exception( + 'multi-user failed bootstrap for whatever reason') - d = pixelated.application._start_in_multi_user_mode(args_mock, root_resources_mock, services_factory_mock) + d = pixelated.application._start_in_multi_user_mode( + args_mock, root_resources_mock, services_factory_mock) def _assert_the_same_error_is_relayed_in_the_deferred(e): self.assertIsInstance(e.value, Exception) - self.assertEqual(e.value.message, 'multi-user failed bootstrap for whatever reason') + self.assertEqual( + e.value.message, 'multi-user failed bootstrap for whatever reason') d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred) return d @@ -144,13 +161,16 @@ def test_should_defer_fail_errors_during_multi_user_bootstrap(self, ignore_setup root_resources_mock = MagicMock() services_factory_mock = MagicMock() - mock_start_site.side_effect = Exception('multi-user failed start site for whatever reason') + mock_start_site.side_effect = Exception( + 'multi-user failed start site for whatever reason') - d = pixelated.application._start_in_multi_user_mode(args_mock, root_resources_mock, services_factory_mock) + d = pixelated.application._start_in_multi_user_mode( + args_mock, root_resources_mock, services_factory_mock) def _assert_the_same_error_is_relayed_in_the_deferred(e): self.assertIsInstance(e.value, Exception) - self.assertEqual(e.value.message, 'multi-user failed start site for whatever reason') + self.assertEqual( + e.value.message, 'multi-user failed start site for whatever reason') d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred) return d diff --git a/service/test/unit/test_authentication.py b/service/test/unit/test_authentication.py index 3e1b4a105..4e179f50b 100644 --- a/service/test/unit/test_authentication.py +++ b/service/test/unit/test_authentication.py @@ -20,6 +20,7 @@ class AuthenticatorTest(unittest.TestCase): + def setUp(self): self._domain = 'domain.org' with patch.object(LeapProvider, 'fetch_provider_json', return_value=PROVIDER_JSON): @@ -35,17 +36,20 @@ def test_bonafide_srp_exceptions_should_raise_unauthorized_login(self): try: yield auth.authenticate('username', 'password') except UnauthorizedLogin as e: - self.assertEqual("User typed wrong password/username combination.", e.message) + self.assertEqual( + "User typed wrong password/username combination.", e.message) raise @inlineCallbacks def test_domain_name_is_stripped_before_making_bonafide_srp_auth(self): username_without_domain = 'username' - username_with_domain = '%s@%s' % (username_without_domain, self._domain) + username_with_domain = '%s@%s' % ( + username_without_domain, self._domain) auth = Authenticator(self._leap_provider) with patch.object(Authenticator, '_bonafide_auth') as mock_leap_authenticate: yield auth.authenticate(username_with_domain, 'password') - mock_leap_authenticate.assert_called_once_with(username_without_domain, 'password') + mock_leap_authenticate.assert_called_once_with( + username_without_domain, 'password') @inlineCallbacks def test_successful_bonafide_auth_should_return_the_user_authentication_object(self): @@ -65,17 +69,21 @@ def test_successful_bonafide_auth_should_return_the_user_authentication_object(s def test_username_without_domain_is_not_changed(self): username_without_domain = 'username' auth = Authenticator(self._leap_provider) - self.assertEqual(username_without_domain, auth.clean_username(username_without_domain)) + self.assertEqual(username_without_domain, + auth.clean_username(username_without_domain)) def test_username_with_domain_is_stripped(self): username_without_domain = 'username' - username_with_domain = '%s@%s' % (username_without_domain, self._domain) + username_with_domain = '%s@%s' % ( + username_without_domain, self._domain) auth = Authenticator(self._leap_provider) - self.assertEqual(username_without_domain, auth.clean_username(username_with_domain)) + self.assertEqual(username_without_domain, + auth.clean_username(username_with_domain)) def test_username_with_wrong_domain_raises_exception(self): username_without_domain = 'username' - username_with_wrong_domain = '%s@%s' % (username_without_domain, 'wrongdomain.org') + username_with_wrong_domain = '%s@%s' % ( + username_without_domain, 'wrongdomain.org') auth = Authenticator(self._leap_provider) with self.assertRaises(UnauthorizedLogin): try: From ab600f022f1f8794d90d8d898ec8a5eab327b345 Mon Sep 17 00:00:00 2001 From: KrunoKnego Date: Fri, 13 Jan 2017 00:58:07 +0100 Subject: [PATCH 15/15] Ran autopep8 --- .../listeners/mailbox_indexer_listener.py | 2 +- .../adapter/mailstore/body_parser.py | 20 ++- .../mailstore/leap_attachment_store.py | 17 ++- .../adapter/mailstore/leap_mailstore.py | 89 ++++++++--- .../pixelated/adapter/mailstore/mailstore.py | 6 +- .../adapter/mailstore/maintenance/__init__.py | 21 ++- .../adapter/mailstore/searchable_mailstore.py | 2 +- service/pixelated/adapter/model/mail.py | 9 +- service/pixelated/adapter/model/status.py | 3 +- service/pixelated/adapter/search/__init__.py | 43 ++++-- service/pixelated/adapter/search/contacts.py | 5 +- .../adapter/search/index_storage_key.py | 5 +- .../adapter/services/feedback_service.py | 3 +- .../pixelated/adapter/services/mail_sender.py | 9 +- .../adapter/services/mail_service.py | 18 ++- service/pixelated/application.py | 38 ++++- service/pixelated/authentication.py | 6 +- .../pixelated/bitmask_libraries/keymanager.py | 17 ++- .../pixelated/bitmask_libraries/provider.py | 37 ++++- service/pixelated/bitmask_libraries/smtp.py | 7 +- service/pixelated/config/arguments.py | 129 ++++++++++++---- service/pixelated/config/leap.py | 11 +- service/pixelated/config/logger.py | 9 +- service/pixelated/config/services.py | 3 +- service/pixelated/config/sessions.py | 17 ++- .../pixelated/extensions/requests_urllib3.py | 3 +- service/pixelated/register.py | 12 +- .../resources/attachments_resource.py | 6 +- service/pixelated/resources/auth.py | 10 +- service/pixelated/resources/login_resource.py | 32 +++- service/pixelated/resources/mail_resource.py | 3 +- service/pixelated/resources/mails_resource.py | 3 +- service/pixelated/resources/root_resource.py | 19 ++- service/pixelated/resources/users.py | 3 +- service/pixelated/support/__init__.py | 10 +- service/pixelated/support/clock.py | 8 +- .../support/encrypted_file_storage.py | 5 +- service/pixelated/support/mail_generator.py | 11 +- service/pixelated/support/replier.py | 5 +- service/pixelated/support/tls_adapter.py | 17 ++- .../functional/features/steps/attachments.py | 3 +- .../test/functional/features/steps/common.py | 39 +++-- .../functional/features/steps/data_setup.py | 6 +- .../functional/features/steps/mail_list.py | 3 +- .../test/integration/test_leap_mailstore.py | 61 +++++++- service/test/integration/test_logout.py | 3 +- .../test/integration/test_multi_user_login.py | 21 ++- .../integration/test_retrieve_attachment.py | 26 ++-- service/test/integration/test_tags.py | 6 +- service/test/perf/contacts/test_Contacts.py | 18 ++- service/test/perf/search/test_Search.py | 21 ++- .../support/integration/app_test_client.py | 100 ++++++++++--- service/test/support/integration/model.py | 9 +- .../support/integration/multi_user_client.py | 30 +++- service/test/support/test_helper.py | 41 ++++-- .../adapter/mailstore/test_body_parser.py | 33 +++-- .../mailstore/test_leap_attachment_store.py | 105 +++++++++---- .../unit/adapter/mailstore/test_leap_mail.py | 60 ++++++-- .../adapter/mailstore/test_leap_mailstore.py | 138 +++++++++++++----- .../test/unit/adapter/search/test_search.py | 12 +- .../unit/adapter/services/test_mail_sender.py | 31 ++-- service/test/unit/adapter/test_mail.py | 42 ++++-- .../test/unit/adapter/test_mail_service.py | 6 +- .../bitmask_libraries/test_abstract_leap.py | 15 +- .../unit/bitmask_libraries/test_keymanager.py | 12 +- .../unit/bitmask_libraries/test_provider.py | 34 +++-- .../test_smtp_client_certificate.py | 13 +- service/test/unit/config/test_leap.py | 23 ++- service/test/unit/config/test_register.py | 4 +- service/test/unit/config/test_services.py | 6 +- service/test/unit/config/test_sessions.py | 12 +- service/test/unit/config/test_site.py | 12 +- .../test/unit/maintenance/test_commands.py | 14 +- service/test/unit/resources/__init__.py | 3 +- .../resources/test_attachments_resource.py | 28 +++- .../unit/resources/test_login_resource.py | 31 ++-- .../unit/resources/test_mails_resource.py | 10 +- .../test/unit/resources/test_root_resource.py | 6 +- .../unit/resources/test_sandbox_resource.py | 18 ++- service/test/unit/support/test_markov.py | 4 +- service/test/unit/support/test_replier.py | 3 +- service/test/unit/test_application.py | 36 +++-- service/test/unit/test_authentication.py | 3 +- 83 files changed, 1316 insertions(+), 458 deletions(-) diff --git a/service/pixelated/adapter/listeners/mailbox_indexer_listener.py b/service/pixelated/adapter/listeners/mailbox_indexer_listener.py index a660062b3..dfb8bf2dd 100644 --- a/service/pixelated/adapter/listeners/mailbox_indexer_listener.py +++ b/service/pixelated/adapter/listeners/mailbox_indexer_listener.py @@ -48,7 +48,7 @@ def notify_new(self): missing_idents = soledad_idents.difference(indexed_idents) self.search_engine.index_mails((yield self.mail_store.get_mails(missing_idents, include_body=True))) - except Exception, e: # this is a event handler, don't let exceptions escape + except Exception as e: # this is a event handler, don't let exceptions escape logger.error(e) def __eq__(self, other): diff --git a/service/pixelated/adapter/mailstore/body_parser.py b/service/pixelated/adapter/mailstore/body_parser.py index c40a744f8..9349eca58 100644 --- a/service/pixelated/adapter/mailstore/body_parser.py +++ b/service/pixelated/adapter/mailstore/body_parser.py @@ -22,16 +22,25 @@ logger = Logger() -def _parse_charset_header(content_type_and_charset_header, default_charset='us-ascii'): +def _parse_charset_header( + content_type_and_charset_header, + default_charset='us-ascii'): try: - return re.compile('.*charset="?([a-zA-Z0-9-]+)"?', re.MULTILINE | re.DOTALL).match(content_type_and_charset_header).group(1) + return re.compile( + '.*charset="?([a-zA-Z0-9-]+)"?', + re.MULTILINE | re.DOTALL).match(content_type_and_charset_header).group(1) except: return default_charset class BodyParser(object): - def __init__(self, content, content_type='text/plain; charset="us-ascii"', content_transfer_encoding=None, charset=None): + def __init__( + self, + content, + content_type='text/plain; charset="us-ascii"', + content_transfer_encoding=None, + charset=None): self._content = content self._content_type = content_type self._content_transfer_encoding = content_transfer_encoding @@ -62,9 +71,10 @@ def _serialize_for_parser(self, charset): if isinstance(self._content, unicode): try: return encoded_text + self._content.encode(charset) - except UnicodeError, e: + except UnicodeError as e: logger.warn( - 'Failed to encode content for charset %s. Ignoring invalid chars: %s' % (charset, e)) + 'Failed to encode content for charset %s. Ignoring invalid chars: %s' % + (charset, e)) return encoded_text + self._content.encode(charset, 'ignore') else: return encoded_text + self._content diff --git a/service/pixelated/adapter/mailstore/leap_attachment_store.py b/service/pixelated/adapter/mailstore/leap_attachment_store.py index 7f9025b16..bb192f715 100644 --- a/service/pixelated/adapter/mailstore/leap_attachment_store.py +++ b/service/pixelated/adapter/mailstore/leap_attachment_store.py @@ -19,8 +19,9 @@ def get_mail_attachment(self, attachment_id): results = yield self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', attachment_id) if attachment_id else [] if results: content = ContentDocWrapper(**results[0].content) - defer.returnValue({'content-type': content.content_type, 'content': self._try_decode( - content.raw, content.content_transfer_encoding)}) + defer.returnValue({'content-type': content.content_type, + 'content': self._try_decode(content.raw, + content.content_transfer_encoding)}) else: raise ValueError('No attachment with id %s found!' % attachment_id) @@ -45,7 +46,11 @@ def _try_decode(self, raw, encoding): return bytearray(data) - def _attachment_to_cdoc(self, content, content_type, encoder=encoders.encode_base64): + def _attachment_to_cdoc( + self, + content, + content_type, + encoder=encoders.encode_base64): major, sub = content_type.split('/') attachment = MIMENonMultipart(major, sub) attachment.set_payload(content) @@ -62,6 +67,10 @@ def _attachment_to_cdoc(self, content, content_type, encoder=encoders.encode_bas cdoc = tmp_mail.get_wrapper().cdocs[1] return cdoc - def _calc_attachment_id_(self, content, content_type, encoder=encoders.encode_base64): + def _calc_attachment_id_( + self, + content, + content_type, + encoder=encoders.encode_base64): cdoc = self._attachment_to_cdoc(content, content_type, encoder) return cdoc.phash diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 1a4887198..374a0f57a 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -31,7 +31,13 @@ class AttachmentInfo(object): - def __init__(self, ident, name, encoding=None, ctype='application/octet-stream', size=0): + def __init__( + self, + ident, + name, + encoding=None, + ctype='application/octet-stream', + size=0): self.ident = ident self.name = name self.encoding = encoding @@ -39,18 +45,33 @@ def __init__(self, ident, name, encoding=None, ctype='application/octet-stream', self.size = size def __repr__(self): - return 'AttachmentInfo[%s, %s, %s]' % (self.ident, self.name, self.encoding) + return 'AttachmentInfo[%s, %s, %s]' % ( + self.ident, self.name, self.encoding) def __str__(self): - return 'AttachmentInfo[%s, %s, %s]' % (self.ident, self.name, self.encoding) + return 'AttachmentInfo[%s, %s, %s]' % ( + self.ident, self.name, self.encoding) def as_dict(self): - return {'ident': self.ident, 'name': self.name, 'encoding': self.encoding, 'size': self.size, 'content-type': self.ctype} + return { + 'ident': self.ident, + 'name': self.name, + 'encoding': self.encoding, + 'size': self.size, + 'content-type': self.ctype} class LeapMail(Mail): - def __init__(self, mail_id, mailbox_name, headers=None, tags=set(), flags=set(), body=None, attachments=[]): + def __init__( + self, + mail_id, + mailbox_name, + headers=None, + tags=set(), + flags=set(), + body=None, + attachments=[]): self._mail_id = mail_id self._mailbox_name = mailbox_name self._headers = headers if headers is not None else {} @@ -62,7 +83,8 @@ def __init__(self, mail_id, mailbox_name, headers=None, tags=set(), flags=set(), @property def headers(self): cpy = dict(self._headers) - for name in set(self._headers.keys()).intersection(['To', 'Cc', 'Bcc']): + for name in set(self._headers.keys()).intersection( + ['To', 'Cc', 'Bcc']): cpy[name] = [address.strip() for address in ( self._headers[name].split(',') if self._headers[name] else [])] @@ -129,14 +151,21 @@ def _remove_duplicates(self, values): def _decoded_header_utf_8(self, header_value): if isinstance(header_value, list): - return self._remove_duplicates([self._decoded_header_utf_8(v) for v in header_value]) + return self._remove_duplicates( + [self._decoded_header_utf_8(v) for v in header_value]) elif header_value is not None: def encode_chunk(content, encoding): - return unicode(content.strip(), encoding=encoding or 'ascii', errors='ignore') + return unicode( + content.strip(), + encoding=encoding or 'ascii', + errors='ignore') try: - encoded_chunks = [encode_chunk( - content, encoding) for content, encoding in decode_header(header_value)] + encoded_chunks = [ + encode_chunk( + content, + encoding) for content, + encoding in decode_header(header_value)] # decode_header strips whitespaces on all chunks, joining over # ' ' is only a workaround, not a proper fix return ' '.join(encoded_chunks) @@ -145,16 +174,19 @@ def encode_chunk(content, encoding): def as_dict(self): return { - 'header': {k.lower(): self._decoded_header_utf_8(v) for k, v in self.headers.items()}, + 'header': { + k.lower(): self._decoded_header_utf_8(v) for k, + v in self.headers.items()}, 'ident': self._mail_id, 'tags': self.tags, - 'status': list(self.status), + 'status': list( + self.status), 'body': self._body, 'security_casing': self.security_casing, 'textPlainBody': self._body, 'mailbox': self._mailbox_name.lower(), - 'attachments': [attachment.as_dict() for attachment in self._attachments] - } + 'attachments': [ + attachment.as_dict() for attachment in self._attachments]} @staticmethod def from_dict(mail_dict): @@ -223,7 +255,11 @@ def get_raw_mail(self, mail_id, include_body=False): defer.returnValue(leap_mail) @defer.inlineCallbacks - def get_mails(self, mail_ids, gracefully_ignore_errors=False, include_body=False): + def get_mails( + self, + mail_ids, + gracefully_ignore_errors=False, + include_body=False): deferreds = [] for mail_id in mail_ids: deferreds.append(self.get_mail(mail_id, include_body=include_body)) @@ -342,8 +378,13 @@ def _leap_message_to_leap_mail(self, mail_id, message, include_body): mbox_uuid = message.get_wrapper().fdoc.mbox_uuid mbox_name = yield self._mailbox_name_from_uuid(mbox_uuid) attachments = self._extract_attachment_info_from(message) - mail = LeapMail(mail_id, mbox_name, message.get_wrapper().hdoc.headers, set(message.get_tags()), set( - message.get_flags()), body=body, attachments=attachments) # TODO assert flags are passed on + mail = LeapMail(mail_id, + mbox_name, + message.get_wrapper().hdoc.headers, + set(message.get_tags()), + set(message.get_flags()), + body=body, + attachments=attachments) # TODO assert flags are passed on defer.returnValue(mail) @@ -355,8 +396,11 @@ def _raw_message_body(self, message): # It fix the problem when leap doesn'r found body_phash and returns # empty string if not isinstance(content_doc, str): - parser = BodyParser(content_doc.raw, content_type=content_doc.content_type, - content_transfer_encoding=content_doc.content_transfer_encoding, charset=content_doc.charset) + parser = BodyParser( + content_doc.raw, + content_type=content_doc.content_type, + content_transfer_encoding=content_doc.content_transfer_encoding, + charset=content_doc.charset) defer.returnValue(parser.parsed_content()) @@ -375,7 +419,8 @@ def _get_or_create_mailbox(self, mailbox_name): defer.returnValue(mbx) def _fetch_msg_from_soledad(self, mail_id, load_body=False): - return SoledadMailAdaptor().get_msg_from_mdoc_id(Message, self.soledad, mail_id, get_cdocs=load_body) + return SoledadMailAdaptor().get_msg_from_mdoc_id( + Message, self.soledad, mail_id, get_cdocs=load_body) @defer.inlineCallbacks def _dump_soledad(self): @@ -396,7 +441,9 @@ def _is_attachment(self, part_map, headers): if 'multipart' in content_type: return False - if 'text/plain' == content_type and ((disposition == 'inline') or (disposition is None)): + if 'text/plain' == content_type and ( + (disposition == 'inline') or ( + disposition is None)): return False return True diff --git a/service/pixelated/adapter/mailstore/mailstore.py b/service/pixelated/adapter/mailstore/mailstore.py index ff66755bf..9579b74df 100644 --- a/service/pixelated/adapter/mailstore/mailstore.py +++ b/service/pixelated/adapter/mailstore/mailstore.py @@ -26,7 +26,11 @@ def get_raw_mail(self, mail_id): def get_mail_attachment(self, attachment_id): pass - def get_mails(self, mail_ids, gracefully_ignore_errors=False, include_body=False): + def get_mails( + self, + mail_ids, + gracefully_ignore_errors=False, + include_body=False): pass def all_mails(self): diff --git a/service/pixelated/adapter/mailstore/maintenance/__init__.py b/service/pixelated/adapter/mailstore/maintenance/__init__.py index 9316f7968..cc8392e8b 100644 --- a/service/pixelated/adapter/mailstore/maintenance/__init__.py +++ b/service/pixelated/adapter/mailstore/maintenance/__init__.py @@ -33,15 +33,18 @@ def _is_key_doc(doc): def _is_private_key_doc(doc): - return _is_key_doc(doc) and doc.content.get(leap_doc.KEY_PRIVATE_KEY, False) + return _is_key_doc(doc) and doc.content.get( + leap_doc.KEY_PRIVATE_KEY, False) def _is_active_key_doc(doc): - return _is_key_doc(doc) and doc.content.get(leap_doc.KEY_TYPE_KEY, None) == TYPE_OPENPGP_ACTIVE + return _is_key_doc(doc) and doc.content.get( + leap_doc.KEY_TYPE_KEY, None) == TYPE_OPENPGP_ACTIVE def _is_public_key(doc): - return _is_key_doc(doc) and not doc.content.get(leap_doc.KEY_PRIVATE_KEY, False) + return _is_key_doc(doc) and not doc.content.get( + leap_doc.KEY_PRIVATE_KEY, False) def _key_fingerprint(doc): @@ -65,7 +68,8 @@ def repair(self): docs) for doc in docs: - if _is_key_doc(doc) and _key_fingerprint(doc) not in private_key_fingerprints: + if _is_key_doc(doc) and _key_fingerprint( + doc) not in private_key_fingerprints: logger.warn('Deleting doc %s for key %s of <%s>' % (doc.doc_id, _key_fingerprint(doc), _address(doc))) yield self._soledad.delete_doc(doc) @@ -83,7 +87,8 @@ def _repair_missing_active_docs(self, docs, private_key_fingerprints): yield self._soledad.create_doc_from_json(OpenPGPKey(email, fingerprint=fingerprint, private=False).get_active_json()) def _key_fingerprints_with_private_key(self, docs): - return [doc.content[leap_doc.KEY_FINGERPRINT_KEY] for doc in docs if _is_private_key_doc(doc)] + return [doc.content[leap_doc.KEY_FINGERPRINT_KEY] + for doc in docs if _is_private_key_doc(doc)] def _missing_active_docs(self, docs, private_key_fingerprints): active_doc_ids = self._active_docs_for_key_fingerprint(docs) @@ -92,7 +97,8 @@ def _missing_active_docs(self, docs, private_key_fingerprints): def _emails_for_key_fingerprint(self, docs, fingerprint): for doc in docs: - if _is_private_key_doc(doc) and _key_fingerprint(doc) == fingerprint: + if _is_private_key_doc(doc) and _key_fingerprint( + doc) == fingerprint: email = _address(doc) if email is None: return [] @@ -101,4 +107,5 @@ def _emails_for_key_fingerprint(self, docs, fingerprint): return [email] def _active_docs_for_key_fingerprint(self, docs): - return [doc.content[leap_doc.KEY_FINGERPRINT_KEY] for doc in docs if _is_active_key_doc(doc) and _is_public_key(doc)] + return [doc.content[leap_doc.KEY_FINGERPRINT_KEY] + for doc in docs if _is_active_key_doc(doc) and _is_public_key(doc)] diff --git a/service/pixelated/adapter/mailstore/searchable_mailstore.py b/service/pixelated/adapter/mailstore/searchable_mailstore.py index e578e6a70..ffcc6818f 100644 --- a/service/pixelated/adapter/mailstore/searchable_mailstore.py +++ b/service/pixelated/adapter/mailstore/searchable_mailstore.py @@ -73,7 +73,7 @@ def __getattr__(self, name): :return: method or attribute """ methods = ([key for key, value in MailStore.__dict__.items() - if type(value) == FunctionType]) + if isinstance(value, FunctionType)]) if name in methods: SearchableMailStore._create_delegator(name) diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 78ac0f2c7..3f645b5b5 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -134,7 +134,9 @@ def _charset(self): def _parse_charset_header(self, charset_header, default_charset='utf-8'): try: - return re.compile('.*charset=([a-zA-Z0-9-]+)', re.MULTILINE | re.DOTALL).match(charset_header).group(1) + return re.compile( + '.*charset=([a-zA-Z0-9-]+)', + re.MULTILINE | re.DOTALL).match(charset_header).group(1) except: return default_charset @@ -201,7 +203,10 @@ def delivery_error_template(delivery_address): def from_dict(mail_dict, from_address): input_mail = InputMail() input_mail.headers = { - key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} + key.capitalize(): value for key, + value in mail_dict.get( + 'header', + {}).items()} input_mail.headers['Date'] = date.mail_date_now() input_mail.headers['From'] = from_address diff --git a/service/pixelated/adapter/model/status.py b/service/pixelated/adapter/model/status.py index 7101f4c01..232c7dd31 100644 --- a/service/pixelated/adapter/model/status.py +++ b/service/pixelated/adapter/model/status.py @@ -34,7 +34,8 @@ def from_flag(flag): @staticmethod def from_flags(flags): - return set(Status.from_flag(flag) for flag in flags if flag in Status.FLAGS_TO_STATUSES.keys()) + return set(Status.from_flag(flag) + for flag in flags if flag in Status.FLAGS_TO_STATUSES.keys()) @staticmethod def to_flags(statuses): diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py index 79f0b2812..b0bca085d 100644 --- a/service/pixelated/adapter/search/__init__.py +++ b/service/pixelated/adapter/search/__init__.py @@ -43,18 +43,31 @@ def __init__(self, key, user_home=DEFAULT_INDEX_HOME): os.makedirs(self.index_folder) self._index = self._create_index() - def _add_to_tags(self, tags, group, skip_default_tags, count_type, query=None): + def _add_to_tags( + self, + tags, + group, + skip_default_tags, + count_type, + query=None): query_matcher = re.compile( ".*%s.*" % query.lower()) if query else re.compile(".*") for tag, count in group.iteritems(): - if skip_default_tags and tag in self.DEFAULT_TAGS or not query_matcher.match(tag): + if skip_default_tags and tag in self.DEFAULT_TAGS or not query_matcher.match( + tag): continue if not tags.get(tag): - tags[tag] = {'ident': tag, 'name': tag, 'default': False, 'counts': {'total': 0, 'read': 0}, - 'mails': []} + tags[tag] = { + 'ident': tag, + 'name': tag, + 'default': False, + 'counts': { + 'total': 0, + 'read': 0}, + 'mails': []} tags[tag]['counts'][count_type] += count def _search_tag_groups(self, is_filtering_tags): @@ -118,7 +131,10 @@ def _mail_schema(self): def _create_index(self): storage = EncryptedFileStorage(self.index_folder, self.key) - return FileIndex.create(storage, self._mail_schema(), indexname='mails') + return FileIndex.create( + storage, + self._mail_schema(), + indexname='mails') def index_mail(self, mail): if mail is not None: @@ -168,7 +184,7 @@ def index_mails(self, mails, callback=None): self._index_mail(writer, mail) if callback: callback() - except Exception, e: + except Exception as e: traceback.print_exc(e) raise @@ -180,7 +196,8 @@ def _search_with_options(self, options, query): def search(self, query, window=25, page=1, all_mails=False): query = self.prepare_query(query) - return self._search_all_mails(query) if all_mails else self._paginated_search_mails(query, window, page) + return self._search_all_mails( + query) if all_mails else self._paginated_search_mails(query, window, page) def _search_all_mails(self, query): with self._index.searcher() as searcher: @@ -198,8 +215,13 @@ def _paginated_search_mails(self, query, window, page): 'tag', allow_overlap=True, maptype=sorting.Count) sorting_facet = sorting.FieldFacet('date', reverse=True) results = searcher.search_page( - query, page, pagelen=window, groupedby=tags_facet, sortedby=sorting_facet) - return unique([mail['ident'] for mail in results]), sum(results.results.groups().values()) + query, + page, + pagelen=window, + groupedby=tags_facet, + sortedby=sorting_facet) + return unique([mail['ident'] for mail in results]), sum( + results.results.groups().values()) def prepare_query(self, query): query = ( @@ -207,7 +229,8 @@ def prepare_query(self, query): .replace('-in:', 'AND NOT tag:') .replace('in:all', '*') ) - return MultifieldParser(['body', 'subject', 'raw'], self._index.schema).parse(query) + return MultifieldParser( + ['body', 'subject', 'raw'], self._index.schema).parse(query) def remove_from_index(self, mail_id): with AsyncWriter(self._index) as writer: diff --git a/service/pixelated/adapter/search/contacts.py b/service/pixelated/adapter/search/contacts.py index 733489b0e..845aa3924 100644 --- a/service/pixelated/adapter/search/contacts.py +++ b/service/pixelated/adapter/search/contacts.py @@ -36,7 +36,10 @@ def extract_mail_address(text): def contacts_suggestions(query, searcher): - return address_duplication_filter(search_addresses(searcher, query)) if query else [] + return address_duplication_filter( + search_addresses( + searcher, + query)) if query else [] def search_addresses(searcher, query): diff --git a/service/pixelated/adapter/search/index_storage_key.py b/service/pixelated/adapter/search/index_storage_key.py index b27618492..e06d2ddf3 100644 --- a/service/pixelated/adapter/search/index_storage_key.py +++ b/service/pixelated/adapter/search/index_storage_key.py @@ -39,4 +39,7 @@ def _new_index_key(self): return os.urandom(64) # 32 for encryption, 32 for hmac def _store_key_in_soledad(self, index_key): - return self._soledad.create_doc(dict(type='index_key', value=base64.encodestring(index_key))) + return self._soledad.create_doc( + dict( + type='index_key', + value=base64.encodestring(index_key))) diff --git a/service/pixelated/adapter/services/feedback_service.py b/service/pixelated/adapter/services/feedback_service.py index 5200a9ff5..1d1b57531 100644 --- a/service/pixelated/adapter/services/feedback_service.py +++ b/service/pixelated/adapter/services/feedback_service.py @@ -14,7 +14,6 @@ def open_ticket(self, feedback): "ticket[comments_attributes][0][body]": feedback, "ticket[subject]": "Feedback user-agent from {0}".format(account_mail), "ticket[email]": account_mail, - "ticket[regarding_user]": account_mail - } + "ticket[regarding_user]": account_mail} return requests.post(self.FEEDBACK_URL, data=data, verify=False) diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 26fdb0f1d..b2baac5ea 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -23,6 +23,7 @@ from twisted.internet import reactor, defer from pixelated.support.functional import flatten from twisted.mail.smtp import User +from functools import reduce class SMTPDownException(Exception): @@ -75,7 +76,10 @@ def _send_mail_to_all_recipients(self, mail, recipients): deferreds.append(outgoing_mail.send_message( mail.to_smtp_format(), smtp_recipient)) - return defer.DeferredList(deferreds, fireOnOneErrback=False, consumeErrors=True) + return defer.DeferredList( + deferreds, + fireOnOneErrback=False, + consumeErrors=True) def _define_bcc_field(self, mail, recipient, bccs): if recipient in bccs: @@ -85,7 +89,8 @@ def _define_bcc_field(self, mail, recipient, bccs): def _build_error_map(self, recipients, results): error_map = {} - for email, error in [(recipients[idx], r[1]) for idx, r in enumerate(results)]: + for email, error in [(recipients[idx], r[1]) + for idx, r in enumerate(results)]: error_map[email] = error return error_map diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 2ca7c702c..587f63459 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -28,7 +28,13 @@ class MailService(object): - def __init__(self, mail_sender, mail_store, search_engine, account_email, attachment_store): + def __init__( + self, + mail_sender, + mail_store, + search_engine, + account_email, + attachment_store): self.mail_store = mail_store self.search_engine = search_engine self.mail_sender = mail_sender @@ -50,7 +56,7 @@ def mails(self, query, window_size, page): try: mails = yield self.mail_store.get_mails(mail_ids) defer.returnValue((mails, total)) - except Exception, e: + except Exception as e: import traceback traceback.print_exc() raise @@ -61,7 +67,8 @@ def update_tags(self, mail_id, new_tags): reserved_words = extract_reserved_tags(new_tags) if len(reserved_words): raise ValueError( - 'None of the following words can be used as tags: ' + ' '.join(reserved_words)) + 'None of the following words can be used as tags: ' + + ' '.join(reserved_words)) new_tags = self._favor_existing_tags_casing(new_tags) mail = yield self.mail(mail_id) mail.tags = set(new_tags) @@ -80,7 +87,8 @@ def _favor_existing_tags_casing(self, new_tags): def _use_current_casing(new_tag_lower): return current_tags[current_tags_lower.index(new_tag_lower)] - return [_use_current_casing(new_tag.lower()) if new_tag.lower() in current_tags_lower else new_tag for new_tag in new_tags] + return [_use_current_casing(new_tag.lower()) if new_tag.lower( + ) in current_tags_lower else new_tag for new_tag in new_tags] def mail(self, mail_id): return self.mail_store.get_mail(mail_id, include_body=True) @@ -96,7 +104,7 @@ def mail_exists(self, mail_id): try: mail = yield self.mail_store.get_mail(mail_id, include_body=False) defer.returnValue(mail is not None) - except Exception, e: + except Exception as e: defer.returnValue(False) @defer.inlineCallbacks diff --git a/service/pixelated/application.py b/service/pixelated/application.py index 667c34a80..50954158b 100644 --- a/service/pixelated/application.py +++ b/service/pixelated/application.py @@ -50,7 +50,11 @@ def __init__(self, is_single_user): @defer.inlineCallbacks -def start_user_agent_in_single_user_mode(root_resource, services_factory, leap_home, leap_session): +def start_user_agent_in_single_user_mode( + root_resource, + services_factory, + leap_home, + leap_session): log.info('Bootstrap done, loading services for user %s' % leap_session.user_auth.username) @@ -115,8 +119,10 @@ def _quit_on_error(failure): def _log_user_out(event, user_data): log.info('Invalid soledad token, logging out %s' % user_data) - user_data = {'user_id': user_data['uuid']} if 'uuid' in user_data else { - 'user_id': user_data, 'using_email': True} + user_data = { + 'user_id': user_data['uuid']} if 'uuid' in user_data else { + 'user_id': user_data, + 'using_email': True} services_factory.destroy_session(**user_data) def _log_user_out_on_token_expire(leap_session): @@ -152,20 +158,31 @@ def _setup_multi_user(args, root_resource, services_factory): init_monkeypatches() events_server.ensure_server() provider = initialize_leap_provider( - args.provider, args.leap_provider_cert, args.leap_provider_cert_fingerprint, args.leap_home) + args.provider, + args.leap_provider_cert, + args.leap_provider_cert_fingerprint, + args.leap_home) protected_resource = set_up_protected_resources( root_resource, provider, services_factory, banner=args.banner) return protected_resource -def set_up_protected_resources(root_resource, provider, services_factory, banner=None, authenticator=None): +def set_up_protected_resources( + root_resource, + provider, + services_factory, + banner=None, + authenticator=None): session_checker = SessionChecker(services_factory) realm = PixelatedRealm() _portal = portal.Portal(realm, [session_checker, AllowAnonymousAccess()]) anonymous_resource = LoginResource( - services_factory, provider, disclaimer_banner=banner, authenticator=authenticator) + services_factory, + provider, + disclaimer_banner=banner, + authenticator=authenticator) protected_resource = PixelatedAuthSessionWrapper( _portal, root_resource, anonymous_resource, []) root_resource.initialize( @@ -220,7 +237,12 @@ def start_site(config, resource): site = PixelatedSite(resource) site.displayTracebacks = False if config.sslkey and config.sslcert: - reactor.listenSSL(config.port, site, _ssl_options(config.sslkey, config.sslcert), - interface=config.host) + reactor.listenSSL( + config.port, + site, + _ssl_options( + config.sslkey, + config.sslcert), + interface=config.host) else: reactor.listenTCP(config.port, site, interface=config.host) diff --git a/service/pixelated/authentication.py b/service/pixelated/authentication.py index 4e74ad2db..ffb472579 100644 --- a/service/pixelated/authentication.py +++ b/service/pixelated/authentication.py @@ -54,8 +54,10 @@ def _bonafide_auth(self, user, password): srp_auth = Session(credentials, srp_provider, self._leap_provider.local_ca_crt) yield srp_auth.authenticate() - returnValue(Authentication(user, srp_auth.token, - srp_auth.uuid, 'session_id', {'is_admin': False})) + returnValue( + Authentication( + user, srp_auth.token, srp_auth.uuid, 'session_id', { + 'is_admin': False})) def clean_username(self, username): if '@' not in username: diff --git a/service/pixelated/bitmask_libraries/keymanager.py b/service/pixelated/bitmask_libraries/keymanager.py index 62ec5325c..58de51777 100644 --- a/service/pixelated/bitmask_libraries/keymanager.py +++ b/service/pixelated/bitmask_libraries/keymanager.py @@ -37,12 +37,17 @@ class Keymanager(object): def __init__(self, provider, soledad, email_address, token, uuid): nicknym_url = provider._discover_nicknym_server() self._email = email_address - self.keymanager = KeyManager(self._email, nicknym_url, - soledad, - token=token, ca_cert_path=provider.provider_api_cert, api_uri=provider.api_uri, - api_version=provider.api_version, - uid=uuid, gpgbinary=leap_config.gpg_binary, - combined_ca_bundle=provider.combined_cerfificates_path) + self.keymanager = KeyManager( + self._email, + nicknym_url, + soledad, + token=token, + ca_cert_path=provider.provider_api_cert, + api_uri=provider.api_uri, + api_version=provider.api_version, + uid=uuid, + gpgbinary=leap_config.gpg_binary, + combined_ca_bundle=provider.combined_cerfificates_path) @defer.inlineCallbacks def generate_openpgp_key(self): diff --git a/service/pixelated/bitmask_libraries/provider.py b/service/pixelated/bitmask_libraries/provider.py index 22b1652c0..9bb775d96 100644 --- a/service/pixelated/bitmask_libraries/provider.py +++ b/service/pixelated/bitmask_libraries/provider.py @@ -37,11 +37,25 @@ def __init__(self, server_name): @property def provider_api_cert(self): - return str(os.path.join(leap_config.leap_home, 'providers', self.server_name, 'keys', 'client', 'api.pem')) + return str( + os.path.join( + leap_config.leap_home, + 'providers', + self.server_name, + 'keys', + 'client', + 'api.pem')) @property def combined_cerfificates_path(self): - return str(os.path.join(leap_config.leap_home, 'providers', self.server_name, 'keys', 'client', 'ca_bundle')) + return str( + os.path.join( + leap_config.leap_home, + 'providers', + self.server_name, + 'keys', + 'client', + 'ca_bundle')) @property def api_uri(self): @@ -125,8 +139,9 @@ def validate_certificate(self, cert_data=None): digest = get_digest(cert_data, method) if fingerprint.strip() != digest: - raise Exception('Certificate fingerprints don\'t match! Expected [%s] but got [%s]' % ( - fingerprint.strip(), digest)) + raise Exception( + 'Certificate fingerprints don\'t match! Expected [%s] but got [%s]' % + (fingerprint.strip(), digest)) def smtp_info(self): hosts = self.smtp_json['hosts'] @@ -138,7 +153,9 @@ def _validated_get(self, url): session = requests.session() try: session.mount( - 'https://', EnforceTLSv1Adapter(assert_fingerprint=LeapCertificate.LEAP_FINGERPRINT)) + 'https://', + EnforceTLSv1Adapter( + assert_fingerprint=LeapCertificate.LEAP_FINGERPRINT)) response = session.get(url, verify=LeapCertificate( self).provider_web_cert, timeout=REQUESTS_TIMEOUT) response.raise_for_status() @@ -156,7 +173,9 @@ def fetch_soledad_json(self): service_url = "%s/%s/config/soledad-service.json" % ( self.api_uri, self.api_version) response = requests.get( - service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) + service_url, + verify=self.provider_api_cert, + timeout=REQUESTS_TIMEOUT) response.raise_for_status() return json.loads(response.content) @@ -164,7 +183,9 @@ def fetch_smtp_json(self): service_url = '%s/%s/config/smtp-service.json' % ( self.api_uri, self.api_version) response = requests.get( - service_url, verify=self.provider_api_cert, timeout=REQUESTS_TIMEOUT) + service_url, + verify=self.provider_api_cert, + timeout=REQUESTS_TIMEOUT) response.raise_for_status() return json.loads(response.content) @@ -203,7 +224,7 @@ def setup_ca_bundle(self): path = os.path.join(leap_config.leap_home, 'providers', self.server_name, 'keys', 'client') if not os.path.isdir(path): - os.makedirs(path, 0700) + os.makedirs(path, 0o700) self._download_cert(self.provider_api_cert) def _download_cert(self, cert_file_name): diff --git a/service/pixelated/bitmask_libraries/smtp.py b/service/pixelated/bitmask_libraries/smtp.py index 643d4d4a9..d477f5765 100644 --- a/service/pixelated/bitmask_libraries/smtp.py +++ b/service/pixelated/bitmask_libraries/smtp.py @@ -17,7 +17,12 @@ class LeapSMTPConfig(object): - def __init__(self, account_email, cert_path, remote_smtp_host, remote_smtp_port): + def __init__( + self, + account_email, + cert_path, + remote_smtp_host, + remote_smtp_port): self.account_email = account_email self.cert_path = cert_path self.remote_smtp_host = remote_smtp_host diff --git a/service/pixelated/config/arguments.py b/service/pixelated/config/arguments.py index 9ce255f36..d9387acb4 100644 --- a/service/pixelated/config/arguments.py +++ b/service/pixelated/config/arguments.py @@ -27,17 +27,38 @@ def parse_user_agent_args(): help='the host to run the user agent on') parser.add_argument('--port', type=int, default=3333, help='the port to run the user agent on') - parser.add_argument('-sk', '--sslkey', metavar='', default=None, - help='use specified file as web server\'s SSL key (when using the user-agent in server-mode)') - parser.add_argument('-sc', '--sslcert', metavar='', default=None, - help='use specified file as web server\'s SSL certificate (when using the user-agent in server-mode)') - parser.add_argument('--multi-user', help='Run user agent in multi user mode', - action='store_false', default=True, dest='single_user') - parser.add_argument('-p', '--provider', help='specify a provider for mutli-user mode', - metavar='', default=None, dest='provider') + parser.add_argument( + '-sk', + '--sslkey', + metavar='', + default=None, + help='use specified file as web server\'s SSL key (when using the user-agent in server-mode)') + parser.add_argument( + '-sc', + '--sslcert', + metavar='', + default=None, + help='use specified file as web server\'s SSL certificate (when using the user-agent in server-mode)') + parser.add_argument( + '--multi-user', + help='Run user agent in multi user mode', + action='store_false', + default=True, + dest='single_user') + parser.add_argument( + '-p', + '--provider', + help='specify a provider for mutli-user mode', + metavar='', + default=None, + dest='provider') parser.add_argument('--banner', help='banner file to show on login screen') - parser.add_argument('--manhole', help='Run an interactive Python shell on port 8008', - action='store_true', default=False, dest='manhole') + parser.add_argument( + '--manhole', + help='Run an interactive Python shell on port 8008', + action='store_true', + default=False, + dest='manhole') args = parser.parse_args() @@ -56,9 +77,16 @@ def parse_maintenance_args(): markov_mails_parser = subparsers.add_parser( 'markov-generate', help='generate mails using markov chains') markov_mails_parser.add_argument( - '--seed', default=None, help='Specify a seed to always generate the same output') - markov_mails_parser.add_argument('-l', '--limit', metavar='count', - default='5', help='limit number of generated mails', dest='limit') + '--seed', + default=None, + help='Specify a seed to always generate the same output') + markov_mails_parser.add_argument( + '-l', + '--limit', + metavar='count', + default='5', + help='limit number of generated mails', + dest='limit') markov_mails_parser.add_argument( 'file', nargs='+', help='file(s) with mail data') @@ -75,26 +103,67 @@ def parse_register_args(): parser = argparse.ArgumentParser(description='Pixelated register') parser.add_argument('provider', metavar='provider', action='store') parser.add_argument('username', metavar='username', action='store') - parser.add_argument('-p', '--password', metavar='password', action='store', - default=None, help='used just to register account automatically by scripts') - parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, - help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') - parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, - help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') - parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', - dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) parser.add_argument( - '--invite-code', help='invite code to register a user, if required', dest='invite_code', default=None) + '-p', + '--password', + metavar='password', + action='store', + default=None, + help='used just to register account automatically by scripts') + parser.add_argument( + '-lc', + '--leap-provider-cert', + metavar='', + default=None, + help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') + parser.add_argument( + '-lf', + '--leap-provider-cert-fingerprint', + metavar='', + default=None, + help='use specified fingerprint to validate connection with LEAP provider', + dest='leap_provider_cert_fingerprint') + parser.add_argument( + '--leap-home', + help='The folder where the user agent stores its data. Defaults to ~/.leap', + dest='leap_home', + default=os.path.join( + os.path.expanduser("~"), + '.leap')) + parser.add_argument( + '--invite-code', + help='invite code to register a user, if required', + dest='invite_code', + default=None) return parser.parse_args() def parser_add_default_arguments(parser): parser.add_argument('--debug', action='store_true', help='DEBUG mode.') - parser.add_argument('-c', '--config', dest='credentials_file', metavar='', - default=None, help='use specified file for credentials (for test purposes only)') - parser.add_argument('--leap-home', help='The folder where the user agent stores its data. Defaults to ~/.leap', - dest='leap_home', default=os.path.join(os.path.expanduser("~"), '.leap')) - parser.add_argument('-lc', '--leap-provider-cert', metavar='', default=None, - help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') - parser.add_argument('-lf', '--leap-provider-cert-fingerprint', metavar='', default=None, - help='use specified fingerprint to validate connection with LEAP provider', dest='leap_provider_cert_fingerprint') + parser.add_argument( + '-c', + '--config', + dest='credentials_file', + metavar='', + default=None, + help='use specified file for credentials (for test purposes only)') + parser.add_argument( + '--leap-home', + help='The folder where the user agent stores its data. Defaults to ~/.leap', + dest='leap_home', + default=os.path.join( + os.path.expanduser("~"), + '.leap')) + parser.add_argument( + '-lc', + '--leap-provider-cert', + metavar='', + default=None, + help='use specified file for LEAP provider cert authority certificate (url https:///ca.crt)') + parser.add_argument( + '-lf', + '--leap-provider-cert-fingerprint', + metavar='', + default=None, + help='use specified fingerprint to validate connection with LEAP provider', + dest='leap_provider_cert_fingerprint') diff --git a/service/pixelated/config/leap.py b/service/pixelated/config/leap.py index b1fabe48f..2e5f515d9 100644 --- a/service/pixelated/config/leap.py +++ b/service/pixelated/config/leap.py @@ -14,7 +14,11 @@ log = Logger() -def initialize_leap_provider(provider_hostname, provider_cert, provider_fingerprint, leap_home): +def initialize_leap_provider( + provider_hostname, + provider_cert, + provider_fingerprint, + leap_home): LeapCertificate.set_cert_and_fingerprint(provider_cert, provider_fingerprint) leap_config.set_leap_home(leap_home) @@ -55,7 +59,10 @@ def initialize_leap_single_user(leap_provider_cert, provider, username, password = credentials.read(credentials_file) provider = initialize_leap_provider( - provider, leap_provider_cert, leap_provider_cert_fingerprint, leap_home) + provider, + leap_provider_cert, + leap_provider_cert_fingerprint, + leap_home) auth = yield Authenticator(provider).authenticate(username, password) diff --git a/service/pixelated/config/logger.py b/service/pixelated/config/logger.py index 47eb6dcb2..737af4ba0 100644 --- a/service/pixelated/config/logger.py +++ b/service/pixelated/config/logger.py @@ -34,10 +34,11 @@ def init(debug=False): debug_enabled = debug or os.environ.get('DEBUG', False) logging_level = logging.DEBUG if debug_enabled else logging.INFO - logging.basicConfig(level=logging_level, - format='%(asctime)s [%(name)s] %(levelname)s %(message)s', - datefmt='%Y-%m-%d %H:%M:%S', - filemode='a') + logging.basicConfig( + level=logging_level, + format='%(asctime)s [%(name)s] %(levelname)s %(message)s', + datefmt='%Y-%m-%d %H:%M:%S', + filemode='a') logging.getLogger('gnupg').setLevel(logging.WARN) logging.getLogger('gnupg').addFilter(PrivateKeyFilter()) diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py index 7e976024e..b7363ea70 100644 --- a/service/pixelated/config/services.py +++ b/service/pixelated/config/services.py @@ -67,7 +67,8 @@ def _setup_search_engine(self, namespace, search_index_storage_key): def _setup_mail_service(self, search_engine): pixelated_mail_sender = MailSender( - self._leap_session.smtp_config, self._leap_session.keymanager.keymanager) + self._leap_session.smtp_config, + self._leap_session.keymanager.keymanager) return MailService( pixelated_mail_sender, diff --git a/service/pixelated/config/sessions.py b/service/pixelated/config/sessions.py index 1d6d4782b..0902f266d 100644 --- a/service/pixelated/config/sessions.py +++ b/service/pixelated/config/sessions.py @@ -88,7 +88,7 @@ def setup_soledad(self, auth_token=user_token, defer_encryption=False) defer.returnValue(soledad) - except (WrongMacError, UnknownMacMethodError), e: + except (WrongMacError, UnknownMacMethodError) as e: raise SoledadWrongPassphraseException(e) @defer.inlineCallbacks @@ -131,7 +131,8 @@ def _create_database_dir(self, user_uuid): try: os.makedirs(self._soledad_path(user_uuid)) except OSError as exc: - if exc.errno == errno.EEXIST and os.path.isdir(self._soledad_path(user_uuid)): + if exc.errno == errno.EEXIST and os.path.isdir( + self._soledad_path(user_uuid)): pass else: raise @@ -139,7 +140,14 @@ def _create_database_dir(self, user_uuid): class LeapSession(object): - def __init__(self, provider, user_auth, mail_store, soledad, keymanager, smtp_config): + def __init__( + self, + provider, + user_auth, + mail_store, + soledad, + keymanager, + smtp_config): self.smtp_config = smtp_config self.provider = provider self.user_auth = user_auth @@ -205,7 +213,8 @@ def remove_from_cache(self): SessionCache.remove_session(key) @defer.inlineCallbacks - def _create_incoming_mail_fetcher(self, keymanager, soledad, account, user_mail): + def _create_incoming_mail_fetcher( + self, keymanager, soledad, account, user_mail): inbox = yield account.callWhenReady(lambda _: account.get_collection_by_mailbox('INBOX')) defer.returnValue(IncomingMail(keymanager.keymanager, soledad, diff --git a/service/pixelated/extensions/requests_urllib3.py b/service/pixelated/extensions/requests_urllib3.py index 5b0a6644e..fbb502329 100644 --- a/service/pixelated/extensions/requests_urllib3.py +++ b/service/pixelated/extensions/requests_urllib3.py @@ -53,7 +53,8 @@ def patched_connect(self): address=(self.host, self.port), timeout=self.timeout) except SocketTimeout: raise ConnectTimeoutError( - self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) + self, "Connection to %s timed out. (connect timeout=%s)" % + (self.host, self.timeout)) resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs) resolved_ssl_version = resolve_ssl_version(self.ssl_version) diff --git a/service/pixelated/register.py b/service/pixelated/register.py index 30eba86dd..f8e9c6851 100644 --- a/service/pixelated/register.py +++ b/service/pixelated/register.py @@ -38,7 +38,11 @@ def _validate(username, password): validate_password(password) -def _set_provider(provider_cert, provider_cert_fingerprint, server_name, leap_home=None): +def _set_provider( + provider_cert, + provider_cert_fingerprint, + server_name, + leap_home=None): if leap_home: leap_config.set_leap_home(leap_home) @@ -51,7 +55,11 @@ def _set_provider(provider_cert, provider_cert_fingerprint, server_name, leap_ho def _set_leap_provider(args): - return _set_provider(args.leap_provider_cert, args.leap_provider_cert_fingerprint, args.provider, args.leap_home) + return _set_provider( + args.leap_provider_cert, + args.leap_provider_cert_fingerprint, + args.provider, + args.leap_home) def _bonafide_session(username, password, provider): diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 2c765a29d..769751d9c 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -88,8 +88,10 @@ def getChild(self, attachment_id, request): def render_POST(self, request): _mail_service = self.mail_service(request) - fields = cgi.FieldStorage(fp=request.content, headers=(request.getAllHeaders()), - environ={'REQUEST_METHOD': 'POST'}) + fields = cgi.FieldStorage( + fp=request.content, headers=( + request.getAllHeaders()), environ={ + 'REQUEST_METHOD': 'POST'}) _file = fields['attachment'] deferred = _mail_service.save_attachment(_file.value, _file.type) diff --git a/service/pixelated/resources/auth.py b/service/pixelated/resources/auth.py index c5f0271eb..1b61a1462 100644 --- a/service/pixelated/resources/auth.py +++ b/service/pixelated/resources/auth.py @@ -76,7 +76,12 @@ class PixelatedAuthSessionWrapper(object): isLeaf = False - def __init__(self, portal, root_resource, anonymous_resource, credentialFactories): + def __init__( + self, + portal, + root_resource, + anonymous_resource, + credentialFactories): self._portal = portal self._credentialFactories = credentialFactories self._root_resource = root_resource @@ -98,7 +103,8 @@ def _login(self, credentials, request): def loginSucceeded(args): interface, avatar, logout = args - if avatar == checkers.ANONYMOUS and not pattern.match(request.path): + if avatar == checkers.ANONYMOUS and not pattern.match( + request.path): return self._anonymous_resource else: return self._root_resource diff --git a/service/pixelated/resources/login_resource.py b/service/pixelated/resources/login_resource.py index 60802ef25..2fc27c381 100644 --- a/service/pixelated/resources/login_resource.py +++ b/service/pixelated/resources/login_resource.py @@ -45,7 +45,14 @@ def _get_static_folder(): # 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", "dist")) + os.path.join( + os.path.abspath(__file__), + "..", + "..", + "..", + "..", + "web-ui", + "dist")) if not os.path.exists(static_folder): static_folder = os.path.join( '/', 'usr', 'share', 'pixelated-user-agent') @@ -81,9 +88,13 @@ def render(self, request): try: return super(DisclaimerElement, self).render(request) except SAXParseException: - return ["Invalid XML template format for %s." % self._banner_filename] + return [ + "Invalid XML template format for %s." % + self._banner_filename] except IOError: - return ["Disclaimer banner file %s could not be read or does not exit." % self._banner_filename] + return [ + "Disclaimer banner file %s could not be read or does not exit." % + self._banner_filename] class LoginWebSite(Element): @@ -109,7 +120,12 @@ def disclaimer(self, request, tag): class LoginResource(BaseResource): BASE_URL = 'login' - def __init__(self, services_factory, provider=None, disclaimer_banner=None, authenticator=None): + def __init__( + self, + services_factory, + provider=None, + disclaimer_banner=None, + authenticator=None): BaseResource.__init__(self, services_factory) self._static_folder = _get_static_folder() self._startup_folder = _get_startup_folder() @@ -155,7 +171,8 @@ def render_error(error): log.info('Login error for %s' % request.args['username'][0]) log.info('%s' % error) request.setResponseCode(UNAUTHORIZED) - return self._render_template(request, 'Invalid username or password') + return self._render_template( + request, 'Invalid username or password') d = self._handle_login(request) d.addCallbacks(render_response, render_error) @@ -172,8 +189,9 @@ def _handle_login(self, request): def _complete_bootstrap(self, user_auth, request): def log_error(error): - log.error('Login error during %s services setup: %s \n %s' % ( - user_auth.username, error.getErrorMessage(), error.getTraceback())) + log.error( + 'Login error during %s services setup: %s \n %s' % + (user_auth.username, error.getErrorMessage(), error.getTraceback())) def set_session_cookies(_): session = IPixelatedSession(request.getSession()) diff --git a/service/pixelated/resources/mail_resource.py b/service/pixelated/resources/mail_resource.py index ec5a441ce..0ee9c14de 100644 --- a/service/pixelated/resources/mail_resource.py +++ b/service/pixelated/resources/mail_resource.py @@ -26,7 +26,8 @@ def render_POST(self, request): def handle403(failure): failure.trap(ValueError) - return respond_json_deferred(failure.getErrorMessage(), request, 403) + return respond_json_deferred( + failure.getErrorMessage(), request, 403) d.addErrback(handle403) return NOT_DONE_YET diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index 9150e7207..5ef5e4719 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -152,7 +152,8 @@ def getChild(self, action, request): if action == 'unread': return MailsUnreadResource(_mail_service) - def _build_mails_response(self, (mails, total)): + def _build_mails_response(self, xxx_todo_changeme): + (mails, total) = xxx_todo_changeme return { "stats": { "total": total, diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index 243d3a800..1eabbb3e6 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -91,13 +91,19 @@ def _is_xsrf_valid(self, request): request.content.read()).get('csrftoken', [None])[0] return csrf_input and csrf_input == xsrf_token - def initialize(self, provider=None, disclaimer_banner=None, authenticator=None): + def initialize( + self, + provider=None, + disclaimer_banner=None, + authenticator=None): self._child_resources.add( 'sandbox', SandboxResource(self._static_folder)) self._child_resources.add('assets', File(self._static_folder)) self._child_resources.add('keys', KeysResource(self._services_factory)) self._child_resources.add( - AttachmentsResource.BASE_URL, AttachmentsResource(self._services_factory)) + AttachmentsResource.BASE_URL, + AttachmentsResource( + self._services_factory)) self._child_resources.add( 'contacts', ContactsResource(self._services_factory)) self._child_resources.add('features', FeaturesResource(provider)) @@ -111,8 +117,13 @@ def initialize(self, provider=None, disclaimer_banner=None, authenticator=None): 'user-settings', UserSettingsResource(self._services_factory)) self._child_resources.add( 'users', UsersResource(self._services_factory)) - self._child_resources.add(LoginResource.BASE_URL, - LoginResource(self._services_factory, provider, disclaimer_banner=disclaimer_banner, authenticator=authenticator)) + self._child_resources.add( + LoginResource.BASE_URL, + LoginResource( + self._services_factory, + provider, + disclaimer_banner=disclaimer_banner, + authenticator=authenticator)) self._child_resources.add( LogoutResource.BASE_URL, LogoutResource(self._services_factory)) diff --git a/service/pixelated/resources/users.py b/service/pixelated/resources/users.py index a3e6118e3..6005cab08 100644 --- a/service/pixelated/resources/users.py +++ b/service/pixelated/resources/users.py @@ -26,5 +26,6 @@ def __init__(self, services_factory): def render_GET(self, request): if self.is_admin(request): - return respond_json({"count": self._services_factory.online_sessions()}, request) + return respond_json( + {"count": self._services_factory.online_sessions()}, request) return UnAuthorizedResource().render_GET(request) diff --git a/service/pixelated/support/__init__.py b/service/pixelated/support/__init__.py index 7806b0ecc..55a18ffa9 100644 --- a/service/pixelated/support/__init__.py +++ b/service/pixelated/support/__init__.py @@ -51,8 +51,9 @@ def wrapper(*args, **kwds): time_duration, clock_duration, estimate_percent_io = _stop_stopwatch( start) - log.info('Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % ( - time_duration, clock_duration, estimate_percent_io, f)) + log.info( + 'Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % + (time_duration, clock_duration, estimate_percent_io, f)) return result @@ -64,8 +65,9 @@ def log_time_deferred(f): def log_time(result, start): time_duration, clock_duration, estimate_percent_io = _stop_stopwatch( start) - log.info('after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % ( - time_duration, clock_duration, estimate_percent_io, f)) + log.info( + 'after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % + (time_duration, clock_duration, estimate_percent_io, f)) return result @wraps(f) diff --git a/service/pixelated/support/clock.py b/service/pixelated/support/clock.py index 2d947bb22..bfe976c56 100644 --- a/service/pixelated/support/clock.py +++ b/service/pixelated/support/clock.py @@ -13,5 +13,9 @@ def stop(self, fresh=False, user=None): end = datetime.now() with open(expanduser('~/MetricsTime'), 'a') as f: flag = ' fresh-account' if fresh else '' - f.write('{} {:.5f} {} {}\n'.format((self.user or user or 'Unknown'), - (end - self.start).total_seconds(), self.label, flag)) + f.write( + '{} {:.5f} {} {}\n'.format( + (self.user or user or 'Unknown'), + (end - self.start).total_seconds(), + self.label, + flag)) diff --git a/service/pixelated/support/encrypted_file_storage.py b/service/pixelated/support/encrypted_file_storage.py index d4e06e0bf..11e8f45a6 100644 --- a/service/pixelated/support/encrypted_file_storage.py +++ b/service/pixelated/support/encrypted_file_storage.py @@ -117,7 +117,8 @@ def wrapper(struct_file): struct_file.seek(0) content = struct_file.file.read() file_hash = sha256(content).digest() - if name in self.length_cache and file_hash == self.length_cache[name][1]: + if name in self.length_cache and file_hash == self.length_cache[ + name][1]: return self.length_cache[name] = (len(content), file_hash) encrypted_content = self.encrypt(content) @@ -148,6 +149,6 @@ def _store_file(self, name, content): encrypted_content = self.encrypt(content) with open(self._fpath(name), 'w+b') as f: f.write(encrypted_content) - except Exception, e: + except Exception as e: print e raise diff --git a/service/pixelated/support/mail_generator.py b/service/pixelated/support/mail_generator.py index e01274f38..2afdb584c 100644 --- a/service/pixelated/support/mail_generator.py +++ b/service/pixelated/support/mail_generator.py @@ -78,7 +78,9 @@ def search_for_tags(content): def filter_too_short_texts(texts): - return [text for text in texts if text is not None and len(text.split()) >= 3] + return [ + text for text in texts if text is not None and len( + text.split()) >= 3] def load_all_mails(mail_list): @@ -95,7 +97,8 @@ def load_all_mails(mail_list): else: raise Exception(mail.get_content_type()) - return filter_too_short_texts(subjects), filter_too_short_texts(mail_bodies) + return filter_too_short_texts( + subjects), filter_too_short_texts(mail_bodies) class MailGenerator(object): @@ -112,7 +115,9 @@ def __init__(self, receiver, domain_name, sample_mail_list, random=None): self._subject_markov = MarkovGenerator( self._subjects, random=self._random) self._body_markov = MarkovGenerator( - self._bodies, random=self._random, add_paragraph_on_empty_chain=True) + self._bodies, + random=self._random, + add_paragraph_on_empty_chain=True) def generate_mail(self): body = self._body_markov.generate(150) diff --git a/service/pixelated/support/replier.py b/service/pixelated/support/replier.py index 83324eae7..99234769a 100644 --- a/service/pixelated/support/replier.py +++ b/service/pixelated/support/replier.py @@ -22,7 +22,10 @@ def remove_duplicates(recipients): def remove_address(recipients, current_user): - return [recipient for recipient in recipients if not parsed_mail_matches(recipient, current_user)] + return [ + recipient for recipient in recipients if not parsed_mail_matches( + recipient, + current_user)] def parsed_mail_matches(to_parse, expected): diff --git a/service/pixelated/support/tls_adapter.py b/service/pixelated/support/tls_adapter.py index 301a21231..25a5f1e8b 100644 --- a/service/pixelated/support/tls_adapter.py +++ b/service/pixelated/support/tls_adapter.py @@ -34,14 +34,19 @@ def latest_available_ssl_version(): class EnforceTLSv1Adapter(HTTPAdapter): __slots__ = ('_assert_hostname', '_assert_fingerprint') - def __init__(self, assert_hostname=VERIFY_HOSTNAME, assert_fingerprint=None): + def __init__( + self, + assert_hostname=VERIFY_HOSTNAME, + assert_fingerprint=None): self._assert_hostname = assert_hostname self._assert_fingerprint = assert_fingerprint super(EnforceTLSv1Adapter, self).__init__() def init_poolmanager(self, connections, maxsize, block=False): - self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize, - block=block, - assert_hostname=self._assert_hostname, - assert_fingerprint=self._assert_fingerprint, - cert_reqs=ssl.CERT_REQUIRED) + self.poolmanager = PoolManager( + num_pools=connections, + maxsize=maxsize, + block=block, + assert_hostname=self._assert_hostname, + assert_fingerprint=self._assert_fingerprint, + cert_reqs=ssl.CERT_REQUIRED) diff --git a/service/test/functional/features/steps/attachments.py b/service/test/functional/features/steps/attachments.py index 1df2dcd29..0611db786 100644 --- a/service/test/functional/features/steps/attachments.py +++ b/service/test/functional/features/steps/attachments.py @@ -50,7 +50,8 @@ def build_mail_with_attachment(subject): @wait_for(timeout=10.0) def load_mail_into_soledad(context, mail): - return context.single_user_client.mail_store.add_mail('INBOX', mail.as_string()) + return context.single_user_client.mail_store.add_mail( + 'INBOX', mail.as_string()) @then(u'I see the mail has an attachment') diff --git a/service/test/functional/features/steps/common.py b/service/test/functional/features/steps/common.py index eebcd0ac7..6d2a98530 100644 --- a/service/test/functional/features/steps/common.py +++ b/service/test/functional/features/steps/common.py @@ -42,7 +42,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): DEFAULT_IMPLICIT_WAIT_TIMEOUT_IN_S) -def wait_until_element_is_invisible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): +def wait_until_element_is_invisible_by_locator( + context, locator_tuple, timeout=TIMEOUT_IN_S): wait = WebDriverWait(context.browser, timeout) wait.until(EC.invisibility_of_element_located(locator_tuple)) @@ -57,19 +58,25 @@ def wait_for_user_alert_to_disapear(context, timeout=TIMEOUT_IN_S): context, (By.ID, 'user-alerts'), timeout) -def _wait_until_elements_are_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): +def _wait_until_elements_are_visible_by_locator( + context, locator_tuple, timeout=TIMEOUT_IN_S): wait = WebDriverWait(context.browser, timeout) wait.until(EC.presence_of_all_elements_located(locator_tuple)) return context.browser.find_elements(locator_tuple[0], locator_tuple[1]) -def _wait_until_element_is_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S): +def _wait_until_element_is_visible_by_locator( + context, locator_tuple, timeout=TIMEOUT_IN_S): wait = WebDriverWait(context.browser, timeout) wait.until(EC.presence_of_element_located(locator_tuple)) return context.browser.find_element(locator_tuple[0], locator_tuple[1]) -def wait_for_condition(context, predicate_func, timeout=TIMEOUT_IN_S, poll_frequency=0.1): +def wait_for_condition( + context, + predicate_func, + timeout=TIMEOUT_IN_S, + poll_frequency=0.1): wait = WebDriverWait(context.browser, timeout, poll_frequency=poll_frequency) wait.until(predicate_func) @@ -99,7 +106,8 @@ def page_has_css(context, css): def find_element_by_xpath(context, xpath): - return _wait_until_element_is_visible_by_locator(context, (By.XPATH, xpath)) + return _wait_until_element_is_visible_by_locator( + context, (By.XPATH, xpath)) def find_element_by_id(context, id): @@ -107,23 +115,29 @@ def find_element_by_id(context, id): def find_element_by_css_selector(context, css_selector, timeout=TIMEOUT_IN_S): - return _wait_until_element_is_visible_by_locator(context, (By.CSS_SELECTOR, css_selector), timeout=timeout) + return _wait_until_element_is_visible_by_locator( + context, (By.CSS_SELECTOR, css_selector), timeout=timeout) def find_element_by_class_name(context, class_name): - return _wait_until_element_is_visible_by_locator(context, (By.CLASS_NAME, class_name)) + return _wait_until_element_is_visible_by_locator( + context, (By.CLASS_NAME, class_name)) def find_elements_by_css_selector(context, css_selector, timeout=TIMEOUT_IN_S): - return _wait_until_elements_are_visible_by_locator(context, (By.CSS_SELECTOR, css_selector), timeout=timeout) + return _wait_until_elements_are_visible_by_locator( + context, (By.CSS_SELECTOR, css_selector), timeout=timeout) def find_elements_by_xpath(context, xpath, timeout=TIMEOUT_IN_S): - return _wait_until_elements_are_visible_by_locator(context, (By.XPATH, xpath), timeout=timeout) + return _wait_until_elements_are_visible_by_locator( + context, (By.XPATH, xpath), timeout=timeout) def find_element_containing_text(context, text, element_type='*'): - return find_element_by_xpath(context, "//%s[contains(.,'%s')]" % (element_type, text)) + return find_element_by_xpath( + context, "//%s[contains(.,'%s')]" % + (element_type, text)) def element_should_have_content(context, css_selector, content): @@ -154,7 +168,10 @@ def click_button(context, title, element='button'): def mail_list_with_subject_exists(context, subject): - return find_element_by_xpath(context, "//*[@class='mail-list-entry__item-subject' and contains(.,'%s')]" % subject) + return find_element_by_xpath( + context, + "//*[@class='mail-list-entry__item-subject' and contains(.,'%s')]" % + subject) def reply_subject(context): diff --git a/service/test/functional/features/steps/data_setup.py b/service/test/functional/features/steps/data_setup.py index 88b3069dd..d2a8b91b7 100644 --- a/service/test/functional/features/steps/data_setup.py +++ b/service/test/functional/features/steps/data_setup.py @@ -27,8 +27,10 @@ def add_mail_impl(context): input_mail = MailBuilder().with_subject(subject).build_input_mail() load_mail_into_soledad(context, input_mail) - wait_for_condition(context, lambda _: context.single_user_client.search_engine.search( - subject)[1] > 0, poll_frequency=0.1) + wait_for_condition( + context, + lambda _: context.single_user_client.search_engine.search(subject)[1] > 0, + poll_frequency=0.1) context.last_subject = subject diff --git a/service/test/functional/features/steps/mail_list.py b/service/test/functional/features/steps/mail_list.py index 1c44ec166..3deda8158 100644 --- a/service/test/functional/features/steps/mail_list.py +++ b/service/test/functional/features/steps/mail_list.py @@ -123,7 +123,8 @@ def _wait_for_mail_list_to_be_empty(context): def mail_list_is_empty(_): with ImplicitWait(context, timeout=0.1): try: - return 0 == len(context.browser.find_elements_by_css_selector('.mail-list-entry')) + return 0 == len( + context.browser.find_elements_by_css_selector('.mail-list-entry')) except TimeoutException: return False diff --git a/service/test/integration/test_leap_mailstore.py b/service/test/integration/test_leap_mailstore.py index 8d1bc23e9..ff2fd5ddc 100644 --- a/service/test/integration/test_leap_mailstore.py +++ b/service/test/integration/test_leap_mailstore.py @@ -45,8 +45,26 @@ def test_get_mail_for_measuring(self): self.maxDiff = None mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set( - []), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + expected_mail_dict = { + 'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'header': { + u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', + u'to': [u'carmel@murazikortiz.name'], + u'x-tw-pixelated-tags': u'nite, macro, trash', + u'from': u'darby.senger@zemlak.biz', + u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, + 'ident': mail_id, + 'status': [], + 'tags': set( + []), + 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'mailbox': u'inbox', + 'attachments': [], + 'security_casing': { + 'imprints': [ + { + 'state': 'no_signature_information'}], + 'locks': []}} with measure(): result = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) @@ -58,8 +76,26 @@ def test_get_mail_with_body(self): self.maxDiff = None mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set( - []), 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + expected_mail_dict = { + 'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'header': { + u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', + u'to': [u'carmel@murazikortiz.name'], + u'x-tw-pixelated-tags': u'nite, macro, trash', + u'from': u'darby.senger@zemlak.biz', + u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, + 'ident': mail_id, + 'status': [], + 'tags': set( + []), + 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'mailbox': u'inbox', + 'attachments': [], + 'security_casing': { + 'imprints': [ + { + 'state': 'no_signature_information'}], + 'locks': []}} result = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) self.assertIsNotNone(result) @@ -112,8 +148,18 @@ def test_attachment_name_with_lowercase_header(self): def test_round_trip_through_soledad_does_not_modify_content(self): mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': { - u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [], 'tags': set([])} + expected_mail_dict = { + 'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'header': { + u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', + u'to': [u'carmel@murazikortiz.name'], + u'x-tw-pixelated-tags': u'nite, macro, trash', + u'from': u'darby.senger@zemlak.biz', + u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, + 'ident': mail_id, + 'status': [], + 'tags': set( + [])} mail = yield self.app_test_client.mail_store.add_mail('INBOX', mail.as_string()) fetched_mail = yield self.app_test_client.mail_store.get_mail(mail_id, include_body=True) @@ -144,7 +190,8 @@ def test_all_mails(self): self.assertEqual(1, len(mails)) self.assertEqual( - 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + 'Itaque consequatur repellendus provident sunt quia.', + mails[0].subject) @defer.inlineCallbacks def test_add_and_remove_mail(self): diff --git a/service/test/integration/test_logout.py b/service/test/integration/test_logout.py index c9d39d175..0aba33180 100644 --- a/service/test/integration/test_logout.py +++ b/service/test/integration/test_logout.py @@ -28,7 +28,8 @@ def wait_for_session_user_id_to_finish(self): yield self.adaptor.initialize_store(self.app_test_client.soledad) @defer.inlineCallbacks - def test_logout_deletes_services_stop_background_reactor_tasks_and_closes_soledad(self): + def test_logout_deletes_services_stop_background_reactor_tasks_and_closes_soledad( + self): response, login_request = yield self.app_test_client.login() yield response diff --git a/service/test/integration/test_multi_user_login.py b/service/test/integration/test_multi_user_login.py index 51913dff3..f0b78a161 100644 --- a/service/test/integration/test_multi_user_login.py +++ b/service/test/integration/test_multi_user_login.py @@ -38,8 +38,25 @@ def test_logged_in_users_sees_resources(self): mail = load_mail_from_file('mbox00000000') mail_id = yield self._create_mail_in_soledad(mail) - expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': [u'carmel@murazikortiz.name'], u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'status': [ - ], 'tags': [], 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'mailbox': u'inbox', 'attachments': [], 'security_casing': {'imprints': [{'state': 'no_signature_information'}], 'locks': []}} + expected_mail_dict = { + 'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'header': { + u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', + u'to': [u'carmel@murazikortiz.name'], + u'x-tw-pixelated-tags': u'nite, macro, trash', + u'from': u'darby.senger@zemlak.biz', + u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, + 'ident': mail_id, + 'status': [], + 'tags': [], + 'textPlainBody': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', + 'mailbox': u'inbox', + 'attachments': [], + 'security_casing': { + 'imprints': [ + { + 'state': 'no_signature_information'}], + 'locks': []}} response, request = self.app_test_client.get( "/mail/%s" % mail_id, from_request=login_request) response = yield response diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py index 8b0970324..dc78d01a7 100644 --- a/service/test/integration/test_retrieve_attachment.py +++ b/service/test/integration/test_retrieve_attachment.py @@ -39,8 +39,9 @@ def test_attachment_content_is_retrieved(self): self.assertEqual(200, req.code) self.assertEquals('pretend to be binary attachment data', attachment) - self.assertEquals(expected_content_disposition, - req.responseHeaders.getRawHeaders('content-disposition')[0]) + self.assertEquals( + expected_content_disposition, + req.responseHeaders.getRawHeaders('content-disposition')[0]) self.assertEquals(expected_content_type, req.responseHeaders.getRawHeaders('content-type')[0]) @@ -60,8 +61,9 @@ def test_should_retrieve_attachment_even_if_xsrf_token_not_passed(self): self.assertEqual(200, req.code) self.assertEquals('pretend to be binary attachment data', attachment) - self.assertEquals(expected_content_disposition, - req.responseHeaders.getRawHeaders('content-disposition')[0]) + self.assertEquals( + expected_content_disposition, + req.responseHeaders.getRawHeaders('content-disposition')[0]) self.assertEquals(expected_content_type, req.responseHeaders.getRawHeaders('content-type')[0]) @@ -96,11 +98,13 @@ def test_post_new_attachment(self): _, req = yield self.app_test_client.post_attachment(post_data, headers) self.assertEqual(201, req.code) - self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', - req.responseHeaders.getRawHeaders('location')[0]) - response_json = {'ident': 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', - 'content-type': content_type, - 'name': filename, - 'size': len(data), - 'encoding': 'base64'} + self.assertEqual( + '/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', + req.responseHeaders.getRawHeaders('location')[0]) + response_json = { + 'ident': 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', + 'content-type': content_type, + 'name': filename, + 'size': len(data), + 'encoding': 'base64'} self.assertEqual(response_json, json.loads(req.written[0])) diff --git a/service/test/integration/test_tags.py b/service/test/integration/test_tags.py index ef4fc2864..3dbb50c8f 100644 --- a/service/test/integration/test_tags.py +++ b/service/test/integration/test_tags.py @@ -40,7 +40,8 @@ def test_add_tag_to_an_inbox_mail_and_query(self): self.assertEquals('Mail with tags', mails[0].subject) @defer.inlineCallbacks - def test_use_old_casing_when_same_tag_with_different_casing_is_posted(self): + def test_use_old_casing_when_same_tag_with_different_casing_is_posted( + self): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() mail = yield self.app_test_client.add_mail_to_inbox(input_mail) yield self.app_test_client.post_tags(mail.ident, self._tags_json(['ImPoRtAnT'])) @@ -93,7 +94,8 @@ def test_addition_of_reserved_tags_is_not_allowed(self): for tag in SPECIAL_TAGS: response = yield self.app_test_client.post_tags(mail.ident, self._tags_json([tag.name.upper()])) self.assertEquals( - "None of the following words can be used as tags: %s" % tag.name, response) + "None of the following words can be used as tags: %s" % + tag.name, response) mail = yield self.app_test_client.mail_store.get_mail(mail.ident) self.assertNotIn('drafts', mail.tags) diff --git a/service/test/perf/contacts/test_Contacts.py b/service/test/perf/contacts/test_Contacts.py index 12f4e586a..a4d0cc9b3 100644 --- a/service/test/perf/contacts/test_Contacts.py +++ b/service/test/perf/contacts/test_Contacts.py @@ -39,11 +39,23 @@ def setUpBench(self): # setup data client.add_multiple_to_mailbox( - 10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com') + 10, + 'INBOX', + to='to@inbox.com', + cc='cc@inbox.com', + bcc='bcc@inbox.com') client.add_multiple_to_mailbox( - 10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com') + 10, + 'TRASH', + to='to@trash.com', + cc='cc@trash.com', + bcc='bcc@trash.com') client.add_multiple_to_mailbox( - 10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com') + 10, + 'DRAFTS', + to='to@drafts.com', + cc='cc@drafts.com', + bcc='bcc@drafts.com') self.call_to_terminate = proxy.run_on_a_thread() diff --git a/service/test/perf/search/test_Search.py b/service/test/perf/search/test_Search.py index 3f58e5aa3..4086765c0 100644 --- a/service/test/perf/search/test_Search.py +++ b/service/test/perf/search/test_Search.py @@ -29,11 +29,26 @@ def setUpBench(self): # setup data CLIENT.add_multiple_to_mailbox( - 10, 'INBOX', to='to@inbox.com', cc='cc@inbox.com', bcc='bcc@inbox.com', tags=['inbox']) + 10, + 'INBOX', + to='to@inbox.com', + cc='cc@inbox.com', + bcc='bcc@inbox.com', + tags=['inbox']) CLIENT.add_multiple_to_mailbox( - 10, 'TRASH', to='to@trash.com', cc='cc@trash.com', bcc='bcc@trash.com', tags=['trash']) + 10, + 'TRASH', + to='to@trash.com', + cc='cc@trash.com', + bcc='bcc@trash.com', + tags=['trash']) CLIENT.add_multiple_to_mailbox( - 10, 'DRAFTS', to='to@drafts.com', cc='cc@drafts.com', bcc='bcc@drafts.com', tags=['drafts']) + 10, + 'DRAFTS', + to='to@drafts.com', + cc='cc@drafts.com', + bcc='bcc@drafts.com', + tags=['drafts']) self.call_to_terminate = CLIENT.run_on_a_thread( logfile='results/app.log') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index dd51d8a24..ed5243a28 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -118,8 +118,18 @@ def _initialize_account(self): self.account = Account(self.soledad, self._user_id) return self.account.deferred_initialization - def _create_mail_service(self, mail_sender, mail_store, search_engine, attachment_store): - return MailService(mail_sender, mail_store, search_engine, self._mail_address, attachment_store) + def _create_mail_service( + self, + mail_sender, + mail_store, + search_engine, + attachment_store): + return MailService( + mail_sender, + mail_store, + search_engine, + self._mail_address, + attachment_store) def _create_mail_sender(self): mail_sender = Mock() @@ -143,8 +153,19 @@ def add_user(self, username, password): def requestAvatarId(self, credentials): if(self._credentials[credentials.username] == credentials.password): leap_auth = Authentication( - credentials.username, uuid.uuid4(), uuid.uuid4(), uuid.uuid4(), {}) - return defer.succeed(LeapSession(self._leap_provider, leap_auth, None, None, None, None)) + credentials.username, + uuid.uuid4(), + uuid.uuid4(), + uuid.uuid4(), + {}) + return defer.succeed( + LeapSession( + self._leap_provider, + leap_auth, + None, + None, + None, + None)) else: return defer.fail() @@ -234,8 +255,12 @@ def start_client(self, mode=UserAgentMode(is_single_user=True)): bonafide_checker = StubAuthenticator(provider) bonafide_checker.add_user('username', 'password') - self.resource = set_up_protected_resources(RootResource( - self.service_factory), provider, self.service_factory, authenticator=bonafide_checker) + self.resource = set_up_protected_resources( + RootResource( + self.service_factory), + provider, + self.service_factory, + authenticator=bonafide_checker) @defer.inlineCallbacks def create_user(self, account_name): @@ -270,7 +295,11 @@ def get_request_written_data(_=None): def listenTCP(self, port=4567, host='127.0.0.1'): reactor.listenTCP(port, PixelatedSite(self.resource), interface=host) - def run_on_a_thread(self, logfile='/tmp/app_test_client.log', port=4567, host='127.0.0.1'): + def run_on_a_thread( + self, + logfile='/tmp/app_test_client.log', + port=4567, + host='127.0.0.1'): def _start(): self.listenTCP(port, host) reactor.run() @@ -289,18 +318,35 @@ def get(self, path, get_args='', as_json=True, ajax=True, csrf='token'): def post(self, path, body='', headers=None, ajax=True, csrf='token'): headers = headers or {'Content-Type': 'application/json'} - request = request_mock(path=path, method="POST", - body=body, headers=headers, ajax=ajax, csrf=csrf) + request = request_mock( + path=path, + method="POST", + body=body, + headers=headers, + ajax=ajax, + csrf=csrf) return self._render(request) def put(self, path, body, ajax=True, csrf='token'): - request = request_mock(path=path, method="PUT", body=body, headers={ - 'Content-Type': ['application/json']}, ajax=ajax, csrf=csrf) + request = request_mock( + path=path, + method="PUT", + body=body, + headers={ + 'Content-Type': ['application/json']}, + ajax=ajax, + csrf=csrf) return self._render(request) def delete(self, path, body="", ajax=True, csrf='token'): - request = request_mock(path=path, body=body, headers={ - 'Content-Type': ['application/json']}, method="DELETE", ajax=ajax, csrf=csrf) + request = request_mock( + path=path, + body=body, + headers={ + 'Content-Type': ['application/json']}, + method="DELETE", + ajax=ajax, + csrf=csrf) return self._render(request) @defer.inlineCallbacks @@ -313,10 +359,19 @@ def account_for(self, username): # TODO: remove def add_mail_to_user_inbox(self, input_mail, username): - return self.account_for(username).mail_store.add_mail('INBOX', input_mail.raw) + return self.account_for(username).mail_store.add_mail( + 'INBOX', input_mail.raw) @defer.inlineCallbacks - def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[], to='recipient@to.com', cc='recipient@cc.com', bcc='recipient@bcc.com'): + def add_multiple_to_mailbox( + self, + num, + mailbox='', + flags=[], + tags=[], + to='recipient@to.com', + cc='recipient@cc.com', + bcc='recipient@bcc.com'): mails = [] yield self.mail_store.add_mailbox(mailbox) for _ in range(num): @@ -342,7 +397,8 @@ def _create_mail_sender(self): return mail_sender # TODO: remove - def _generate_soledad_test_folder_name(self, soledad_test_folder='/tmp/soledad-test/test'): + def _generate_soledad_test_folder_name( + self, soledad_test_folder='/tmp/soledad-test/test'): return os.path.join(soledad_test_folder, str(uuid.uuid4())) def get_mails_by_tag(self, tag, page=1, window=100): @@ -367,14 +423,22 @@ def get_mails_by_mailbox_name(self, mbox_name): defer.returnValue(mails) @defer.inlineCallbacks - def get_attachment(self, ident, encoding, filename=None, content_type=None, ajax=True, csrf='token'): + def get_attachment( + self, + ident, + encoding, + filename=None, + content_type=None, + ajax=True, + csrf='token'): params = {'encoding': [encoding]} if filename: params['filename'] = [filename] if content_type: params['content_type'] = [content_type] deferred_result, req = self.get( - "/attachment/%s" % ident, params, as_json=False, ajax=ajax, csrf=csrf) + "/attachment/%s" % + ident, params, as_json=False, ajax=ajax, csrf=csrf) res = yield deferred_result defer.returnValue((res, req)) diff --git a/service/test/support/integration/model.py b/service/test/support/integration/model.py index 714fe5964..de2282ab6 100644 --- a/service/test/support/integration/model.py +++ b/service/test/support/integration/model.py @@ -29,11 +29,9 @@ def __init__(self): 'cc': ['recipient@cc.com'], 'bcc': ['recipient@bcc.com'], 'subject': 'Hi! This the subject', - 'date': date.mail_date_now() - }, + 'date': date.mail_date_now()}, 'body': "Hello,\nThis is the body of this message\n\nRegards,\n\n--\nPixelated.\n", - 'status': [] - } + 'status': []} def with_body(self, body): self.mail['body'] = body @@ -80,7 +78,8 @@ def build_json(self): return json.dumps(self.mail) def build_input_mail(self): - return InputMail.from_dict(self.mail, from_address='Formatted Sender ') + return InputMail.from_dict( + self.mail, from_address='Formatted Sender ') def build_leap_mail(self): return LeapMail.from_dict(self.mail) diff --git a/service/test/support/integration/multi_user_client.py b/service/test/support/integration/multi_user_client.py index 1709226b6..90711e363 100644 --- a/service/test/support/integration/multi_user_client.py +++ b/service/test/support/integration/multi_user_client.py @@ -63,7 +63,8 @@ def _mock_bonafide_auth(self, username, password): def login(self, username='username', password='password'): session = Authentication( - username, 'some_user_token', 'some_user_uuid', 'session_id', {'is_admin': False}) + username, 'some_user_token', 'some_user_uuid', 'session_id', { + 'is_admin': False}) leap_session = self._test_account.leap_session leap_session.user_auth = session config = mock() @@ -84,8 +85,12 @@ def login(self, username='username', password='password'): when(pixelated.config.services).Services( ANY()).thenReturn(self.services) - request = request_mock(path='/login', method="POST", - body={'username': username, 'password': password}) + request = request_mock( + path='/login', + method="POST", + body={ + 'username': username, + 'password': password}) return self._render(request, as_json=False) def get(self, path, get_args='', as_json=True, from_request=None): @@ -96,10 +101,23 @@ def get(self, path, get_args='', as_json=True, from_request=None): request.session = session return self._render(request, as_json) - def post(self, path, body='', headers=None, ajax=True, csrf='token', as_json=True, from_request=None): + def post( + self, + path, + body='', + headers=None, + ajax=True, + csrf='token', + as_json=True, + from_request=None): headers = headers or {'Content-Type': 'application/json'} - request = request_mock(path=path, method="POST", - body=body, headers=headers, ajax=ajax, csrf=csrf) + request = request_mock( + path=path, + method="POST", + body=body, + headers=headers, + ajax=ajax, + csrf=csrf) if from_request: session = from_request.getSession() diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index ba0340c7c..14d58c627 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -48,15 +48,24 @@ def mail_dict(): def duplicates_in_fields_mail_dict(): return { 'header': { - 'to': ['to@pixelated.org', 'another@pixelated.org', 'third@pixelated.org', 'third@pixelated.org'], - 'cc': ['cc@pixelated.org', 'another@pixelated.org', 'third@pixelated.org', 'cc@pixelated.org'], - 'bcc': ['bcc@pixelated.org', 'another@pixelated.org', 'bcc@pixelated.org'], - 'subject': 'Subject' - }, + 'to': [ + 'to@pixelated.org', + 'another@pixelated.org', + 'third@pixelated.org', + 'third@pixelated.org'], + 'cc': [ + 'cc@pixelated.org', + 'another@pixelated.org', + 'third@pixelated.org', + 'cc@pixelated.org'], + 'bcc': [ + 'bcc@pixelated.org', + 'another@pixelated.org', + 'bcc@pixelated.org'], + 'subject': 'Subject'}, 'body': 'Body', 'ident': '', - 'tags': [] - } + 'tags': []} class TestDoc(object): @@ -68,8 +77,14 @@ def __getitem__(self, key): return self.content[key] -def leap_mail(uid=0, flags=LEAP_FLAGS, headers=None, extra_headers={}, mbox_uuid='INBOX', body='body', - chash='chash'): +def leap_mail( + uid=0, + flags=LEAP_FLAGS, + headers=None, + extra_headers={}, + mbox_uuid='INBOX', + body='body', + chash='chash'): fdoc = TestDoc({'flags': flags, 'mbox_uuid': mbox_uuid, 'type': 'flags', 'uid': uid, 'chash': chash}) @@ -123,7 +138,13 @@ def getCookie(self, key): return self.cookies.get(key) -def request_mock(path='', method='GET', body='', headers={}, ajax=True, csrf='token'): +def request_mock( + path='', + method='GET', + body='', + headers={}, + ajax=True, + csrf='token'): dummy = PixRequestMock(path[1:].split('/')) for name, val in headers.iteritems(): dummy.requestHeaders.setRawHeaders(name.lower(), [val]) diff --git a/service/test/unit/adapter/mailstore/test_body_parser.py b/service/test/unit/adapter/mailstore/test_body_parser.py index fe70396cb..66027af6d 100644 --- a/service/test/unit/adapter/mailstore/test_body_parser.py +++ b/service/test/unit/adapter/mailstore/test_body_parser.py @@ -28,14 +28,18 @@ def test_simple_text(self): def test_base64_text(self): parser = BodyParser( - 'dGVzdCB0ZXh0\n', content_type='text/plain; charset="utf-8"', content_transfer_encoding='base64') + 'dGVzdCB0ZXh0\n', + content_type='text/plain; charset="utf-8"', + content_transfer_encoding='base64') self.assertEqual('test text', parser.parsed_content()) def test_8bit_transfer_encoding_with_iso_8859_1_str_input(self): data = 'Hmm, here are \xdcml\xe4\xfcts again!' parser = BodyParser( - data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + data, + content_type='text/plain; charset=iso-8859-1', + content_transfer_encoding='8bit') self.assertEqual(u'Hmm, here are Ümläüts again!', parser.parsed_content()) @@ -43,29 +47,40 @@ def test_8bit_transfer_encoding_with_iso_8859_1_str_input(self): def test_8bit_transfer_encoding_with_iso_8859_1_unicode_input(self): data = u'Hmm, here are \xdcml\xe4\xfcts again!' parser = BodyParser( - data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + data, + content_type='text/plain; charset=iso-8859-1', + content_transfer_encoding='8bit') self.assertEqual(u'Hmm, here are Ümläüts again!', parser.parsed_content()) def test_base64_with_default_us_ascii_encoding(self): parser = BodyParser( - 'dGVzdCB0ZXh0\n', content_type='text/plain', content_transfer_encoding='base64') + 'dGVzdCB0ZXh0\n', + content_type='text/plain', + content_transfer_encoding='base64') self.assertEqual('test text', parser.parsed_content()) @patch('pixelated.adapter.mailstore.body_parser.logger') - def test_body_parser_logs_problems_and_then_ignores_invalid_chars(self, logger_mock): + def test_body_parser_logs_problems_and_then_ignores_invalid_chars( + self, logger_mock): data = u'unkown char: \ufffd' parser = BodyParser( - data, content_type='text/plain; charset=iso-8859-1', content_transfer_encoding='8bit') + data, + content_type='text/plain; charset=iso-8859-1', + content_transfer_encoding='8bit') self.assertEqual(u'unkown char: ', parser.parsed_content()) logger_mock.warn.assert_called_with( u'Failed to encode content for charset iso-8859-1. Ignoring invalid chars: \'latin-1\' codec can\'t encode character u\'\\ufffd\' in position 13: ordinal not in range(256)') - def test_charset_can_be_explicitely_set_and_take_precedence_over_content_type_if_set(self): - parser = BodyParser('YmzDoQ==\n', content_type='text/plain; us-ascii', content_transfer_encoding='base64', - charset="utf-8") + def test_charset_can_be_explicitely_set_and_take_precedence_over_content_type_if_set( + self): + parser = BodyParser( + 'YmzDoQ==\n', + content_type='text/plain; us-ascii', + content_transfer_encoding='base64', + charset="utf-8") self.assertEqual(u'bl\xe1', parser.parsed_content()) diff --git a/service/test/unit/adapter/mailstore/test_leap_attachment_store.py b/service/test/unit/adapter/mailstore/test_leap_attachment_store.py index a3f096835..7ddc9d6f2 100644 --- a/service/test/unit/adapter/mailstore/test_leap_attachment_store.py +++ b/service/test/unit/adapter/mailstore/test_leap_attachment_store.py @@ -40,8 +40,12 @@ def setUp(self): self.mbox_soledad_docs = [] with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_from_index( - 'by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) + when( + self.soledad).get_from_index( + 'by-type', + 'mbox').thenAnswer( + lambda: defer.succeed( + self.mbox_soledad_docs)) self._mock_get_mailbox('INBOX') @defer.inlineCallbacks @@ -49,8 +53,13 @@ def test_get_mail_attachment(self): attachment_id = 'AAAA9AAD9E153D24265395203C53884506ABA276394B9FEC02B214BF9E77E48E' doc = SoledadDocument(json=json.dumps( {'content_type': 'foo/bar', 'raw': 'asdf'})) - when(self.soledad).get_from_index('by-type-and-payloadhash', - 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + when( + self.soledad).get_from_index( + 'by-type-and-payloadhash', + 'cnt', + attachment_id).thenReturn( + defer.succeed( + [doc])) store = LeapAttachmentStore(self.soledad) attachment = yield store.get_mail_attachment(attachment_id) @@ -62,17 +71,24 @@ def test_get_mail_attachment(self): def test_store_attachment(self): content = 'this is some attachment content' content_type = 'text/plain' - cdoc_serialized = {'content_transfer_encoding': 'base64', 'lkf': [], 'content_disposition': 'attachment', - 'ctype': '', 'raw': 'dGhpcyBpcyBzb21lIGF0dGFjaG1lbnQgY29udGVudA==', - 'phash': '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', - 'content_type': 'text/plain', 'type': 'cnt', 'charset': None} + cdoc_serialized = { + 'content_transfer_encoding': 'base64', + 'lkf': [], + 'content_disposition': 'attachment', + 'ctype': '', + 'raw': 'dGhpcyBpcyBzb21lIGF0dGFjaG1lbnQgY29udGVudA==', + 'phash': '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', + 'content_type': 'text/plain', + 'type': 'cnt', + 'charset': None} store = LeapAttachmentStore(self.soledad) attachment_id = yield store.add_attachment(content, content_type) self.assertEqual( - '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', attachment_id) + '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', + attachment_id) verify(self.soledad).create_doc(cdoc_serialized, doc_id=attachment_id) @@ -81,19 +97,32 @@ def test_store_attachment_twice_does_not_cause_exception(self): attachment_id = '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0' content = 'this is some attachment content' content_type = 'text/plain' - cdoc_serialized = {'content_transfer_encoding': 'base64', 'lkf': [], 'content_disposition': 'attachment', - 'ctype': '', 'raw': 'dGhpcyBpcyBzb21lIGF0dGFjaG1lbnQgY29udGVudA==', - 'phash': '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', - 'content_type': 'text/plain', 'type': 'cnt'} + cdoc_serialized = { + 'content_transfer_encoding': 'base64', + 'lkf': [], + 'content_disposition': 'attachment', + 'ctype': '', + 'raw': 'dGhpcyBpcyBzb21lIGF0dGFjaG1lbnQgY29udGVudA==', + 'phash': '9863729729D2E2EE8E52F0A7115CE33AD18DDA4B58E49AE08DD092D1C8E699B0', + 'content_type': 'text/plain', + 'type': 'cnt'} doc = SoledadDocument(json=json.dumps( {'content_type': content_type, 'raw': content})) - when(self.soledad).get_from_index('by-type-and-payloadhash', - 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + when( + self.soledad).get_from_index( + 'by-type-and-payloadhash', + 'cnt', + attachment_id).thenReturn( + defer.succeed( + [doc])) store = LeapAttachmentStore(self.soledad) - when(self.soledad).create_doc(cdoc_serialized, - doc_id=attachment_id).thenRaise(l2db.errors.RevisionConflict()) + when( + self.soledad).create_doc( + cdoc_serialized, + doc_id=attachment_id).thenRaise( + l2db.errors.RevisionConflict()) actual_attachment_id = yield store.add_attachment(content, content_type) @@ -107,10 +136,19 @@ def test_get_mail_attachment_different_content_encodings(self): ('quoted-printable', 'äsdf', '=C3=A4sdf')] for transfer_encoding, data, encoded_data in encoding_examples: - doc = SoledadDocument(json=json.dumps({'content_type': 'foo/bar', 'raw': encoded_data, - 'content_transfer_encoding': transfer_encoding})) - when(self.soledad).get_from_index('by-type-and-payloadhash', - 'cnt', attachment_id).thenReturn(defer.succeed([doc])) + doc = SoledadDocument( + json=json.dumps( + { + 'content_type': 'foo/bar', + 'raw': encoded_data, + 'content_transfer_encoding': transfer_encoding})) + when( + self.soledad).get_from_index( + 'by-type-and-payloadhash', + 'cnt', + attachment_id).thenReturn( + defer.succeed( + [doc])) store = LeapAttachmentStore(self.soledad) attachment = yield store.get_mail_attachment(attachment_id) @@ -118,10 +156,16 @@ def test_get_mail_attachment_different_content_encodings(self): self.assertEqual(bytearray(data), attachment['content']) @defer.inlineCallbacks - def test_get_mail_attachment_throws_exception_if_attachment_does_not_exist(self): + def test_get_mail_attachment_throws_exception_if_attachment_does_not_exist( + self): attachment_id = '1B0A9AAD9E153D24265395203C53884506ABA276394B9FEC02B214BF9E77E48E' - when(self.soledad).get_from_index('by-type-and-payloadhash', - 'cnt', attachment_id).thenReturn(defer.succeed([])) + when( + self.soledad).get_from_index( + 'by-type-and-payloadhash', + 'cnt', + attachment_id).thenReturn( + defer.succeed( + [])) store = LeapAttachmentStore(self.soledad) try: yield store.get_mail_attachment(attachment_id) @@ -131,14 +175,21 @@ def test_get_mail_attachment_throws_exception_if_attachment_does_not_exist(self) def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): mbox_uuid = self.mbox_uuid if not create_new_uuid else str(uuid4()) - when(self.soledad).list_indexes().thenReturn(defer.succeed(MAIL_INDEXES)).thenReturn( + when( + self.soledad).list_indexes().thenReturn( + defer.succeed(MAIL_INDEXES)).thenReturn( defer.succeed(MAIL_INDEXES)) doc_id = str(uuid4()) mbox = MailboxWrapper(doc_id=doc_id, mbox=mailbox_name, uuid=mbox_uuid) soledad_doc = SoledadDocument( doc_id, json=json.dumps(mbox.serialize())) - when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', - mailbox_name).thenReturn(defer.succeed([soledad_doc])) + when( + self.soledad).get_from_index( + 'by-type-and-mbox', + 'mbox', + mailbox_name).thenReturn( + defer.succeed( + [soledad_doc])) self._mock_get_soledad_doc(doc_id, mbox) self.mbox_uuid_by_name[mailbox_name] = mbox_uuid diff --git a/service/test/unit/adapter/mailstore/test_leap_mail.py b/service/test/unit/adapter/mailstore/test_leap_mail.py index 85158714e..33aacba0a 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mail.py +++ b/service/test/unit/adapter/mailstore/test_leap_mail.py @@ -23,8 +23,11 @@ class TestLeapMail(TestCase): def test_leap_mail(self): - mail = LeapMail('', 'INBOX', { - 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}) + mail = LeapMail('', + 'INBOX', + {'From': 'test@example.test', + 'Subject': 'A test Mail', + 'To': 'receiver@example.test'}) self.assertEqual('test@example.test', mail.from_sender) self.assertEqual(['receiver@example.test'], mail.to) @@ -57,8 +60,13 @@ def test_email_addresses_might_be_empty_array(self): self.assertEqual([], mail.headers['Cc']) def test_as_dict(self): - mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', - 'To': 'receiver@example.test,receiver2@other.test'}, ('foo', 'bar')) + mail = LeapMail('doc id', + 'INBOX', + {'From': 'test@example.test', + 'Subject': 'A test Mail', + 'To': 'receiver@example.test,receiver2@other.test'}, + ('foo', + 'bar')) self.maxDiff = None expected = { 'header': { @@ -84,8 +92,14 @@ def test_as_dict(self): def test_as_dict_with_body(self): body = 'some body content' - mail = LeapMail('doc id', 'INBOX', { - 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) + mail = LeapMail('doc id', + 'INBOX', + {'From': 'test@example.test', + 'Subject': 'A test Mail', + 'To': 'receiver@example.test'}, + ('foo', + 'bar'), + body=body) self.assertEqual(body, mail.as_dict()['body']) @@ -94,17 +108,24 @@ def test_as_dict_with_attachments(self): 'id', 'name', 'encoding', ctype='text/plain', size=2) mail = LeapMail('doc id', 'INBOX', attachments=[attachment_info]) - self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding', 'content-type': 'text/plain', 'size': 2}], + self.assertEqual([{'ident': 'id', + 'name': 'name', + 'encoding': 'encoding', + 'content-type': 'text/plain', + 'size': 2}], mail.as_dict()['attachments']) def test_as_dict_headers_with_special_chars(self): expected_address = u'"\xc4lbert \xdcbr\xf6" <\xe4\xfc\xf6@example.mail>' expected_subject = u'H\xe4ll\xf6 W\xf6rld' - mail = LeapMail('', 'INBOX', - {'From': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', - 'To': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', - 'Cc': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', - 'Subject': '=?iso-8859-1?q?H=E4ll=F6_W=F6rld?='}) + mail = LeapMail( + '', + 'INBOX', + { + 'From': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', + 'To': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', + 'Cc': '=?iso-8859-1?q?=22=C4lbert_=DCbr=F6=22_=3C=E4=FC=F6=40example=2Email=3E?=', + 'Subject': '=?iso-8859-1?q?H=E4ll=F6_W=F6rld?='}) self.assertEqual(expected_address, mail.as_dict()['header']['from']) self.assertEqual([expected_address], mail.as_dict()['header']['to']) @@ -121,8 +142,14 @@ def test_as_dict_with_mixed_encodings(self): def test_raw_constructed_by_headers_and_body(self): body = 'some body content' - mail = LeapMail('doc id', 'INBOX', { - 'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body) + mail = LeapMail('doc id', + 'INBOX', + {'From': 'test@example.test', + 'Subject': 'A test Mail', + 'To': 'receiver@example.test'}, + ('foo', + 'bar'), + body=body) result = mail.raw @@ -139,8 +166,9 @@ def test_headers_none_recipients_are_converted_to_empty_array(self): def test_security_casing(self): # No Encryption, no Signature mail = LeapMail('id', 'INBOX', {}) - self.assertEqual({'locks': [], 'imprints': [ - {'state': 'no_signature_information'}]}, mail.security_casing) + self.assertEqual({'locks': [], + 'imprints': [{'state': 'no_signature_information'}]}, + mail.security_casing) # Encryption mail = LeapMail('id', 'INBOX', {'X-Leap-Encryption': 'decrypted'}) diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py index 969f631e8..b3975e26f 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py @@ -51,8 +51,12 @@ def setUp(self): self.mbox_soledad_docs = [] with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_from_index( - 'by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs)) + when( + self.soledad).get_from_index( + 'by-type', + 'mbox').thenAnswer( + lambda: defer.succeed( + self.mbox_soledad_docs)) self._mock_get_mailbox('INBOX') @defer.inlineCallbacks @@ -79,7 +83,8 @@ def test_get_mail(self): self.assertEqual('darby.senger@zemlak.biz', mail.from_sender) self.assertEqual(['carmel@murazikortiz.name'], mail.to) self.assertEqual( - 'Itaque consequatur repellendus provident sunt quia.', mail.subject) + 'Itaque consequatur repellendus provident sunt quia.', + mail.subject) self.assertIsNone(mail.body) self.assertEqual('INBOX', mail.mailbox_name) @@ -109,7 +114,8 @@ def test_get_two_different_mails(self): self.assertNotEqual(mail1, mail2) self.assertEqual( - 'Itaque consequatur repellendus provident sunt quia.', mail1.subject) + 'Itaque consequatur repellendus provident sunt quia.', + mail1.subject) self.assertEqual( 'Error illum dignissimos autem eos aspernatur.', mail2.subject) @@ -126,7 +132,8 @@ def test_get_mails(self): self.assertEqual(2, len(mails)) self.assertEqual( - 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + 'Itaque consequatur repellendus provident sunt quia.', + mails[0].subject) self.assertEqual( 'Error illum dignissimos autem eos aspernatur.', mails[1].subject) @@ -137,7 +144,7 @@ def test_get_mails_fails_for_invalid_mail_id(self): try: yield store.get_mails(['invalid']) self.fail('Exception expected') - except Exception, e: + except Exception as e: pass @defer.inlineCallbacks @@ -177,8 +184,9 @@ def test_all_mails(self): 'mbox00000000') second_mdoc_id, _ = self._add_mail_fixture_to_soledad_from_file( 'mbox00000001') - when(self.soledad).get_from_index('by-type', 'meta').thenReturn( - defer.succeed([self.doc_by_id[first_mdoc_id], self.doc_by_id[second_mdoc_id]])) + when(self.soledad).get_from_index('by-type', + 'meta').thenReturn(defer.succeed([self.doc_by_id[first_mdoc_id], + self.doc_by_id[second_mdoc_id]])) store = LeapMailStore(self.soledad) @@ -187,7 +195,8 @@ def test_all_mails(self): self.assertIsNotNone(mails) self.assertEqual(2, len(mails)) self.assertEqual( - 'Itaque consequatur repellendus provident sunt quia.', mails[0].subject) + 'Itaque consequatur repellendus provident sunt quia.', + mails[0].subject) self.assertEqual( 'Error illum dignissimos autem eos aspernatur.', mails[1].subject) @@ -195,8 +204,13 @@ def test_all_mails(self): def test_add_mailbox(self): when(self.soledad).list_indexes().thenReturn(defer.succeed( MAIL_INDEXES)).thenReturn(defer.succeed(MAIL_INDEXES)) - when(self.soledad).get_from_index('by-type-and-mbox', - 'mbox', 'TEST').thenReturn(defer.succeed([])) + when( + self.soledad).get_from_index( + 'by-type-and-mbox', + 'mbox', + 'TEST').thenReturn( + defer.succeed( + [])) self._mock_create_soledad_doc( self.mbox_uuid, MailboxWrapper(mbox='TEST')) with patch('mockito.invocation.AnswerSelector', AnswerSelector): @@ -272,8 +286,15 @@ def test_add_mail_with_attachment(self): message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message( - mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [ + { + 'ident': self._cdoc_phash_from_message( + mocked_message, + 2), + 'name': 'filename.txt', + 'encoding': 'base64', + 'size': 48, + 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) @defer.inlineCallbacks @@ -289,8 +310,15 @@ def test_add_mail_with_inline_attachment(self): message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message( - mocked_message, 2), 'name': 'super_nice_photo.jpg', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [ + { + 'ident': self._cdoc_phash_from_message( + mocked_message, + 2), + 'name': 'super_nice_photo.jpg', + 'encoding': 'base64', + 'size': 48, + 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) @defer.inlineCallbacks @@ -308,8 +336,15 @@ def test_add_mail_with_nested_attachments(self): message = yield store.add_mail('INBOX', input_mail.as_string()) - expected = [{'ident': self._cdoc_phash_from_message( - mocked_message, 2), 'name': 'filename.txt', 'encoding': 'base64', 'size': 48, 'content-type': 'application/octet-stream'}] + expected = [ + { + 'ident': self._cdoc_phash_from_message( + mocked_message, + 2), + 'name': 'filename.txt', + 'encoding': 'base64', + 'size': 48, + 'content-type': 'application/octet-stream'}] self.assertEqual(expected, message.as_dict()['attachments']) def test_extract_attachment_filename_with_or_without_quotes(self): @@ -349,7 +384,8 @@ def test_extract_attachment_filename_from_other_headers(self): input_mail.attach(attachment_without_description) attachment_with_name_in_content_type = MIMEApplication( - 'pretend to be an attachment from thunderbird', _subtype='pgp-signature; name="signature.asc"') + 'pretend to be an attachment from thunderbird', + _subtype='pgp-signature; name="signature.asc"') attachment_with_name_in_content_type.add_header( 'Content-Disposition', 'inline') input_mail.attach(attachment_with_name_in_content_type) @@ -376,7 +412,8 @@ def test_add_mail_with_special_chars(self): message.as_dict()['header']['from']) def _cdoc_phash_from_message(self, mocked_message, attachment_nr): - return mocked_message.get_wrapper().cdocs[attachment_nr].future_doc_id[2:] + return mocked_message.get_wrapper().cdocs[ + attachment_nr].future_doc_id[2:] @defer.inlineCallbacks def test_delete_mail(self): @@ -393,8 +430,15 @@ def test_delete_mail(self): def test_get_mailbox_mail_ids(self): mdoc_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( 'mbox00000000') - when(self.soledad).get_from_index('by-type-and-mbox-uuid', 'flags', - underscore_uuid(self.mbox_uuid)).thenReturn(defer.succeed([self.doc_by_id[fdoc_id]])) + when( + self.soledad).get_from_index( + 'by-type-and-mbox-uuid', + 'flags', + underscore_uuid( + self.mbox_uuid)).thenReturn( + defer.succeed( + [ + self.doc_by_id[fdoc_id]])) self._mock_get_mailbox('INBOX') store = LeapMailStore(self.soledad) @@ -449,11 +493,14 @@ def test_move_to_mailbox(self): def test_all_mail_graceful_error_handling(self): mail_id, fdoc_id = self._add_mail_fixture_to_soledad_from_file( 'mbox00000000') - when(self.soledad).get_from_index( - 'by-type', 'meta').thenReturn(defer.succeed([self.doc_by_id[mail_id]])) + when(self.soledad).get_from_index('by-type', + 'meta').thenReturn(defer.succeed([self.doc_by_id[mail_id]])) with patch('mockito.invocation.AnswerSelector', AnswerSelector): - when(self.soledad).get_doc(self.doc_by_id[mail_id].content['cdocs'][ - 0]).thenAnswer(lambda: defer.fail(Exception('fail loading attachment'))) + when( + self.soledad).get_doc( + self.doc_by_id[mail_id].content['cdocs'][0]).thenAnswer( + lambda: defer.fail( + Exception('fail loading attachment'))) store = LeapMailStore(self.soledad) mails = yield store.all_mails(gracefully_ignore_errors=True) @@ -464,7 +511,11 @@ def _assert_mail_got_deleted(self, fdoc_id, mail_id): verify(self.soledad).delete_doc(self.doc_by_id[mail_id]) verify(self.soledad).delete_doc(self.doc_by_id[fdoc_id]) - def _assert_message_docs_created(self, expected_message, actual_message, only_mdoc_and_fdoc=False): + def _assert_message_docs_created( + self, + expected_message, + actual_message, + only_mdoc_and_fdoc=False): wrapper = expected_message.get_wrapper() verify(self.soledad).create_doc( @@ -475,19 +526,28 @@ def _assert_message_docs_created(self, expected_message, actual_message, only_md verify(self.soledad).create_doc( wrapper.hdoc.serialize(), doc_id=wrapper.hdoc.future_doc_id) for nr, cdoc in wrapper.cdocs.items(): - verify(self.soledad).create_doc(cdoc.serialize(), - doc_id=wrapper.cdocs[nr].future_doc_id) + verify( + self.soledad).create_doc( + cdoc.serialize(), + doc_id=wrapper.cdocs[nr].future_doc_id) def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): mbox_uuid = self.mbox_uuid if not create_new_uuid else str(uuid4()) - when(self.soledad).list_indexes().thenReturn(defer.succeed(MAIL_INDEXES)).thenReturn( + when( + self.soledad).list_indexes().thenReturn( + defer.succeed(MAIL_INDEXES)).thenReturn( defer.succeed(MAIL_INDEXES)) doc_id = str(uuid4()) mbox = MailboxWrapper(doc_id=doc_id, mbox=mailbox_name, uuid=mbox_uuid) soledad_doc = SoledadDocument( doc_id, json=json.dumps(mbox.serialize())) - when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', - mailbox_name).thenReturn(defer.succeed([soledad_doc])) + when( + self.soledad).get_from_index( + 'by-type-and-mbox', + 'mbox', + mailbox_name).thenReturn( + defer.succeed( + [soledad_doc])) self._mock_get_soledad_doc(doc_id, mbox) self.mbox_uuid_by_name[mailbox_name] = mbox_uuid @@ -495,7 +555,8 @@ def _mock_get_mailbox(self, mailbox_name, create_new_uuid=False): return mbox, soledad_doc - def _add_mail_fixture_to_soledad_from_file(self, mail_file, mbox_uuid=None): + def _add_mail_fixture_to_soledad_from_file( + self, mail_file, mbox_uuid=None): mail = self._load_mail_from_file(mail_file) return self._add_mail_fixture_to_soledad(mail, mbox_uuid) @@ -556,11 +617,16 @@ def _mock_get_soledad_doc(self, doc_id, doc): def _mock_create_soledad_doc(self, doc_id, doc): soledad_doc = SoledadDocument(doc_id, json=json.dumps(doc.serialize())) if doc.future_doc_id: - when(self.soledad).create_doc(doc.serialize(), - doc_id=doc_id).thenReturn(defer.succeed(soledad_doc)) + when( + self.soledad).create_doc( + doc.serialize(), + doc_id=doc_id).thenReturn( + defer.succeed(soledad_doc)) else: - when(self.soledad).create_doc(doc.serialize() - ).thenReturn(defer.succeed(soledad_doc)) + when( + self.soledad).create_doc( + doc.serialize()).thenReturn( + defer.succeed(soledad_doc)) self.doc_by_id[doc_id] = soledad_doc def _load_mail_from_file(self, mail_file): diff --git a/service/test/unit/adapter/search/test_search.py b/service/test/unit/adapter/search/test_search.py index 4bc605ce6..ed898f87d 100644 --- a/service/test/unit/adapter/search/test_search.py +++ b/service/test/unit/adapter/search/test_search.py @@ -60,8 +60,8 @@ def test_headers_encoding(self): 'To': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Cc': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Subject': 'Some test mail', - 'Date': str(datetime.now()) - } + 'Date': str( + datetime.now())} # when # test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) @@ -80,8 +80,8 @@ def test_contents_encoding_accents(self): 'To': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Cc': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Subject': 'Some test mail', - 'Date': str(datetime.now()) - } + 'Date': str( + datetime.now())} body = "When doing the search, it's not possible to find words with graphical accents, e.g.: 'coração', 'é', 'Fièvre', La Pluie d'été, 'não'." @@ -107,8 +107,8 @@ def test_contents_encoding_special_characters(self): 'To': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Cc': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', 'Subject': 'Some test mail', - 'Date': str(datetime.now()) - } + 'Date': str( + datetime.now())} body = "When doing the search, 您好 أهلا" diff --git a/service/test/unit/adapter/services/test_mail_sender.py b/service/test/unit/adapter/services/test_mail_sender.py index 19437fb77..6299da8fe 100644 --- a/service/test/unit/adapter/services/test_mail_sender.py +++ b/service/test/unit/adapter/services/test_mail_sender.py @@ -61,7 +61,10 @@ def setUp(self): self._remote_smtp_host = 'some.host.test' self._remote_smtp_port = 1234 self._smtp_config = LeapSMTPConfig( - 'someone@somedomain.tld', self._cert_path, self._remote_smtp_host, self._remote_smtp_port) + 'someone@somedomain.tld', + self._cert_path, + self._remote_smtp_host, + self._remote_smtp_port) self.sender = MailSender(self._smtp_config, self._keymanager_mock) def tearDown(self): @@ -77,7 +80,8 @@ def test_iterates_over_recipients(self): yield self.sender.sendmail(input_mail) - for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): + for recipient in flatten( + [input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message( any(), TwistedSmtpUserCapture(recipient)) @@ -109,8 +113,9 @@ def test_problem_with_email_raises_exception(self): try: yield self.sender.sendmail(input_mail) self.fail('Exception expected!') - except MailSenderException, e: - for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): + except MailSenderException as e: + for recipient in flatten( + [input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map) @defer.inlineCallbacks @@ -122,15 +127,22 @@ def test_keymanager_encrypt_problem_raises_exception(self): defer.succeed(None)) when(OutgoingMail)._fix_headers(any(), any(), any()).thenReturn( defer.succeed((None, mock()))) - when(self._keymanager_mock).encrypt(any(), any(), sign=any(), - fetch_remote=any()).thenReturn(defer.fail(Exception('pretend key expired'))) + when( + self._keymanager_mock).encrypt( + any(), + any(), + sign=any(), + fetch_remote=any()).thenReturn( + defer.fail( + Exception('pretend key expired'))) with patch('leap.bitmask.mail.outgoing.service.emit_async'): try: yield self.sender.sendmail(input_mail) self.fail('Exception expected!') - except MailSenderException, e: - for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): + except MailSenderException as e: + for recipient in flatten( + [input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map) @defer.inlineCallbacks @@ -144,6 +156,7 @@ def test_iterates_over_recipients_and_send_whitout_bcc_field(self): yield self.sender.sendmail(input_mail) - for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): + for recipient in flatten( + [input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message(MailToSmtpFormatCapture( recipient, bccs), TwistedSmtpUserCapture(recipient)) diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 02b348548..a29348909 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -56,20 +56,28 @@ def multipart_mail_dict(): def with_attachment_mail_dict(): - return { - 'attachments': [{'content-type': 'text/plain', 'name': 'ayoyo.txt', 'raw': 'Hamburg Ayoyoyooooo!!!', 'ident': 'some_attachment_id'}, - {'content-type': 'text/html', 'name': 'hello.html', 'raw': '

Hello html Hamburg!

', 'ident': 'other_attachment_id'}], - 'body': [{'content-type': 'plain', 'raw': 'Hello world!'}, - {'content-type': 'html', 'raw': '

Hello html world!

'}], - 'header': { - 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], - 'to': ['to@pixelated.org', 'anotherto@pixelated.org'], - 'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'], - 'subject': 'Oi', - }, - 'ident': '', - 'tags': ['sent'] - } + return {'attachments': [{'content-type': 'text/plain', + 'name': 'ayoyo.txt', + 'raw': 'Hamburg Ayoyoyooooo!!!', + 'ident': 'some_attachment_id'}, + {'content-type': 'text/html', + 'name': 'hello.html', + 'raw': '

Hello html Hamburg!

', + 'ident': 'other_attachment_id'}], + 'body': [{'content-type': 'plain', + 'raw': 'Hello world!'}, + {'content-type': 'html', + 'raw': '

Hello html world!

'}], + 'header': {'cc': ['cc@pixelated.org', + 'anothercc@pixelated.org'], + 'to': ['to@pixelated.org', + 'anotherto@pixelated.org'], + 'bcc': ['bcc@pixelated.org', + 'anotherbcc@pixelated.org'], + 'subject': 'Oi', + }, + 'ident': '', + 'tags': ['sent']} class InputMailTest(unittest.TestCase): @@ -112,7 +120,8 @@ def test_to_mime_multipart(self): pixelated.support.date.mail_date_now = lambda: 'date now' mime_multipart = InputMail.from_dict( - simple_mail_dict(), from_address='pixelated@org').to_mime_multipart() + simple_mail_dict(), + from_address='pixelated@org').to_mime_multipart() self.assertRegexpMatches(mime_multipart.as_string( ), "\nTo: to@pixelated.org, anotherto@pixelated.org\n") @@ -147,7 +156,8 @@ def test_smtp_format(self): def test_to_mime_multipart_handles_alternative_bodies(self): mime_multipart = InputMail.from_dict( - multipart_mail_dict(), from_address='pixelated@org').to_mime_multipart() + multipart_mail_dict(), + from_address='pixelated@org').to_mime_multipart() part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!' part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n

Hello html world!

' diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py index 19e3355a0..d21d89871 100644 --- a/service/test/unit/adapter/test_mail_service.py +++ b/service/test/unit/adapter/test_mail_service.py @@ -40,7 +40,11 @@ def setUp(self): self.mail_sender = mock() self.search_engine = mock() self.mail_service = MailService( - self.mail_sender, self.mail_store, self.search_engine, 'acount@email', self.attachment_store) + self.mail_sender, + self.mail_store, + self.search_engine, + 'acount@email', + self.attachment_store) self.mail = InputMail.from_dict( duplicates_in_fields_mail_dict(), from_address='pixelated@org') diff --git a/service/test/unit/bitmask_libraries/test_abstract_leap.py b/service/test/unit/bitmask_libraries/test_abstract_leap.py index 026a41136..9e1752bd0 100644 --- a/service/test/unit/bitmask_libraries/test_abstract_leap.py +++ b/service/test/unit/bitmask_libraries/test_abstract_leap.py @@ -30,10 +30,17 @@ def setUp(self): self.leap_home = os.path.join(tempfile.mkdtemp(), 'leap') - self.config = Mock(leap_home=self.leap_home, bootstrap_ca_cert_bundle='/some/path/to/ca_cert', - ca_cert_bundle='/some/path/to/provider_ca_cert', gpg_binary='/path/to/gpg') - self.provider = Mock(config=self.config, server_name='some-server.test', domain='some-server.test', - api_uri='https://api.some-server.test:4430', api_version='1') + self.config = Mock( + leap_home=self.leap_home, + bootstrap_ca_cert_bundle='/some/path/to/ca_cert', + ca_cert_bundle='/some/path/to/provider_ca_cert', + gpg_binary='/path/to/gpg') + self.provider = Mock( + config=self.config, + server_name='some-server.test', + domain='some-server.test', + api_uri='https://api.some-server.test:4430', + api_version='1') self.soledad = Mock() self.soledad_session = Mock(soledad=self.soledad) self.auth = Mock(username='test_user', diff --git a/service/test/unit/bitmask_libraries/test_keymanager.py b/service/test/unit/bitmask_libraries/test_keymanager.py index 7b6f452cf..fc65dd0cd 100644 --- a/service/test/unit/bitmask_libraries/test_keymanager.py +++ b/service/test/unit/bitmask_libraries/test_keymanager.py @@ -81,7 +81,8 @@ def test_keymanager_generate_openpgp_key_generates_key_correctly(self): self.leap_keymanager.gen_key.assert_called_once() self.leap_keymanager.send_key.assert_called_once() - def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key(self): + def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key( + self): mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) when(self.keymanager)._key_exists( @@ -93,7 +94,8 @@ def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key(self) self.leap_keymanager.gen_key.assert_not_called() - def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self): + def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key( + self): self.keymanager._synchronize_remote_key = MagicMock() mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=False) @@ -107,7 +109,8 @@ def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self): self.leap_keymanager.send_key.assert_not_called() @defer.inlineCallbacks - def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails(self): + def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails( + self): when(self.keymanager)._key_exists( 'test_user@some-server.test').thenReturn(None) @@ -121,7 +124,8 @@ def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails(self): self.keymanager.delete_key_pair.assert_called_once() @defer.inlineCallbacks - def test_keymanager_regenerate_key_pair_if_current_key_is_about_to_expire(self): + def test_keymanager_regenerate_key_pair_if_current_key_is_about_to_expire( + self): self.keymanager._synchronize_remote_key = MagicMock() mock_open_pgp_key = MagicMock() mock_open_pgp_key.needs_renewal = MagicMock(return_value=True) diff --git a/service/test/unit/bitmask_libraries/test_provider.py b/service/test/unit/bitmask_libraries/test_provider.py index 84f1a262d..24224a1ba 100644 --- a/service/test/unit/bitmask_libraries/test_provider.py +++ b/service/test/unit/bitmask_libraries/test_provider.py @@ -33,12 +33,14 @@ def not_found_mock(url, request): @urlmatch(netloc=r'(.*\.)?some-provider\.test$', path='/provider.json') def provider_json_mock(url, request): - return provider_json_response("SHA256: 06e2300bdbc118c290eda0dc977c24080718f4eeca68c8b0ad431872a2baa22d") + return provider_json_response( + "SHA256: 06e2300bdbc118c290eda0dc977c24080718f4eeca68c8b0ad431872a2baa22d") @urlmatch(netloc=r'(.*\.)?some-provider\.test$', path='/provider.json') def provider_json_invalid_fingerprint_mock(url, request): - return provider_json_response("SHA256: 0123456789012345678901234567890123456789012345678901234567890123") + return provider_json_response( + "SHA256: 0123456789012345678901234567890123456789012345678901234567890123") def provider_json_response(fingerprint): @@ -58,7 +60,8 @@ def provider_json_response(fingerprint): } -@urlmatch(netloc=r'api\.some-provider\.test:4430$', path='/1/config/soledad-service.json') +@urlmatch(netloc=r'api\.some-provider\.test:4430$', + path='/1/config/soledad-service.json') def soledad_json_mock(url, request): content = { "some key": "some value", @@ -69,7 +72,8 @@ def soledad_json_mock(url, request): } -@urlmatch(netloc=r'api\.some-provider\.test:4430$', path='/1/config/smtp-service.json') +@urlmatch(netloc=r'api\.some-provider\.test:4430$', + path='/1/config/smtp-service.json') def smtp_json_mock(url, request): content = { "hosts": { @@ -156,8 +160,9 @@ def test_provider_fetches_provider_json(self): "https://api.some-provider.test:4430", provider.api_uri) self.assertEqual( "https://some-provider.test/ca.crt", provider.ca_cert_uri) - self.assertEqual("SHA256: 06e2300bdbc118c290eda0dc977c24080718f4eeca68c8b0ad431872a2baa22d", - provider.ca_cert_fingerprint) + self.assertEqual( + "SHA256: 06e2300bdbc118c290eda0dc977c24080718f4eeca68c8b0ad431872a2baa22d", + provider.ca_cert_fingerprint) self.assertEqual(["mx"], provider.services) def test_provider_json_throws_exception_on_status_code(self): @@ -210,9 +215,13 @@ def test_that_bootstrap_cert_is_used_to_fetch_certificate(self): provider.fetch_valid_certificate() session.get.assert_any_call( - 'https://some-provider.test/ca.crt', verify=PROVIDER_WEB_CERT, timeout=15) + 'https://some-provider.test/ca.crt', + verify=PROVIDER_WEB_CERT, + timeout=15) session.get.assert_any_call( - 'https://some-provider.test/provider.json', verify=PROVIDER_WEB_CERT, timeout=15) + 'https://some-provider.test/provider.json', + verify=PROVIDER_WEB_CERT, + timeout=15) def test_that_provider_cert_is_used_to_fetch_soledad_json(self): get_func = MagicMock(wraps=requests.get) @@ -221,8 +230,10 @@ def test_that_provider_cert_is_used_to_fetch_soledad_json(self): with HTTMock(provider_json_mock, soledad_json_mock, not_found_mock): provider = LeapProvider('some-provider.test') provider.fetch_soledad_json() - get_func.assert_called_with('https://api.some-provider.test:4430/1/config/soledad-service.json', - verify='/tmp/foobar/providers/some-provider.test/keys/client/api.pem', timeout=15) + get_func.assert_called_with( + 'https://api.some-provider.test:4430/1/config/soledad-service.json', + verify='/tmp/foobar/providers/some-provider.test/keys/client/api.pem', + timeout=15) def test_that_leap_fingerprint_is_validated(self): session = MagicMock(wraps=requests.session()) @@ -244,4 +255,5 @@ def test_provider_api_cert(self): certs = provider.provider_api_cert self.assertEqual( - '/tmp/foobar/providers/some-provider.test/keys/client/api.pem', certs) + '/tmp/foobar/providers/some-provider.test/keys/client/api.pem', + certs) diff --git a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py index 332f09826..2480685a2 100644 --- a/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py +++ b/service/test/unit/bitmask_libraries/test_smtp_client_certificate.py @@ -35,7 +35,8 @@ def not_found_mock(url, request): 'content': 'foobar'} -@urlmatch(netloc='api.some-server.test:4430', path='/1/smtp_cert', method='POST') +@urlmatch(netloc='api.some-server.test:4430', + path='/1/smtp_cert', method='POST') def smtp_cert_mock(url, request): if request.body == 'address=%s' % USERNAME: return { @@ -59,7 +60,12 @@ def setUp(self): self.provider.domain = 'some-provider.tld' self.auth = Authentication(USERNAME, 'token', 'uuid', 'session_id', {}) self.pem_path = os.path.join( - self.tmp_dir.name, 'providers', 'some-provider.tld', 'keys', 'client', 'smtp.pem') + self.tmp_dir.name, + 'providers', + 'some-provider.tld', + 'keys', + 'client', + 'smtp.pem') def tearDown(self): self.tmp_dir.dissolve() @@ -73,7 +79,8 @@ def test_download_certificate(self): self.assertEqual(self.pem_path, result) - def test_download_certificate_if_redownload_necessary_e_g_certificate_expired(self): + def test_download_certificate_if_redownload_necessary_e_g_certificate_expired( + self): self.pretend_all_paths_exist() when(certs).should_redownload(self.pem_path).thenReturn(True) diff --git a/service/test/unit/config/test_leap.py b/service/test/unit/config/test_leap.py index dae4de6ca..3b0b86398 100644 --- a/service/test/unit/config/test_leap.py +++ b/service/test/unit/config/test_leap.py @@ -10,7 +10,8 @@ class TestLeapInit(unittest.TestCase): @patch('pixelated.config.sessions.SessionCache.session_key') @defer.inlineCallbacks - def test_create_leap_session_calls_initial_sync_and_caches_sessions(self, mock_session_key): + def test_create_leap_session_calls_initial_sync_and_caches_sessions( + self, mock_session_key): mock_session_key.return_value = 'mocked key' provider_mock = MagicMock() auth_mock = MagicMock() @@ -23,7 +24,8 @@ def test_create_leap_session_calls_initial_sync_and_caches_sessions(self, mock_s @patch('pixelated.config.sessions.SessionCache.lookup_session') @defer.inlineCallbacks - def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_cache_lookup_session): + def test_create_leap_session_uses_caches_when_available_and_not_sync( + self, mock_cache_lookup_session): mock_cache_lookup_session.return_value = 'mocked key' provider_mock = MagicMock() auth_mock = MagicMock() @@ -40,7 +42,8 @@ def test_create_leap_session_uses_caches_when_available_and_not_sync(self, mock_ @patch('pixelated.config.leap.credentials') @patch('pixelated.config.leap.events_server') @defer.inlineCallbacks - def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock_event_server, mock_credentials, mock_init_leap_provider): + def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session( + self, mock_event_server, mock_credentials, mock_init_leap_provider): provider_mock = MagicMock() mock_init_leap_provider.return_value = provider_mock mock_credentials.read.return_value = ( @@ -59,7 +62,10 @@ def test_init_single_user_does_bonafide_auth_and_gives_a_leap_session(self, mock mock_event_server.ensure_server.assert_called_once() mock_credentials.read.assert_called_once_with('credentials_file') mock_init_leap_provider.asser_called_once_with( - 'provider_url', 'leap_provider_cert', 'leap_provider_cert_fingerprint', 'leap_home') + 'provider_url', + 'leap_provider_cert', + 'leap_provider_cert_fingerprint', + 'leap_home') mock_instantiate_authenticator.assert_called_once_with(provider_mock) mock_authenticator.authenticate.assert_called_once_with( 'username', 'password') @@ -103,7 +109,8 @@ def test_should_create_leap_session(self, mock_create_leap_session): self._provider, self.username, self.password, self.user_auth) @patch('pixelated.config.leap.create_leap_session') - def test_should_setup_user_services_and_map_email(self, mock_create_leap_session): + def test_should_setup_user_services_and_map_email( + self, mock_create_leap_session): mock_create_leap_session.return_value = self.leap_session self._service_factory.has_session.return_value = False @@ -115,7 +122,8 @@ def test_should_setup_user_services_and_map_email(self, mock_create_leap_session self.username, self.uuid) @patch('pixelated.config.leap.create_leap_session') - def test_should_not_user_services_if_there_is_already_a_session(self, mock_create_leap_session): + def test_should_not_user_services_if_there_is_already_a_session( + self, mock_create_leap_session): mock_create_leap_session.return_value = self.leap_session self._service_factory.has_session.return_value = True @@ -125,7 +133,8 @@ def test_should_not_user_services_if_there_is_already_a_session(self, mock_creat @patch('pixelated.config.leap.add_welcome_mail') @patch('pixelated.config.leap.create_leap_session') - def test_should_add_welcome_email_on_a_fresh_account(self, mock_create_leap_session, mock_add_welcome_email): + def test_should_add_welcome_email_on_a_fresh_account( + self, mock_create_leap_session, mock_add_welcome_email): self.leap_session.fresh_account = True mail_store = Mock() self.leap_session.mail_store = mail_store diff --git a/service/test/unit/config/test_register.py b/service/test/unit/config/test_register.py index 822517d10..b8e2a4cc0 100644 --- a/service/test/unit/config/test_register.py +++ b/service/test/unit/config/test_register.py @@ -30,7 +30,9 @@ def test_sets_provider(self): mock_provider = Mock() with patch('pixelated.register.LeapProvider', return_value=mock_provider) as mock_instantiate_provider: provider = _set_provider( - 'mocked_provider_cert', 'mocked_provider_cert_fingerprint', 'mocked_server_name') + 'mocked_provider_cert', + 'mocked_provider_cert_fingerprint', + 'mocked_server_name') mock_instantiate_provider.assert_called_once_with( 'mocked_server_name') self.assertEqual(provider, mock_provider) diff --git a/service/test/unit/config/test_services.py b/service/test/unit/config/test_services.py index 6361a3da3..ba2e0a271 100644 --- a/service/test/unit/config/test_services.py +++ b/service/test/unit/config/test_services.py @@ -60,7 +60,8 @@ def test_online_sessions_counts_logged_in_users(self): self.assertEqual(2, self.service_factory.online_sessions()) @patch('pixelated.config.services.Services.setup') - def test_create_services_from_leap_session_sets_up_services_and_add_a_user_session(self, mock_setup_services): + def test_create_services_from_leap_session_sets_up_services_and_add_a_user_session( + self, mock_setup_services): leap_session = Mock() user_id = 'irrelevant' leap_session.user_auth.uuid = user_id @@ -70,7 +71,8 @@ def test_create_services_from_leap_session_sets_up_services_and_add_a_user_sessi self.assertTrue(mock_setup_services.called) self.assertTrue(self.service_factory.has_session(user_id)) - def test_destroy_session_using_close_user_services_and_deletes_sessions(self): + def test_destroy_session_using_close_user_services_and_deletes_sessions( + self): user_id = 'irrelevant' some_service = Mock() self.service_factory.add_session(user_id, some_service) diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index 1af0d483f..e9d0e346e 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -69,7 +69,9 @@ def test_session_registers_to_generated_keys(self): session = self._create_session() register_mock.assert_called_once_with( - KEYMANAGER_FINISHED_KEY_GENERATION, session._set_fresh_account, uid=email) + KEYMANAGER_FINISHED_KEY_GENERATION, + session._set_fresh_account, + uid=email) @patch('pixelated.config.sessions.register') def test_close_unregisters_from_generate_keys_events(self, _): @@ -158,7 +160,13 @@ def test_session_does_not_set_status_fresh_for_unkown_emails(self, _): self.assertFalse(session.fresh_account) def _create_session(self): - return LeapSession(self.provider, self.auth, self.mail_store, self.soledad_session, self.keymanager, self.smtp_mock) + return LeapSession( + self.provider, + self.auth, + self.mail_store, + self.soledad_session, + self.keymanager, + self.smtp_mock) def _execute_func(func): diff --git a/service/test/unit/config/test_site.py b/service/test/unit/config/test_site.py index 9721c568e..0b11338ab 100644 --- a/service/test/unit/config/test_site.py +++ b/service/test/unit/config/test_site.py @@ -20,8 +20,10 @@ def test_add_security_headers(self): self.assertEqual('SAMEORIGIN', request.responseHeaders.getRawHeaders( 'X-Frame-Options'.lower())[0]) - self.assertEqual('1; mode=block', request.responseHeaders.getRawHeaders( - 'X-XSS-Protection'.lower())[0]) + self.assertEqual( + '1; mode=block', + request.responseHeaders.getRawHeaders( + 'X-XSS-Protection'.lower())[0]) self.assertEqual('nosniff', request.responseHeaders.getRawHeaders( 'X-Content-Type-Options'.lower())[0]) @@ -33,8 +35,10 @@ def test_add_strict_transport_security_header_if_secure(self): self.assertTrue(request.responseHeaders.hasHeader( 'Strict-Transport-Security'.lower())) - self.assertEqual('max-age=31536000; includeSubDomains', - request.responseHeaders.getRawHeaders('Strict-Transport-Security'.lower())[0]) + self.assertEqual( + 'max-age=31536000; includeSubDomains', + request.responseHeaders.getRawHeaders( + 'Strict-Transport-Security'.lower())[0]) def test_does_not_add_strict_transport_security_header_if_plain_http(self): request = self.create_request() diff --git a/service/test/unit/maintenance/test_commands.py b/service/test/unit/maintenance/test_commands.py index 1d2fdfec7..88a2587d8 100644 --- a/service/test/unit/maintenance/test_commands.py +++ b/service/test/unit/maintenance/test_commands.py @@ -95,9 +95,19 @@ def test_load_mails_adds_mails(self): def assert_mails_added(_): self.assertTrue(self.mail_store.add_mail.called) self.mail_store.add_mail.assert_any_call( - 'INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000000'))) + 'INBOX', + self._mail_content( + join( + mail_root, + 'new', + 'mbox00000000'))) self.mail_store.add_mail.assert_any_call( - 'INBOX', self._mail_content(join(mail_root, 'new', 'mbox00000001'))) + 'INBOX', + self._mail_content( + join( + mail_root, + 'new', + 'mbox00000001'))) # TODO Should we check for flags? def error_callack(err): diff --git a/service/test/unit/resources/__init__.py b/service/test/unit/resources/__init__.py index faeeeaf04..aea2051dc 100644 --- a/service/test/unit/resources/__init__.py +++ b/service/test/unit/resources/__init__.py @@ -20,4 +20,5 @@ def resolve_result(request, result): class DummySite(Site): def get(self, request): - return resolve_result(request, self.getResourceFor(request).render(request)) + return resolve_result( + request, self.getResourceFor(request).render(request)) diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index aabaa390c..0b3797aa4 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -38,17 +38,26 @@ def test_post_new_attachment(self, mock_fields): _file.type = 'some mocked type' _file.filename = 'filename.txt' mock_fields.return_value = {'attachment': _file} - when(self.mail_service).save_attachment('some mocked value', - 'some mocked type').thenReturn(defer.succeed(attachment_id)) + when( + self.mail_service).save_attachment( + 'some mocked value', + 'some mocked type').thenReturn( + defer.succeed(attachment_id)) d = self.web.get(request) def assert_response(_): self.assertEqual(201, request.code) - self.assertEqual('/attachment/%s' % attachment_id, - request.responseHeaders.getRawHeaders("location")[0]) - response_json = {'ident': attachment_id, 'content-type': 'some mocked type', - 'name': 'filename.txt', 'size': 17, 'encoding': 'base64'} + self.assertEqual( + '/attachment/%s' % + attachment_id, + request.responseHeaders.getRawHeaders("location")[0]) + response_json = { + 'ident': attachment_id, + 'content-type': 'some mocked type', + 'name': 'filename.txt', + 'size': 17, + 'encoding': 'base64'} self.assertEqual(response_json, json.loads(request.written[0])) verify(self.mail_service).save_attachment( 'some mocked value', 'some mocked type') @@ -66,8 +75,11 @@ def test_post_attachment_fails(self, mock_fields): _file.value = 'some mocked value' _file.type = 'some mocked type' mock_fields.return_value = {'attachment': _file} - when(self.mail_service).save_attachment('some mocked value', - 'some mocked type').thenReturn(defer.fail(Exception)) + when( + self.mail_service).save_attachment( + 'some mocked value', + 'some mocked type').thenReturn( + defer.fail(Exception)) d = self.web.get(request) diff --git a/service/test/unit/resources/test_login_resource.py b/service/test/unit/resources/test_login_resource.py index f90159879..e5497e8dd 100644 --- a/service/test/unit/resources/test_login_resource.py +++ b/service/test/unit/resources/test_login_resource.py @@ -54,7 +54,8 @@ def assert_unauthorized_resources(_): return d @patch('pixelated.resources.session.PixelatedSession.is_logged_in') - def test_there_are_no_grand_children_resources_when_logged_in(self, mock_is_logged_in): + def test_there_are_no_grand_children_resources_when_logged_in( + self, mock_is_logged_in): request = DummyRequest(['/login/grand_children']) mock_is_logged_in.return_value = True when(self.services_factory).has_session(ANY()).thenReturn(True) @@ -119,7 +120,8 @@ def tear_down(_): d.addCallback(tear_down) return d - def test_non_xml_compliant_banner_will_send_default_invalid_format_banner(self): + def test_non_xml_compliant_banner_will_send_default_invalid_format_banner( + self): request = DummyRequest(['']) banner_file_name = 'banner.txt' @@ -134,7 +136,8 @@ def assert_default_invalid_banner_disclaimer_rendered(_): self.assertEqual(200, request.responseCode) written_response = ''.join(request.written) self.assertIn( - "Invalid XML template format for service/_trial_temp/banner.txt.", written_response) + "Invalid XML template format for service/_trial_temp/banner.txt.", + written_response) def tear_down(_): os.remove(banner_file_name) @@ -143,7 +146,8 @@ def tear_down(_): d.addCallback(tear_down) return d - def test_wrong_banner_file_location_will_send_default_invalid_format_banner(self): + def test_wrong_banner_file_location_will_send_default_invalid_format_banner( + self): request = DummyRequest(['']) non_existing_banner_file = 'banner.txt' @@ -156,7 +160,8 @@ def assert_default_invalid_banner_disclaimer_rendered(_): self.assertEqual(200, request.responseCode) written_response = ''.join(request.written) self.assertIn( - "Disclaimer banner file banner.txt could not be read or does not exit.", written_response) + "Disclaimer banner file banner.txt could not be read or does not exit.", + written_response) d.addCallback(assert_default_invalid_banner_disclaimer_rendered) return d @@ -185,7 +190,8 @@ def setUp(self): @patch('pixelated.authentication.Authenticator.authenticate') @patch('twisted.web.util.redirectTo') @patch('pixelated.resources.session.PixelatedSession.is_logged_in') - def test_should_redirect_to_home_if_user_if_already_logged_in(self, mock_logged_in, mock_redirect, mock_authenticate): + def test_should_redirect_to_home_if_user_if_already_logged_in( + self, mock_logged_in, mock_redirect, mock_authenticate): mock_logged_in.return_value = True when(self.services_factory).has_session(ANY()).thenReturn(True) mock_redirect.return_value = "mocked redirection" @@ -201,8 +207,8 @@ def assert_redirected_to_home(_): @patch('pixelated.config.leap.BootstrapUserServices.setup') @patch('pixelated.authentication.Authenticator.authenticate') - def test_should_return_form_back_with_error_message_when_login_fails(self, mock_authenticate, - mock_user_bootstrap_setup): + def test_should_return_form_back_with_error_message_when_login_fails( + self, mock_authenticate, mock_user_bootstrap_setup): mock_authenticate.side_effect = UnauthorizedLogin() d = self.web.get(self.request) @@ -222,7 +228,8 @@ def assert_error_response_and_user_services_not_setup(_): @patch('pixelated.config.leap.BootstrapUserServices.setup') @patch('pixelated.authentication.Authenticator.authenticate') - def test_successful_login_responds_interstitial(self, mock_authenticate, mock_user_bootstrap_setup): + def test_successful_login_responds_interstitial( + self, mock_authenticate, mock_user_bootstrap_setup): mock_authenticate.return_value = self.user_auth d = self.web.get(self.request) @@ -238,7 +245,8 @@ def assert_interstitial_in_response(_): @patch('pixelated.config.leap.BootstrapUserServices.setup') @patch('pixelated.authentication.Authenticator.authenticate') - def test_successful_login_runs_user_services_bootstrap_when_interstitial_loaded(self, mock_authenticate, mock_user_bootstrap_setup): + def test_successful_login_runs_user_services_bootstrap_when_interstitial_loaded( + self, mock_authenticate, mock_user_bootstrap_setup): mock_authenticate.return_value = self.user_auth d = self.web.get(self.request) @@ -252,7 +260,8 @@ def assert_login_setup_service_for_user(_): @patch('pixelated.config.leap.BootstrapUserServices.setup') @patch('pixelated.authentication.Authenticator.authenticate') - def test_successful_adds_cookies_to_indicat_logged_in_status_when_services_are_loaded(self, mock_authenticate, mock_user_bootstrap_setup): + def test_successful_adds_cookies_to_indicat_logged_in_status_when_services_are_loaded( + self, mock_authenticate, mock_user_bootstrap_setup): mock_authenticate.return_value = self.user_auth irrelevant = None mock_user_bootstrap_setup.return_value = defer.succeed(irrelevant) diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py index f2def52ac..7e576662a 100644 --- a/service/test/unit/resources/test_mails_resource.py +++ b/service/test/unit/resources/test_mails_resource.py @@ -62,7 +62,8 @@ def assert_response(_): return d @patch('leap.common.events.register') - def test_render_PUT_should_store_draft_with_attachments(self, mock_register): + def test_render_PUT_should_store_draft_with_attachments( + self, mock_register): request = DummyRequest([]) request.method = 'PUT' request.content = mock() @@ -82,7 +83,8 @@ def assert_response(_): return d @patch('leap.common.events.register') - def test_render_POST_should_send_email_with_attachments(self, mock_register): + def test_render_POST_should_send_email_with_attachments( + self, mock_register): request = DummyRequest([]) request.method = 'POST' request.content = mock() @@ -92,8 +94,8 @@ def test_render_POST_should_send_email_with_attachments(self, mock_register): defer.succeed({"content": "some content"})) as_dictable = mock() when(as_dictable).as_dict().thenReturn({}) - when(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})\ - .thenReturn(defer.succeed(as_dictable)) + when(self.mail_service).send_mail({"attachments": [ + {"ident": "some fake attachment id", "raw": "some content"}]}) .thenReturn(defer.succeed(as_dictable)) mails_resource = MailsResource(self.services_factory) web = DummySite(mails_resource) diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index d5787eb5b..7b245882e 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -90,7 +90,8 @@ def _mock_ajax_csrf(self, request, csrf_token): 'x-requested-with', ['XMLHttpRequest']) request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) - def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch(self): + def test_should_unauthorize_child_resource_ajax_requests_when_csrf_mismatch( + self): request = DummyRequest(['/child']) request.method = 'POST' self._mock_ajax_csrf(request, 'stubbed csrf token') @@ -137,7 +138,8 @@ def assert_unauthorized(_): d.addCallback(assert_unauthorized) return d - def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch(self): + def test_should_unauthorize_child_resource_non_ajax_POST_requests_when_csrf_input_mismatch( + self): request = DummyRequest(['mails']) request.method = 'POST' request.addArg('csrftoken', 'some csrf token') diff --git a/service/test/unit/resources/test_sandbox_resource.py b/service/test/unit/resources/test_sandbox_resource.py index 96e6f5949..50097cc8c 100644 --- a/service/test/unit/resources/test_sandbox_resource.py +++ b/service/test/unit/resources/test_sandbox_resource.py @@ -34,9 +34,15 @@ def test_render_GET_should_set_sandbox_csp_header(self): yield self.web.get(request) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( - 'X-Content-Security-Policy'.lower())[0]) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( - 'Content-Security-Policy'.lower())[0]) - self.assertEquals(expected_csp_headers, request.responseHeaders.getRawHeaders( - 'X-Webkit-CSP'.lower())[0]) + self.assertEquals( + expected_csp_headers, + request.responseHeaders.getRawHeaders( + 'X-Content-Security-Policy'.lower())[0]) + self.assertEquals( + expected_csp_headers, + request.responseHeaders.getRawHeaders( + 'Content-Security-Policy'.lower())[0]) + self.assertEquals( + expected_csp_headers, + request.responseHeaders.getRawHeaders( + 'X-Webkit-CSP'.lower())[0]) diff --git a/service/test/unit/support/test_markov.py b/service/test/unit/support/test_markov.py index b258a9a43..8c7ac131f 100644 --- a/service/test/unit/support/test_markov.py +++ b/service/test/unit/support/test_markov.py @@ -56,7 +56,9 @@ def test_minimum_three_words(self): def test_add_paragraph_on_empty_chain(self): gen = MarkovGenerator( - [SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True) + [SAMPLE_TEXT], + random=self.random, + add_paragraph_on_empty_chain=True) result = gen.generate(5) diff --git a/service/test/unit/support/test_replier.py b/service/test/unit/support/test_replier.py index 38a38a621..b486f3778 100644 --- a/service/test/unit/support/test_replier.py +++ b/service/test/unit/support/test_replier.py @@ -36,7 +36,8 @@ def test_reply_all_does_not_contain_own_address_in_cc(self): 'to-field': ['test@pixelated.org', sender], 'cc-field': ['test2@pixelated.org']}} self.assertEquals(expected, reply_dict) - def test_reply_single_swaps_current_user_and_recipient_if_a_am_the_sender(self): + def test_reply_single_swaps_current_user_and_recipient_if_a_am_the_sender( + self): current_user = sender = 'me@pixelated.org' to = ['test@pixelated.org'] cc = [] diff --git a/service/test/unit/test_application.py b/service/test/unit/test_application.py index f6b1b8989..67be69529 100644 --- a/service/test/unit/test_application.py +++ b/service/test/unit/test_application.py @@ -9,7 +9,13 @@ class ApplicationTest(unittest.TestCase): class MockConfig: - def __init__(self, port, host, sslkey=None, sslcert=None, manhole=False): + def __init__( + self, + port, + host, + sslkey=None, + sslcert=None, + manhole=False): self.port = port self.host = host self.sslkey = sslkey @@ -19,7 +25,8 @@ def __init__(self, port, host, sslkey=None, sslcert=None, manhole=False): @patch('leap.common.events.client') @patch('pixelated.application.reactor') - def test_that_start_site_binds_to_tcp_port_if_no_ssl_options(self, reactor_mock, _): + def test_that_start_site_binds_to_tcp_port_if_no_ssl_options( + self, reactor_mock, _): app_mock = MagicMock() config = ApplicationTest.MockConfig(12345, '127.0.0.1') @@ -30,7 +37,8 @@ def test_that_start_site_binds_to_tcp_port_if_no_ssl_options(self, reactor_mock, @patch('leap.common.events.client') @patch('pixelated.application.reactor') - def test_that_start_site_binds_to_ssl_if_ssl_options(self, reactor_mock, _): + def test_that_start_site_binds_to_ssl_if_ssl_options( + self, reactor_mock, _): app_mock = MagicMock() pixelated.application._ssl_options = lambda x, y: 'options' @@ -45,7 +53,8 @@ def test_that_start_site_binds_to_ssl_if_ssl_options(self, reactor_mock, _): @patch('leap.common.events.client') @patch('pixelated.application.reactor') @patch('pixelated.application.services.Services') - def test_that_start_user_agent_binds_to_tcp_port_if_no_ssl_options(self, services_mock, reactor_mock, _): + def test_that_start_user_agent_binds_to_tcp_port_if_no_ssl_options( + self, services_mock, reactor_mock, _): # FIXME patch something closer, instead of leap.common app_mock = MagicMock() services_factory_mock = MagicMock() @@ -65,7 +74,8 @@ def _assert(_): @patch('leap.common.events.client') @patch('pixelated.application.reactor') @patch('pixelated.application.services.Services') - def test_that_start_user_agent_binds_to_ssl_if_ssl_options(self, services_mock, reactor_mock, _): + def test_that_start_user_agent_binds_to_ssl_if_ssl_options( + self, services_mock, reactor_mock, _): # FIXME patch something closer, instead of leap.common app_mock = MagicMock() services_factory_mock = MagicMock() @@ -88,7 +98,8 @@ def _assert(_): @patch('leap.common.events.client') @patch('pixelated.application.reactor') @patch('pixelated.application.services.Services') - def test_should_log_user_out_if_invalid_soledad_token(self, services_mock, reactor_mock, events_mock): + def test_should_log_user_out_if_invalid_soledad_token( + self, services_mock, reactor_mock, events_mock): app_mock = MagicMock() services_factory_mock = MagicMock() @@ -134,7 +145,8 @@ def _assert_user_logged_out_using_email_id(_): @patch('pixelated.application.reactor') @patch('pixelated.application._setup_multi_user') - def test_should_defer_fail_errors_during_multi_user_start_site(self, mock_multi_user_bootstrap, reactor_mock): + def test_should_defer_fail_errors_during_multi_user_start_site( + self, mock_multi_user_bootstrap, reactor_mock): args_mock = MagicMock() root_resources_mock = MagicMock() services_factory_mock = MagicMock() @@ -147,8 +159,8 @@ def test_should_defer_fail_errors_during_multi_user_start_site(self, mock_multi_ def _assert_the_same_error_is_relayed_in_the_deferred(e): self.assertIsInstance(e.value, Exception) - self.assertEqual( - e.value.message, 'multi-user failed bootstrap for whatever reason') + self.assertEqual(e.value.message, + 'multi-user failed bootstrap for whatever reason') d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred) return d @@ -156,7 +168,8 @@ def _assert_the_same_error_is_relayed_in_the_deferred(e): @patch('pixelated.application.reactor') @patch('pixelated.application.start_site') @patch('pixelated.application._setup_multi_user') - def test_should_defer_fail_errors_during_multi_user_bootstrap(self, ignore_setup_multi_user, mock_start_site, reactor_mock): + def test_should_defer_fail_errors_during_multi_user_bootstrap( + self, ignore_setup_multi_user, mock_start_site, reactor_mock): args_mock = MagicMock() root_resources_mock = MagicMock() services_factory_mock = MagicMock() @@ -170,7 +183,8 @@ def test_should_defer_fail_errors_during_multi_user_bootstrap(self, ignore_setup def _assert_the_same_error_is_relayed_in_the_deferred(e): self.assertIsInstance(e.value, Exception) self.assertEqual( - e.value.message, 'multi-user failed start site for whatever reason') + e.value.message, + 'multi-user failed start site for whatever reason') d.addErrback(_assert_the_same_error_is_relayed_in_the_deferred) return d diff --git a/service/test/unit/test_authentication.py b/service/test/unit/test_authentication.py index 4e179f50b..83eec12c5 100644 --- a/service/test/unit/test_authentication.py +++ b/service/test/unit/test_authentication.py @@ -52,7 +52,8 @@ def test_domain_name_is_stripped_before_making_bonafide_srp_auth(self): username_without_domain, 'password') @inlineCallbacks - def test_successful_bonafide_auth_should_return_the_user_authentication_object(self): + def test_successful_bonafide_auth_should_return_the_user_authentication_object( + self): auth = Authenticator(self._leap_provider) mock_bonafide_session = Mock() mock_srp_auth = Mock()