diff options
-rw-r--r-- | server/src/leap/soledad/server/_incoming.py | 27 | ||||
-rw-r--r-- | testing/tests/server/test_incoming_resource.py | 12 | ||||
-rw-r--r-- | testing/tests/server/test_incoming_server.py | 10 |
3 files changed, 39 insertions, 10 deletions
diff --git a/server/src/leap/soledad/server/_incoming.py b/server/src/leap/soledad/server/_incoming.py index a9845af1..009c0578 100644 --- a/server/src/leap/soledad/server/_incoming.py +++ b/server/src/leap/soledad/server/_incoming.py @@ -21,10 +21,13 @@ from twisted.web.resource import Resource from leap.soledad.common.document import ServerDocument from ._config import get_config from leap.soledad.common.couch.state import CouchServerState -import json +from leap.soledad.common.crypto import ENC_JSON_KEY +from leap.soledad.common.crypto import ENC_SCHEME_KEY +from leap.soledad.common.crypto import EncryptionSchemes __all__ = ['IncomingResource'] +ACCEPTED_SCHEMES = [EncryptionSchemes.NONE, EncryptionSchemes.PUBKEY] def _default_backend(): @@ -37,11 +40,29 @@ class IncomingResource(Resource): def __init__(self, backend_factory=None): self.factory = backend_factory or _default_backend() + self.formatter = IncomingFormatter() def render_PUT(self, request): - uuid, doc_id = request.postpath + uuid, doc_id, scheme = request.postpath + assert scheme in ACCEPTED_SCHEMES db = self.factory.open_database(uuid) doc = ServerDocument(doc_id) - doc.content = json.loads(request.content.read()) + doc.content = self.formatter.format(request.content.read(), scheme) db.put_doc(doc) return '' + + +class IncomingFormatter(object): + """ + Formats an incoming document. Today as it was by leap_mx and as expected by + leap_mail, but the general usage should evolve towards a generic way for + the user to receive external documents. + """ + INCOMING_KEY = 'incoming' + ERROR_DECRYPTING_KEY = 'errdecr' # TODO: Always false on MX, remove it + + def format(self, raw_content, enc_scheme): + return {self.INCOMING_KEY: True, + self.ERROR_DECRYPTING_KEY: False, + ENC_SCHEME_KEY: EncryptionSchemes.NONE, + ENC_JSON_KEY: raw_content} diff --git a/testing/tests/server/test_incoming_resource.py b/testing/tests/server/test_incoming_resource.py index ccec2198..81925580 100644 --- a/testing/tests/server/test_incoming_resource.py +++ b/testing/tests/server/test_incoming_resource.py @@ -20,6 +20,8 @@ Unit tests for incoming API resource from twisted.trial import unittest from twisted.web.test.test_web import DummyRequest from leap.soledad.server._incoming import IncomingResource +from leap.soledad.server._incoming import IncomingFormatter +from leap.soledad.common.crypto import EncryptionSchemes from io import BytesIO from uuid import uuid4 from mock import Mock @@ -35,9 +37,11 @@ class BlobServerTestCase(unittest.TestCase): self.user_uuid = uuid4().hex def test_save_document(self): - doc_id = uuid4().hex - request = DummyRequest([self.user_uuid, doc_id]) - request.content = BytesIO('{}') + formatter = IncomingFormatter() + doc_id, scheme = uuid4().hex, EncryptionSchemes.NONE + content = 'Incoming content' + request = DummyRequest([self.user_uuid, doc_id, scheme]) + request.content = BytesIO(content) self.resource.render_PUT(request) open_database = self.backend_factory.open_database @@ -45,4 +49,4 @@ class BlobServerTestCase(unittest.TestCase): self.couchdb.put_doc.assert_called_once() doc = self.couchdb.put_doc.call_args[0][0] self.assertEquals(doc_id, doc.doc_id) - self.assertEquals('{}', doc.get_json()) + self.assertEquals(formatter.format(content, scheme), doc.content) diff --git a/testing/tests/server/test_incoming_server.py b/testing/tests/server/test_incoming_server.py index 6e05206a..d0410520 100644 --- a/testing/tests/server/test_incoming_server.py +++ b/testing/tests/server/test_incoming_server.py @@ -26,6 +26,8 @@ from twisted.internet import defer import treq from leap.soledad.server._incoming import IncomingResource +from leap.soledad.server._incoming import IncomingFormatter +from leap.soledad.common.crypto import EncryptionSchemes from test_soledad.util import CouchServerStateForTests from test_soledad.util import CouchDBTestCase @@ -49,9 +51,11 @@ class BlobServerTestCase(CouchDBTestCase): @pytest.mark.usefixtures("method_tmpdir") def test_put_incoming_creates_a_document(self): user_id, doc_id = self.user_id, uuid4().hex - incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id) - yield treq.put(incoming_endpoint, BytesIO('{}'), persistent=False) + content, scheme = 'Hi', EncryptionSchemes.NONE + formatter = IncomingFormatter() + incoming_endpoint = self.uri + '%s/%s/%s' % (user_id, doc_id, scheme) + yield treq.put(incoming_endpoint, BytesIO(content), persistent=False) db = self.state.open_database(user_id) doc = db.get_doc(doc_id) - self.assertEquals(doc.content, {}) + self.assertEquals(doc.content, formatter.format(content, scheme)) |